Initial plugin framework
authorplaa <plaa@180e2498-e6e9-4542-8430-84ac67f01cd8>
Tue, 20 Mar 2012 20:19:38 +0000 (20:19 +0000)
committerplaa <plaa@180e2498-e6e9-4542-8430-84ac67f01cd8>
Tue, 20 Mar 2012 20:19:38 +0000 (20:19 +0000)
git-svn-id: https://openrocket.svn.sourceforge.net/svnroot/openrocket/trunk@477 180e2498-e6e9-4542-8430-84ac67f01cd8

core/.classpath
core/lib/jspf.core-1.0.2.jar [new file with mode: 0644]
core/src/net/sf/openrocket/plugin/AbstractService.java [new file with mode: 0644]
core/src/net/sf/openrocket/plugin/JSPFPluginFactory.java [new file with mode: 0644]
core/src/net/sf/openrocket/plugin/PluginFactory.java [new file with mode: 0644]
core/src/net/sf/openrocket/plugin/Service.java [new file with mode: 0644]
core/src/net/sf/openrocket/plugin/example/ExampleMain.java [new file with mode: 0644]
core/src/net/sf/openrocket/plugin/example/ExamplePlugin.java [new file with mode: 0644]
core/src/net/sf/openrocket/plugin/example/ExamplePluginInterface.java [new file with mode: 0644]
core/src/net/sf/openrocket/plugin/example/ExampleService.java [new file with mode: 0644]
core/src/net/sf/openrocket/plugin/example/ExampleSingletonPlugin.java [new file with mode: 0644]

index 10ef69c68136c740d1a14881d72754b76b29bd27..deeb71e4f008b01e4b4900ed6fd8b6533fd252a9 100644 (file)
@@ -25,5 +25,6 @@
        <classpathentry kind="lib" path="lib-test/junit-dep-4.8.2.jar"/>
        <classpathentry kind="lib" path="lib-test/uispec4j-2.3-jdk16.jar"/>
        <classpathentry kind="lib" path="resources"/>
+       <classpathentry kind="lib" path="lib/jspf.core-1.0.2.jar" sourcepath="/home/sampo/Projects/lib/jspf/documentation/api"/>
        <classpathentry kind="output" path="bin"/>
 </classpath>
diff --git a/core/lib/jspf.core-1.0.2.jar b/core/lib/jspf.core-1.0.2.jar
new file mode 100644 (file)
index 0000000..02dec5b
Binary files /dev/null and b/core/lib/jspf.core-1.0.2.jar differ
diff --git a/core/src/net/sf/openrocket/plugin/AbstractService.java b/core/src/net/sf/openrocket/plugin/AbstractService.java
new file mode 100644 (file)
index 0000000..0271964
--- /dev/null
@@ -0,0 +1,45 @@
+package net.sf.openrocket.plugin;
+
+import java.util.Collections;
+import java.util.List;
+
+import net.sf.openrocket.util.BugException;
+
+/**
+ * An abstract service implementation that returns plugins of type P.
+ * 
+ * @param <P>  the plugin type that this service returns.
+ * @author Sampo Niskanen <sampo.niskanen@iki.fi>
+ */
+public abstract class AbstractService<P> implements Service {
+       
+       private final Class<P> type;
+       
+       protected AbstractService(Class<P> type) {
+               this.type = type;
+       }
+       
+       @SuppressWarnings("unchecked")
+       @Override
+       public <E> List<E> getPlugins(Class<E> e, Object... args) {
+               
+               if (e != type) {
+                       return Collections.emptyList();
+               }
+               
+               List<P> plugins = getPlugins(args);
+               
+               // Check list content types to avoid mysterious bugs later on
+               for (P p : plugins) {
+                       if (!type.isInstance(p)) {
+                               throw new BugException("Requesting plugins of type " + type + " but received " +
+                                               ((p != null) ? p.getClass() : "null"));
+                       }
+               }
+               
+               return (List<E>) plugins;
+       }
+       
+       protected abstract List<P> getPlugins(Object... args);
+       
+}
diff --git a/core/src/net/sf/openrocket/plugin/JSPFPluginFactory.java b/core/src/net/sf/openrocket/plugin/JSPFPluginFactory.java
new file mode 100644 (file)
index 0000000..6b598b6
--- /dev/null
@@ -0,0 +1,50 @@
+package net.sf.openrocket.plugin;
+
+import java.net.URI;
+import java.net.URISyntaxException;
+import java.util.ArrayList;
+import java.util.List;
+
+import net.sf.openrocket.util.BugException;
+import net.xeoh.plugins.base.Plugin;
+import net.xeoh.plugins.base.PluginManager;
+import net.xeoh.plugins.base.impl.PluginManagerFactory;
+import net.xeoh.plugins.base.util.JSPFProperties;
+import net.xeoh.plugins.base.util.PluginManagerUtil;
+
+public class JSPFPluginFactory implements PluginFactory {
+       
+       private final PluginManager pluginManager;
+       
+       public JSPFPluginFactory() {
+               
+               final JSPFProperties props = new JSPFProperties();
+               
+               //              props.setProperty(PluginManager.class, "cache.enabled", "true");
+               //              props.setProperty(PluginManager.class, "cache.mode", "weak"); //optional
+               //              props.setProperty(PluginManager.class, "cache.file", "jspf.cache");
+               
+               try {
+                       pluginManager = PluginManagerFactory.createPluginManager(props);
+                       pluginManager.addPluginsFrom(new URI("classpath://*"));
+               } catch (URISyntaxException e) {
+                       throw new BugException(e);
+               }
+       }
+       
+       @Override
+       public <E extends Plugin> List<E> getPlugins(Class<E> e, Object... args) {
+               
+               List<E> plugins = new ArrayList<E>();
+               
+               PluginManagerUtil pluginManagerUtil = new PluginManagerUtil(pluginManager);
+               plugins.addAll(pluginManagerUtil.getPlugins(e));
+               
+               for (Service s : pluginManagerUtil.getPlugins(Service.class)) {
+                       plugins.addAll(s.getPlugins(e, args));
+               }
+               
+               return plugins;
+               
+       }
+}
diff --git a/core/src/net/sf/openrocket/plugin/PluginFactory.java b/core/src/net/sf/openrocket/plugin/PluginFactory.java
new file mode 100644 (file)
index 0000000..babf01d
--- /dev/null
@@ -0,0 +1,11 @@
+package net.sf.openrocket.plugin;
+
+import java.util.List;
+
+import net.xeoh.plugins.base.Plugin;
+
+public interface PluginFactory {
+       
+       public <E extends Plugin> List<E> getPlugins(Class<E> e, Object... args);
+       
+}
diff --git a/core/src/net/sf/openrocket/plugin/Service.java b/core/src/net/sf/openrocket/plugin/Service.java
new file mode 100644 (file)
index 0000000..f19c6b1
--- /dev/null
@@ -0,0 +1,13 @@
+package net.sf.openrocket.plugin;
+
+import java.util.List;
+
+import net.xeoh.plugins.base.Plugin;
+
+public interface Service extends Plugin {
+       
+       
+       public <E> List<E> getPlugins(Class<E> e, Object... args);
+       
+       
+}
diff --git a/core/src/net/sf/openrocket/plugin/example/ExampleMain.java b/core/src/net/sf/openrocket/plugin/example/ExampleMain.java
new file mode 100644 (file)
index 0000000..c45afee
--- /dev/null
@@ -0,0 +1,21 @@
+package net.sf.openrocket.plugin.example;
+
+import net.sf.openrocket.plugin.JSPFPluginFactory;
+import net.sf.openrocket.plugin.PluginFactory;
+
+public class ExampleMain {
+       
+       
+       public static void main(String[] args) {
+               PluginFactory factory = new JSPFPluginFactory();
+               
+               System.out.println("Plugins:");
+               System.out.println("---------");
+               for (ExamplePluginInterface plugin : factory.getPlugins(ExamplePluginInterface.class)) {
+                       plugin.print();
+               }
+               System.out.println("---------");
+       }
+       
+       
+}
diff --git a/core/src/net/sf/openrocket/plugin/example/ExamplePlugin.java b/core/src/net/sf/openrocket/plugin/example/ExamplePlugin.java
new file mode 100644 (file)
index 0000000..4579f9e
--- /dev/null
@@ -0,0 +1,17 @@
+package net.sf.openrocket.plugin.example;
+
+
+public class ExamplePlugin implements ExamplePluginInterface {
+       
+       private final String str;
+       
+       public ExamplePlugin(String str) {
+               this.str = str;
+       }
+       
+       @Override
+       public void print() {
+               System.out.println("ExamplePlugin: " + str);
+       }
+       
+}
diff --git a/core/src/net/sf/openrocket/plugin/example/ExamplePluginInterface.java b/core/src/net/sf/openrocket/plugin/example/ExamplePluginInterface.java
new file mode 100644 (file)
index 0000000..ddf157d
--- /dev/null
@@ -0,0 +1,9 @@
+package net.sf.openrocket.plugin.example;
+
+import net.xeoh.plugins.base.Plugin;
+
+public interface ExamplePluginInterface extends Plugin {
+       
+       public void print();
+       
+}
diff --git a/core/src/net/sf/openrocket/plugin/example/ExampleService.java b/core/src/net/sf/openrocket/plugin/example/ExampleService.java
new file mode 100644 (file)
index 0000000..9960bcd
--- /dev/null
@@ -0,0 +1,24 @@
+package net.sf.openrocket.plugin.example;
+
+import java.util.ArrayList;
+import java.util.List;
+
+import net.sf.openrocket.plugin.AbstractService;
+import net.xeoh.plugins.base.annotations.PluginImplementation;
+
+@PluginImplementation
+public class ExampleService extends AbstractService<ExamplePluginInterface> {
+       
+       protected ExampleService() {
+               super(ExamplePluginInterface.class);
+       }
+       
+       @Override
+       protected List<ExamplePluginInterface> getPlugins(Object... args) {
+               List<ExamplePluginInterface> plugins = new ArrayList<ExamplePluginInterface>();
+               plugins.add(new ExamplePlugin("a"));
+               plugins.add(new ExamplePlugin("b"));
+               return plugins;
+       }
+       
+}
diff --git a/core/src/net/sf/openrocket/plugin/example/ExampleSingletonPlugin.java b/core/src/net/sf/openrocket/plugin/example/ExampleSingletonPlugin.java
new file mode 100644 (file)
index 0000000..aa01518
--- /dev/null
@@ -0,0 +1,13 @@
+package net.sf.openrocket.plugin.example;
+
+import net.xeoh.plugins.base.annotations.PluginImplementation;
+
+@PluginImplementation
+public class ExampleSingletonPlugin implements ExamplePluginInterface {
+       
+       @Override
+       public void print() {
+               System.out.println("ExampleSingletonPlugin");
+       }
+       
+}