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