View Javadoc

1   package com.atlassian.config.util;
2   
3   import com.atlassian.config.bootstrap.AtlassianBootstrapManager;
4   import com.atlassian.config.bootstrap.BootstrapException;
5   import com.atlassian.config.HomeLocator;
6   import org.apache.log4j.Logger;
7   import org.springframework.context.ApplicationContext;
8   import org.springframework.context.ConfigurableApplicationContext;
9   
10  import javax.servlet.ServletContext;
11  
12  public class BootstrapUtils
13  {
14      private static final Logger log = Logger.getLogger(BootstrapUtils.class);
15  
16      private static ApplicationContext bootstrapContext;
17      private static AtlassianBootstrapManager bootstrapManager;
18  
19      /**
20       * Initialise the bootstrap manager.
21       *
22       * @param bootstrapContext the Spring bootstrap context
23       * @param servletContext the servlet context of the web application
24       */
25      public static void init(ApplicationContext bootstrapContext, ServletContext servletContext) throws BootstrapException
26      {
27          // the HomeLocator needs the servletContext in case the home is defined inside a web.xml property (CONF-4054)
28          ((HomeLocator)bootstrapContext.getBean("homeLocator")).lookupServletHomeProperty(servletContext);
29  
30          setBootstrapContext(bootstrapContext);
31          AtlassianBootstrapManager bootstrapManager = getBootstrapManager();
32          if (bootstrapManager == null)
33              throw new BootstrapException("Could not initialise boostrap manager");
34  
35          bootstrapManager.init();
36  
37          if (!bootstrapManager.isBootstrapped())
38              throw new BootstrapException("Unable to bootstrap application: " + bootstrapManager.getBootstrapFailureReason());
39      }
40  
41      public static ApplicationContext getBootstrapContext()
42      {
43          return bootstrapContext;
44      }
45  
46      public static void setBootstrapContext(ApplicationContext bootstrapContext)
47      {
48          BootstrapUtils.bootstrapContext = bootstrapContext;
49      }
50  
51      public static AtlassianBootstrapManager getBootstrapManager()
52      {
53          if (bootstrapManager == null && bootstrapContext != null)
54              bootstrapManager = (AtlassianBootstrapManager) bootstrapContext.getBean("bootstrapManager");
55  
56          if (bootstrapManager == null)
57          {
58              Exception e = new Exception();
59              String context = e.getStackTrace().length > 1 ? e.getStackTrace()[1].toString() : "Unknown caller";
60              log.warn("Attempting to retrieve bootstrap manager before it is set up: " + context);
61          }
62  
63          return bootstrapManager;
64      }
65  
66      public static void setBootstrapManager(AtlassianBootstrapManager bootstrapManager)
67      {
68          BootstrapUtils.bootstrapManager = bootstrapManager;
69      }
70  
71      public static void closeContext()
72      {
73          if (bootstrapContext != null && bootstrapContext instanceof ConfigurableApplicationContext)
74          {
75              ((ConfigurableApplicationContext) bootstrapContext).close();
76          }
77  
78          bootstrapContext = null;
79          bootstrapManager = null;
80      }
81  
82  
83  }