KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > juddi > util > Config


1 /*
2  * Copyright 2001-2004 The Apache Software Foundation.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */

16 package org.apache.juddi.util;
17
18 import java.net.MalformedURLException JavaDoc;
19 import java.net.URL JavaDoc;
20 import java.util.Enumeration JavaDoc;
21 import java.util.Iterator JavaDoc;
22 import java.util.Properties JavaDoc;
23 import java.util.SortedSet JavaDoc;
24 import java.util.TreeSet JavaDoc;
25
26 import org.apache.commons.logging.Log;
27 import org.apache.commons.logging.LogFactory;
28 import org.apache.juddi.registry.RegistryEngine;
29
30 /**
31  * This class provides read access to key/value pairs loaded
32  * from a properties file.
33  *
34  * @author Steve Viens (sviens@apache.org)
35  */

36 public class Config extends Properties JavaDoc
37 {
38   // private reference to the log
39
private static Log log = LogFactory.getLog(Config.class);
40
41   // shared instance
42
static Config config;
43
44   /**
45    * shared Config constructor. Made private to
46    * avoid creation of more than one Config.
47    */

48   private Config()
49   {
50     super();
51   }
52
53   /**
54    * Returns a reference to the singleton Properties instance.
55    *
56    * @return Config A reference to the singleton Properties instance.
57    */

58   public static void addProperties(Properties JavaDoc props)
59   {
60     if (config == null)
61       config = createConfig();
62     config.putAll(props);
63   }
64
65   /**
66    * Returns a reference to the singleton Properties instance.
67    *
68    * @return Config A reference to the singleton Properties instance.
69    */

70   public static Properties JavaDoc getProperties()
71   {
72     if (config == null)
73       config = createConfig();
74     return config;
75   }
76
77   /**
78    *
79    */

80   public static String JavaDoc getOperator()
81   {
82     return getStringProperty(RegistryEngine.PROPNAME_OPERATOR_NAME,
83               RegistryEngine.DEFAULT_OPERATOR_NAME);
84   }
85
86   /**
87    *
88    */

89   public static String JavaDoc getDiscoveryURL()
90   {
91     return getStringProperty(RegistryEngine.PROPNAME_DISCOVERY_URL,
92               RegistryEngine.DEFAULT_DISCOVERY_URL);
93   }
94
95   /**
96    *
97    */

98   public static int getMaxNameLengthAllowed()
99   {
100     return getIntProperty(RegistryEngine.PROPNAME_MAX_NAME_LENGTH,
101               RegistryEngine.DEFAULT_MAX_NAME_LENGTH);
102   }
103
104   /**
105    *
106    */

107   public static int getMaxNameElementsAllowed()
108   {
109     return getIntProperty(RegistryEngine.PROPNAME_MAX_NAME_ELEMENTS,
110               RegistryEngine.DEFAULT_MAX_NAME_ELEMENTS);
111   }
112
113   /**
114    * Retrieves a configuration property as a String object.
115    * Loads the juddi.properties file if not already initialized.
116    *
117    * @param key Name of the property to be returned.
118    * @return Value of the property as a string or null if no property found.
119    */

120   public static String JavaDoc getStringProperty(String JavaDoc key, String JavaDoc defaultValue)
121   {
122     String JavaDoc stringVal = defaultValue;
123
124     String JavaDoc propValue = getStringProperty(key);
125     if (propValue != null)
126       stringVal = propValue;
127
128     return stringVal;
129   }
130
131   /**
132    * Get a configuration property as an int primitive.
133    *
134    * @param key Name of the numeric property to be returned.
135    * @return Value of the property as an Integer or null if no property found.
136    */

137   public static int getIntProperty(String JavaDoc key, int defaultValue)
138   {
139     int intVal = defaultValue;
140
141     String JavaDoc propValue = getStringProperty(key);
142     if (propValue != null)
143       intVal = Integer.parseInt(propValue);
144
145     return intVal;
146   }
147
148   /**
149    * Get a configuration property as an long primitive.
150    *
151    * @param key Name of the numeric property to be returned.
152    * @return Value of the property as an Long or null if no property found.
153    */

154   public static long getLongProperty(String JavaDoc key, long defaultValue)
155   {
156     long longVal = defaultValue;
157
158     String JavaDoc propValue = getStringProperty(key);
159     if (propValue != null)
160       longVal = Long.parseLong(propValue);
161
162     return longVal;
163   }
164
165   /**
166    * Get a configuration property as a boolean primitive. Note
167    * that the value of the returned Boolean will be false if
168    * the property sought after exists but is not equal to
169    * "true" (ignoring case).
170    *
171    * @param key Name of the numeric property to be returned.
172    * @return Value of the property as an Boolean or null if no property found.
173    */

174   public static boolean getBooleanProperty(String JavaDoc key, boolean defaultValue)
175   {
176     boolean boolVal = defaultValue;
177
178     String JavaDoc propValue = getStringProperty(key);
179     if ((propValue != null) && (propValue.equalsIgnoreCase("true")))
180       boolVal = true;
181
182     return boolVal;
183   }
184
185   /**
186    * Get a configuration property as a URL object.
187    *
188    * @param key Name of the url property to be returned.
189    * @return Value of the property as an URL or null if no property found.
190    */

191   public static URL JavaDoc getURLProperty(String JavaDoc key, URL JavaDoc defaultValue)
192   {
193     URL JavaDoc urlVal = defaultValue;
194
195     String JavaDoc propValue = getStringProperty(key);
196     if (propValue != null)
197     {
198       try
199       {
200         urlVal = new URL JavaDoc(propValue);
201       }
202       catch (MalformedURLException JavaDoc muex)
203       {
204         log.error(
205           "The " + key + " property value is invalid: " + propValue,muex);
206       }
207     }
208
209     return urlVal;
210   }
211
212   /**
213    * Retrieves a configuration property as a String object.
214    * Loads the juddi.properties file if not already initialized.
215    *
216    * @param key Name of the property to be returned.
217    * @return Value of the property as a string or null if no property found.
218    */

219   public static String JavaDoc getStringProperty(String JavaDoc key)
220   {
221     if (config == null)
222       config = createConfig();
223
224     // no properties to look into, return null
225
if (config == null)
226       return null;
227
228     // no property name/key to lookup, return null
229
if (key == null)
230       return null;
231
232     return config.getProperty(key);
233   }
234
235   /**
236    * Sets a property value in jUDDI's property
237    * registry. Loads the juddi.properties file if
238    * not already initialized.
239    *
240    * @param name Name of the property to be returned.
241    * @param value Property value as a string.
242    */

243   public static void setStringProperty(String JavaDoc name, String JavaDoc value)
244   {
245     if (config == null)
246       config = createConfig();
247
248     // no properties to save to, just return
249
if (config == null)
250       return;
251
252     // no property name/key, just return
253
if (name == null)
254       return;
255
256     // no property value, attempt removal (otherwise save/replace)
257
if (value == null)
258       config.remove(name);
259     else
260       config.setProperty(name, value); // save or replace prop value
261
}
262
263   /**
264    * Creates a single (singleton) instance of "org.util.Config"
265    *
266    * @return Config A reference to the singleton Config instance.
267    */

268   private static synchronized Config createConfig()
269   {
270     // If multiple threads are waiting to envoke
271
// this method only allow the first one to do so.
272

273     if (config == null)
274       config = new Config();
275     return config;
276   }
277
278   /**
279    * Returns a String containing a pipe-delimited ('|') list
280    * of name/value pairs.
281    * @return String pipe-delimited list of name/value pairs.
282    */

283   public String JavaDoc toString()
284   {
285     // let's create a place to put the property information
286
StringBuffer JavaDoc buff = new StringBuffer JavaDoc(100);
287
288     // gran an enumeration of the property names (or keys)
289
Enumeration JavaDoc propKeys = keys();
290     while (propKeys.hasMoreElements())
291     {
292       // extract the Property Name (aka Key) and Value
293
String JavaDoc propName = (String JavaDoc) propKeys.nextElement();
294       String JavaDoc propValue = getProperty(propName);
295
296       // append the name=value pair to the return buffer
297
buff.append(propName.trim());
298       buff.append("=");
299       buff.append(propValue.trim());
300       buff.append("\n");
301     }
302
303     return buff.toString();
304   }
305
306   /***************************************************************************/
307   /***************************** TEST DRIVER *********************************/
308   /***************************************************************************/
309
310   public static void main(String JavaDoc[] args)
311   {
312     Properties JavaDoc sysProps = null;
313     SortedSet JavaDoc sortedPropsSet = null;
314
315     sysProps = Config.getProperties();
316     sortedPropsSet = new TreeSet JavaDoc(sysProps.keySet());
317     for (Iterator JavaDoc keys = sortedPropsSet.iterator(); keys.hasNext();)
318     {
319       String JavaDoc key = (String JavaDoc) keys.next();
320       System.out.println(key + ": " + sysProps.getProperty(key));
321     }
322   }
323 }
Popular Tags