[Bprocessor-commit] bprocessor/src/net/sourceforge/bprocessor Main.java, NONE, 1.1 Shell.java, NONE
Status: Pre-Alpha
Brought to you by:
henryml
From: Michael L. <he...@us...> - 2010-12-03 11:41:02
|
Update of /cvsroot/bprocessor/bprocessor/src/net/sourceforge/bprocessor In directory sfp-cvsdas-2.v30.ch3.sourceforge.com:/tmp/cvs-serv8723/src/net/sourceforge/bprocessor Modified Files: Application.java Added Files: Main.java Shell.java Plugin.java Loader.java Log Message: Index: Application.java =================================================================== RCS file: /cvsroot/bprocessor/bprocessor/src/net/sourceforge/bprocessor/Application.java,v retrieving revision 1.10 retrieving revision 1.11 diff -C2 -d -r1.10 -r1.11 *** Application.java 25 Jun 2009 20:29:20 -0000 1.10 --- Application.java 3 Dec 2010 11:40:54 -0000 1.11 *************** *** 1,16 **** package net.sourceforge.bprocessor; ! import net.sourceforge.bprocessor.facade.FacadeMain; ! import net.sourceforge.bprocessor.gl.Editor; ! import net.sourceforge.bprocessor.gui.GUI; ! import org.apache.log4j.PropertyConfigurator; public class Application { ! public Application() { super(); } /** * @param args --- 1,51 ---- package net.sourceforge.bprocessor; ! import java.io.File; ! import java.net.URL; import org.apache.log4j.PropertyConfigurator; public class Application { ! private Shell shell; ! private Loader loader; ! public Application() { super(); + shell = new Shell(); + loader = new Loader(shell); + } + + public String applicationPath() { + URL url = Application.class.getProtectionDomain().getCodeSource().getLocation(); + return url.getFile(); + } + public String pluginsPath() { + String current = applicationPath(); + while (current != null) { + File tester = new File(current, "plugins"); + if (tester.isDirectory()) { + return tester.getAbsolutePath(); + } + File directory = new File(current); + current = directory.getParent(); + } + return "."; } + public void load() throws Exception { + String path = pluginsPath(); + System.out.println("plugins-path: " + path); + loader.load(path); + } + + public void run() { + try { + load(); + } catch (Exception e) { + // TODO Auto-generated catch block + e.printStackTrace(); + } + } + + /** * @param args *************** *** 21,27 **** PropertyConfigurator.configure(args[0]); } ! new GUI(); ! new Editor(); ! new FacadeMain(); } } --- 56,61 ---- PropertyConfigurator.configure(args[0]); } ! ! new Application().run(); } } --- NEW FILE: Main.java --- package net.sourceforge.bprocessor; import org.apache.log4j.PropertyConfigurator; public class Main { private Shell shell; public Main() { shell = new Shell(); } public void run() { shell.prepare(); shell.start(); } /** * @param args */ public static void main(String[] args) { System.out.println("os = " + System.getProperty("os.name")); if (args.length > 0) { PropertyConfigurator.configure(args[0]); } new Main().run(); } } --- NEW FILE: Plugin.java --- package net.sourceforge.bprocessor; public interface Plugin { public String name(); public void prepare(); public void start(); } --- NEW FILE: Shell.java --- package net.sourceforge.bprocessor; import java.util.LinkedList; import java.util.List; import net.sourceforge.bprocessor.facade.FacadeMain; import net.sourceforge.bprocessor.gl.Editor; import net.sourceforge.bprocessor.gui.GUI; public class Shell { private List<Plugin> plugins; public Shell() { plugins = new LinkedList<Plugin>(); new GUI(); new Editor(); new FacadeMain(); } public void add(Plugin plugin) { plugins.add(plugin); } public void prepare() { for (Plugin current : plugins) { current.prepare(); } } public void start() { for (Plugin current : plugins) { current.start(); } } } --- NEW FILE: Loader.java --- package net.sourceforge.bprocessor; import java.io.File; import java.net.URL; import java.net.URLClassLoader; import java.util.LinkedList; import java.util.List; import java.util.jar.Attributes; import java.util.jar.JarFile; import java.util.jar.Manifest; public class Loader { private Shell shell; public Loader(Shell shell) { this.shell = shell; } public void load(List<String> paths) throws Exception { List<String> names = new LinkedList<String>(); List<URL> urls = new LinkedList<URL>(); for (String path : paths) { File file = new File(path); URL url = file.toURI().toURL(); System.out.println("loading: " + url); JarFile jarfile = new JarFile(path); Manifest manifest = jarfile.getManifest(); Attributes attributes = manifest.getMainAttributes(); String name = attributes.getValue("Plugin"); if (name != null) { names.add(name); } urls.add(url); } URL[] urla = new URL[urls.size()]; urla = urls.toArray(urla); URLClassLoader loader = new URLClassLoader(urla); for (String name : names) { Class clazz = Class.forName(name, true, loader); if (clazz != null) { System.out.println("located clazz " + name); Plugin plugin = (Plugin) clazz.newInstance(); shell.add(plugin); } } shell.prepare(); shell.start(); } public void load(String path) throws Exception { File file = new File(path); List<String> paths = new LinkedList<String>(); for (File current : file.listFiles()) { String name = current.getName(); if (name.endsWith(".jar")) { paths.add(current.getAbsolutePath()); } } load(paths); } } |