James Moger
2014-10-23 c57a28630e1ca45282137419e13ae88a954cc11b
src/main/java/com/gitblit/manager/PluginManager.java
@@ -34,13 +34,15 @@
import java.util.Map;
import java.util.TreeMap;
import javax.inject.Inject;
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;
@@ -60,6 +62,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
@@ -70,13 +74,14 @@
 * @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;
@@ -85,18 +90,7 @@
   @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
@@ -111,6 +105,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();
@@ -424,6 +440,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");
@@ -437,7 +457,7 @@
      }
      if (sha1File == null && md5File == null && verifyChecksum) {
      if (sha1File == null && md5File == null) {
         throw new IOException("Missing SHA1 and MD5 checksums for " + url);
      }
@@ -572,4 +592,41 @@
   protected String getProxyAuthorization(URL url) {
      return "";
   }
   /**
    * 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;
      }
   }
}