From a9a2ffcf9a34bd25fe2e05bfdd4cde74725bb17d Mon Sep 17 00:00:00 2001 From: Milos Cubrilo <milos.cubrilo@gmail.com> Date: Sun, 11 Jan 2015 07:41:29 -0500 Subject: [PATCH] #230 - Improve empty folder navigation. --- src/main/java/com/gitblit/manager/PluginManager.java | 94 +++++++++++++++++++++++++++++++++-------------- 1 files changed, 66 insertions(+), 28 deletions(-) diff --git a/src/main/java/com/gitblit/manager/PluginManager.java b/src/main/java/com/gitblit/manager/PluginManager.java index 874e24b..bc3be52 100644 --- a/src/main/java/com/gitblit/manager/PluginManager.java +++ b/src/main/java/com/gitblit/manager/PluginManager.java @@ -22,6 +22,7 @@ import java.io.IOException; import java.io.InputStream; import java.net.HttpURLConnection; +import java.net.InetSocketAddress; import java.net.Proxy; import java.net.URL; import java.net.URLConnection; @@ -37,10 +38,12 @@ 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 +108,18 @@ public PluginManager start() { File dir = runtimeManager.getFileOrFolder(Keys.plugins.folder, "${baseFolder}/plugins"); dir.mkdirs(); + 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; - } + protected PluginFactory createPluginFactory() { + return new GuicePluginFactory(); + } - }; - } - }; - addPluginStateListener(extensionFinder); - - return extensionFinder; - } + @Override + protected ExtensionFactory createExtensionFactory() { + return new GuiceExtensionFactory(); + } }; try { @@ -452,6 +441,10 @@ protected File download(String url, boolean verifyChecksum) throws IOException { File file = downloadFile(url); + if (!verifyChecksum) { + return file; + } + File sha1File = null; try { sha1File = downloadFile(url + ".sha1"); @@ -465,7 +458,7 @@ } - if (sha1File == null && md5File == null && verifyChecksum) { + if (sha1File == null && md5File == null) { throw new IOException("Missing SHA1 and MD5 checksums for " + url); } @@ -594,10 +587,55 @@ } protected Proxy getProxy(URL url) { - return java.net.Proxy.NO_PROXY; + String proxyHost = runtimeManager.getSettings().getString(Keys.plugins.httpProxyHost, ""); + String proxyPort = runtimeManager.getSettings().getString(Keys.plugins.httpProxyPort, ""); + + if (!StringUtils.isEmpty(proxyHost) && !StringUtils.isEmpty(proxyPort)) { + return new Proxy(Proxy.Type.HTTP, new InetSocketAddress(proxyHost, Integer.parseInt(proxyPort))); + } else { + return java.net.Proxy.NO_PROXY; + } } protected String getProxyAuthorization(URL url) { - return ""; + String proxyAuth = runtimeManager.getSettings().getString(Keys.plugins.httpProxyAuthorization, ""); + return proxyAuth; + } + + /** + * 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; + } } } -- Gitblit v1.9.1