James Moger
2014-05-16 aa1361d04cfe09f90e7d8bece90c00dd6e4185bb
src/main/java/com/gitblit/manager/PluginManager.java
@@ -34,6 +34,8 @@
import java.util.Map;
import java.util.TreeMap;
import javax.inject.Inject;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
@@ -42,10 +44,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 +67,7 @@
 * the Dagger DI and retrieve extensions provided by active plugins.
 *
 * @author David Ostrovsky
 * @author James Moger
 *
 */
public class PluginManager implements IPluginManager, PluginStateListener {
@@ -78,11 +83,25 @@
   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);
      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
@@ -126,10 +145,17 @@
         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);
@@ -138,11 +164,18 @@
         return false;
      }
      if (deletePlugin(pluginId)) {
      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);
@@ -167,9 +200,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
@@ -185,7 +230,6 @@
                     (file.getName().toLowerCase().endsWith(".sha1")
                           || file.getName().toLowerCase().endsWith(".md5"));
            }
         });
         if (checksums != null) {
@@ -322,7 +366,7 @@
      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();
@@ -336,7 +380,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();
         }
      }
@@ -362,7 +406,7 @@
      PluginRelease pv;
      if (StringUtils.isEmpty(version)) {
         pv = reg.getCurrentRelease();
         pv = reg.getCurrentRelease(getSystemVersion());
      } else {
         pv = reg.getRelease(version);
      }