Author: kono
Date: 2011-11-01 17:02:29 -0700 (Tue, 01 Nov 2011)
New Revision: 27366

Modified:
   core3/impl/trunk/plugin-impl/osgi.bnd
   
core3/impl/trunk/plugin-impl/src/main/java/org/cytoscape/plugin/internal/PluginLoaderTask.java
   
core3/impl/trunk/plugin-impl/src/main/java/org/cytoscape/plugin/internal/PluginLoaderTaskFactory.java
   
core3/impl/trunk/plugin-impl/src/test/java/org/cytoscape/plugin/internal/PluginLoaderTaskFactoryTest.java
Log:
Simple Plugin loader problem had been fixed.  Each Jar URLs will be stored in 
the task factory, and plugins can access all other plugins just like 2.x 
series.  This is only for ease of development who prefer simple plugin. 

Modified: core3/impl/trunk/plugin-impl/osgi.bnd
===================================================================
--- core3/impl/trunk/plugin-impl/osgi.bnd       2011-11-01 23:45:45 UTC (rev 
27365)
+++ core3/impl/trunk/plugin-impl/osgi.bnd       2011-11-02 00:02:29 UTC (rev 
27366)
@@ -2,6 +2,6 @@
 # Use this file to add customized Bnd instructions for the bundle
 #-----------------------------------------------------------------
 
-Bundle-Activator:  ${bundle.namespace}.internal.CyActivator
+Bundle-Activator: ${bundle.namespace}.internal.CyActivator
 Private-Package: ${bundle.namespace}.internal, ${bundle.namespace}.internal.*
-
+DynamicImport-Package: *

Modified: 
core3/impl/trunk/plugin-impl/src/main/java/org/cytoscape/plugin/internal/PluginLoaderTask.java
===================================================================
--- 
core3/impl/trunk/plugin-impl/src/main/java/org/cytoscape/plugin/internal/PluginLoaderTask.java
      2011-11-01 23:45:45 UTC (rev 27365)
+++ 
core3/impl/trunk/plugin-impl/src/main/java/org/cytoscape/plugin/internal/PluginLoaderTask.java
      2011-11-02 00:02:29 UTC (rev 27366)
@@ -1,51 +1,63 @@
 package org.cytoscape.plugin.internal;
 
 
-import org.cytoscape.plugin.CyPlugin;
+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.cytoscape.work.TaskMonitor;
-
-import java.util.jar.JarFile;
-import java.util.jar.Manifest;
-import java.lang.reflect.Constructor;
-import java.io.File;
-import java.net.URLClassLoader;
-import java.net.URL;
-
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
 
 
 public class PluginLoaderTask extends AbstractTask {
+       
        private static final Logger logger = LoggerFactory.getLogger( 
PluginLoaderTask.class );
-       private CyPluginAdapter adapter;
+       private static final String PLUGIN_TAG = "Cytoscape-Plugin";
+       
+       private final CyPluginAdapter adapter;
 
        @Tunable(description="Select plugin JAR to load",params="input=true")
        public File filename;
-
-       PluginLoaderTask(CyPluginAdapter adapter) {
+       
+       // 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 final Set<URL> urls;
+       
+       PluginLoaderTask(final CyPluginAdapter adapter, final Set<URL> urls) {
                this.adapter = adapter;
+               this.urls = urls;
        }
 
        @Override
        public void run(TaskMonitor tm) throws Exception {
-               Object o = null; 
-               JarFile jar = null; 
+               Object plugin = null;
+               JarFile jar = null;
+               
                try {
                        jar = new JarFile(filename);
                        logger.debug("attempting to load simple plugin jar: " + 
filename);
-                       String name = 
jar.getManifest().getMainAttributes().getValue("Cytoscape-Plugin");
-                       logger.debug("attempting to load CyPlugin class: " + 
name);     
+                       
+                       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!");
-                       URL jarurl = filename.toURI().toURL(); 
-                       URLClassLoader ucl = URLClassLoader.newInstance( new 
URL[]{jarurl}, 
-                                                             
PluginLoaderTask.class.getClassLoader() );
-                       Class c = ucl.loadClass(name);
-                       Constructor<CyPlugin> con = 
c.getConstructor(CyPluginAdapter.class);
-                       o = con.newInstance(adapter);
+                       
+                       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();
@@ -54,5 +66,12 @@
 
        @Override
        public void cancel() {
+               // TODO: Implement this!
        }
+       
+       private final class MyClassLoader extends URLClassLoader {
+               MyClassLoader(ClassLoader parent) {
+                       super(urls.toArray(new URL[0]), parent);
+               }       
+       }
 }

Modified: 
core3/impl/trunk/plugin-impl/src/main/java/org/cytoscape/plugin/internal/PluginLoaderTaskFactory.java
===================================================================
--- 
core3/impl/trunk/plugin-impl/src/main/java/org/cytoscape/plugin/internal/PluginLoaderTaskFactory.java
       2011-11-01 23:45:45 UTC (rev 27365)
+++ 
core3/impl/trunk/plugin-impl/src/main/java/org/cytoscape/plugin/internal/PluginLoaderTaskFactory.java
       2011-11-02 00:02:29 UTC (rev 27366)
@@ -1,19 +1,28 @@
 package org.cytoscape.plugin.internal;
 
 
+import java.net.URL;
+import java.util.HashSet;
+import java.util.Set;
+
 import org.cytoscape.plugin.CyPluginAdapter;
 import org.cytoscape.work.TaskFactory;
 import org.cytoscape.work.TaskIterator;
 
 
 public class PluginLoaderTaskFactory implements TaskFactory {
-       CyPluginAdapter adapter;
+       
+       private final CyPluginAdapter adapter;
+       
+       // Plugin Jar file URLs
+       private final Set<URL> urls;
 
-       PluginLoaderTaskFactory(CyPluginAdapter adapter) {
+       PluginLoaderTaskFactory(final CyPluginAdapter adapter) {
                this.adapter = adapter;
+               urls = new HashSet<URL>();
        }
 
        public TaskIterator getTaskIterator() {
-               return new TaskIterator(new PluginLoaderTask(adapter));
+               return new TaskIterator(new PluginLoaderTask(adapter, urls));
        }
 }

Modified: 
core3/impl/trunk/plugin-impl/src/test/java/org/cytoscape/plugin/internal/PluginLoaderTaskFactoryTest.java
===================================================================
--- 
core3/impl/trunk/plugin-impl/src/test/java/org/cytoscape/plugin/internal/PluginLoaderTaskFactoryTest.java
   2011-11-01 23:45:45 UTC (rev 27365)
+++ 
core3/impl/trunk/plugin-impl/src/test/java/org/cytoscape/plugin/internal/PluginLoaderTaskFactoryTest.java
   2011-11-02 00:02:29 UTC (rev 27366)
@@ -3,6 +3,7 @@
 import static org.junit.Assert.assertNotNull;
 import static org.junit.Assert.assertTrue;
 import static org.mockito.Mockito.mock;
+
 import org.cytoscape.plugin.CyPluginAdapter;
 import org.cytoscape.work.Task;
 import org.cytoscape.work.TaskIterator;
@@ -11,16 +12,16 @@
 public class PluginLoaderTaskFactoryTest {
        @Test
        public void testGetTaskIterator() {
-               
-               CyPluginAdapter adapter = mock(CyPluginAdapter.class);
-               
+
+               final CyPluginAdapter adapter = mock(CyPluginAdapter.class);
+
                PluginLoaderTaskFactory factory = new 
PluginLoaderTaskFactory(adapter);
-                                               
+
                TaskIterator ti = factory.getTaskIterator();
                assertNotNull(ti);
-               
-               assertTrue( ti.hasNext() );
+
+               assertTrue(ti.hasNext());
                Task t = ti.next();
-               assertNotNull( t );             
+               assertNotNull(t);
        }
 }

-- 
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.

Reply via email to