View Javadoc

1   package com.atlassian.config;
2   
3   import com.atlassian.core.util.ClassLoaderUtils;
4   import com.atlassian.core.util.FileUtils;
5   import org.apache.log4j.Logger;
6   
7   import javax.servlet.ServletContext;
8   import java.io.IOException;
9   import java.io.InputStream;
10  import java.net.URL;
11  import java.util.Properties;
12  
13  /**
14   * A simple strategy bean to locate the application home directory.
15   */
16  public class DefaultHomeLocator implements HomeLocator
17  {
18      private static final Logger log = Logger.getLogger(DefaultHomeLocator.class);
19      private String initPropertyName;
20      private String propertiesFile;
21      private String configFileName;
22      private String servletHomeProperty;
23  
24      public DefaultHomeLocator()
25      {
26      }
27  
28      /**
29       * Use this method to try to get the home variable.
30       * <p/>
31       * This method looks for the variable in the following places (in order):
32       * <ul>
33       * <li>System property
34       * <li>properties file
35       * <li>Servlet context init param</li>
36       * </ul>
37       *
38       * @return
39       */
40      public String getHomePath()
41      {
42          // Allow configured home to be overridden with system property.
43          String home = getHomeFromSystemProperty();
44          // If we could not get the home location from the system properties, try the config file
45          if (home == null)
46          {
47              home = getHomeFromConfigFile();
48          }
49          // As a fall-back, try getting the location from the servlet context parameters (CONF-4054)
50          if (home == null)
51          {
52              home = servletHomeProperty;
53          }
54          if (log.isDebugEnabled())
55          {
56              log.debug("Found " + initPropertyName + "  property with value: " + home);
57          }
58          return home;
59      }
60  
61      public String getConfigFileName()
62      {
63          return configFileName;
64      }
65  
66      public void setConfigFileName(String configFileName)
67      {
68          this.configFileName = configFileName;
69      }
70  
71      private String getHomeFromSystemProperty()
72      {
73          log.debug("Trying to load " + initPropertyName + " from System property parameter... ");
74          String sysProperty = System.getProperty(initPropertyName);
75          if (sysProperty == null)
76          {
77              log.debug("Could not find " + initPropertyName + " property as a System property.");
78          }
79          return sysProperty;
80      }
81  
82      private String getHomeFromConfigFile()
83      {
84          log.debug("Trying to load " + initPropertyName + " from properties file... ");
85          String confHome = null;
86          try
87          {
88              Properties props = new Properties();
89              URL url = ClassLoaderUtils.getResource(getPropertiesFile(), DefaultHomeLocator.class);
90              if (url != null)
91              {
92                  InputStream inputStream = null;
93                  try
94                  {
95                      inputStream = url.openStream();
96                      props.load(inputStream);
97                  }
98                  finally
99                  {
100                     FileUtils.shutdownStream(inputStream);
101                 }
102             }
103             if (props.getProperty(initPropertyName) != null)
104             {
105                 confHome = props.getProperty(initPropertyName);
106             }
107             else
108             {
109                 log.debug("Could not find " + initPropertyName + " property in the " + getPropertiesFile() + " file. trying other methods.");
110             }
111         }
112         catch (IOException e)
113         {
114             if (log.isDebugEnabled())
115             {
116                 log.debug("Could not find " + getPropertiesFile() + " in the classpath, trying other methods.");
117             }
118         }
119         return confHome;
120     }
121 
122     public String getPropertiesFile()
123     {
124         return propertiesFile;
125     }
126 
127     public void setPropertiesFile(String propertiesFile)
128     {
129         this.propertiesFile = propertiesFile;
130     }
131 
132     public void setInitPropertyName(String initPropertyName)
133     {
134         this.initPropertyName = initPropertyName;
135     }
136 
137     public void lookupServletHomeProperty(ServletContext context)
138     {
139         log.debug("Trying to load " + initPropertyName + " from servlet context parameter... ");
140         if ((context != null) && (context.getInitParameter(initPropertyName) != null))
141         {
142             servletHomeProperty = context.getInitParameter(initPropertyName);
143         }
144         else
145         {
146             log.debug("Could not find " + initPropertyName + " property in the servlet context. Trying other methods.");
147         }
148     }
149 }