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