1 package com.atlassian.config;
2
3 import org.slf4j.Logger;
4 import org.slf4j.LoggerFactory;
5
6 import java.io.File;
7 import java.io.IOException;
8 import java.util.HashMap;
9 import java.util.Iterator;
10 import java.util.Map;
11 import java.util.TreeMap;
12
13 public class ApplicationConfig implements ApplicationConfiguration
14 {
15
16 @Deprecated
17 public static final org.apache.log4j.Logger log = org.apache.log4j.Logger.getLogger(ApplicationConfig.class);
18 private static final Logger privateLog = LoggerFactory.getLogger(ApplicationConfig.class);
19
20 public static final boolean NULL_BOOLEAN_VALUE = false;
21 public static final int NULL_INTEGER_VALUE = Integer.MIN_VALUE;
22 public static final String DEFAULT_CONFIG_FILE_NAME = "atlassian-config.xml";
23 public static final String DEFAULT_APPLICATION_HOME = ".";
24
25
26 private String setupStepNode = "setupStep";
27 private String setupTypeNode = "setupType";
28 private String buildNumberNode = "buildNumber";
29
30 private String applicationHome = DEFAULT_APPLICATION_HOME;
31 private boolean homeOk = false;
32 private Map properties = new TreeMap();
33 private String buildNumber = "0";
34 private int majorVersion = 0;
35 private int minorVersion = 0;
36 private boolean setupComplete = false;
37
38 private String currentSetupStep;
39 private String setupType;
40 private String configurationFileName;
41
42 protected ConfigurationPersister configurationPersister;
43
44 public ApplicationConfig()
45 {
46 }
47
48 public void reset()
49 {
50 homeOk = false;
51 applicationHome = DEFAULT_APPLICATION_HOME;
52 properties.clear();
53 buildNumber = "0";
54 majorVersion = 0;
55 minorVersion = 0;
56 setupComplete = false;
57 configurationPersister = null;
58 }
59
60 public void setApplicationHome(String home) throws ConfigurationException
61 {
62 File homeDir = new File(home);
63 if (!homeDir.isDirectory())
64 {
65 privateLog.warn("Application home does not exist. Creating directory: {}", homeDir.getAbsolutePath());
66 homeOk = homeDir.mkdirs();
67 if (!homeOk)
68 {
69 throw new ConfigurationException("Could not make directory/ies: " + homeDir.getAbsolutePath());
70 }
71 }
72 try
73 {
74 this.applicationHome = homeDir.getCanonicalPath();
75 homeOk = true;
76 }
77 catch (IOException e)
78 {
79 homeOk = false;
80 throw new ConfigurationException("Failed to locate application home: " + home, e);
81 }
82 }
83
84 public String getApplicationHome()
85 {
86 return applicationHome;
87 }
88
89 public boolean isApplicationHomeValid()
90 {
91 return homeOk;
92 }
93
94 public void setProperty(Object key, Object value)
95 {
96 properties.put(key, value);
97 }
98
99 public Object removeProperty(Object key)
100 {
101 return properties.remove(key);
102 }
103
104 public Object getProperty(Object key)
105 {
106 return properties.get(key);
107 }
108
109 public Map getProperties()
110 {
111 return properties;
112 }
113
114 public void setBuildNumber(String build)
115 {
116 buildNumber = build;
117 }
118
119 public String getBuildNumber()
120 {
121 return buildNumber;
122 }
123
124 public int getMajorVersion()
125 {
126 return majorVersion;
127 }
128
129 public void setMajorVersion(int majorVersion)
130 {
131 this.majorVersion = majorVersion;
132 }
133
134 public int getMinorVersion()
135 {
136 return minorVersion;
137 }
138
139 public void setMinorVersion(int minorVersion)
140 {
141 this.minorVersion = minorVersion;
142 }
143
144 public String getApplicationVersion()
145 {
146 return getMajorVersion() + "." + getMinorVersion() + " build: " + getBuildNumber();
147 }
148
149 public Map getPropertiesWithPrefix(String prefix)
150 {
151 Map newProps = new HashMap();
152 for (Iterator iter = properties.entrySet().iterator(); iter.hasNext();)
153 {
154 Map.Entry entry = (Map.Entry) iter.next();
155 String key = (String) entry.getKey();
156 if (key.startsWith(prefix))
157 {
158 newProps.put(key, entry.getValue());
159 }
160 }
161 return newProps;
162 }
163
164 public boolean isSetupComplete()
165 {
166 return setupComplete;
167 }
168
169 public void setSetupComplete(boolean setupComplete)
170 {
171 this.setupComplete = setupComplete;
172 }
173
174 public void setProperty(Object key, int value)
175 {
176 properties.put(key, new Integer(value));
177 }
178
179 public void setProperty(Object key, boolean value)
180 {
181 properties.put(key, new Boolean(value));
182 }
183
184 public boolean getBooleanProperty(Object key)
185 {
186 Object temp = properties.get(key);
187 if (temp == null)
188 {
189 return NULL_BOOLEAN_VALUE;
190 }
191 else if (temp instanceof Boolean)
192 {
193 return ((Boolean) temp).booleanValue();
194 }
195 else
196 {
197 return Boolean.valueOf(temp.toString()).booleanValue();
198 }
199 }
200
201 public int getIntegerProperty(Object key)
202 {
203 Object temp = properties.get(key);
204 if (temp == null)
205 {
206 return NULL_INTEGER_VALUE;
207 }
208 else if (temp instanceof Integer)
209 {
210 return ((Integer) temp).intValue();
211 }
212 else
213 {
214 return Integer.valueOf(temp.toString()).intValue();
215 }
216 }
217
218
219
220 public void setConfigurationPersister(ConfigurationPersister configurationPersister)
221 {
222 this.configurationPersister = configurationPersister;
223 }
224
225
226
227
228 public void setInitialProperties(Map initalProperties)
229 {
230 properties.putAll(initalProperties);
231 }
232
233 protected String getConfigurationFileName()
234 {
235 return configurationFileName;
236 }
237
238 public void setConfigurationFileName(String configurationFileName)
239 {
240 this.configurationFileName = configurationFileName;
241 }
242
243 public String getSetupType()
244 {
245 return setupType;
246 }
247
248 public void setSetupType(String setupType)
249 {
250 this.setupType = setupType;
251 }
252
253 public String getCurrentSetupStep()
254 {
255 return currentSetupStep;
256 }
257
258 public void setCurrentSetupStep(String currentSetupStep)
259 {
260 this.currentSetupStep = currentSetupStep;
261 }
262
263 public void load() throws ConfigurationException
264 {
265 configurationPersister.load(getApplicationHome(), getConfigurationFileName());
266 setBuildNumber(configurationPersister.getStringConfigElement(buildNumberNode));
267 setSetupType(configurationPersister.getStringConfigElement(setupTypeNode));
268 setCurrentSetupStep(configurationPersister.getStringConfigElement(setupStepNode));
269 Map props = (Map) configurationPersister.getConfigElement(Map.class, "properties");
270 getProperties().putAll(props);
271 }
272
273 public boolean configFileExists()
274 {
275 File file = new File(getApplicationHome(), getConfigurationFileName());
276 return file.exists();
277 }
278
279 public void save() throws ConfigurationException
280 {
281 configurationPersister.clear();
282 configurationPersister.addConfigElement(getCurrentSetupStep(), setupStepNode);
283 configurationPersister.addConfigElement(getSetupType(), setupTypeNode);
284 configurationPersister.addConfigElement(getBuildNumber(), buildNumberNode);
285 configurationPersister.addConfigElement(getProperties(), "properties");
286 configurationPersister.save(getApplicationHome(), getConfigurationFileName());
287 }
288 }