1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 package org.apache.juddi.util;
17
18 import java.net.MalformedURLException;
19 import java.net.URL;
20 import java.util.Enumeration;
21 import java.util.Iterator;
22 import java.util.Properties;
23 import java.util.SortedSet;
24 import java.util.TreeSet;
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
37 {
38
39 private static Log log = LogFactory.getLog(Config.class);
40
41
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 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 getProperties()
71 {
72 if (config == null)
73 config = createConfig();
74 return config;
75 }
76
77 /***
78 *
79 */
80 public static String getOperator()
81 {
82 return getStringProperty(RegistryEngine.PROPNAME_OPERATOR_NAME,
83 RegistryEngine.DEFAULT_OPERATOR_NAME);
84 }
85
86 /***
87 *
88 */
89 public static String 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 *
115 */
116 public static int getMaxBusinessesPerPublisher()
117 {
118 return getIntProperty(RegistryEngine.PROPNAME_MAX_BUSINESSES_PER_PUBLISHER,
119 RegistryEngine.DEFAULT_MAX_BUSINESSES_PER_PUBLISHER);
120 }
121
122 /***
123 *
124 */
125 public static int getMaxServicesPerBusiness()
126 {
127 return getIntProperty(RegistryEngine.PROPNAME_MAX_SERVICES_PER_BUSINESS,
128 RegistryEngine.DEFAULT_MAX_SERVICES_PER_BUSINESS);
129 }
130
131 /***
132 *
133 */
134 public static int getMaxBindingsPerService()
135 {
136 return getIntProperty(RegistryEngine.PROPNAME_MAX_BINDINGS_PER_SERVICE,
137 RegistryEngine.DEFAULT_MAX_BINDINGS_PER_SERVICE);
138 }
139
140 /***
141 *
142 */
143 public static int getMaxTModelsPerPublisher()
144 {
145 return getIntProperty(RegistryEngine.PROPNAME_MAX_TMODELS_PER_PUBLISHER,
146 RegistryEngine.DEFAULT_MAX_TMODELS_PER_PUBLISHER);
147 }
148
149 /***
150 * Retrieves a configuration property as a String object.
151 * Loads the juddi.properties file if not already initialized.
152 *
153 * @param key Name of the property to be returned.
154 * @return Value of the property as a string or null if no property found.
155 */
156 public static String getStringProperty(String key, String defaultValue)
157 {
158 String stringVal = defaultValue;
159
160 String propValue = getStringProperty(key);
161 if (propValue != null)
162 stringVal = propValue;
163
164 return stringVal;
165 }
166
167 /***
168 * Get a configuration property as an int primitive.
169 *
170 * @param key Name of the numeric property to be returned.
171 * @return Value of the property as an Integer or null if no property found.
172 */
173 public static int getIntProperty(String key, int defaultValue)
174 {
175 int intVal = defaultValue;
176
177 String propValue = getStringProperty(key);
178 if (propValue != null)
179 intVal = Integer.parseInt(propValue);
180
181 return intVal;
182 }
183
184 /***
185 * Get a configuration property as an long primitive.
186 *
187 * @param key Name of the numeric property to be returned.
188 * @return Value of the property as an Long or null if no property found.
189 */
190 public static long getLongProperty(String key, long defaultValue)
191 {
192 long longVal = defaultValue;
193
194 String propValue = getStringProperty(key);
195 if (propValue != null)
196 longVal = Long.parseLong(propValue);
197
198 return longVal;
199 }
200
201 /***
202 * Get a configuration property as a boolean primitive. Note
203 * that the value of the returned Boolean will be false if
204 * the property sought after exists but is not equal to
205 * "true" (ignoring case).
206 *
207 * @param key Name of the numeric property to be returned.
208 * @return Value of the property as an Boolean or null if no property found.
209 */
210 public static boolean getBooleanProperty(String key, boolean defaultValue)
211 {
212 boolean boolVal = defaultValue;
213
214 String propValue = getStringProperty(key);
215 if (propValue != null) {
216 if (propValue.equalsIgnoreCase("true")) {
217 boolVal = true;
218 } else if (propValue.equalsIgnoreCase("false")) {
219 boolVal = false;
220 }
221 }
222
223 return boolVal;
224 }
225
226 /***
227 * Get a configuration property as a URL object.
228 *
229 * @param key Name of the url property to be returned.
230 * @return Value of the property as an URL or null if no property found.
231 */
232 public static URL getURLProperty(String key, URL defaultValue)
233 {
234 URL urlVal = defaultValue;
235
236 String propValue = getStringProperty(key);
237 if (propValue != null)
238 {
239 try
240 {
241 urlVal = new URL(propValue);
242 }
243 catch (MalformedURLException muex)
244 {
245 log.error(
246 "The " + key + " property value is invalid: " + propValue,muex);
247 }
248 }
249
250 return urlVal;
251 }
252
253 /***
254 * Retrieves a configuration property as a String object.
255 * Loads the juddi.properties file if not already initialized.
256 *
257 * @param key Name of the property to be returned.
258 * @return Value of the property as a string or null if no property found.
259 */
260 public static String getStringProperty(String key)
261 {
262 if (config == null)
263 config = createConfig();
264
265
266 if (config == null)
267 return null;
268
269
270 if (key == null)
271 return null;
272
273 return config.getProperty(key);
274 }
275
276 /***
277 * Sets a property value in jUDDI's property
278 * registry. Loads the juddi.properties file if
279 * not already initialized.
280 *
281 * @param name Name of the property to be returned.
282 * @param value Property value as a string.
283 */
284 public static void setStringProperty(String name, String value)
285 {
286 if (config == null)
287 config = createConfig();
288
289
290 if (config == null)
291 return;
292
293
294 if (name == null)
295 return;
296
297
298 if (value == null)
299 config.remove(name);
300 else
301 config.setProperty(name, value);
302 }
303
304 /***
305 * Creates a single (singleton) instance of "org.util.Config"
306 *
307 * @return Config A reference to the singleton Config instance.
308 */
309 private static synchronized Config createConfig()
310 {
311
312
313
314 if (config == null)
315 config = new Config();
316 return config;
317 }
318
319 /***
320 * Returns a String containing a pipe-delimited ('|') list
321 * of name/value pairs.
322 * @return String pipe-delimited list of name/value pairs.
323 */
324 public String toString()
325 {
326
327 StringBuffer buff = new StringBuffer(100);
328
329
330 Enumeration propKeys = keys();
331 while (propKeys.hasMoreElements())
332 {
333
334 String propName = (String) propKeys.nextElement();
335 String propValue = getProperty(propName);
336
337
338 buff.append(propName.trim());
339 buff.append("=");
340 buff.append(propValue.trim());
341 buff.append("\n");
342 }
343
344 return buff.toString();
345 }
346
347 /****************************************************************************/
348 /****************************** TEST DRIVER *********************************/
349 /****************************************************************************/
350
351 public static void main(String[] args)
352 {
353 Properties sysProps = null;
354 SortedSet sortedPropsSet = null;
355
356 sysProps = Config.getProperties();
357 sortedPropsSet = new TreeSet(sysProps.keySet());
358 for (Iterator keys = sortedPropsSet.iterator(); keys.hasNext();)
359 {
360 String key = (String) keys.next();
361 System.out.println(key + ": " + sysProps.getProperty(key));
362 }
363 }
364 }