OpenSeSim/src/opensesim/old_sesim/AutoTraderLoader.java

146 lines
5.1 KiB
Java
Raw Normal View History

/*
* 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.
*/
2018-11-30 12:27:41 +01:00
package opensesim.old_sesim;
2018-11-30 12:27:41 +01:00
import opensesim.gui.Globals;
import java.util.ArrayList;
import java.util.logging.Level;
import java.util.logging.Logger;
/**
*
* @author 7u83 <7u83@mail.ru>
*/
public class AutoTraderLoader extends SeSimClassLoader<AutoTraderInterface> {
2017-03-24 07:27:25 +01:00
private ArrayList<Class<AutoTraderInterface>> traders_cache = null;
2017-03-24 07:27:25 +01:00
public AutoTraderLoader(ArrayList<String> pathlist) {
2017-11-21 00:34:03 +01:00
super(AutoTraderInterface.class, pathlist);
}
2017-03-24 07:27:25 +01:00
private ClassLoader cl;
@Override
2017-11-19 12:25:09 +01:00
public AutoTraderInterface newInstance(Class<?> cls) {
// ClassLoader cur = Thread.currentThread().getContextClassLoader();
// Thread.currentThread().setContextClassLoader(cl);
2017-03-24 07:27:25 +01:00
AutoTraderInterface ai;
2017-03-24 07:27:25 +01:00
try {
ai = (AutoTraderInterface) cls.newInstance();
2017-03-24 07:27:25 +01:00
} catch (InstantiationException | IllegalAccessException ex) {
2017-03-24 07:27:25 +01:00
Logger.getLogger(AutoTraderLoader.class.getName()).log(Level.SEVERE, null, ex);
ai = null;
2017-03-24 07:27:25 +01:00
}
// Thread.currentThread().setContextClassLoader(cur);
2017-03-24 07:27:25 +01:00
return ai;
}
2017-04-16 16:16:52 +02:00
/**
* Get a list of all traders found in class path
*
2017-04-16 16:16:52 +02:00
* @return List of traders
2017-03-24 07:27:25 +01:00
*/
2017-04-16 16:16:52 +02:00
public ArrayList<Class<AutoTraderInterface>> getInstalledTraders() {
2017-03-19 21:58:49 +01:00
2017-11-24 02:15:10 +01:00
return getInstalledClasses(new ArrayList<String>());
2017-01-25 00:24:53 +01:00
}
2017-02-02 18:39:55 +01:00
public ArrayList<String> getDefaultStrategyNames(boolean devel) {
2017-02-11 08:36:45 +01:00
ArrayList<Class<AutoTraderInterface>> trclasses;
// trclasses = this.getInstalledTraders();
2017-03-24 07:27:25 +01:00
2017-01-26 01:52:03 +01:00
ArrayList<String> ret = new ArrayList<>();
2017-04-16 16:16:52 +02:00
trclasses = getInstalledTraders();
2017-01-27 23:34:00 +01:00
2017-01-26 01:52:03 +01:00
for (int i = 0; i < trclasses.size(); i++) {
try {
2017-03-19 21:58:49 +01:00
2017-03-24 07:27:25 +01:00
//AutoTraderInterface ac = trclasses.get(i).newInstance();
2017-11-19 12:25:09 +01:00
AutoTraderInterface ac = this.newInstance(trclasses.get(i));
2017-03-19 21:58:49 +01:00
if (ac.getDevelStatus() && devel == false) {
2017-02-02 18:39:55 +01:00
continue;
}
2017-01-30 22:18:10 +01:00
ret.add(ac.getClass().getCanonicalName());
2017-02-11 08:36:45 +01:00
} catch (Exception e) {
System.out.printf("Can't load \n");
2017-01-27 23:34:00 +01:00
}
2017-01-26 01:52:03 +01:00
}
return ret;
}
2017-03-19 21:58:49 +01:00
2017-02-02 18:39:55 +01:00
public ArrayList<String> getDefaultStrategyNames() {
return this.getDefaultStrategyNames(true);
}
2017-01-26 01:52:03 +01:00
public AutoTraderInterface getAutoTraderInterface(String name) {
2017-04-16 16:16:52 +02:00
ArrayList<Class<AutoTraderInterface>> traders = this.getInstalledTraders();
2017-01-27 23:34:00 +01:00
for (int i = 0; i < traders.size(); i++) {
try {
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");
// }
2017-03-24 07:27:25 +01:00
// AutoTraderInterface ac = traders.get(i).newInstance();
2017-11-19 12:25:09 +01:00
AutoTraderInterface ac = newInstance(traders.get(i));
2017-01-29 22:21:12 +01:00
return ac;
// System.out.printf("Looking for in %s == %s\n", ac.getClass().getCanonicalName(), name);
2017-03-19 21:58:49 +01:00
// if (ac.getClass().getCanonicalName().equals(name)) {
// return ac;
// if (ac.getDisplayName().equals(name)) {
// return ac;}
// }
2017-01-27 23:34:00 +01:00
} catch (Exception ex) {
2017-03-24 07:27:25 +01:00
Globals.LOGGER.info(String.format("Instance failed %s", ex.getMessage()));
2017-01-27 23:34:00 +01:00
}
}
2017-01-29 22:21:12 +01:00
2017-01-27 23:34:00 +01:00
return null;
}
}