First Steps for ClassLoader of AutoTraders
This commit is contained in:
@ -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) {
|
||||
|
Reference in New Issue
Block a user