James Moger
2014-05-31 6a437ec4a6853bdd15f7c33f7fbafdd247a3790c
src/main/java/com/gitblit/servlet/GitblitContext.java
@@ -38,15 +38,18 @@
import com.gitblit.Keys;
import com.gitblit.WebXmlSettings;
import com.gitblit.dagger.DaggerContext;
import com.gitblit.extensions.LifeCycleListener;
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;
@@ -77,9 +80,7 @@
    * Construct a Gitblit WAR/Express context.
    */
   public GitblitContext() {
      this.goSettings = null;
      this.goBaseFolder = null;
      gitblit = this;
      this(null, null);
   }
   /**
@@ -149,7 +150,11 @@
         String contextRealPath = context.getRealPath("/");
         File contextFolder = (contextRealPath != null) ? new File(contextRealPath) : null;
         if (!StringUtils.isEmpty(System.getenv("OPENSHIFT_DATA_DIR"))) {
         // 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();
         if (!StringUtils.isEmpty(System.getenv("OPENSHIFT_DATA_DIR")) && defaultBase.equals(base)) {
            // RedHat OpenShift
            baseFolder = configureExpress(context, webxmlSettings, contextFolder, runtimeSettings);
         } else {
@@ -174,14 +179,40 @@
      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.get(IPluginManager.class);
      for (LifeCycleListener listener : pluginManager.getExtensions(LifeCycleListener.class)) {
         try {
            listener.onStartup();
         } catch (Throwable t) {
            logger.error(null, t);
         }
      }
   }
   private String lookupBaseFolderFromJndi() {
      try {
         // try to lookup JNDI env-entry for the baseFolder
         InitialContext ic = new InitialContext();
         Context env = (Context) ic.lookup("java:comp/env");
         return (String) env.lookup("baseFolder");
      } catch (NamingException n) {
         logger.error("Failed to get JNDI env-entry: " + n.getExplanation());
      }
      return null;
   }
   protected <X extends IManager> X startManager(ObjectGraph injector, Class<X> clazz) {
@@ -204,6 +235,16 @@
   @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();
@@ -268,16 +309,9 @@
         logger.error("");
      }
      try {
         // try to lookup JNDI env-entry for the baseFolder
         InitialContext ic = new InitialContext();
         Context env = (Context) ic.lookup("java:comp/env");
         String val = (String) env.lookup("baseFolder");
         if (!StringUtils.isEmpty(val)) {
            path = val;
         }
      } catch (NamingException n) {
         logger.error("Failed to get JNDI env-entry: " + n.getExplanation());
      String baseFromJndi = lookupBaseFolderFromJndi();
      if (!StringUtils.isEmpty(baseFromJndi)) {
         path = baseFromJndi;
      }
      File base = com.gitblit.utils.FileUtils.resolveParameter(Constants.contextFolder$, contextFolder, path);
@@ -338,6 +372,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);