]> source.dussan.org Git - gitblit.git/commitdiff
Allow Plugins to have injected members and Extensions to be constructed 62/162/1
authorJames Moger <james.moger@gitblit.com>
Fri, 5 Sep 2014 00:17:17 +0000 (20:17 -0400)
committerJames Moger <james.moger@gitblit.com>
Fri, 5 Sep 2014 00:17:17 +0000 (20:17 -0400)
.classpath
build.moxie
gitblit.iml
src/main/java/com/gitblit/manager/PluginManager.java

index 3e6b8a90ff7867a99353aa7736a3f02d69cdbf5e..53faa53619abe6fe36a9bffc5c0baf58440a198f 100644 (file)
@@ -75,7 +75,7 @@
        <classpathentry kind="lib" path="ext/args4j-2.0.26.jar" sourcepath="ext/src/args4j-2.0.26.jar" />
        <classpathentry kind="lib" path="ext/jedis-2.3.1.jar" sourcepath="ext/src/jedis-2.3.1.jar" />
        <classpathentry kind="lib" path="ext/commons-pool2-2.0.jar" sourcepath="ext/src/commons-pool2-2.0.jar" />
-       <classpathentry kind="lib" path="ext/pf4j-0.8.0.jar" sourcepath="ext/src/pf4j-0.8.0.jar" />
+       <classpathentry kind="lib" path="ext/pf4j-0.9.0.jar" sourcepath="ext/src/pf4j-0.9.0.jar" />
        <classpathentry kind="lib" path="ext/tika-core-1.5.jar" sourcepath="ext/src/tika-core-1.5.jar" />
        <classpathentry kind="lib" path="ext/junit-4.11.jar" sourcepath="ext/src/junit-4.11.jar" />
        <classpathentry kind="lib" path="ext/hamcrest-core-1.3.jar" sourcepath="ext/src/hamcrest-core-1.3.jar" />
index b265115468998d9c29a144c89147f9de245373d6..ea2763c47599fb712961de0d1bc27d9ac17aa7ae 100644 (file)
@@ -176,7 +176,7 @@ dependencies:
 - compile 'args4j:args4j:2.0.26' :war :fedclient
 - compile 'commons-codec:commons-codec:1.7' :war
 - compile 'redis.clients:jedis:2.3.1' :war
-- compile 'ro.fortsoft.pf4j:pf4j:0.8.0' :war
+- compile 'ro.fortsoft.pf4j:pf4j:0.9.0' :war
 - compile 'org.apache.tika:tika-core:1.5' :war
 - test 'junit'
 # Dependencies for Selenium web page testing
index d36f3218d2696bdc605c7620c2620b4a56a4ec71..4061daf40621a6756934e8d11f3ac75286396f17 100644 (file)
       </library>
     </orderEntry>
     <orderEntry type="module-library">
-      <library name="pf4j-0.8.0.jar">
+      <library name="pf4j-0.9.0.jar">
         <CLASSES>
-          <root url="jar://$MODULE_DIR$/ext/pf4j-0.8.0.jar!/" />
+          <root url="jar://$MODULE_DIR$/ext/pf4j-0.9.0.jar!/" />
         </CLASSES>
         <JAVADOC />
         <SOURCES>
-          <root url="jar://$MODULE_DIR$/ext/src/pf4j-0.8.0.jar!/" />
+          <root url="jar://$MODULE_DIR$/ext/src/pf4j-0.9.0.jar!/" />
         </SOURCES>
       </library>
     </orderEntry>
index 874e24b5baa8ec3843385a23c04fe7791ca12804..583037505e8d499a7c25a4e68a7832bac9286360 100644 (file)
@@ -37,10 +37,12 @@ import java.util.TreeMap;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
 
-import ro.fortsoft.pf4j.DefaultExtensionFinder;
+import ro.fortsoft.pf4j.DefaultPluginFactory;
 import ro.fortsoft.pf4j.DefaultPluginManager;
-import ro.fortsoft.pf4j.ExtensionFinder;
+import ro.fortsoft.pf4j.ExtensionFactory;
+import ro.fortsoft.pf4j.Plugin;
 import ro.fortsoft.pf4j.PluginClassLoader;
+import ro.fortsoft.pf4j.PluginFactory;
 import ro.fortsoft.pf4j.PluginState;
 import ro.fortsoft.pf4j.PluginStateEvent;
 import ro.fortsoft.pf4j.PluginStateListener;
@@ -105,32 +107,18 @@ public class PluginManager implements IPluginManager, PluginStateListener {
        public PluginManager start() {
                File dir = runtimeManager.getFileOrFolder(Keys.plugins.folder, "${baseFolder}/plugins");
                dir.mkdirs();
+
                pf4j = new DefaultPluginManager(dir) {
+
+                       @Override
+                       protected PluginFactory createPluginFactory() {
+                               return new GuicePluginFactory();
+                       }
+
                        @Override
-                   protected ExtensionFinder createExtensionFinder() {
-                       DefaultExtensionFinder extensionFinder = new DefaultExtensionFinder(this) {
-                               @Override
-                                       protected ExtensionFactory createExtensionFactory() {
-                                       return new ExtensionFactory() {
-                                               @Override
-                                               public Object create(Class<?> extensionType) {
-                                                       // instantiate && inject the extension
-                                                       logger.debug("Create instance for extension '{}'", extensionType.getName());
-                                                       try {
-                                                               return runtimeManager.getInjector().getInstance(extensionType);
-                                                       } catch (Exception e) {
-                                                               logger.error(e.getMessage(), e);
-                                                       }
-                                                       return null;
-                                               }
-
-                                       };
-                               }
-                       };
-                       addPluginStateListener(extensionFinder);
-
-                       return extensionFinder;
-                   }
+                       protected ExtensionFactory createExtensionFactory() {
+                               return new GuiceExtensionFactory();
+                       }
                };
 
                try {
@@ -600,4 +588,41 @@ public class PluginManager implements IPluginManager, PluginStateListener {
        protected String getProxyAuthorization(URL url) {
                return "";
        }
+
+       /**
+        * Instantiates a plugin using pf4j but injects member fields
+        * with Guice.
+        */
+       private class GuicePluginFactory extends DefaultPluginFactory {
+
+               @Override
+               public Plugin create(PluginWrapper pluginWrapper) {
+                       // use pf4j to create the plugin
+                       Plugin plugin = super.create(pluginWrapper);
+
+                       if (plugin != null) {
+                               // allow Guice to inject member fields
+                               runtimeManager.getInjector().injectMembers(plugin);
+                       }
+
+                       return plugin;
+               }
+       }
+
+       /**
+        * Instantiates an extension using Guice.
+        */
+       private class GuiceExtensionFactory implements ExtensionFactory {
+               @Override
+               public Object create(Class<?> extensionClass) {
+                       // instantiate && inject the extension
+                       logger.debug("Create instance for extension '{}'", extensionClass.getName());
+                       try {
+                               return runtimeManager.getInjector().getInstance(extensionClass);
+                       } catch (Exception e) {
+                               logger.error(e.getMessage(), e);
+                       }
+                       return null;
+               }
+       }
 }