Author: pwang
Date: 2011-11-18 16:13:21 -0800 (Fri, 18 Nov 2011)
New Revision: 27528
Added:
core3/impl/trunk/plugin-impl/src/main/java/org/cytoscape/plugin/internal/PluginLoaderTask2.java
core3/impl/trunk/plugin-impl/src/main/java/org/cytoscape/plugin/internal/PluginLoaderTaskFactory2.java
Log:
Original creation
Added:
core3/impl/trunk/plugin-impl/src/main/java/org/cytoscape/plugin/internal/PluginLoaderTask2.java
===================================================================
---
core3/impl/trunk/plugin-impl/src/main/java/org/cytoscape/plugin/internal/PluginLoaderTask2.java
(rev 0)
+++
core3/impl/trunk/plugin-impl/src/main/java/org/cytoscape/plugin/internal/PluginLoaderTask2.java
2011-11-19 00:13:21 UTC (rev 27528)
@@ -0,0 +1,79 @@
+package org.cytoscape.plugin.internal;
+
+
+import java.io.File;
+import java.lang.reflect.Constructor;
+import java.net.URL;
+import java.net.URLClassLoader;
+import java.util.Set;
+import java.util.jar.JarFile;
+
+import org.cytoscape.plugin.CyPluginAdapter;
+import org.cytoscape.work.AbstractTask;
+import org.cytoscape.work.TaskMonitor;
+import org.cytoscape.work.Tunable;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+
+public class PluginLoaderTask2 extends AbstractTask {
+
+ private static final Logger logger = LoggerFactory.getLogger(
PluginLoaderTask2.class );
+ private static final String PLUGIN_TAG = "Cytoscape-Plugin";
+
+ private CyPluginAdapter adapter;
+
+ private File filename;
+
+ // All plugin URLs are saved here. This means plugin conflict can
happen in this ClassLoader.
+ // If plugin developers want to avoid it, they need to try regular
bundle plugin.
+ private Set<URL> urls= PluginLoaderTaskFactory.urls;
+
+ public PluginLoaderTask2(CyPluginAdapter adapter) {
+ this.adapter = adapter;
+ }
+
+ public void setFile(File filename){
+ this.filename = filename;
+ }
+
+ @Override
+ public void run(TaskMonitor tm) throws Exception {
+ Object plugin = null;
+ JarFile jar = null;
+
+ try {
+ jar = new JarFile(filename);
+ logger.debug("attempting to load simple plugin jar: " +
filename);
+
+ final String name =
jar.getManifest().getMainAttributes().getValue(PLUGIN_TAG);
+ logger.debug("attempting to load CyPlugin class: " +
name);
+ if ( name == null || name.isEmpty() )
+ throw new IllegalArgumentException("This plugin
jar is missing the \"Cytoscape-Plugin: your.package.YourCyPlugin\" entry in the
META-INF/MANIFEST.MF file. Without that entry we can't start the plugin!");
+
+ final URL jarurl = filename.toURI().toURL();
+ final ClassLoader parentLoader =
adapter.getClass().getClassLoader();
+ urls.add(jarurl);
+ final MyClassLoader ucl = new
MyClassLoader(parentLoader);
+ final Class<?> c = ucl.loadClass(name);
+
+ final Constructor<?> con =
c.getConstructor(CyPluginAdapter.class);
+ plugin = con.newInstance(adapter);
+ logger.info("Plugin loaded: " + plugin);
+ } finally {
+ if (jar != null)
+ jar.close();
+ }
+ }
+
+ @Override
+ public void cancel() {
+ // TODO: Implement this!
+ }
+
+ private final class MyClassLoader extends URLClassLoader {
+ MyClassLoader(ClassLoader parent) {
+ super(urls.toArray(new URL[0]), parent);
+ }
+ }
+}
Added:
core3/impl/trunk/plugin-impl/src/main/java/org/cytoscape/plugin/internal/PluginLoaderTaskFactory2.java
===================================================================
---
core3/impl/trunk/plugin-impl/src/main/java/org/cytoscape/plugin/internal/PluginLoaderTaskFactory2.java
(rev 0)
+++
core3/impl/trunk/plugin-impl/src/main/java/org/cytoscape/plugin/internal/PluginLoaderTaskFactory2.java
2011-11-19 00:13:21 UTC (rev 27528)
@@ -0,0 +1,22 @@
+package org.cytoscape.plugin.internal;
+
+
+import org.cytoscape.work.TaskFactory;
+import org.cytoscape.work.TaskIterator;
+
+
+public class PluginLoaderTaskFactory2 implements TaskFactory {
+
+ private PluginLoaderTask2 task = null;
+
+ PluginLoaderTaskFactory2() {
+ }
+
+ public TaskIterator getTaskIterator() {
+ return new TaskIterator(task);
+ }
+
+ public void setTask(PluginLoaderTask2 task){
+ this.task = task;
+ }
+}
--
You received this message because you are subscribed to the Google Groups
"cytoscape-cvs" group.
To post to this group, send email to [email protected].
To unsubscribe from this group, send email to
[email protected].
For more options, visit this group at
http://groups.google.com/group/cytoscape-cvs?hl=en.