SeSimClassLoader works fine now
This commit is contained in:
parent
03cfa59899
commit
cfb18e0584
@ -139,25 +139,14 @@ public class Globals {
|
|||||||
static AutoTraderLoader tloader;
|
static AutoTraderLoader tloader;
|
||||||
|
|
||||||
static void initGlobals() {
|
static void initGlobals() {
|
||||||
ArrayList pathlist = new ArrayList<>();
|
ArrayList default_pathlist = new ArrayList<>();
|
||||||
String default_path = new java.io.File(SeSimApplication.class.getProtectionDomain()
|
String default_path = new java.io.File(SeSimApplication.class.getProtectionDomain()
|
||||||
.getCodeSource()
|
.getCodeSource()
|
||||||
.getLocation()
|
.getLocation()
|
||||||
.getPath()).toString();
|
.getPath()).toString();
|
||||||
|
|
||||||
pathlist.add(default_path);
|
default_pathlist.add(default_path);
|
||||||
System.out.printf("Default_path: %s\n",default_path);
|
tloader = new AutoTraderLoader(default_pathlist);
|
||||||
|
|
||||||
SeSimClassLoader cl = new SeSimClassLoader(pathlist);
|
|
||||||
|
|
||||||
ArrayList<String> plist = new ArrayList<>();
|
|
||||||
plist.add("/home/tube/sesim_lib");
|
|
||||||
|
|
||||||
// cl.getInstalledClasses(plist, Indicator.class);
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
tloader = new AutoTraderLoader(pathlist);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
static public final Logger LOGGER = Logger.getLogger("com.cauwersin.sesim");
|
static public final Logger LOGGER = Logger.getLogger("com.cauwersin.sesim");
|
||||||
|
36
src/indicators/RSIIndicator.java
Normal file
36
src/indicators/RSIIndicator.java
Normal 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 RSIIndicator implements Indicator{
|
||||||
|
|
||||||
|
}
|
@ -53,7 +53,7 @@ public class AutoTraderLoader extends SeSimClassLoader {
|
|||||||
|
|
||||||
public AutoTraderLoader(ArrayList<String> pathlist) {
|
public AutoTraderLoader(ArrayList<String> pathlist) {
|
||||||
super(pathlist);
|
super(pathlist);
|
||||||
// setDefaultPathList(pathlist);
|
// setDefaultPathList(default_pathlist);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -80,7 +80,7 @@ public class AutoTraderLoader extends SeSimClassLoader {
|
|||||||
|
|
||||||
private ClassLoader cl;
|
private ClassLoader cl;
|
||||||
|
|
||||||
public AutoTraderInterface MakeInstance(Class<?> cls) {
|
public AutoTraderInterface newInstance(Class<?> cls) {
|
||||||
// ClassLoader cur = Thread.currentThread().getContextClassLoader();
|
// ClassLoader cur = Thread.currentThread().getContextClassLoader();
|
||||||
// Thread.currentThread().setContextClassLoader(cl);
|
// Thread.currentThread().setContextClassLoader(cl);
|
||||||
|
|
||||||
@ -183,7 +183,7 @@ public class AutoTraderLoader extends SeSimClassLoader {
|
|||||||
ArrayList<Class<AutoTraderInterface>> traders;
|
ArrayList<Class<AutoTraderInterface>> traders;
|
||||||
traders = new ArrayList<>();
|
traders = new ArrayList<>();
|
||||||
|
|
||||||
for (String classpathEntry : pathlist) {
|
for (String classpathEntry : default_pathlist) {
|
||||||
|
|
||||||
Consumer<? super Path> pf;
|
Consumer<? super Path> pf;
|
||||||
pf = (Object t) -> {
|
pf = (Object t) -> {
|
||||||
@ -266,7 +266,7 @@ public class AutoTraderLoader extends SeSimClassLoader {
|
|||||||
try {
|
try {
|
||||||
|
|
||||||
//AutoTraderInterface ac = trclasses.get(i).newInstance();
|
//AutoTraderInterface ac = trclasses.get(i).newInstance();
|
||||||
AutoTraderInterface ac = this.MakeInstance(trclasses.get(i));
|
AutoTraderInterface ac = this.newInstance(trclasses.get(i));
|
||||||
|
|
||||||
if (ac.getDevelStatus() && devel == false) {
|
if (ac.getDevelStatus() && devel == false) {
|
||||||
continue;
|
continue;
|
||||||
@ -303,7 +303,7 @@ public class AutoTraderLoader extends SeSimClassLoader {
|
|||||||
// 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 = newInstance(traders.get(i));
|
||||||
|
|
||||||
return ac;
|
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);
|
||||||
|
@ -53,7 +53,7 @@ import javax.swing.JPanel;
|
|||||||
*/
|
*/
|
||||||
public class SeSimClassLoader {
|
public class SeSimClassLoader {
|
||||||
|
|
||||||
protected ArrayList<String> pathlist;
|
protected ArrayList<String> default_pathlist;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Set the path list where to search for traders
|
* Set the path list where to search for traders
|
||||||
@ -61,9 +61,10 @@ public class SeSimClassLoader {
|
|||||||
* @param pathlist List of paths
|
* @param pathlist List of paths
|
||||||
*/
|
*/
|
||||||
public final void setDefaultPathList(ArrayList<String> pathlist) {
|
public final void setDefaultPathList(ArrayList<String> pathlist) {
|
||||||
this.pathlist = pathlist;
|
this.default_pathlist = pathlist;
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Create a SeSimClassLoader object with an empty default path
|
* Create a SeSimClassLoader object with an empty default path
|
||||||
*/
|
*/
|
||||||
@ -73,17 +74,16 @@ public class SeSimClassLoader {
|
|||||||
|
|
||||||
/**
|
/**
|
||||||
* Create a SeSimClassLoader object with fiven default path
|
* Create a SeSimClassLoader object with fiven default path
|
||||||
|
*
|
||||||
* @param pathlist Default path to search classes for
|
* @param pathlist Default path to search classes for
|
||||||
*/
|
*/
|
||||||
public SeSimClassLoader(ArrayList<String> pathlist) {
|
public SeSimClassLoader(ArrayList<String> pathlist) {
|
||||||
setDefaultPathList(pathlist);
|
setDefaultPathList(pathlist);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Get a list of all files in a given directory and
|
* Get a list of all files in a given directory and its sub-directories.
|
||||||
* its sub-directories.
|
*
|
||||||
* @param path Directory to list
|
* @param path Directory to list
|
||||||
* @return List of files
|
* @return List of files
|
||||||
*/
|
*/
|
||||||
@ -91,7 +91,14 @@ public class SeSimClassLoader {
|
|||||||
|
|
||||||
ArrayList<File> files = new ArrayList<>();
|
ArrayList<File> files = new ArrayList<>();
|
||||||
|
|
||||||
|
File fp = new File(path);
|
||||||
|
if (!fp.isDirectory()) {
|
||||||
|
files.add(fp);
|
||||||
|
return files;
|
||||||
|
}
|
||||||
|
|
||||||
File[] fList = new File(path).listFiles();
|
File[] fList = new File(path).listFiles();
|
||||||
|
|
||||||
for (File file : fList) {
|
for (File file : fList) {
|
||||||
if (file.isFile()) {
|
if (file.isFile()) {
|
||||||
files.add(file);
|
files.add(file);
|
||||||
@ -102,21 +109,19 @@ public class SeSimClassLoader {
|
|||||||
return files;
|
return files;
|
||||||
}
|
}
|
||||||
|
|
||||||
public Object MakeInstance(Class<?> cls) {
|
/**
|
||||||
// ClassLoader cur = Thread.currentThread().getContextClassLoader();
|
* Create a new instance of specified class
|
||||||
// Thread.currentThread().setContextClassLoader(cl);
|
*
|
||||||
|
* @param cls Class to create an instance of
|
||||||
|
* @return the instance, null if not successfull
|
||||||
|
*/
|
||||||
|
public Object newInstance(Class<?> cls) {
|
||||||
try {
|
try {
|
||||||
return cls.newInstance();
|
return cls.newInstance();
|
||||||
} catch (InstantiationException | IllegalAccessException ex) {
|
} catch (InstantiationException | IllegalAccessException ex) {
|
||||||
|
|
||||||
System.out.printf("Error: %s\n", ex.getMessage());
|
|
||||||
|
|
||||||
}
|
|
||||||
// Thread.currentThread().setContextClassLoader(cur);
|
|
||||||
|
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Check if a given class provides an certain interface and also if the
|
* Check if a given class provides an certain interface and also if the
|
||||||
@ -143,67 +148,62 @@ public class SeSimClassLoader {
|
|||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
Class<?> localClass(String filename, String classname) {
|
private Class<?> loadClass(String directory, String class_name, Class<?> iface) {
|
||||||
|
|
||||||
if (classname == null) {
|
if (class_name == null) {
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
|
|
||||||
String clnam = classname.substring(1, classname.length() - 6).replace('/', '.');
|
|
||||||
|
|
||||||
File f = new File(filename);
|
|
||||||
URL url = null;
|
URL url = null;
|
||||||
|
|
||||||
try {
|
try {
|
||||||
|
url = new File(directory).toURI().toURL();
|
||||||
url = f.toURI().toURL();
|
|
||||||
|
|
||||||
// f = new File("/home/tube/sesim_lib/");
|
|
||||||
url = f.toURI().toURL();
|
|
||||||
|
|
||||||
} catch (MalformedURLException ex) {
|
} catch (MalformedURLException ex) {
|
||||||
return null;
|
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};
|
URL[] urls = new URL[]{url};
|
||||||
|
|
||||||
ClassLoader cl;
|
ClassLoader cl;
|
||||||
// Create a new class loader with the directory
|
|
||||||
cl = new URLClassLoader(urls);
|
cl = new URLClassLoader(urls);
|
||||||
|
|
||||||
// ClassLoader cur = Thread.currentThread().getContextClassLoader();
|
|
||||||
// Thread.currentThread().setContextClassLoader(cl);
|
|
||||||
try {
|
try {
|
||||||
|
|
||||||
Class<?> cls;
|
Class<?> cls;
|
||||||
//cls = Class.forName(clnam);
|
cls = cl.loadClass(class_name);
|
||||||
|
|
||||||
cls = cl.loadClass(clnam);
|
|
||||||
|
|
||||||
if (cls == null) {
|
if (cls == null) {
|
||||||
System.out.printf("nullclass\n");
|
return null;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if (iface != null){
|
||||||
|
if (!isInstance(cls, iface)) {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if (newInstance(cls) == null) {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
return cls;
|
return cls;
|
||||||
|
|
||||||
} catch (ClassNotFoundException ex) {
|
} catch (ClassNotFoundException ex) {
|
||||||
// something wnet wrong, but we ignore it
|
|
||||||
System.out.printf("Class not found\n");
|
|
||||||
|
|
||||||
}
|
|
||||||
return null;
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
*
|
*
|
||||||
* @param pathlist
|
* @param additional_pathlist
|
||||||
* @param iface
|
* @param iface
|
||||||
|
* @return
|
||||||
*/
|
*/
|
||||||
public void getInstalledClasses(ArrayList<String> pathlist, Class<?> iface) {
|
public ArrayList<Class<?>> getInstalledClasses(ArrayList<String> additional_pathlist, Class<?> iface) {
|
||||||
|
|
||||||
|
ArrayList<Class<?>> result = new ArrayList<>();
|
||||||
|
|
||||||
|
ArrayList<String> pathlist = new ArrayList<>();
|
||||||
|
pathlist.addAll(default_pathlist);
|
||||||
|
pathlist.addAll(additional_pathlist);
|
||||||
|
|
||||||
for (String path : pathlist) {
|
for (String path : pathlist) {
|
||||||
|
|
||||||
@ -212,26 +212,23 @@ public class SeSimClassLoader {
|
|||||||
for (File file : files) {
|
for (File file : files) {
|
||||||
|
|
||||||
String fn = file.toString();
|
String fn = file.toString();
|
||||||
|
|
||||||
if (fn.toLowerCase().endsWith(".class")) {
|
if (fn.toLowerCase().endsWith(".class")) {
|
||||||
String class_name = fn.substring(path.length());
|
|
||||||
Class<?> c = localClass(fn, class_name);
|
|
||||||
|
|
||||||
if (this.isInstance(c, AutoTraderInterface.class)) {
|
String class_name;
|
||||||
System.out.printf("Her is an autotrader %s\n", class_name);
|
class_name = fn.substring(path.length());
|
||||||
|
class_name = class_name.substring(1, class_name.length() - 6).replace('/', '.');
|
||||||
|
|
||||||
Object a = MakeInstance(c);
|
Class<?> c = loadClass(path, class_name, iface);
|
||||||
if (a == null) {
|
if (null == c) {
|
||||||
System.out.printf("Can't Instanciate: %s\n", class_name);
|
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
|
result.add(c);
|
||||||
System.out.printf("Hava na Instance of %s\n", class_name);
|
//System.out.printf("Here is an instance for %s: %s\n", iface.getName(), class_name);
|
||||||
// System.out.printf("AutoName: %s\n", a.getConfig().toString());
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/* if (fn.toLowerCase().endsWith(".jar")) {
|
if (fn.toLowerCase().endsWith(".jar")) {
|
||||||
JarInputStream jarstream = null;
|
JarInputStream jarstream = null;
|
||||||
try {
|
try {
|
||||||
File jarfile = new File(fn);
|
File jarfile = new File(fn);
|
||||||
@ -239,14 +236,21 @@ public class SeSimClassLoader {
|
|||||||
JarEntry jarentry;
|
JarEntry jarentry;
|
||||||
|
|
||||||
while ((jarentry = jarstream.getNextJarEntry()) != null) {
|
while ((jarentry = jarstream.getNextJarEntry()) != null) {
|
||||||
if (jarentry.getName().endsWith(".class")) {
|
|
||||||
|
|
||||||
Class<?> cls;
|
String class_name = jarentry.getName();
|
||||||
|
|
||||||
cls = localClass(fn, "/" + jarentry.getName());
|
System.out.printf("Looking into jar: %s - %s\n", path, class_name);
|
||||||
if (cls != null) {
|
|
||||||
|
|
||||||
|
if (class_name.endsWith(".class")) {
|
||||||
|
|
||||||
|
class_name = class_name.substring(0, class_name.length() - 6).replace('/', '.');
|
||||||
|
|
||||||
|
Class<?> c = loadClass(path, class_name, iface);
|
||||||
|
if (null == c) {
|
||||||
|
continue;
|
||||||
}
|
}
|
||||||
|
result.add(c);
|
||||||
|
//System.out.printf("Here is an instance for %s: %s\n", iface.getName(), class_name);
|
||||||
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -261,10 +265,11 @@ public class SeSimClassLoader {
|
|||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
*/
|
|
||||||
}
|
}
|
||||||
// System.exit(0);
|
// System.exit(0);
|
||||||
}
|
}
|
||||||
|
return result;
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user