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"> <open-files xmlns="http://www.netbeans.org/ns/projectui-open-files/2">
<group> <group>
<file>file:/home/tube/NetBeansProjects/SeSim/src/chart/MasterChart.java</file> <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/chart/painter/ChartPainter.java</file>
<file>file:/home/tube/NetBeansProjects/SeSim/src/sesim/OHLCDataProvider.java</file> <file>file:/home/tube/NetBeansProjects/SeSim/src/sesim/OHLCData.java</file>
<file>file:/home/tube/NetBeansProjects/SeSim/src/traders/RandomTraderB.java</file> <file>file:/home/tube/NetBeansProjects/SeSim/src/indicators/SMAIndicator.java</file>
<file>file:/home/tube/NetBeansProjects/SeSim/src/sesim/Logger.java</file> <file>file:/home/tube/NetBeansProjects/SeSim/.git/config</file>
<file>file:/home/tube/NetBeansProjects/SeSim/src/gui/EditPreferencesDialog.java</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/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> </group>
</open-files> </open-files>
</project-private> </project-private>

View File

@ -42,7 +42,9 @@ import javax.swing.JFrame;
import javax.swing.UIManager; import javax.swing.UIManager;
import org.json.JSONArray; import org.json.JSONArray;
import org.json.JSONObject; import org.json.JSONObject;
import sesim.AutoTraderInterface;
import sesim.AutoTraderLoader; import sesim.AutoTraderLoader;
import sesim.SeSimClassLoader;
/** /**
* *
@ -143,6 +145,12 @@ public class Globals {
.getPath()).toString(); .getPath()).toString();
pathlist.add(default_path); pathlist.add(default_path);
SeSimClassLoader cl = new SeSimClassLoader(pathlist);
cl.getInstalledClasses(AutoTraderInterface.class);
tloader = new AutoTraderLoader(pathlist); 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> * @author 7u83 <7u83@mail.ru>
*/ */
public class AutoTraderLoader { public class AutoTraderLoader extends SeSimClassLoader {
private ArrayList<String> pathlist; private ArrayList<Class<AutoTraderInterface>> traders_cache = null;
private ArrayList<Class<AutoTraderInterface>> traders_cache;
public AutoTraderLoader(ArrayList<String> pathlist) { public AutoTraderLoader(ArrayList<String> pathlist) {
setPathList(pathlist); super(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;
} }
/** /**
@ -87,36 +78,32 @@ public class AutoTraderLoader {
return false; return false;
} }
private ClassLoader cl; private ClassLoader cl;
public AutoTraderInterface MakeInstance(Class<?> cls){ public AutoTraderInterface MakeInstance(Class<?> cls) {
// ClassLoader cur = Thread.currentThread().getContextClassLoader(); // ClassLoader cur = Thread.currentThread().getContextClassLoader();
// Thread.currentThread().setContextClassLoader(cl); // Thread.currentThread().setContextClassLoader(cl);
AutoTraderInterface ai; AutoTraderInterface ai;
try { try {
ai = (AutoTraderInterface)cls.newInstance(); ai = (AutoTraderInterface) cls.newInstance();
} catch (InstantiationException | IllegalAccessException ex) { } catch (InstantiationException | IllegalAccessException ex) {
Logger.getLogger(AutoTraderLoader.class.getName()).log(Level.SEVERE, null, 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; return ai;
} }
Class<AutoTraderInterface> loadAutoTraderClass(String filename, String classname) { Class<AutoTraderInterface> loadAutoTraderClass(String filename, String classname) {
// System.out.printf("Comming in width %s %s", 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)); // Globals.LOGGER.info(String.format("Comming in width %s %s\n", filename, classname));
if (classname == null) { if (classname == null) {
return null; return null;
} }
@ -126,25 +113,21 @@ public class AutoTraderLoader {
URL url = null; URL url = null;
try { try {
url = f.toURL(); f.toURI().toURL();
//url = f.toURL();
} catch (MalformedURLException ex) { } catch (MalformedURLException ex) {
Logger.getLogger(AutoTraderLoader.class.getName()).log(Level.SEVERE, null, 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}; URL[] urls = new URL[]{url};
// Create a new class loader with the directory // Create a new class loader with the directory
cl = new URLClassLoader(urls); cl = new URLClassLoader(urls);
//Thread.currentThread().setContextClassLoader(cl); //Thread.currentThread().setContextClassLoader(cl);
cl = Thread.currentThread().getContextClassLoader(); cl = Thread.currentThread().getContextClassLoader();
try { try {
// Globals.LOGGER.info("try cl"); // Globals.LOGGER.info("try cl");
// Class<?> cls = cl.loadClass(clnam); // Class<?> cls = cl.loadClass(clnam);
@ -162,7 +145,7 @@ public class AutoTraderLoader {
System.out.printf("Check Class: %s\n", cls.getCanonicalName()); 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"); Globals.LOGGER.info("We have found an autotrader interface");
Class<AutoTraderInterface> claa; Class<AutoTraderInterface> claa;
claa = (Class<AutoTraderInterface>) cls; claa = (Class<AutoTraderInterface>) cls;
@ -186,10 +169,9 @@ public class AutoTraderLoader {
} }
/** /**
* Get a list of all traders found in class path * Get a list of all traders found in class path
*
* @return List of traders * @return List of traders
*/ */
public ArrayList<Class<AutoTraderInterface>> getInstalledTraders() { public ArrayList<Class<AutoTraderInterface>> getInstalledTraders() {
@ -203,8 +185,8 @@ public class AutoTraderLoader {
for (String classpathEntry : pathlist) { for (String classpathEntry : pathlist) {
Consumer<? super Path> pf;
Consumer<? super Path> pf = (Object t) -> { pf = (Object t) -> {
String fn = ((Path) t).toString(); String fn = ((Path) t).toString();
@ -234,13 +216,12 @@ public class AutoTraderLoader {
if (entry.getName().endsWith(".class")) { 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(); // String fn0 = entry.getName();
Class<AutoTraderInterface> cls = loadAutoTraderClass(fn, "/" + entry.getName()); Class<AutoTraderInterface> cls = loadAutoTraderClass(fn, "/" + entry.getName());
if (cls != null) { if (cls != null) {
traders.add(cls); traders.add(cls);
} }
//Globals.LOGGER.info("clas was null"); //Globals.LOGGER.info("clas was null");
} }
} }
@ -266,12 +247,10 @@ public class AutoTraderLoader {
Logger.getLogger(AutoTraderLoader.class.getName()).log(Level.SEVERE, null, ex); Logger.getLogger(AutoTraderLoader.class.getName()).log(Level.SEVERE, null, ex);
} }
} }
traders_cache = traders; 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; return traders;
} }
@ -312,7 +291,7 @@ public class AutoTraderLoader {
for (int i = 0; i < traders.size(); i++) { for (int i = 0; i < traders.size(); i++) {
try { try {
if (!name.equals(traders.get(i).getCanonicalName())){ if (!name.equals(traders.get(i).getCanonicalName())) {
// System.out.printf("Contnue trader\n"); // System.out.printf("Contnue trader\n");
continue; continue;
} }
@ -320,11 +299,9 @@ public class AutoTraderLoader {
// System.exit(0); // System.exit(0);
// Globals.LOGGER.info(String.format("Making lll instance of %s", traders.get(i).getCanonicalName())); // Globals.LOGGER.info(String.format("Making lll instance of %s", traders.get(i).getCanonicalName()));
// if (traders.get(i)==null){ // if (traders.get(i)==null){
// Globals.LOGGER.info("We have null"); // Globals.LOGGER.info("We have null");
// } // }
// AutoTraderInterface ac = traders.get(i).newInstance(); // AutoTraderInterface ac = traders.get(i).newInstance();
AutoTraderInterface ac = MakeInstance(traders.get(i)); AutoTraderInterface ac = MakeInstance(traders.get(i));
@ -333,7 +310,6 @@ public class AutoTraderLoader {
// if (ac.getClass().getCanonicalName().equals(name)) { // if (ac.getClass().getCanonicalName().equals(name)) {
// return ac; // return ac;
// if (ac.getDisplayName().equals(name)) { // if (ac.getDisplayName().equals(name)) {
// return ac;} // return ac;}
// } // }
@ -345,12 +321,13 @@ public class AutoTraderLoader {
return null; return null;
} }
public ArrayList getTraders(String dir) { /*
public ArrayList xgetTraders(String dir) {
File f = new File(dir); File f = new File(dir);
File[] ff = f.listFiles(); File[] ff = f.listFiles();
ArrayList a = new ArrayList(); ArrayList a = new ArrayList();
a.addAll(Arrays.asList(ff)); a.addAll(Arrays.asList(ff));
return a; 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);
}
}
}