]> source.dussan.org Git - gitblit.git/commitdiff
Support injection of plugin Extensions
authorJames Moger <james.moger@gitblit.com>
Thu, 3 Jul 2014 18:30:22 +0000 (14:30 -0400)
committerJames Moger <james.moger@gitblit.com>
Thu, 3 Jul 2014 21:00:41 +0000 (17:00 -0400)
src/main/java/com/gitblit/manager/PluginManager.java

index a35ff6feb8b04a7558959ecd798417d526628e6a..874e24b5baa8ec3843385a23c04fe7791ca12804 100644 (file)
@@ -37,7 +37,9 @@ import java.util.TreeMap;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
 
+import ro.fortsoft.pf4j.DefaultExtensionFinder;
 import ro.fortsoft.pf4j.DefaultPluginManager;
+import ro.fortsoft.pf4j.ExtensionFinder;
 import ro.fortsoft.pf4j.PluginClassLoader;
 import ro.fortsoft.pf4j.PluginState;
 import ro.fortsoft.pf4j.PluginStateEvent;
@@ -103,7 +105,33 @@ public class PluginManager implements IPluginManager, PluginStateListener {
        public PluginManager start() {
                File dir = runtimeManager.getFileOrFolder(Keys.plugins.folder, "${baseFolder}/plugins");
                dir.mkdirs();
-               pf4j = new DefaultPluginManager(dir);
+               pf4j = new DefaultPluginManager(dir) {
+                       @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;
+                   }
+               };
 
                try {
                        Version systemVersion = Version.createVersion(Constants.getVersion());