KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > openharmonise > rm > config > ConfigSettings


1 /*
2  * The contents of this file are subject to the
3  * Mozilla Public License Version 1.1 (the "License");
4  * you may not use this file except in compliance with the License.
5  * You may obtain a copy of the License at http://www.mozilla.org/MPL/
6  *
7  * Software distributed under the License is distributed on an "AS IS"
8  * basis, WITHOUT WARRANTY OF ANY KIND, either express or implied.
9  * See the License for the specific language governing rights and
10  * limitations under the License.
11  *
12  * The Initial Developer of the Original Code is Simulacra Media Ltd.
13  * Portions created by Simulacra Media Ltd are Copyright (C) Simulacra Media Ltd, 2004.
14  *
15  * All Rights Reserved.
16  *
17  * Contributor(s):
18  */

19 package org.openharmonise.rm.config;
20
21 import java.sql.*;
22 import java.util.*;
23 import java.util.logging.*;
24
25 import org.openharmonise.commons.dsi.*;
26 import org.openharmonise.rm.dsi.*;
27
28
29 /**
30  * Singleton class to provide interface to Harmonise configuration settings.
31  *
32  * @author Matt Treanor
33  * @author Michael Bell
34  * @version $Revision: 1.2 $
35  *
36  */

37 public class ConfigSettings {
38     
39     /**
40      * The singleton instance of this class
41      */

42     private static ConfigSettings m_instance = null;
43
44     /**
45      * Separator string used to separate values if stored in a list
46      */

47     private static String JavaDoc m_value_separator = ";";
48     
49     /**
50      * Database table which holds the configuration settings
51      */

52     private static String JavaDoc TBL_OH_PROP = "oh_prop";
53     
54     /**
55      * Map which provides an in-memory cache of config property values
56      */

57     private static Map m_props = null;
58     
59     /**
60      * Data store interface for this class
61      */

62     private static AbstractDataStoreInterface m_dsi = null;
63     
64     /**
65      * Logger for this class
66      */

67     private static final Logger m_logger = Logger.getLogger(ConfigSettings.class.getName());
68
69     private static boolean m_bIsInitialised = false;
70
71     /**
72      * Constructs a configuration settings instance.
73      *
74      * @throws ConfigException if an error occurs during initialisation
75      */

76     private ConfigSettings() throws ConfigException {
77         initialise();
78     }
79
80     /**
81      * Initialises the cache of properties and values.
82      *
83      * @throws ConfigException if an error occurs accessing data from
84      * database
85      */

86     private synchronized static void initialise() throws ConfigException {
87         ResultSet rs = null;
88         m_props = new HashMap();
89         try {
90
91             m_dsi = DataStoreInterfaceFactory.getDataStoreInterface();
92
93             rs = m_dsi.executeQuery("select * from " + TBL_OH_PROP);
94             while (rs.next()) {
95                 String JavaDoc sKey = rs.getString(2);
96                 String JavaDoc sValue = rs.getString(3);
97                 
98                 m_props.put(sKey, sValue);
99                 
100                 if(m_logger.isLoggable(Level.CONFIG)) {
101                     m_logger.log(Level.CONFIG, "Config value pair: " + sKey + " = " + sValue);
102                 }
103             }
104             
105             m_bIsInitialised = true;
106
107         } catch (SQLException e) {
108             m_logger.log(Level.WARNING, "Error initialising CongigSettings", e);
109             throw new ConfigException(e);
110         } catch (DataStoreException e) {
111             m_logger.log(Level.WARNING, "Error initialising CongigSettings", e);
112             throw new ConfigException(e);
113         } finally {
114             try {
115                 if (rs != null) {
116                     rs.close();
117                 }
118             } catch (Exception JavaDoc e) {
119                 if(m_logger.isLoggable(Level.WARNING)) {
120                     m_logger.log(Level.WARNING, "Exception thrown when closing result set", e);
121                 }
122             }
123         }
124     }
125
126     /**
127      * Returns the singleton instance of this class.
128      *
129      * @return the singleton instance of this class
130      * @throws ConfigException if an error occurs constructing the singleton instance
131      */

132     synchronized public static ConfigSettings getInstance()
133         throws ConfigException {
134         if (m_instance == null) {
135             m_instance = new ConfigSettings();
136         }
137
138         return m_instance;
139     }
140
141     /**
142      * Returns the <code>String</code> value which corresponds to the property name.
143      * If no property exists a null is returned.
144      *
145      * @param property_name the property
146      * @return the <code>String</code> value which corresponds to the property name
147      * @throws ConfigException if an error occurs accessing the property value
148      */

149     public static final String JavaDoc getProperty(String JavaDoc property_name)
150         throws ConfigException {
151         return getProperty(property_name, null);
152     }
153
154     /**
155      * Returns the <code>String</code> value which corresponds to the property name.
156      * If no property exists the default value is returned.
157      *
158      * @param property_name the property name
159      * @param default_value the default value
160      * @return the <code>String</code> value which corresponds to the property name
161      * @throws ConfigException if an error occurs accessing the property value
162      */

163     public static final String JavaDoc getProperty(
164         String JavaDoc property_name,
165         String JavaDoc default_value)
166         throws ConfigException {
167         if (isInitialised() == false) {
168             initialise();
169         }
170         String JavaDoc sVal = default_value;
171
172         if (m_props.containsKey(property_name) == true) {
173             sVal = (String JavaDoc) m_props.get(property_name);
174         }
175
176         return sVal;
177     }
178
179     /**
180      * Returns <code>true</code> if the local cache of properties is
181      * initialised.
182      *
183      * @return <code>true</code> if the local cache of properties is
184      * initialised
185      */

186     private static boolean isInitialised() {
187         
188         return m_bIsInitialised;
189     }
190
191     /**
192      * Adds a new property and property value.
193      *
194      * @param property_name the property name
195      * @param property_value the property value
196      * @throws ConfigException if any errors occur, either due to
197      * there already being a property of the specified name or a problem
198      * inserting the data in to the database
199      */

200     public static void insertProperty(
201         String JavaDoc property_name,
202         String JavaDoc property_value)
203         throws ConfigException {
204         if (property_name == null || property_value == null) {
205             throw new ConfigException("Must have values for both property name and value");
206         }
207         if (isInitialised() == false || m_dsi == null) {
208             initialise();
209         }
210         if (m_props.containsKey(property_name)) {
211             throw new ConfigException("Property name already exists");
212         }
213         try {
214             int nId = m_dsi.getSequenceNextValue("seq_oh_prop");
215             String JavaDoc sSql =
216                 "insert into oh_prop values("
217                     + nId
218                     + ",'"
219                     + property_name
220                     + "','"
221                     + property_value
222                     + "')";
223             if (m_logger.isLoggable(Level.CONFIG)) {
224                 m_logger.logp(Level.CONFIG, ConfigSettings.class.getName(), "insertProperty", sSql);
225             }
226             
227             m_dsi.execute(sSql);
228             //update the props
229
initialise();
230         } catch (DataStoreException e) {
231             throw new ConfigException(e);
232         } catch (SQLException e) {
233             throw new ConfigException(e);
234         }
235         
236     }
237
238     /**
239      * Sets a new value for the specified configuration property.
240      *
241      * @param property_name the property name
242      * @param property_value the property value
243      * @throws ConfigException if the specified property does not exist
244      * or there is a problem updating the database
245      */

246     public static void setProperty(String JavaDoc property_name, String JavaDoc property_value)
247         throws ConfigException {
248         String JavaDoc sPropVal = property_value;
249         if (isInitialised() == false || m_dsi == null) {
250             initialise();
251         }
252         if (m_props.containsKey(property_name) == false) {
253             throw new ConfigException(
254                 "Property " + property_name + " does not exist");
255         }
256
257         String JavaDoc sSql =
258             "update oh_prop set prop_value='"
259                 + property_value
260                 + "' where prop_name ='"
261                 + property_name
262                 + "'";
263         try {
264             m_dsi.execute(sSql);
265         } catch (DataStoreException e) {
266             throw new ConfigException(e);
267         }
268         //update the props
269
initialise();
270
271     }
272     
273     /**
274      * Removes the specified configuration property.
275      *
276      * @param prop_name the property name
277      * @throws ConfigException if there is an error updating the database
278      */

279     public static void removeProperty(String JavaDoc prop_name)
280         throws ConfigException {
281         if (m_dsi == null) {
282             initialise();
283         }
284         String JavaDoc sSql =
285             "delete from oh_prop where prop_name ='" + prop_name + "'";
286         if (m_logger.isLoggable(Level.CONFIG)) {
287             m_logger.logp(Level.CONFIG, ConfigSettings.class.getName(), "removeProperty", sSql);
288         }
289         
290         try {
291             // update the props
292
m_dsi.execute(sSql);
293         } catch (DataStoreException e) {
294             throw new ConfigException(e);
295         }
296         
297         initialise();
298         
299     }
300     
301     /**
302      * Returns a comma serparated list of the property names.
303      *
304      * @return a comma serparated list of the property names
305      * @throws ConfigException if an error occurs initialising this class
306      */

307     public static String JavaDoc getPropertyNames() throws ConfigException {
308         String JavaDoc props = "";
309         if (m_props == null || m_dsi == null) {
310             initialise();
311         }
312         Set keyset = m_props.keySet();
313         Object JavaDoc[] keys = keyset.toArray();
314         for (int i = 0; i < keys.length; i++) {
315             if (i != 0) {
316                 props += ",";
317             }
318             props += keys[i];
319         }
320         return props;
321     }
322     
323     /**
324      * Returns a comma serparated list of the property names.
325      *
326      * @return a comma serparated list of the property names
327      * @throws ConfigException if an error occurs initialising this class
328      */

329     public static List getPropertyNameArray() throws ConfigException {
330         String JavaDoc props = "";
331         if (m_props == null || m_dsi == null) {
332             initialise();
333         }
334         Set keyset = m_props.keySet();
335         
336         ArrayList list = new ArrayList();
337         
338         Iterator iter = keyset.iterator();
339         
340         while (iter.hasNext()) {
341             String JavaDoc sName = (String JavaDoc) iter.next();
342             list.add(sName);
343         }
344         
345         
346         return list;
347     }
348     
349     /**
350      * Returns a list of values for the specified property.
351      *
352      * @param property_name the property name
353      * @return a list of values for the specified property
354      * @throws ConfigException if there is a problem accessing the property value
355      */

356     public static final List getMultiValuedProperty(String JavaDoc property_name)
357         throws ConfigException {
358         Vector vec = new Vector();
359
360         String JavaDoc property_value = getProperty(property_name, "");
361
362         StringTokenizer tokenizer =
363             new StringTokenizer(property_value, m_value_separator);
364
365         while (tokenizer.hasMoreTokens() == true) {
366             vec.add(tokenizer.nextToken());
367         }
368
369         return vec;
370     }
371
372     /**
373      * Returns the <code>int</code> value which corresponds to the property
374      * name. If no property exists, the default value is returned.
375      *
376      * @param property_name the property name
377      * @param default_value the default value
378      * @return the <code>int</code> value of the property
379      * @throws ConfigException if an error occurs accessing the property value
380      */

381     public static final int getIntProperty(String JavaDoc property_name, String JavaDoc default_value)
382         throws ConfigException {
383         String JavaDoc sVal = null;
384         int property_value = -1;
385
386         try {
387             sVal = getProperty(property_name, default_value);
388             property_value = Integer.parseInt(sVal);
389
390         } catch (NumberFormatException JavaDoc ne) {
391             throw new ConfigException(
392                 "Cannot cast property with name:"
393                     + property_name
394                     + ", and value:"
395                     + sVal
396                     + " to an integer");
397         }
398
399         return property_value;
400     }
401
402     /**
403      * Returns the <code>boolean</code> value which corresponds to the
404      * property name. If no property exists the default value is returned.
405      *
406      * @param property_name the property name
407      * @param default_value the default value
408      * @return the <code>boolean</code> value of the property
409      * @throws ConfigException if an error occurs accessing the property value
410      */

411     public static final boolean getBoolProperty(
412         String JavaDoc property_name,
413         String JavaDoc default_value)
414         throws ConfigException {
415         boolean property_value;
416
417         String JavaDoc property_value_str =
418             (String JavaDoc) getProperty(property_name, default_value);
419
420         if (property_value_str.equalsIgnoreCase("" + true)) {
421             property_value = true;
422         } else if (property_value_str.equalsIgnoreCase("" + false)) {
423             property_value = false;
424         } else {
425             throw new ConfigException(
426                 "Cannot cast property with name:"
427                     + property_name
428                     + ", and value:"
429                     + property_value_str
430                     + " to a boolean");
431         }
432
433         return property_value;
434     }
435 }
Popular Tags