diff options
author | James Moger <james.moger@gitblit.com> | 2014-09-04 20:17:17 -0400 |
---|---|---|
committer | James Moger <james.moger@gitblit.com> | 2014-09-04 20:17:17 -0400 |
commit | 22ed6a50dd6742d5b3a964be4b7a8d7c2b58f88e (patch) | |
tree | 811226b3ebb95538152b380217173b6740b60161 /src/main/java | |
parent | 426f5c1729554d318989041457554f2abfa79d3e (diff) | |
download | gitblit-22ed6a50dd6742d5b3a964be4b7a8d7c2b58f88e.tar.gz gitblit-22ed6a50dd6742d5b3a964be4b7a8d7c2b58f88e.zip |
Allow Plugins to have injected members and Extensions to be constructed
Diffstat (limited to 'src/main/java')
-rw-r--r-- | src/main/java/com/gitblit/manager/PluginManager.java | 77 |
1 files changed, 51 insertions, 26 deletions
diff --git a/src/main/java/com/gitblit/manager/PluginManager.java b/src/main/java/com/gitblit/manager/PluginManager.java index 874e24b5..58303750 100644 --- a/src/main/java/com/gitblit/manager/PluginManager.java +++ b/src/main/java/com/gitblit/manager/PluginManager.java @@ -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; + } + } } |