From 03cfa5989999ad3838616a14c0e64f2418b02171 Mon Sep 17 00:00:00 2001 From: 7u83 <7u83@mail.ru> Date: Sun, 19 Nov 2017 09:43:41 +0100 Subject: [PATCH] Work on SeSimClassLoader --- nbproject/private/private.xml | 11 ++- src/gui/Globals.java | 8 +- src/indicators/EMAIndicator.java | 36 -------- src/sesim/AutoTraderLoader.java | 2 +- src/sesim/SeSimClassLoader.java | 149 +++++++++++++++++++++++-------- 5 files changed, 131 insertions(+), 75 deletions(-) delete mode 100644 src/indicators/EMAIndicator.java diff --git a/nbproject/private/private.xml b/nbproject/private/private.xml index 3a27a44..3709b8c 100644 --- a/nbproject/private/private.xml +++ b/nbproject/private/private.xml @@ -4,13 +4,20 @@ file:/home/tube/NetBeansProjects/SeSim/src/chart/MasterChart.java - file:/home/tube/NetBeansProjects/SeSim/src/chart/painter/ChartPainter.java file:/home/tube/NetBeansProjects/SeSim/src/sesim/OHLCData.java + file:/home/tube/NetBeansProjects/SeSim/src/resources/files/defaultcfg.json + file:/home/tube/NetBeansProjects/SeSim/src/sesim/AutoTraderInterface.java + file:/home/tube/NetBeansProjects/SeSim/src/chart/painter/LineChartPainter.java + file:/home/tube/NetBeansProjects/SeSim/src/sesim/AutoTraderGui.java + file:/home/tube/NetBeansProjects/SeSim/src/sesim/AutoTraderLoader.java + file:/home/tube/NetBeansProjects/SeSim/src/gui/Globals.java + file:/home/tube/NetBeansProjects/SeSim/src/chart/painter/ChartPainter.java file:/home/tube/NetBeansProjects/SeSim/src/indicators/SMAIndicator.java file:/home/tube/NetBeansProjects/SeSim/.git/config file:/home/tube/NetBeansProjects/SeSim/src/chart/painter/OHLCChartPainter.java - file:/home/tube/NetBeansProjects/SeSim/src/chart/painter/LineChartPainter.java file:/home/tube/NetBeansProjects/SeSim/src/sesim/Indicator.java + file:/home/tube/NetBeansProjects/SeSim/src/indicators/EMAIndicator.java + file:/home/tube/NetBeansProjects/SeSim/src/sesim/SeSimClassLoader.java diff --git a/src/gui/Globals.java b/src/gui/Globals.java index b8626f2..1ba8a15 100644 --- a/src/gui/Globals.java +++ b/src/gui/Globals.java @@ -44,6 +44,7 @@ import org.json.JSONArray; import org.json.JSONObject; import sesim.AutoTraderInterface; import sesim.AutoTraderLoader; +import sesim.Indicator; import sesim.SeSimClassLoader; /** @@ -145,9 +146,14 @@ public class Globals { .getPath()).toString(); pathlist.add(default_path); + System.out.printf("Default_path: %s\n",default_path); SeSimClassLoader cl = new SeSimClassLoader(pathlist); - cl.getInstalledClasses(AutoTraderInterface.class); + + ArrayList plist = new ArrayList<>(); + plist.add("/home/tube/sesim_lib"); + + // cl.getInstalledClasses(plist, Indicator.class); diff --git a/src/indicators/EMAIndicator.java b/src/indicators/EMAIndicator.java deleted file mode 100644 index 85c2f3c..0000000 --- a/src/indicators/EMAIndicator.java +++ /dev/null @@ -1,36 +0,0 @@ -/* - * 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{ - -} diff --git a/src/sesim/AutoTraderLoader.java b/src/sesim/AutoTraderLoader.java index 44ed233..ba6b2eb 100644 --- a/src/sesim/AutoTraderLoader.java +++ b/src/sesim/AutoTraderLoader.java @@ -53,7 +53,7 @@ public class AutoTraderLoader extends SeSimClassLoader { public AutoTraderLoader(ArrayList pathlist) { super(pathlist); - // setPathList(pathlist); + // setDefaultPathList(pathlist); } /** diff --git a/src/sesim/SeSimClassLoader.java b/src/sesim/SeSimClassLoader.java index c954277..a58f58c 100644 --- a/src/sesim/SeSimClassLoader.java +++ b/src/sesim/SeSimClassLoader.java @@ -28,6 +28,7 @@ package sesim; import gui.Globals; import java.io.File; import java.io.FileFilter; +import java.io.FileInputStream; import java.io.IOException; import java.lang.reflect.Modifier; import java.net.MalformedURLException; @@ -40,7 +41,10 @@ import java.util.ArrayList; import java.util.Arrays; import java.util.List; import java.util.function.Consumer; +import java.util.jar.JarEntry; +import java.util.jar.JarInputStream; import java.util.logging.Level; +import java.util.logging.Logger; import javax.swing.JPanel; /** @@ -56,20 +60,37 @@ public class SeSimClassLoader { * * @param pathlist List of paths */ - public final void setPathList(ArrayList pathlist) { + public final void setDefaultPathList(ArrayList pathlist) { this.pathlist = pathlist; } - - public SeSimClassLoader(ArrayList pathlist) { - setPathList(pathlist); + /** + * Create a SeSimClassLoader object with an empty default path + */ + public SeSimClassLoader(){ + this(new ArrayList()); } + /** + * Create a SeSimClassLoader object with fiven default path + * @param pathlist Default path to search classes for + */ + public SeSimClassLoader(ArrayList pathlist) { + setDefaultPathList(pathlist); + } + + + + /** + * Get a list of all files in a given directory and + * its sub-directories. + * @param path Directory to list + * @return List of files + */ public ArrayList listFiles(String path) { ArrayList files = new ArrayList<>(); - - // get all the files from a directory + File[] fList = new File(path).listFiles(); for (File file : fList) { if (file.isFile()) { @@ -85,14 +106,12 @@ public class SeSimClassLoader { // ClassLoader cur = Thread.currentThread().getContextClassLoader(); // Thread.currentThread().setContextClassLoader(cl); - - try { - return cls.newInstance(); + return cls.newInstance(); } catch (InstantiationException | IllegalAccessException ex) { - System.out.printf("Error: %s\n",ex.getMessage()); - + System.out.printf("Error: %s\n", ex.getMessage()); + } // Thread.currentThread().setContextClassLoader(cur); @@ -100,9 +119,8 @@ public class SeSimClassLoader { } /** - * Check if a given class provides an certain interface - * and also if the class is not abstract, so it could be - * instanciated. + * 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 @@ -125,7 +143,7 @@ public class SeSimClassLoader { return false; } - Class xloadClass(String filename, String classname) { + Class localClass(String filename, String classname) { if (classname == null) { return null; @@ -133,9 +151,39 @@ public class SeSimClassLoader { String clnam = classname.substring(1, classname.length() - 6).replace('/', '.'); + File f = new File(filename); + URL url = null; + try { - Class cls = Class.forName(clnam); + url = f.toURI().toURL(); + + // f = new File("/home/tube/sesim_lib/"); + url = f.toURI().toURL(); + + } catch (MalformedURLException ex) { + return null; + } + + System.out.printf("URL: %s\n", url); + + // Globals.LOGGER.info(String.format("URL: %s", url.toString())); + // URL[] urls = new URL[]{url}; + URL[] urls = new URL[]{url}; + + ClassLoader cl; + // Create a new class loader with the directory + cl = new URLClassLoader(urls); + +// ClassLoader cur = Thread.currentThread().getContextClassLoader(); + // Thread.currentThread().setContextClassLoader(cl); + try { + + Class cls; + //cls = Class.forName(clnam); + + cls = cl.loadClass(clnam); + if (cls == null) { System.out.printf("nullclass\n"); } @@ -151,40 +199,71 @@ public class SeSimClassLoader { } /** - * - * @param iface + * + * @param pathlist + * @param iface */ - public void getInstalledClasses(Class iface) { - - for (String classpathEntry : pathlist) { - System.out.printf("Classpath Entry: %s\n", classpathEntry); + public void getInstalledClasses(ArrayList pathlist, Class iface) { + + for (String path : pathlist) { + + ArrayList files = listFiles(path); - ArrayList 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 cls; - Class c = xloadClass(fn, class_name); + String class_name = fn.substring(path.length()); + Class c = localClass(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) + + Object a = MakeInstance(c); + if (a == null) { + System.out.printf("Can't Instanciate: %s\n", class_name); continue; - System.out.printf("AutoName: %s\n", a.getConfig().toString()); + } + + System.out.printf("Hava na Instance of %s\n", class_name); + // System.out.printf("AutoName: %s\n", a.getConfig().toString()); } } + /* if (fn.toLowerCase().endsWith(".jar")) { + JarInputStream jarstream = null; + try { + File jarfile = new File(fn); + jarstream = new JarInputStream(new FileInputStream(jarfile)); + JarEntry jarentry; + + while ((jarentry = jarstream.getNextJarEntry()) != null) { + if (jarentry.getName().endsWith(".class")) { + + Class cls; + + cls = localClass(fn, "/" + jarentry.getName()); + if (cls != null) { + + } + + } + } + } catch (IOException ex) { + + } finally { + try { + jarstream.close(); + } catch (IOException ex) { + java.util.logging.Logger.getLogger(AutoTraderLoader.class.getName()).log(Level.SEVERE, null, ex); + } + } + + } + */ } - //System.exit(0); + // System.exit(0); } }