James Moger
2014-05-24 8f50853cf767c869d63f39c23073b6beb7e0593d
src/main/java/com/gitblit/servlet/GitblitContext.java
@@ -31,41 +31,51 @@
import javax.servlet.ServletContext;
import javax.servlet.ServletContextEvent;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import com.gitblit.Constants;
import com.gitblit.DaggerModule;
import com.gitblit.FileSettings;
import com.gitblit.IStoredSettings;
import com.gitblit.Keys;
import com.gitblit.WebXmlSettings;
import com.gitblit.dagger.DaggerContext;
import com.gitblit.extensions.LifeCycleListener;
import com.gitblit.guice.CoreModule;
import com.gitblit.guice.WebModule;
import com.gitblit.manager.IAuthenticationManager;
import com.gitblit.manager.IFederationManager;
import com.gitblit.manager.IGitblit;
import com.gitblit.manager.IManager;
import com.gitblit.manager.INotificationManager;
import com.gitblit.manager.IPluginManager;
import com.gitblit.manager.IProjectManager;
import com.gitblit.manager.IRepositoryManager;
import com.gitblit.manager.IRuntimeManager;
import com.gitblit.manager.IUserManager;
import com.gitblit.transport.ssh.IPublicKeyManager;
import com.gitblit.utils.ContainerUtils;
import com.gitblit.utils.StringUtils;
import dagger.ObjectGraph;
import com.google.inject.AbstractModule;
import com.google.inject.Guice;
import com.google.inject.Injector;
import com.google.inject.servlet.GuiceServletContextListener;
/**
 * This class is the main entry point for the entire webapp.  It is a singleton
 * created manually by Gitblit GO or dynamically by the WAR/Express servlet
 * container.  This class instantiates and starts all managers.  Servlets and
 * filters are instantiated defined in web.xml and instantiated by the servlet
 * container, but those servlets and filters use Dagger to manually inject their
 * dependencies.
 * container.  This class instantiates and starts all managers.
 *
 * Servlets and filters are injected which allows Gitblit to be completely
 * code-driven.
 *
 * @author James Moger
 *
 */
public class GitblitContext extends DaggerContext {
public class GitblitContext extends GuiceServletContextListener {
   private static GitblitContext gitblit;
   protected final Logger logger = LoggerFactory.getLogger(getClass());
   private final List<IManager> managers = new ArrayList<IManager>();
@@ -77,9 +87,7 @@
    * Construct a Gitblit WAR/Express context.
    */
   public GitblitContext() {
      this.goSettings = null;
      this.goBaseFolder = null;
      gitblit = this;
      this(null, null);
   }
   /**
@@ -110,12 +118,16 @@
      return null;
   }
   /**
    * Returns Gitblit's Dagger injection modules.
    */
   @Override
   protected Object [] getModules() {
      return new Object [] { new DaggerModule() };
   protected Injector getInjector() {
      return Guice.createInjector(getModules());
   }
   /**
    * Returns Gitblit's Guice injection modules.
    */
   protected AbstractModule [] getModules() {
      return new AbstractModule [] { new CoreModule(), new WebModule() };
   }
   /**
@@ -126,18 +138,20 @@
    */
   @Override
   public final void contextInitialized(ServletContextEvent contextEvent) {
      super.contextInitialized(contextEvent);
      ServletContext context = contextEvent.getServletContext();
      configureContext(context);
      startCore(context);
   }
   /**
    * Prepare runtime settings and start all manager instances.
    */
   protected void configureContext(ServletContext context) {
      ObjectGraph injector = getInjector(context);
   protected void startCore(ServletContext context) {
      Injector injector = (Injector) context.getAttribute(Injector.class.getName());
      // create the runtime settings object
      IStoredSettings runtimeSettings = injector.get(IStoredSettings.class);
      IStoredSettings runtimeSettings = injector.getInstance(IStoredSettings.class);
      final File baseFolder;
      if (goSettings != null) {
@@ -149,7 +163,7 @@
         String contextRealPath = context.getRealPath("/");
         File contextFolder = (contextRealPath != null) ? new File(contextRealPath) : null;
         // if the base folder dosen't match the default assume they don't want to use express,
         // if the base folder dosen't match the default assume they don't want to use express,
         // this allows for other containers to customise the basefolder per context.
         String defaultBase = Constants.contextFolder$ + "/WEB-INF/data";
         String base = lookupBaseFolderFromJndi();
@@ -167,25 +181,42 @@
      // Manually configure IRuntimeManager
      logManager(IRuntimeManager.class);
      IRuntimeManager runtime = injector.get(IRuntimeManager.class);
      IRuntimeManager runtime = injector.getInstance(IRuntimeManager.class);
      runtime.setBaseFolder(baseFolder);
      runtime.getStatus().isGO = goSettings != null;
      runtime.getStatus().servletContainer = context.getServerInfo();
      runtime.start();
      managers.add(runtime);
      // create the plugin manager instance but do not start it
      loadManager(injector, IPluginManager.class);
      // start all other managers
      startManager(injector, INotificationManager.class);
      startManager(injector, IUserManager.class);
      startManager(injector, IAuthenticationManager.class);
      startManager(injector, IPublicKeyManager.class);
      startManager(injector, IRepositoryManager.class);
      startManager(injector, IProjectManager.class);
      startManager(injector, IFederationManager.class);
      startManager(injector, IGitblit.class);
      // start the plugin manager last so that plugins can depend on
      // deterministic access to all other managers in their start() methods
      startManager(injector, IPluginManager.class);
      logger.info("");
      logger.info("All managers started.");
      logger.info("");
      IPluginManager pluginManager = injector.getInstance(IPluginManager.class);
      for (LifeCycleListener listener : pluginManager.getExtensions(LifeCycleListener.class)) {
         try {
            listener.onStartup();
         } catch (Throwable t) {
            logger.error(null, t);
         }
      }
   }
   private String lookupBaseFolderFromJndi() {
@@ -200,9 +231,14 @@
      return null;
   }
   protected <X extends IManager> X startManager(ObjectGraph injector, Class<X> clazz) {
   protected <X extends IManager> X loadManager(Injector injector, Class<X> clazz) {
      X x = injector.getInstance(clazz);
      return x;
   }
   protected <X extends IManager> X startManager(Injector injector, Class<X> clazz) {
      X x = loadManager(injector, clazz);
      logManager(clazz);
      X x = injector.get(clazz);
      x.start();
      managers.add(x);
      return x;
@@ -213,13 +249,29 @@
      logger.info("----[{}]----", clazz.getName());
   }
   @Override
   public final void contextDestroyed(ServletContextEvent contextEvent) {
      super.contextDestroyed(contextEvent);
      ServletContext context = contextEvent.getServletContext();
      destroyContext(context);
   }
   /**
    * Gitblit is being shutdown either because the servlet container is
    * shutting down or because the servlet container is re-deploying Gitblit.
    */
   @Override
   protected void destroyContext(ServletContext context) {
      logger.info("Gitblit context destroyed by servlet container.");
      IPluginManager pluginManager = getManager(IPluginManager.class);
      for (LifeCycleListener listener : pluginManager.getExtensions(LifeCycleListener.class)) {
         try {
            listener.onShutdown();
         } catch (Throwable t) {
            logger.error(null, t);
         }
      }
      for (IManager manager : managers) {
         logger.debug("stopping {}", manager.getClass().getSimpleName());
         manager.stop();
@@ -347,6 +399,22 @@
         }
      }
      // Copy the included gitignore files to the configured gitignore folder
      String gitignorePath = webxmlSettings.getString(Keys.git.gitignoreFolder, "gitignore");
      File localGitignores = com.gitblit.utils.FileUtils.resolveParameter(Constants.baseFolder$, base, gitignorePath);
      if (!localGitignores.exists()) {
         File warGitignores = new File(contextFolder, "/WEB-INF/data/gitignore");
         if (!warGitignores.equals(localGitignores)) {
            try {
               com.gitblit.utils.FileUtils.copy(localGitignores, warGitignores.listFiles());
            } catch (IOException e) {
               logger.error(MessageFormat.format(
                     "Failed to copy included .gitignore files from {0} to {1}",
                     warGitignores, localGitignores));
            }
         }
      }
      // merge the WebXmlSettings into the runtime settings (for backwards-compatibilty)
      runtimeSettings.merge(webxmlSettings);