From fbc7a7dd5fa61486610cf11c09e0007f2900a3e1 Mon Sep 17 00:00:00 2001
From: James Moger <james.moger@gitblit.com>
Date: Wed, 03 Dec 2014 15:05:49 -0500
Subject: [PATCH] Add support for specifying the `Proxy-Authorization` header for the PluginManager
---
src/main/java/com/gitblit/manager/PluginManager.java | 162 +++++++++++++++++++++++++++++++++++++++++++++++++----
1 files changed, 148 insertions(+), 14 deletions(-)
diff --git a/src/main/java/com/gitblit/manager/PluginManager.java b/src/main/java/com/gitblit/manager/PluginManager.java
index 5bd97ea..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,15 +38,21 @@
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
+import ro.fortsoft.pf4j.DefaultPluginFactory;
import ro.fortsoft.pf4j.DefaultPluginManager;
+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;
-import ro.fortsoft.pf4j.PluginVersion;
import ro.fortsoft.pf4j.PluginWrapper;
+import ro.fortsoft.pf4j.Version;
+import com.gitblit.Constants;
import com.gitblit.Keys;
+import com.gitblit.extensions.GitblitPlugin;
import com.gitblit.models.PluginRegistry;
import com.gitblit.models.PluginRegistry.InstallState;
import com.gitblit.models.PluginRegistry.PluginRegistration;
@@ -56,6 +63,8 @@
import com.gitblit.utils.StringUtils;
import com.google.common.io.Files;
import com.google.common.io.InputSupplier;
+import com.google.inject.Inject;
+import com.google.inject.Singleton;
/**
* The plugin manager maintains the lifecycle of plugins. It is exposed as
@@ -63,26 +72,31 @@
* the Dagger DI and retrieve extensions provided by active plugins.
*
* @author David Ostrovsky
+ * @author James Moger
*
*/
+@Singleton
public class PluginManager implements IPluginManager, PluginStateListener {
private final Logger logger = LoggerFactory.getLogger(getClass());
- private final DefaultPluginManager pf4j;
-
private final IRuntimeManager runtimeManager;
+
+ private DefaultPluginManager pf4j;
// timeout defaults of Maven 3.0.4 in seconds
private int connectTimeout = 20;
private int readTimeout = 12800;
+ @Inject
public PluginManager(IRuntimeManager runtimeManager) {
- File dir = runtimeManager.getFileOrFolder(Keys.plugins.folder, "${baseFolder}/plugins");
- dir.mkdirs();
this.runtimeManager = runtimeManager;
- this.pf4j = new DefaultPluginManager(dir);
+ }
+
+ @Override
+ public Version getSystemVersion() {
+ return pf4j.getSystemVersion();
}
@Override
@@ -92,6 +106,28 @@
@Override
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 ExtensionFactory createExtensionFactory() {
+ return new GuiceExtensionFactory();
+ }
+ };
+
+ try {
+ Version systemVersion = Version.createVersion(Constants.getVersion());
+ pf4j.setSystemVersion(systemVersion);
+ } catch (Exception e) {
+ logger.error(null, e);
+ }
pf4j.loadPlugins();
logger.debug("Starting plugins");
pf4j.startPlugins();
@@ -126,8 +162,45 @@
return false;
}
+ // allow the plugin to prepare for operation after installation
+ PluginWrapper pluginWrapper = pf4j.getPlugin(pluginId);
+ if (pluginWrapper.getPlugin() instanceof GitblitPlugin) {
+ ((GitblitPlugin) pluginWrapper.getPlugin()).onInstall();
+ }
+
PluginState state = pf4j.startPlugin(pluginId);
return PluginState.STARTED.equals(state);
+ }
+
+ @Override
+ public synchronized boolean upgradePlugin(String pluginId, String url, boolean verifyChecksum) throws IOException {
+ // ensure we can download the update BEFORE we remove the existing one
+ File file = download(url, verifyChecksum);
+ if (file == null || !file.exists()) {
+ logger.error("Failed to download plugin {}", url);
+ return false;
+ }
+
+ Version oldVersion = pf4j.getPlugin(pluginId).getDescriptor().getVersion();
+ if (removePlugin(pluginId, false)) {
+ String newPluginId = pf4j.loadPlugin(file);
+ if (StringUtils.isEmpty(newPluginId)) {
+ logger.error("Failed to load plugin {}", file);
+ return false;
+ }
+
+ // the plugin to handle an upgrade
+ PluginWrapper pluginWrapper = pf4j.getPlugin(newPluginId);
+ if (pluginWrapper.getPlugin() instanceof GitblitPlugin) {
+ ((GitblitPlugin) pluginWrapper.getPlugin()).onUpgrade(oldVersion);
+ }
+
+ PluginState state = pf4j.startPlugin(newPluginId);
+ return PluginState.STARTED.equals(state);
+ } else {
+ logger.error("Failed to delete plugin {}", pluginId);
+ }
+ return false;
}
@Override
@@ -144,9 +217,21 @@
}
@Override
- public synchronized boolean deletePlugin(String pluginId) {
+ public synchronized boolean uninstallPlugin(String pluginId) {
+ return removePlugin(pluginId, true);
+ }
+
+ protected boolean removePlugin(String pluginId, boolean isUninstall) {
PluginWrapper pluginWrapper = getPlugin(pluginId);
final String name = pluginWrapper.getPluginPath().substring(1);
+
+ if (isUninstall) {
+ // allow the plugin to prepare for uninstallation
+ if (pluginWrapper.getPlugin() instanceof GitblitPlugin) {
+ ((GitblitPlugin) pluginWrapper.getPlugin()).onUninstall();
+ }
+ }
+
if (pf4j.deletePlugin(pluginId)) {
// delete the checksums
@@ -162,7 +247,6 @@
(file.getName().toLowerCase().endsWith(".sha1")
|| file.getName().toLowerCase().endsWith(".md5"));
}
-
});
if (checksums != null) {
@@ -296,9 +380,10 @@
map.put(reg.id, reg);
}
}
+
for (PluginWrapper pw : pf4j.getPlugins()) {
String id = pw.getDescriptor().getPluginId();
- PluginVersion pv = pw.getDescriptor().getVersion();
+ Version pv = pw.getDescriptor().getVersion();
PluginRegistration reg = map.get(id);
if (reg != null) {
reg.installedRelease = pv.toString();
@@ -312,7 +397,7 @@
List<PluginRegistration> list = getRegisteredPlugins();
Iterator<PluginRegistration> itr = list.iterator();
while (itr.hasNext()) {
- if (state != itr.next().getInstallState()) {
+ if (state != itr.next().getInstallState(getSystemVersion())) {
itr.remove();
}
}
@@ -338,7 +423,7 @@
PluginRelease pv;
if (StringUtils.isEmpty(version)) {
- pv = reg.getCurrentRelease();
+ pv = reg.getCurrentRelease(getSystemVersion());
} else {
pv = reg.getRelease(version);
}
@@ -356,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");
@@ -369,7 +458,7 @@
}
- if (sha1File == null && md5File == null && verifyChecksum) {
+ if (sha1File == null && md5File == null) {
throw new IOException("Missing SHA1 and MD5 checksums for " + url);
}
@@ -498,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