James Moger
2014-09-24 2fb843db2702f5f6d1df784d53dd6cbba286f4f3
src/main/java/com/gitblit/servlet/GitblitContext.java
@@ -23,15 +23,16 @@
import java.io.OutputStream;
import java.text.MessageFormat;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import javax.naming.Context;
import javax.naming.InitialContext;
import javax.naming.NamingException;
import javax.servlet.ServletContext;
import javax.servlet.annotation.WebListener;
import javax.servlet.ServletContextEvent;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import com.gitblit.Constants;
import com.gitblit.FileSettings;
@@ -39,8 +40,8 @@
import com.gitblit.Keys;
import com.gitblit.WebXmlSettings;
import com.gitblit.extensions.LifeCycleListener;
import com.gitblit.guice.GuiceContext;
import com.gitblit.guice.GuiceModule;
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;
@@ -50,13 +51,16 @@
import com.gitblit.manager.IProjectManager;
import com.gitblit.manager.IRepositoryManager;
import com.gitblit.manager.IRuntimeManager;
import com.gitblit.manager.IServicesManager;
import com.gitblit.manager.IUserManager;
import com.gitblit.tickets.ITicketService;
import com.gitblit.transport.ssh.IPublicKeyManager;
import com.gitblit.utils.ContainerUtils;
import com.gitblit.utils.StringUtils;
import com.gitblit.wicket.GitblitWicketFilter;
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
@@ -69,10 +73,11 @@
 * @author James Moger
 *
 */
@WebListener
public class GitblitContext extends GuiceContext {
public class GitblitContext extends GuiceServletContextListener {
   private static GitblitContext gitblit;
   protected final Logger logger = LoggerFactory.getLogger(getClass());
   private final List<IManager> managers = new ArrayList<IManager>();
@@ -115,20 +120,37 @@
      return null;
   }
   @Override
   protected Injector getInjector() {
      return Guice.createInjector(getModules());
   }
   /**
    * Returns Gitblit's Guice injection modules.
    */
   @Override
   protected AbstractModule [] getModules() {
      return new AbstractModule [] { new GuiceModule() };
      return new AbstractModule [] { new CoreModule(), new WebModule() };
   }
   /**
    * Configure Gitblit from the web.xml, if no configuration has already been
    * specified.
    *
    * @see ServletContextListener.contextInitialize(ServletContextEvent)
    */
   @Override
   public final void contextInitialized(ServletContextEvent contextEvent) {
      super.contextInitialized(contextEvent);
      ServletContext context = contextEvent.getServletContext();
      startCore(context);
   }
   /**
    * Prepare runtime settings and start all manager instances.
    */
   @Override
   protected void beforeServletInjection(ServletContext context) {
      Injector injector = getInjector(context);
   protected void startCore(ServletContext context) {
      Injector injector = (Injector) context.getAttribute(Injector.class.getName());
      // create the runtime settings object
      IStoredSettings runtimeSettings = injector.getInstance(IStoredSettings.class);
@@ -146,7 +168,7 @@
         // 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();
         String base = System.getProperty("GITBLIT_HOME",lookupBaseFolderFromJndi());
         if (!StringUtils.isEmpty(System.getenv("OPENSHIFT_DATA_DIR")) && defaultBase.equals(base)) {
            // RedHat OpenShift
            baseFolder = configureExpress(context, webxmlSettings, contextFolder, runtimeSettings);
@@ -179,7 +201,9 @@
      startManager(injector, IRepositoryManager.class);
      startManager(injector, IProjectManager.class);
      startManager(injector, IFederationManager.class);
      startManager(injector, ITicketService.class);
      startManager(injector, IGitblit.class);
      startManager(injector, IServicesManager.class);
      // start the plugin manager last so that plugins can depend on
      // deterministic access to all other managers in their start() methods
@@ -229,46 +253,17 @@
      logger.info("----[{}]----", clazz.getName());
   }
   /**
    * Instantiate and inject all filters and servlets into the container using
    * the servlet 3 specification.
    */
   @Override
   protected void injectServlets(ServletContext context) {
      // access restricted servlets
      serve(context, Constants.R_PATH, GitServlet.class, GitFilter.class);
      serve(context, Constants.GIT_PATH, GitServlet.class, GitFilter.class);
      serve(context, Constants.RAW_PATH, RawServlet.class, RawFilter.class);
      serve(context, Constants.PAGES, PagesServlet.class, PagesFilter.class);
      serve(context, Constants.RPC_PATH, RpcServlet.class, RpcFilter.class);
      serve(context, Constants.ZIP_PATH, DownloadZipServlet.class, DownloadZipFilter.class);
      serve(context, Constants.SYNDICATION_PATH, SyndicationServlet.class, SyndicationFilter.class);
      // servlets
      serve(context, Constants.FEDERATION_PATH, FederationServlet.class);
      serve(context, Constants.SPARKLESHARE_INVITE_PATH, SparkleShareInviteServlet.class);
      serve(context, Constants.BRANCH_GRAPH_PATH, BranchGraphServlet.class);
      serve(context, Constants.PT_PATH, PtServlet.class);
      file(context, "/robots.txt", RobotsTxtServlet.class);
      file(context, "/logo.png", LogoServlet.class);
      // global filters
      filter(context, "/*", ProxyFilter.class, null);
      filter(context, "/*", EnforceAuthenticationFilter.class, null);
      // Wicket
      String toIgnore = StringUtils.flattenStrings(getRegisteredPaths(), ",");
      Map<String, String> params = new HashMap<String, String>();
      params.put(GitblitWicketFilter.FILTER_MAPPING_PARAM, "/*");
      params.put(GitblitWicketFilter.IGNORE_PATHS_PARAM, toIgnore);
      filter(context, "/*", GitblitWicketFilter.class, params);
   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.");
@@ -345,9 +340,9 @@
         logger.error("");
      }
      String baseFromJndi = lookupBaseFolderFromJndi();
      if (!StringUtils.isEmpty(baseFromJndi)) {
         path = baseFromJndi;
      String externalBase = System.getProperty("GITBLIT_HOME", lookupBaseFolderFromJndi());
      if (!StringUtils.isEmpty(externalBase)) {
         path = externalBase;
      }
      File base = com.gitblit.utils.FileUtils.resolveParameter(Constants.contextFolder$, contextFolder, path);