View Javadoc

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