From 3f5b8f5d9203aa7ffb7fbe9cdbaf9dba3da6cae6 Mon Sep 17 00:00:00 2001
From: Hybris95 <hybris_95@hotmail.com>
Date: Thu, 01 May 2014 16:14:15 -0400
Subject: [PATCH] Fixes sort, page building and search functions on "my tickets" page.

---
 src/main/java/com/gitblit/manager/PluginManager.java |   92 +++++++++++++++++++++++++++++++++++++++------
 1 files changed, 79 insertions(+), 13 deletions(-)

diff --git a/src/main/java/com/gitblit/manager/PluginManager.java b/src/main/java/com/gitblit/manager/PluginManager.java
index 9cefc88..2ee4855 100644
--- a/src/main/java/com/gitblit/manager/PluginManager.java
+++ b/src/main/java/com/gitblit/manager/PluginManager.java
@@ -42,10 +42,12 @@
 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;
@@ -63,6 +65,7 @@
  * the Dagger DI and retrieve extensions provided by active plugins.
  *
  * @author David Ostrovsky
+ * @author James Moger
  *
  */
 public class PluginManager implements IPluginManager, PluginStateListener {
@@ -80,8 +83,22 @@
 
 	public PluginManager(IRuntimeManager runtimeManager) {
 		File dir = runtimeManager.getFileOrFolder(Keys.plugins.folder, "${baseFolder}/plugins");
+		dir.mkdirs();
 		this.runtimeManager = runtimeManager;
+
 		this.pf4j = new DefaultPluginManager(dir);
+
+		try {
+			Version systemVersion = Version.createVersion(Constants.getVersion());
+			pf4j.setSystemVersion(systemVersion);
+		} catch (Exception e) {
+			logger.error(null, e);
+		}
+	}
+
+	@Override
+	public Version getSystemVersion() {
+		return pf4j.getSystemVersion();
 	}
 
 	@Override
@@ -125,8 +142,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
@@ -143,9 +197,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
@@ -161,7 +227,6 @@
 							(file.getName().toLowerCase().endsWith(".sha1")
 									|| file.getName().toLowerCase().endsWith(".md5"));
 				}
-
 			});
 
 			if (checksums != null) {
@@ -229,11 +294,11 @@
 	}
 
 	@Override
-	public synchronized boolean refreshRegistry() {
+	public synchronized boolean refreshRegistry(boolean verifyChecksum) {
 		String dr = "http://gitblit.github.io/gitblit-registry/plugins.json";
 		String url = runtimeManager.getSettings().getString(Keys.plugins.registry, dr);
 		try {
-			File file = download(url, true);
+			File file = download(url, verifyChecksum);
 			if (file != null && file.exists()) {
 				URL selfUrl = new URL(url.substring(0, url.lastIndexOf('/')));
 				// replace ${self} with the registry url
@@ -260,7 +325,7 @@
 		File[] files = folder.listFiles(jsonFilter);
 		if (files == null || files.length == 0) {
 			// automatically retrieve the registry if we don't have a local copy
-			refreshRegistry();
+			refreshRegistry(true);
 			files = folder.listFiles(jsonFilter);
 		}
 
@@ -295,9 +360,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();
@@ -311,7 +377,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();
 			}
 		}
@@ -319,9 +385,9 @@
 	}
 
 	@Override
-	public synchronized PluginRegistration lookupPlugin(String idOrName) {
+	public synchronized PluginRegistration lookupPlugin(String pluginId) {
 		for (PluginRegistration reg : getRegisteredPlugins()) {
-			if (reg.id.equalsIgnoreCase(idOrName) || reg.name.equalsIgnoreCase(idOrName)) {
+			if (reg.id.equalsIgnoreCase(pluginId)) {
 				return reg;
 			}
 		}
@@ -329,15 +395,15 @@
 	}
 
 	@Override
-	public synchronized PluginRelease lookupRelease(String idOrName, String version) {
-		PluginRegistration reg = lookupPlugin(idOrName);
+	public synchronized PluginRelease lookupRelease(String pluginId, String version) {
+		PluginRegistration reg = lookupPlugin(pluginId);
 		if (reg == null) {
 			return null;
 		}
 
 		PluginRelease pv;
 		if (StringUtils.isEmpty(version)) {
-			pv = reg.getCurrentRelease();
+			pv = reg.getCurrentRelease(getSystemVersion());
 		} else {
 			pv = reg.getRelease(version);
 		}

--
Gitblit v1.9.1