<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>
--- /dev/null
+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);
+
+}
--- /dev/null
+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;
+
+ }
+}
--- /dev/null
+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);
+
+}
--- /dev/null
+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);
+
+
+}
--- /dev/null
+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("---------");
+ }
+
+
+}
--- /dev/null
+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);
+ }
+
+}
--- /dev/null
+package net.sf.openrocket.plugin.example;
+
+import net.xeoh.plugins.base.Plugin;
+
+public interface ExamplePluginInterface extends Plugin {
+
+ public void print();
+
+}
--- /dev/null
+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;
+ }
+
+}
--- /dev/null
+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");
+ }
+
+}