Work on a more general implementation for class loadnig

This commit is contained in:
7u83 2017-11-19 02:11:33 +01:00
parent 9ef059c34f
commit dbbf13a63b
5 changed files with 299 additions and 129 deletions

View File

@ -4,59 +4,13 @@
<open-files xmlns="http://www.netbeans.org/ns/projectui-open-files/2">
<group>
<file>file:/home/tube/NetBeansProjects/SeSim/src/chart/MasterChart.java</file>
<file>file:/home/tube/NetBeansProjects/SeSim/src/sesim/Quote.java</file>
<file>file:/home/tube/NetBeansProjects/SeSim/src/gui/tools/NummericCellRenderer.java</file>
<file>file:/home/tube/NetBeansProjects/SeSim/src/sesim/AutoTraderInterface.java</file>
<file>file:/home/tube/NetBeansProjects/SeSim/src/sesim/Account.java</file>
<file>file:/home/tube/NetBeansProjects/SeSim/src/traders/ManTrader/ManTraderConsoleDialog.java</file>
<file>file:/home/tube/NetBeansProjects/SeSim/src/gui/SeSimApplication.java</file>
<file>file:/home/tube/NetBeansProjects/SeSim/src/gui/Globals.java</file>
<file>file:/home/tube/NetBeansProjects/SeSim/src/chart/painter/OHLCChartPainter.java</file>
<file>file:/home/tube/NetBeansProjects/SeSim/src/gui/QuotePanel.java</file>
<file>file:/home/tube/NetBeansProjects/SeSim/src/gui/MainChart.java</file>
<file>file:/home/tube/NetBeansProjects/SeSim/src/chart/ChartDef.java</file>
<file>file:/home/tube/NetBeansProjects/SeSim/src/sesim/OHLCData.java</file>
<file>file:/home/tube/NetBeansProjects/SeSim/src/sesim/MinMax.java</file>
<file>file:/home/tube/NetBeansProjects/SeSim/src/chart/painter/XLegendPainter.java</file>
<file>file:/home/tube/NetBeansProjects/SeSim/src/chart/painter/LineChartPainter.java</file>
<file>file:/home/tube/NetBeansProjects/SeSim/src/sesim/Exchange.java</file>
<file>file:/home/tube/NetBeansProjects/SeSim/src/sesim/Stock.java</file>
<file>file:/home/tube/NetBeansProjects/SeSim/src/sesim/Locker.java</file>
<file>file:/home/tube/NetBeansProjects/SeSim/src/chart/ChartPanel.java</file>
<file>file:/home/tube/NetBeansProjects/SeSim/src/sesim/OHLCDataItem.java</file>
<file>file:/home/tube/NetBeansProjects/SeSim/src/gui/AboutDialog.java</file>
<file>file:/home/tube/NetBeansProjects/SeSim/src/gui/OpenOrdersList.java</file>
<file>file:/home/tube/NetBeansProjects/SeSim/src/chart/painter/YLegendPainter.java</file>
<file>file:/home/tube/NetBeansProjects/SeSim/src/gui/AskBook.java</file>
<file>file:/home/tube/NetBeansProjects/SeSim/src/gui/TestChartdialog.java</file>
<file>file:/home/tube/NetBeansProjects/SeSim/src/traders/RandomTraderA.java</file>
<file>file:/home/tube/NetBeansProjects/SeSim/test/sesim/Test.java</file>
<file>file:/home/tube/NetBeansProjects/SeSim/src/chart/painter/ChartCrossPainter.java</file>
<file>file:/home/tube/NetBeansProjects/SeSim/src/gui/StatisticsPanel.java</file>
<file>file:/home/tube/NetBeansProjects/SeSim/src/sesim/AutoTraderLoader.java</file>
<file>file:/home/tube/NetBeansProjects/SeSim/src/traders/ManTrader/ManTrader.java</file>
<file>file:/home/tube/NetBeansProjects/SeSim/src/chart/painter/ChartPainter.java</file>
<file>file:/home/tube/NetBeansProjects/SeSim/src/sesim/OHLCDataProvider.java</file>
<file>file:/home/tube/NetBeansProjects/SeSim/src/traders/RandomTraderB.java</file>
<file>file:/home/tube/NetBeansProjects/SeSim/src/sesim/Logger.java</file>
<file>file:/home/tube/NetBeansProjects/SeSim/src/gui/EditPreferencesDialog.java</file>
<file>file:/home/tube/NetBeansProjects/SeSim/src/sesim/OHLCData.java</file>
<file>file:/home/tube/NetBeansProjects/SeSim/src/indicators/SMAIndicator.java</file>
<file>file:/home/tube/NetBeansProjects/SeSim/.git/config</file>
<file>file:/home/tube/NetBeansProjects/SeSim/src/chart/painter/OHLCChartPainter.java</file>
<file>file:/home/tube/NetBeansProjects/SeSim/src/chart/painter/LineChartPainter.java</file>
<file>file:/home/tube/NetBeansProjects/SeSim/src/sesim/Indicator.java</file>
<file>file:/home/tube/NetBeansProjects/SeSim/src/sesim/AutoTraderBase.java</file>
<file>file:/home/tube/NetBeansProjects/SeSim/src/gui/Statistics.java</file>
<file>file:/home/tube/NetBeansProjects/SeSim/src/gui/EditStrategies.java</file>
<file>file:/home/tube/NetBeansProjects/SeSim/src/chart/Chart.java</file>
<file>file:/home/tube/NetBeansProjects/SeSim/src/sesim/IDGenerator.java</file>
<file>file:/home/tube/NetBeansProjects/SeSim/src/sesim/AutoTraderGui.java</file>
<file>file:/home/tube/NetBeansProjects/SeSim/src/gui/jp99.java</file>
<file>file:/home/tube/NetBeansProjects/SeSim/src/gui/orderbook/OrderBook.java</file>
<file>file:/home/tube/NetBeansProjects/SeSim/src/gui/OrderBook.java</file>
<file>file:/home/tube/NetBeansProjects/SeSim/src/chart/painter/CandleStickChartPainter.java</file>
<file>file:/home/tube/NetBeansProjects/SeSim/src/gui/BidBook.java</file>
<file>file:/home/tube/NetBeansProjects/SeSim/src/sesim/Scheduler.java</file>
<file>file:/home/tube/NetBeansProjects/SeSim/src/chart/SuperDlg.java</file>
<file>file:/home/tube/NetBeansProjects/SeSim/src/sesim/Order.java</file>
<file>file:/home/tube/NetBeansProjects/SeSim/README.md</file>
<file>file:/home/tube/NetBeansProjects/SeSim/src/sesim/SMAIndicator.java</file>
</group>
</open-files>
</project-private>

View File

@ -42,7 +42,9 @@ import javax.swing.JFrame;
import javax.swing.UIManager;
import org.json.JSONArray;
import org.json.JSONObject;
import sesim.AutoTraderInterface;
import sesim.AutoTraderLoader;
import sesim.SeSimClassLoader;
/**
*
@ -143,6 +145,12 @@ public class Globals {
.getPath()).toString();
pathlist.add(default_path);
SeSimClassLoader cl = new SeSimClassLoader(pathlist);
cl.getInstalledClasses(AutoTraderInterface.class);
tloader = new AutoTraderLoader(pathlist);
}

View File

@ -0,0 +1,36 @@
/*
* Copyright (c) 2017, 7u83 <7u83@mail.ru>
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
*
* * Redistributions of source code must retain the above copyright notice, this
* list of conditions and the following disclaimer.
* * Redistributions in binary form must reproduce the above copyright notice,
* this list of conditions and the following disclaimer in the documentation
* and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
* LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
* POSSIBILITY OF SUCH DAMAGE.
*/
package indicators;
import sesim.Indicator;
/**
*
* @author 7u83 <7u83@mail.ru>
*/
public class EMAIndicator implements Indicator{
}

View File

@ -47,22 +47,13 @@ import java.util.logging.Logger;
*
* @author 7u83 <7u83@mail.ru>
*/
public class AutoTraderLoader {
public class AutoTraderLoader extends SeSimClassLoader {
private ArrayList<String> pathlist;
private ArrayList<Class<AutoTraderInterface>> traders_cache;
private ArrayList<Class<AutoTraderInterface>> traders_cache = null;
public AutoTraderLoader(ArrayList<String> pathlist) {
setPathList(pathlist);
}
/**
* Set the path list where to search for traders
* @param pathlist List of paths
*/
public final void setPathList(ArrayList<String> pathlist) {
this.pathlist = pathlist;
this.traders_cache = null;
super(pathlist);
// setPathList(pathlist);
}
/**
@ -86,36 +77,32 @@ public class AutoTraderLoader {
} while ((cls = cls.getSuperclass()) != null);
return false;
}
private ClassLoader cl;
public AutoTraderInterface MakeInstance(Class<?> cls){
// ClassLoader cur = Thread.currentThread().getContextClassLoader();
// Thread.currentThread().setContextClassLoader(cl);
public AutoTraderInterface MakeInstance(Class<?> cls) {
// ClassLoader cur = Thread.currentThread().getContextClassLoader();
// Thread.currentThread().setContextClassLoader(cl);
AutoTraderInterface ai;
try {
ai = (AutoTraderInterface)cls.newInstance();
ai = (AutoTraderInterface) cls.newInstance();
} catch (InstantiationException | IllegalAccessException ex) {
Logger.getLogger(AutoTraderLoader.class.getName()).log(Level.SEVERE, null, ex);
ai=null;
ai = null;
}
// Thread.currentThread().setContextClassLoader(cur);
// Thread.currentThread().setContextClassLoader(cur);
return ai;
}
Class<AutoTraderInterface> loadAutoTraderClass(String filename, String classname) {
// System.out.printf("Comming in width %s %s", filename, classname);
// Globals.LOGGER.info(String.format("Comming in width %s %s\n", filename, classname));
// System.out.printf("Comming in width %s %s", filename, classname);
// Globals.LOGGER.info(String.format("Comming in width %s %s\n", filename, classname));
if (classname == null) {
return null;
@ -126,33 +113,29 @@ public class AutoTraderLoader {
URL url = null;
try {
url = f.toURL();
f.toURI().toURL();
//url = f.toURL();
} catch (MalformedURLException ex) {
Logger.getLogger(AutoTraderLoader.class.getName()).log(Level.SEVERE, null, ex);
}
// Globals.LOGGER.info(String.format("URL: %s", url.toString()));
// Globals.LOGGER.info(String.format("URL: %s", url.toString()));
URL[] urls = new URL[]{url};
// Create a new class loader with the directory
cl = new URLClassLoader(urls);
//Thread.currentThread().setContextClassLoader(cl);
cl = Thread.currentThread().getContextClassLoader();
try {
// Globals.LOGGER.info("try cl");
// Globals.LOGGER.info("try cl");
// Class<?> cls = cl.loadClass(clnam);
Class<?> c = cl.loadClass(clnam);
if (c == null) {
Globals.LOGGER.info("loader c was null");
}
// Globals.LOGGER.info("Ccast");
// Globals.LOGGER.info("Ccast");
Class<AutoTraderInterface> cls = (Class<AutoTraderInterface>) c; // cl.loadClass(clnam);
// return cls;
if (cls == null) {
@ -160,9 +143,9 @@ public class AutoTraderLoader {
}
System.out.printf("Check Class: %s\n", cls.getCanonicalName());
// Globals.LOGGER.info(String.format("Class prope %s", cls.getCanonicalName()));
// Globals.LOGGER.info(String.format("Class prope %s", cls.getCanonicalName()));
if (isAutoTrader(cls) ) {
if (isAutoTrader(cls)) {
Globals.LOGGER.info("We have found an autotrader interface");
Class<AutoTraderInterface> claa;
claa = (Class<AutoTraderInterface>) cls;
@ -186,10 +169,9 @@ public class AutoTraderLoader {
}
/**
* Get a list of all traders found in class path
*
* @return List of traders
*/
public ArrayList<Class<AutoTraderInterface>> getInstalledTraders() {
@ -203,22 +185,22 @@ public class AutoTraderLoader {
for (String classpathEntry : pathlist) {
Consumer<? super Path> pf = (Object t) -> {
Consumer<? super Path> pf;
pf = (Object t) -> {
String fn = ((Path) t).toString();
// handle a .class file
if (fn.toLowerCase().endsWith(".class")) {
String class_name = fn.substring(classpathEntry.length());
Class<AutoTraderInterface> cls = loadAutoTraderClass(fn, class_name);
if (cls == null) {
return;
}
traders.add(cls);
}
// handle a .jar file
if (fn.toLowerCase().endsWith(".jar")) {
JarInputStream is = null;
@ -233,14 +215,13 @@ public class AutoTraderLoader {
if (entry.getName().endsWith(".class")) {
// System.out.printf("Entry: %s\n", entry.getName());
// System.out.printf("Entry: %s\n", entry.getName());
// String fn0 = entry.getName();
Class<AutoTraderInterface> cls = loadAutoTraderClass(fn, "/" + entry.getName());
if (cls != null) {
traders.add(cls);
}
//Globals.LOGGER.info("clas was null");
//Globals.LOGGER.info("clas was null");
}
}
@ -266,19 +247,17 @@ public class AutoTraderLoader {
Logger.getLogger(AutoTraderLoader.class.getName()).log(Level.SEVERE, null, ex);
}
}
traders_cache = traders;
// Globals.LOGGER.info(String.format("We have found %d traders", traders.size()));
// Globals.LOGGER.info(String.format("We have found %d traders", traders.size()));
return traders;
}
public ArrayList<String> getDefaultStrategyNames(boolean devel) {
ArrayList<Class<AutoTraderInterface>> trclasses;
// trclasses = this.getInstalledTraders();
// trclasses = this.getInstalledTraders();
ArrayList<String> ret = new ArrayList<>();
trclasses = getInstalledTraders();
@ -288,7 +267,7 @@ public class AutoTraderLoader {
//AutoTraderInterface ac = trclasses.get(i).newInstance();
AutoTraderInterface ac = this.MakeInstance(trclasses.get(i));
if (ac.getDevelStatus() && devel == false) {
continue;
}
@ -311,32 +290,29 @@ public class AutoTraderLoader {
ArrayList<Class<AutoTraderInterface>> traders = this.getInstalledTraders();
for (int i = 0; i < traders.size(); i++) {
try {
if (!name.equals(traders.get(i).getCanonicalName())){
// System.out.printf("Contnue trader\n");
if (!name.equals(traders.get(i).getCanonicalName())) {
// System.out.printf("Contnue trader\n");
continue;
}
// System.out.printf("Canon name %s\n", traders.get(i).getCanonicalName());
// System.exit(0);
// Globals.LOGGER.info(String.format("Making lll instance of %s", traders.get(i).getCanonicalName()));
// if (traders.get(i)==null){
// Globals.LOGGER.info("We have null");
// }
// System.out.printf("Canon name %s\n", traders.get(i).getCanonicalName());
// System.exit(0);
// Globals.LOGGER.info(String.format("Making lll instance of %s", traders.get(i).getCanonicalName()));
// if (traders.get(i)==null){
// Globals.LOGGER.info("We have null");
// }
// AutoTraderInterface ac = traders.get(i).newInstance();
AutoTraderInterface ac = MakeInstance(traders.get(i));
return ac;
// System.out.printf("Looking for in %s == %s\n", ac.getClass().getCanonicalName(), name);
// System.out.printf("Looking for in %s == %s\n", ac.getClass().getCanonicalName(), name);
// if (ac.getClass().getCanonicalName().equals(name)) {
// return ac;
// if (ac.getDisplayName().equals(name)) {
// return ac;}
// }
// if (ac.getClass().getCanonicalName().equals(name)) {
// return ac;
// if (ac.getDisplayName().equals(name)) {
// return ac;}
// }
} catch (Exception ex) {
Globals.LOGGER.info(String.format("Instance failed %s", ex.getMessage()));
}
@ -345,12 +321,13 @@ public class AutoTraderLoader {
return null;
}
public ArrayList getTraders(String dir) {
/*
public ArrayList xgetTraders(String dir) {
File f = new File(dir);
File[] ff = f.listFiles();
ArrayList a = new ArrayList();
a.addAll(Arrays.asList(ff));
return a;
}
*/
}

View File

@ -0,0 +1,195 @@
/*
* Copyright (c) 2017, 7u83 <7u83@mail.ru>
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
*
* * Redistributions of source code must retain the above copyright notice, this
* list of conditions and the following disclaimer.
* * Redistributions in binary form must reproduce the above copyright notice,
* this list of conditions and the following disclaimer in the documentation
* and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
* LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
* POSSIBILITY OF SUCH DAMAGE.
*/
package sesim;
import gui.Globals;
import java.io.File;
import java.io.FileFilter;
import java.io.IOException;
import java.lang.reflect.Modifier;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLClassLoader;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.function.Consumer;
import java.util.logging.Level;
import javax.swing.JPanel;
/**
*
* @author 7u83 <7u83@mail.ru>
*/
public class SeSimClassLoader {
protected ArrayList<String> pathlist;
/**
* Set the path list where to search for traders
*
* @param pathlist List of paths
*/
public final void setPathList(ArrayList<String> pathlist) {
this.pathlist = pathlist;
}
public SeSimClassLoader(ArrayList<String> pathlist) {
setPathList(pathlist);
}
public ArrayList<File> listFiles(String path) {
ArrayList<File> files = new ArrayList<>();
// get all the files from a directory
File[] fList = new File(path).listFiles();
for (File file : fList) {
if (file.isFile()) {
files.add(file);
} else if (file.isDirectory()) {
files.addAll(listFiles(file.getAbsolutePath()));
}
}
return files;
}
public Object MakeInstance(Class<?> cls) {
// ClassLoader cur = Thread.currentThread().getContextClassLoader();
// Thread.currentThread().setContextClassLoader(cl);
try {
return cls.newInstance();
} catch (InstantiationException | IllegalAccessException ex) {
System.out.printf("Error: %s\n",ex.getMessage());
}
// Thread.currentThread().setContextClassLoader(cur);
return null;
}
/**
* Check if a given class provides an certain interface
* and also if the class is not abstract, so it could be
* instanciated.
*
* @param cls Class to check
* @param iface Interface which the class should provide
* @return true if it is an instance of insclass, otherwise false
*/
public boolean isInstance(Class<?> cls, Class<?> iface) {
if (Modifier.isAbstract(cls.getModifiers())) {
return false;
}
do {
for (Class<?> i : cls.getInterfaces()) {
if (i == iface) {
return true;
}
}
} while ((cls = cls.getSuperclass()) != null);
return false;
}
Class<?> xloadClass(String filename, String classname) {
if (classname == null) {
return null;
}
String clnam = classname.substring(1, classname.length() - 6).replace('/', '.');
try {
Class<?> cls = Class.forName(clnam);
if (cls == null) {
System.out.printf("nullclass\n");
}
return cls;
} catch (ClassNotFoundException ex) {
// something wnet wrong, but we ignore it
System.out.printf("Class not found\n");
}
return null;
}
/**
*
* @param iface
*/
public void getInstalledClasses(Class<?> iface) {
for (String classpathEntry : pathlist) {
System.out.printf("Classpath Entry: %s\n", classpathEntry);
ArrayList<File> files = listFiles(classpathEntry);
System.out.printf("Number of entries: %d\n", files.size());
for (File file : files) {
// System.out.printf("File: %s\n", file.toString());
String fn = file.toString();
if (fn.toLowerCase().endsWith(".class")) {
String class_name = fn.substring(classpathEntry.length());
Class<AutoTraderInterface> cls;
Class<?> c = xloadClass(fn, class_name);
if (this.isInstance(c, AutoTraderInterface.class)) {
System.out.printf("Her is an autotrader %s\n", class_name);
AutoTraderInterface a = (AutoTraderInterface)MakeInstance(c);
if (a==null)
continue;
System.out.printf("AutoName: %s\n", a.getConfig().toString());
}
//c.getCanonicalName();
//System.out.printf("Canon: %s\n", c.getCanonicalName());
}
}
//System.exit(0);
}
}
}