Initial plugin framework
This commit is contained in:
parent
c5d04e3741
commit
2492a85e88
@ -25,5 +25,6 @@
|
|||||||
<classpathentry kind="lib" path="lib-test/junit-dep-4.8.2.jar"/>
|
<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="lib-test/uispec4j-2.3-jdk16.jar"/>
|
||||||
<classpathentry kind="lib" path="resources"/>
|
<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"/>
|
<classpathentry kind="output" path="bin"/>
|
||||||
</classpath>
|
</classpath>
|
||||||
|
BIN
core/lib/jspf.core-1.0.2.jar
Normal file
BIN
core/lib/jspf.core-1.0.2.jar
Normal file
Binary file not shown.
45
core/src/net/sf/openrocket/plugin/AbstractService.java
Normal file
45
core/src/net/sf/openrocket/plugin/AbstractService.java
Normal file
@ -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);
|
||||||
|
|
||||||
|
}
|
50
core/src/net/sf/openrocket/plugin/JSPFPluginFactory.java
Normal file
50
core/src/net/sf/openrocket/plugin/JSPFPluginFactory.java
Normal file
@ -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;
|
||||||
|
|
||||||
|
}
|
||||||
|
}
|
11
core/src/net/sf/openrocket/plugin/PluginFactory.java
Normal file
11
core/src/net/sf/openrocket/plugin/PluginFactory.java
Normal file
@ -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);
|
||||||
|
|
||||||
|
}
|
13
core/src/net/sf/openrocket/plugin/Service.java
Normal file
13
core/src/net/sf/openrocket/plugin/Service.java
Normal file
@ -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);
|
||||||
|
|
||||||
|
|
||||||
|
}
|
21
core/src/net/sf/openrocket/plugin/example/ExampleMain.java
Normal file
21
core/src/net/sf/openrocket/plugin/example/ExampleMain.java
Normal file
@ -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("---------");
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
}
|
17
core/src/net/sf/openrocket/plugin/example/ExamplePlugin.java
Normal file
17
core/src/net/sf/openrocket/plugin/example/ExamplePlugin.java
Normal file
@ -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);
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
@ -0,0 +1,9 @@
|
|||||||
|
package net.sf.openrocket.plugin.example;
|
||||||
|
|
||||||
|
import net.xeoh.plugins.base.Plugin;
|
||||||
|
|
||||||
|
public interface ExamplePluginInterface extends Plugin {
|
||||||
|
|
||||||
|
public void print();
|
||||||
|
|
||||||
|
}
|
@ -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;
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
@ -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");
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
Loading…
x
Reference in New Issue
Block a user