KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > raptus > owxv3 > Configuration


1 /*
2  * eAdmin/OWX
3  * Copyright (C) 1996-2003 OWX-Project Team <owx-team@gmx.net>
4  */

5
6 /******************************** Package */
7 package com.raptus.owxv3;
8
9
10 /******************************** Imports */
11 import java.util.*;
12 import java.text.ParseException JavaDoc;
13
14 /******************************** Raptus-Header */
15 /**
16  * This class offers an encapsulated access to configuration data, no matter how this
17  * data is stored. An implementation of ConfigManagerIFace depending on how the
18  * configuration is saved fills up this class with configuration-info which
19  * can then be used anywhere in OWX. The reason why not accessing the configuration
20  * directly via kind of ConfigManager-singleton is, that this would end up in
21  * synchronizing wide parts of the ConfigManager for thread-safety. This is more or
22  * less equal to the old ModuleCfg and ModuleCfgAdm-classes in OWX 1-x.
23  *
24  * @see com.raptus.owxv3.ConfigManagerIFace ConfigManagerIFace
25  *
26  * A concrete implementation of this interface is
27  *
28  * @see com.raptus.owxv3.PropertiesConfigManager PropertiesConfigManager
29  *
30  *
31  * <hr>
32  * <table width="100%" border="0">
33  * <tr>
34  * <td width="24%"><b>Filename</b></td><td width="76%">Configuration.java</td>
35  * </tr>
36  * <tr>
37  * <td width="24%"><b>Author</b></td><td width="76%">Pascal Mainini (pmainini@raptus.com)</td>
38  * </tr>
39  * <tr>
40  * <td width="24%"><b>Date</b></td><td width="76%">27th of March 2001</td>
41  * </tr>
42  * </table>
43  * <hr>
44  * <table width="100%" border="0">
45  * <tr>
46  * <td width="24%"><b>Date / Author</b></td><td width="76%"><b>Changes</b></td>
47  * </tr>
48  * <tr>
49  * <td width="24%">2001-03-27/pm</td><td width="76%">created</td>
50  * </tr>
51  * </table>
52  * <hr>
53  */

54 public class Configuration extends Object JavaDoc implements Cloneable JavaDoc
55 {
56
57 /******************************** Constants */
58     /**
59      * This string separates flatified array-entries.
60      *
61      * <b>Change only if you exactly know why!</b>
62      */

63     protected static final String JavaDoc ARRAY_SEPARATOR = ",";
64
65
66 /******************************** Members */
67     /**
68      * This hashtable stores the configuration-properties
69      */

70     protected Hashtable myConfigHash;
71
72
73 /******************************** Constructor */
74     /**
75      * This constructor is used for setting up the
76      * Hashtable with all Key/Value-pairs.
77      *
78      * @param hash Config-Values
79      */

80     public Configuration(Hashtable hash)
81     {
82         this.myConfigHash = hash;
83     }
84
85 /******************************** Methods */
86     /**
87      * @return the cloned instance of itself, or null if failed
88      */

89     public Object JavaDoc clone() throws CloneNotSupportedException JavaDoc
90     {
91         return super.clone();
92     }
93
94     /**
95      * We provide an access to the Hashtable, so configuration-info can also
96      * be retrieved in a custom manner.
97      *
98      * @return The config-hashtable.
99      */

100     public Hashtable getConfigHash()
101     {
102         return this.myConfigHash;
103     }
104
105     /**
106      * This method checks if the specified
107      * key has a value or not.
108      *
109      * @return false if the key has a value, true if not.
110      */

111     public boolean isEmptyKey(String JavaDoc key)
112     {
113         String JavaDoc str = (String JavaDoc) myConfigHash.get(key);
114
115         return (str == null || str.length() == 0);
116     }
117
118     /**
119      * Deletes the specified key.
120      * <b>IMPORTANT:</b>
121      * The key is not really deleted from the config, but it's value is set
122      * to "". So it seems that the key is deleted and isEmptyKey() works properly.
123      *
124      * @param key The key to delete.
125      *
126      * @return true if removed, false if the key didn't exist.
127      */

128      public boolean deleteByKey(String JavaDoc key)
129      {
130         myConfigHash.put(key,"");
131
132         return true;
133      }
134
135
136 /******************************** set-methods */
137     /**
138      * This method sets the config-property identified with the key to the
139      * value of the String value.
140      *
141      * @param key The key to store the value under.
142      * @param value The value to store
143      *
144      * @throws NullPointerException if value is null.
145      */

146      public void setStringByKey(String JavaDoc key, String JavaDoc value) throws NullPointerException JavaDoc
147      {
148         myConfigHash.put(key, value);
149      }
150
151     /**
152      * This saves back the String-array value in the property identified with key.
153      * The array is saved as a single String which is separated with ARRAY_SEPARATOR.
154      *
155      * @param key The key to store the value under.
156      * @param value The array to store.
157      *
158      * @throws NullPointerException if value is null.
159      */

160      public void setStringArrayByKey(String JavaDoc key, String JavaDoc[] value) throws NullPointerException JavaDoc
161      {
162          if(value == null || value.length == 0)
163              throw new NullPointerException JavaDoc("Empty array");
164
165          String JavaDoc tmp = "";
166          for(int i=0; i < value.length; i++)
167          {
168              tmp += value[i];
169              if(i != value.length-1)
170                  tmp += ARRAY_SEPARATOR;
171          }
172          myConfigHash.put(key,tmp);
173      }
174
175     /**
176      * This saves back a boolean value in the property identified with key.
177      * The boolean is converted in a String.
178      *
179      * @param key The key to store the value under.
180      * @param value The boolean to store.
181      *
182      * @throws NullPointerException if value is null.
183      */

184      public void setBooleanByKey(String JavaDoc key, boolean value) throws NullPointerException JavaDoc
185      {
186         if(value)
187             myConfigHash.put(key, "true");
188         else
189             myConfigHash.put(key, "false");
190      }
191
192     /**
193      * This saves back an int value in the property identified with key.
194      * The int is converted in a String.
195      *
196      * @param key The key to store the value under.
197      * @param value The int to store.
198      *
199      * @throws NullPointerException if value is null.
200      */

201      public void setIntByKey(String JavaDoc key, int value) throws NullPointerException JavaDoc
202      {
203         myConfigHash.put(key, Integer.toString(value));
204      }
205
206
207 /******************************** get-methods */
208     /**
209      * Used for getting strings.
210      *
211      * @return A string with the value of the specified key, or null if not set.
212      */

213     public String JavaDoc getStringByKey(String JavaDoc key)
214     {
215         return (String JavaDoc) myConfigHash.get(key);
216     }
217
218     /**
219      * Used for getting a property-array.
220      * A property-array is a property which has more than one value separated by
221      * commas
222      *
223      * @param key the name of the property-array
224      * @return an array of the properties values, or null if none have been found.
225      */

226     public String JavaDoc[] getStringArrayByKey(String JavaDoc key)
227     {
228         String JavaDoc value = getStringByKey(key);
229         if(value == null)
230             return null;
231
232         StringTokenizer st = new StringTokenizer(value, ARRAY_SEPARATOR);
233         int count = st.countTokens();
234         if(count == 0)
235             return null;
236
237         int i = 0;
238         String JavaDoc[] array = new String JavaDoc[count];
239         while(st.hasMoreTokens())
240         {
241             array[i++] = st.nextToken();
242         }
243         return array;
244     }
245
246     /**
247      * Used for getting boolean states.
248      * A key is true if it has a value of "true", "on". "1". The case needn't match.
249      * the key is false if it's value is "false", "off", "0".
250      *
251      * @throws ParseException if the key has another value than specified above.
252      * @return true or false as defined above.
253      */

254     public boolean getBooleanByKey(String JavaDoc key) throws ParseException JavaDoc
255     {
256         if(!isEmptyKey(key))
257         {
258             String JavaDoc str = (String JavaDoc) myConfigHash.get(key);
259
260             if(str.compareToIgnoreCase("true") == 0 ||
261                str.compareToIgnoreCase("on") == 0 ||
262                str.compareToIgnoreCase("1") == 0)
263                return true;
264             else if(str.compareToIgnoreCase("false") == 0 ||
265                     str.compareToIgnoreCase("off") == 0 ||
266                     str.compareToIgnoreCase("0") == 0)
267                     return false;
268         }
269         throw new ParseException JavaDoc("Key has an unknown value",0);
270     }
271
272     /**
273      * Used for getting int's.
274      *
275      * @throws ParseException if the String couldn't have been converted.
276      * @return int-value representing the key's converted value.
277      */

278     public int getIntByKey(String JavaDoc key) throws ParseException JavaDoc, NumberFormatException JavaDoc
279     {
280         if(isEmptyKey(key))
281             throw new ParseException JavaDoc("Key is empty",0);
282
283         return new Integer JavaDoc(getStringByKey(key)).intValue();
284     }
285 }
286
Popular Tags