First Steps for ClassLoader of AutoTraders
This commit is contained in:
parent
3d5aa227d4
commit
41899d9cbd
@ -76,8 +76,11 @@
|
||||
<MenuItem class="javax.swing.JMenuItem" name="deleteMenuItem">
|
||||
<Properties>
|
||||
<Property name="mnemonic" type="int" value="100"/>
|
||||
<Property name="text" type="java.lang.String" value="Delete"/>
|
||||
<Property name="text" type="java.lang.String" value="Traders ..."/>
|
||||
</Properties>
|
||||
<Events>
|
||||
<EventHandler event="actionPerformed" listener="java.awt.event.ActionListener" parameters="java.awt.event.ActionEvent" handler="deleteMenuItemActionPerformed"/>
|
||||
</Events>
|
||||
</MenuItem>
|
||||
<MenuItem class="javax.swing.JMenuItem" name="editPreferences">
|
||||
<Properties>
|
||||
|
@ -26,6 +26,8 @@
|
||||
package gui;
|
||||
|
||||
import java.awt.Dialog;
|
||||
import java.io.File;
|
||||
import javax.swing.JFileChooser;
|
||||
import sesim.AutoTrader;
|
||||
import sesim.AutoTraderConfig;
|
||||
import sesim.Exchange;
|
||||
@ -197,7 +199,12 @@ public class NewMDIApplication extends javax.swing.JFrame {
|
||||
editMenu.add(pasteMenuItem);
|
||||
|
||||
deleteMenuItem.setMnemonic('d');
|
||||
deleteMenuItem.setText("Delete");
|
||||
deleteMenuItem.setText("Traders ...");
|
||||
deleteMenuItem.addActionListener(new java.awt.event.ActionListener() {
|
||||
public void actionPerformed(java.awt.event.ActionEvent evt) {
|
||||
deleteMenuItemActionPerformed(evt);
|
||||
}
|
||||
});
|
||||
editMenu.add(deleteMenuItem);
|
||||
|
||||
editPreferences.setMnemonic('p');
|
||||
@ -302,12 +309,33 @@ public class NewMDIApplication extends javax.swing.JFrame {
|
||||
Globals.se.timer.pause();
|
||||
}//GEN-LAST:event_jButton2ActionPerformed
|
||||
|
||||
private void deleteMenuItemActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_deleteMenuItemActionPerformed
|
||||
JFileChooser fc = new JFileChooser();
|
||||
int f = fc.showOpenDialog(chart1);
|
||||
File file = fc.getSelectedFile();
|
||||
String s = file.getName();
|
||||
System.out.printf("Select filename: %s\n",s);
|
||||
|
||||
|
||||
|
||||
|
||||
}//GEN-LAST:event_deleteMenuItemActionPerformed
|
||||
|
||||
/**
|
||||
* @param args the command line arguments
|
||||
*/
|
||||
public static void main(String args[]) {
|
||||
Globals.se = new Exchange();
|
||||
|
||||
|
||||
sesim.TraderLoader tl = new sesim.TraderLoader();
|
||||
try{
|
||||
tl.get();
|
||||
}catch(Exception e){
|
||||
System.out.print("Execptiojn\n");
|
||||
}
|
||||
//System.exit(0);
|
||||
|
||||
|
||||
/* Create and display the form */
|
||||
java.awt.EventQueue.invokeLater(new Runnable() {
|
||||
public void run() {
|
||||
|
@ -29,6 +29,6 @@ package sesim;
|
||||
*
|
||||
* @author 7u83
|
||||
*/
|
||||
public abstract class AutoTraderConfig {
|
||||
public interface AutoTraderConfig {
|
||||
public abstract AutoTrader createTrader(Exchange se, double money, double shares);
|
||||
}
|
||||
|
187
src/main/java/sesim/TraderLoader.java
Normal file
187
src/main/java/sesim/TraderLoader.java
Normal file
@ -0,0 +1,187 @@
|
||||
/*
|
||||
* 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 java.io.File;
|
||||
import java.io.FileFilter;
|
||||
import java.io.FileInputStream;
|
||||
import java.io.IOException;
|
||||
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.*;
|
||||
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;
|
||||
|
||||
/**
|
||||
*
|
||||
* @author 7u83 <7u83@mail.ru>
|
||||
*/
|
||||
public class TraderLoader {
|
||||
|
||||
public void pf(String file) {
|
||||
System.out.printf("File\n", file);
|
||||
}
|
||||
|
||||
void loadClass(String filename, String classname) throws MalformedURLException {
|
||||
|
||||
String clnam = classname.substring(1, classname.length() - 6).replace('/', '.');
|
||||
System.out.printf("Load class name: %s\n", clnam);
|
||||
|
||||
// Class<?> cls = ClassLoader.loadClass(className);
|
||||
File f = new File(filename);
|
||||
|
||||
URL url = f.toURL(); // file:/c:/myclasses/
|
||||
|
||||
URL[] urls = new URL[]{url};
|
||||
|
||||
// Create a new class loader with the directory
|
||||
ClassLoader cl = new URLClassLoader(urls);
|
||||
|
||||
try {
|
||||
Class<?> cls = cl.loadClass(clnam);
|
||||
|
||||
for(Class<?> i : cls.getInterfaces()) {
|
||||
|
||||
|
||||
if(i.equals(AutoTraderConfig.class)) {
|
||||
System.out.printf("Have found an Auto Trader %s\n", clnam);
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
} catch (ClassNotFoundException ex) {
|
||||
System.out.printf("Cant load class %s\n", clnam);
|
||||
|
||||
//Logger.getLogger(TraderLoader.class.getName()).log(Level.SEVERE, null, ex);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
public void get() throws IOException {
|
||||
|
||||
int curlen = 0;
|
||||
|
||||
/* Consumer<? super Path> pf = (Object t) -> {
|
||||
String fn = ((Path)t).toString();
|
||||
|
||||
|
||||
|
||||
System.out.printf("Have it %s %d %s\n", fn,curlen,fn.substring(curlen));
|
||||
};
|
||||
*/
|
||||
for (String classpathEntry : System.getProperty("java.class.path").split(System.getProperty("path.separator"))) {
|
||||
|
||||
Consumer<? super Path> pf = new Consumer() {
|
||||
@Override
|
||||
public void accept(Object t) {
|
||||
String fn = ((Path) t).toString();
|
||||
if (fn.toLowerCase().endsWith(".class")) {
|
||||
try {
|
||||
//System.out.printf("Halloe: %s %s\n", fn, fn.substring(classpathEntry.length()));
|
||||
loadClass(fn, fn.substring(classpathEntry.length()));
|
||||
} catch (MalformedURLException ex) {
|
||||
Logger.getLogger(TraderLoader.class.getName()).log(Level.SEVERE, null, ex);
|
||||
}
|
||||
}
|
||||
if (fn.toLowerCase().endsWith(".jar")) {
|
||||
JarInputStream is = null;
|
||||
try {
|
||||
File jar = new File(fn);
|
||||
is = new JarInputStream(new FileInputStream(jar));
|
||||
JarEntry entry;
|
||||
while ((entry = is.getNextJarEntry()) != null) {
|
||||
if (entry.getName().endsWith(".class")) {
|
||||
|
||||
System.out.printf("Entry: %s\n", entry.getName());
|
||||
|
||||
}
|
||||
}
|
||||
} catch (IOException ex) {
|
||||
Logger.getLogger(TraderLoader.class.getName()).log(Level.SEVERE, null, ex);
|
||||
} finally {
|
||||
try {
|
||||
is.close();
|
||||
} catch (IOException ex) {
|
||||
Logger.getLogger(TraderLoader.class.getName()).log(Level.SEVERE, null, ex);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
};
|
||||
|
||||
Files.walk(Paths.get(classpathEntry))
|
||||
.filter(Files::isRegularFile)
|
||||
.forEach(pf);
|
||||
|
||||
// Files.walk(Paths.get(classpathEntry));
|
||||
/*
|
||||
System.out.printf("CP ENtry %s\n", classpathEntry);
|
||||
|
||||
|
||||
|
||||
if (classpathEntry.endsWith(".jar")) {
|
||||
File jar = new File(classpathEntry);
|
||||
|
||||
JarInputStream is = new JarInputStream(new FileInputStream(jar));
|
||||
|
||||
JarEntry entry;
|
||||
while ((entry = is.getNextJarEntry()) != null) {
|
||||
if (entry.getName().endsWith(".class")) {
|
||||
|
||||
|
||||
System.out.printf("Entry: %s\n", entry.getName());
|
||||
|
||||
// Class.forName(entry.getName()) and check
|
||||
// for implementation of the interface
|
||||
}
|
||||
}
|
||||
}
|
||||
*/
|
||||
}
|
||||
|
||||
// File[] jarFiles = new File("./plugins").listFiles((File f)
|
||||
// -> f.getName().toLowerCase().endsWith(".jar"));
|
||||
}
|
||||
|
||||
public ArrayList getTraders(String dir) {
|
||||
File f = new File(dir);
|
||||
File[] ff = f.listFiles();
|
||||
ArrayList a = new ArrayList();
|
||||
a.addAll(Arrays.asList(ff));
|
||||
return a;
|
||||
}
|
||||
|
||||
}
|
@ -33,7 +33,7 @@ import sesim.Exchange;
|
||||
*
|
||||
* @author 7u83
|
||||
*/
|
||||
public class RandomTraderConfig extends AutoTraderConfig {
|
||||
public class RandomTraderConfig implements AutoTraderConfig {
|
||||
|
||||
public float[] sell_volume = {100, 100};
|
||||
public float[] sell_limit = {-1f, 1.0101f};
|
||||
|
@ -26,13 +26,14 @@
|
||||
package traders;
|
||||
|
||||
import sesim.AutoTrader;
|
||||
import sesim.AutoTraderConfig;
|
||||
import sesim.Exchange;
|
||||
|
||||
/**
|
||||
*
|
||||
* @author 7u83 <7u83@mail.ru>
|
||||
*/
|
||||
public class SwitchingTraderConfig extends RandomTraderConfig {
|
||||
public class SwitchingTraderConfig extends RandomTraderConfig implements AutoTraderConfig{
|
||||
|
||||
@Override
|
||||
public AutoTrader createTrader(Exchange se, double money, double shares) {
|
||||
|
@ -25,8 +25,19 @@
|
||||
*/
|
||||
package sesim;
|
||||
|
||||
import java.io.File;
|
||||
import java.io.IOException;
|
||||
import java.io.RandomAccessFile;
|
||||
import java.net.MalformedURLException;
|
||||
import java.net.URL;
|
||||
import java.net.URLClassLoader;
|
||||
import java.nio.ByteBuffer;
|
||||
import java.nio.channels.FileChannel;
|
||||
import java.security.ProtectionDomain;
|
||||
import java.util.ArrayList;
|
||||
import java.util.SortedMap;
|
||||
import java.util.TreeMap;
|
||||
import java.lang.ClassLoader.*;
|
||||
|
||||
/**
|
||||
*
|
||||
@ -57,89 +68,82 @@ public class Test {
|
||||
+ "\n"
|
||||
);
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
static public String getFullClassName(String classFileName) throws IOException {
|
||||
File file = new File(classFileName);
|
||||
|
||||
FileChannel roChannel = new RandomAccessFile(file, "r").getChannel();
|
||||
ByteBuffer bb = roChannel.map(FileChannel.MapMode.READ_ONLY, 0, (int)roChannel.size());
|
||||
|
||||
String x = new String();
|
||||
|
||||
//x.getClass().getClassLoader().loadClass(x);
|
||||
//Class<?> clazz = defineClass((String)null, bb, (ProtectionDomain)null);
|
||||
//return clazz.getName();
|
||||
return "";
|
||||
}
|
||||
|
||||
|
||||
|
||||
/**
|
||||
* @param args the command line arguments
|
||||
*/
|
||||
public static void main(String[] args) throws InterruptedException {
|
||||
public static void main(String[] args) throws InterruptedException, MalformedURLException, InstantiationException, IllegalAccessException, IOException {
|
||||
TraderLoader tl = new TraderLoader();
|
||||
|
||||
|
||||
System.out.printf("FFF %f\n",Math.log(1));
|
||||
tl.get();
|
||||
System.exit(0);
|
||||
|
||||
|
||||
OHLCData od = new OHLCData(1000);
|
||||
|
||||
od.realTimeAdd(12, 100, 10);
|
||||
|
||||
od.realTimeAdd(5000, 100, 10);
|
||||
//od.realTimeAdd(12, 100, 10);
|
||||
|
||||
System.out.printf("Size: %d\n", od.size());
|
||||
|
||||
OHLCDataItem di;
|
||||
|
||||
di = od.get(2);
|
||||
|
||||
|
||||
System.exit(0);
|
||||
ArrayList<File> sl = tl.getTraders("./target/classes/traders");
|
||||
|
||||
Scheduler s = new Scheduler();
|
||||
s.start();
|
||||
File file = sl.get(0);
|
||||
|
||||
class Ev implements Scheduler.TimerTask {
|
||||
|
||||
@Override
|
||||
public long timerTask() {
|
||||
System.out.printf("Timer Event Occured %s\n", name);
|
||||
if ("Ev1".equals(this.name)) {
|
||||
return 2000;
|
||||
} else {
|
||||
return 4000;
|
||||
}
|
||||
}
|
||||
|
||||
String name;
|
||||
|
||||
Ev(String name) {
|
||||
this.name = name;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
Ev e1 = new Ev("Ev1");
|
||||
Ev e2 = new Ev("Eb2");
|
||||
|
||||
s.startTimerEvent(e1, 0);
|
||||
s.startTimerEvent(e2, 0);
|
||||
System.out.printf("Filename %s\n", file.getName());
|
||||
|
||||
try {
|
||||
Thread.sleep(90000);
|
||||
} catch (Exception e) {
|
||||
// Convert File to a URL
|
||||
URL url = file.toURL(); // file:/c:/myclasses/
|
||||
|
||||
URL[] urls = new URL[]{url};
|
||||
|
||||
}
|
||||
|
||||
s.halt();
|
||||
while (s.isAlive()) {
|
||||
|
||||
}
|
||||
|
||||
System.out.print("All isstopped\n");
|
||||
|
||||
// s.startTimerEvent(e2, 100);
|
||||
/* long starttime=System.currentTimeMillis();
|
||||
while (s.isAlive()){
|
||||
if (System.currentTimeMillis()>starttime+6650){
|
||||
s.stop();
|
||||
break;
|
||||
}
|
||||
}
|
||||
System.out.print("Waiting fpor Stop\n");
|
||||
while (s.isAlive()){
|
||||
// Create a new class loader with the directory
|
||||
ClassLoader cl = new URLClassLoader(urls);
|
||||
|
||||
|
||||
Class cls = cl.loadClass("traders.RandomTraderConfig");
|
||||
|
||||
System.out.printf("Loaded Class: %s\n",cls.getClass().getName());
|
||||
|
||||
sesim.AutoTraderConfig at = (AutoTraderConfig )cls.newInstance();
|
||||
|
||||
|
||||
String cp = System.getProperty("java.class.path");
|
||||
System.out.printf("CP: %s\n", cp);
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
// Load in the class; MyClass.class should be located in
|
||||
// the directory file:/c:/myclasses/com/mycompany
|
||||
//Class cls = cl.loadClass("com.mycompany.MyClass");
|
||||
|
||||
|
||||
} catch (ClassNotFoundException e) {
|
||||
System.out.printf("hahahah %s\n", e.getClass().getName());
|
||||
System.out.print("\nException was thrown\n");
|
||||
System.out.print(e.getMessage());
|
||||
System.out.print("\n;;;;a\n");
|
||||
}
|
||||
|
||||
System.out.print("All isstopped\n");
|
||||
*/
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user