|
37 | 37 | import org.slf4j.Logger; |
38 | 38 | import org.slf4j.LoggerFactory; |
39 | 39 |
|
40 | | -import ro.fortsoft.pf4j.DefaultExtensionFinder; |
| 40 | +import ro.fortsoft.pf4j.DefaultPluginFactory; |
41 | 41 | import ro.fortsoft.pf4j.DefaultPluginManager; |
42 | | -import ro.fortsoft.pf4j.ExtensionFinder; |
| 42 | +import ro.fortsoft.pf4j.ExtensionFactory; |
| 43 | +import ro.fortsoft.pf4j.Plugin; |
43 | 44 | import ro.fortsoft.pf4j.PluginClassLoader; |
| 45 | +import ro.fortsoft.pf4j.PluginFactory; |
44 | 46 | import ro.fortsoft.pf4j.PluginState; |
45 | 47 | import ro.fortsoft.pf4j.PluginStateEvent; |
46 | 48 | import ro.fortsoft.pf4j.PluginStateListener; |
@@ -105,32 +107,18 @@ public void pluginStateChanged(PluginStateEvent event) { |
105 | 107 | public PluginManager start() { |
106 | 108 | File dir = runtimeManager.getFileOrFolder(Keys.plugins.folder, "${baseFolder}/plugins"); |
107 | 109 | dir.mkdirs(); |
| 110 | + |
108 | 111 | pf4j = new DefaultPluginManager(dir) { |
| 112 | + |
| 113 | + @Override |
| 114 | + protected PluginFactory createPluginFactory() { |
| 115 | + return new GuicePluginFactory(); |
| 116 | + } |
| 117 | + |
109 | 118 | @Override |
110 | | - protected ExtensionFinder createExtensionFinder() { |
111 | | - DefaultExtensionFinder extensionFinder = new DefaultExtensionFinder(this) { |
112 | | - @Override |
113 | | - protected ExtensionFactory createExtensionFactory() { |
114 | | - return new ExtensionFactory() { |
115 | | - @Override |
116 | | - public Object create(Class<?> extensionType) { |
117 | | - // instantiate && inject the extension |
118 | | - logger.debug("Create instance for extension '{}'", extensionType.getName()); |
119 | | - try { |
120 | | - return runtimeManager.getInjector().getInstance(extensionType); |
121 | | - } catch (Exception e) { |
122 | | - logger.error(e.getMessage(), e); |
123 | | - } |
124 | | - return null; |
125 | | - } |
126 | | - |
127 | | - }; |
128 | | - } |
129 | | - }; |
130 | | - addPluginStateListener(extensionFinder); |
131 | | - |
132 | | - return extensionFinder; |
133 | | - } |
| 119 | + protected ExtensionFactory createExtensionFactory() { |
| 120 | + return new GuiceExtensionFactory(); |
| 121 | + } |
134 | 122 | }; |
135 | 123 |
|
136 | 124 | try { |
@@ -600,4 +588,41 @@ protected Proxy getProxy(URL url) { |
600 | 588 | protected String getProxyAuthorization(URL url) { |
601 | 589 | return ""; |
602 | 590 | } |
| 591 | + |
| 592 | + /** |
| 593 | + * Instantiates a plugin using pf4j but injects member fields |
| 594 | + * with Guice. |
| 595 | + */ |
| 596 | + private class GuicePluginFactory extends DefaultPluginFactory { |
| 597 | + |
| 598 | + @Override |
| 599 | + public Plugin create(PluginWrapper pluginWrapper) { |
| 600 | + // use pf4j to create the plugin |
| 601 | + Plugin plugin = super.create(pluginWrapper); |
| 602 | + |
| 603 | + if (plugin != null) { |
| 604 | + // allow Guice to inject member fields |
| 605 | + runtimeManager.getInjector().injectMembers(plugin); |
| 606 | + } |
| 607 | + |
| 608 | + return plugin; |
| 609 | + } |
| 610 | + } |
| 611 | + |
| 612 | + /** |
| 613 | + * Instantiates an extension using Guice. |
| 614 | + */ |
| 615 | + private class GuiceExtensionFactory implements ExtensionFactory { |
| 616 | + @Override |
| 617 | + public Object create(Class<?> extensionClass) { |
| 618 | + // instantiate && inject the extension |
| 619 | + logger.debug("Create instance for extension '{}'", extensionClass.getName()); |
| 620 | + try { |
| 621 | + return runtimeManager.getInjector().getInstance(extensionClass); |
| 622 | + } catch (Exception e) { |
| 623 | + logger.error(e.getMessage(), e); |
| 624 | + } |
| 625 | + return null; |
| 626 | + } |
| 627 | + } |
603 | 628 | } |
0 commit comments