1 /*
2 * Copyright (C) The JContainer Group. All rights reserved.
3 *
4 * This software is published under the terms of the JContainer
5 * Software License version 1.1, a copy of which has been included
6 * with this distribution in the LICENSE.txt file.
7 */
8 package org.jcontainer.dna.impl;
9
10 import java.util.HashSet;
11 import java.util.Iterator;
12 import java.util.Properties;
13 import java.util.Set;
14 import org.jcontainer.dna.ParameterException;
15 import org.jcontainer.dna.Parameters;
16
17 /***
18 * Parameters implementation backed by a Properties object.
19 * The developer should create the DefaultParameters,
20 * associate parameters and then invoke {@link #makeReadOnly()}
21 * before passing the Parameters to the client component.
22 *
23 * @author <a href="mailto:peter at realityforge.org">Peter Donald</a>
24 * @version $Revision: 1.17 $ $Date: 2003/09/23 10:15:26 $
25 */
26 public class DefaultParameters
27 extends AbstractFreezable
28 implements Parameters
29 {
30 /***
31 * Constant for separator between parameters
32 * and child parameters.
33 */
34 private static final String SEPARATOR = ".";
35
36 /***
37 * Constant for empty prefix.
38 */
39 private static final String EMPTY_PREFIX = "";
40
41 /***
42 * The key-value pairs contained by parameters object.
43 */
44 private final Properties m_parameters = new Properties();
45
46 /***
47 * The child parameters objects created from with
48 * parameters object.
49 */
50 private final Set m_children = new HashSet();
51
52 /***
53 * The prefix associated with parameters object.
54 */
55 private final String m_prefix;
56
57 /***
58 * Create a parameters object with empty prefix.
59 */
60 public DefaultParameters()
61 {
62 this( EMPTY_PREFIX );
63 }
64
65 /***
66 * Create a parameters object with specified prefix.
67 *
68 * @param prefix the prefix
69 */
70 public DefaultParameters( final String prefix )
71 {
72 if( null == prefix )
73 {
74 throw new NullPointerException( "prefix" );
75 }
76 m_prefix = prefix;
77 }
78
79 /***
80 * Return the names of all the parameters.
81 *
82 * @return the names of all the parameters.
83 */
84 public String[] getParameterNames()
85 {
86 final Set set = getParameters().keySet();
87 return (String[])set.toArray( new String[ set.size() ] );
88 }
89
90 /***
91 * Return true of parameter with specified name exists.
92 *
93 * @param name the name
94 * @return true of parameter with specified name exists.
95 */
96 public boolean isParameter( final String name )
97 {
98 if( null == name )
99 {
100 throw new NullPointerException( "name" );
101 }
102 return getParameters().containsKey( name );
103 }
104
105 /***
106 * Return value of parameter with specified name.
107 *
108 * @param name the name
109 * @return the value
110 * @throws ParameterException if unable to locate parameter
111 */
112 public String getParameter( final String name )
113 throws ParameterException
114 {
115 if( null == name )
116 {
117 throw new NullPointerException( "name" );
118 }
119 final String property = getParameters().getProperty( name );
120 if( null == property )
121 {
122 final String message =
123 "Unable to locate parameter named " + name;
124 throw new ParameterException( message, name );
125 }
126 return property;
127 }
128
129 /***
130 * Return value of parameter with specified name.
131 *
132 * @param name the name
133 * @param defaultValue the defaultValue if specified parameter
134 * does not exist
135 * @return the value
136 */
137 public String getParameter( final String name,
138 final String defaultValue )
139 {
140 if( null == name )
141 {
142 throw new NullPointerException( "name" );
143 }
144 return getParameters().getProperty( name, defaultValue );
145 }
146
147 /***
148 * Return value of parameter with specified name as a boolean.
149 *
150 * @param name the name
151 * @return the value
152 * @throws ParameterException if unable to locate parameter
153 * or parameter can not be converted to correct type
154 */
155 public boolean getParameterAsBoolean( final String name )
156 throws ParameterException
157 {
158 return getParameter( name ).equals( "true" );
159 }
160
161 /***
162 * Return value of parameter with specified name as a boolean.
163 *
164 * @param name the name
165 * @param defaultValue the defaultValue if specified parameter
166 * does not exist or parameter can not be converted to
167 * the correct type
168 * @return the value
169 */
170 public boolean getParameterAsBoolean( final String name,
171 final boolean defaultValue )
172 {
173 final String value = getParameter( name, null );
174 if( null == value )
175 {
176 return defaultValue;
177 }
178 else
179 {
180 return value.equals( "true" );
181 }
182 }
183
184 /***
185 * Return value of parameter with specified name as an integer.
186 *
187 * @param name the name
188 * @return the value
189 * @throws ParameterException if unable to locate parameter
190 * or parameter can not be converted to correct type
191 */
192 public int getParameterAsInteger( final String name )
193 throws ParameterException
194 {
195 final String value = getParameter( name );
196 try
197 {
198 return Integer.parseInt( value );
199 }
200 catch( final NumberFormatException nfe )
201 {
202 final String prefixedName = prefixedName( name );
203 final String message =
204 "Unable to parse parameter named " + prefixedName +
205 " with value '" + value + "'";
206 throw new ParameterException( message, prefixedName, nfe );
207 }
208 }
209
210 /***
211 * Return value of parameter with specified name as an integer.
212 *
213 * @param name the name
214 * @param defaultValue the defaultValue if specified parameter
215 * does not exist or parameter can not be converted to
216 * the correct type
217 * @return the value
218 */
219 public int getParameterAsInteger( final String name,
220 final int defaultValue )
221 {
222 final String value = getParameter( name, null );
223 if( null == value )
224 {
225 return defaultValue;
226 }
227 else
228 {
229 try
230 {
231 return Integer.parseInt( value );
232 }
233 catch( final NumberFormatException nfe )
234 {
235 return defaultValue;
236 }
237 }
238 }
239
240 /***
241 * Return value of parameter with specified name as a long.
242 *
243 * @param name the name
244 * @return the value
245 * @throws ParameterException if unable to locate parameter
246 * or parameter can not be converted to correct type
247 */
248 public long getParameterAsLong( final String name )
249 throws ParameterException
250 {
251 final String value = getParameter( name );
252 try
253 {
254 return Long.parseLong( value );
255 }
256 catch( final NumberFormatException nfe )
257 {
258 final String prefixedName = prefixedName( name );
259 final String message =
260 "Unable to parse parameter named " + prefixedName +
261 " with value '" + value + "'";
262 throw new ParameterException( message, prefixedName, nfe );
263 }
264 }
265
266 /***
267 * Return value of parameter with specified name as a long.
268 *
269 * @param name the name
270 * @param defaultValue the defaultValue if specified parameter
271 * does not exist or parameter can not be converted to
272 * the correct type
273 * @return the value
274 */
275 public long getParameterAsLong( final String name,
276 final long defaultValue )
277 {
278 final String value = getParameter( name, null );
279 if( null == value )
280 {
281 return defaultValue;
282 }
283 else
284 {
285 try
286 {
287 return Long.parseLong( value );
288 }
289 catch( final NumberFormatException nfe )
290 {
291 return defaultValue;
292 }
293 }
294 }
295
296 /***
297 * Return value of parameter with specified name as a float.
298 *
299 * @param name the name
300 * @return the value
301 * @throws ParameterException if unable to locate parameter
302 * or parameter can not be converted to correct type
303 */
304 public float getParameterAsFloat( final String name )
305 throws ParameterException
306 {
307 final String value = getParameter( name );
308 try
309 {
310 return Float.parseFloat( value );
311 }
312 catch( final NumberFormatException nfe )
313 {
314 final String prefixedName = prefixedName( name );
315 final String message =
316 "Unable to parse parameter named " + name +
317 " with value '" + value + "'";
318 throw new ParameterException( message, prefixedName, nfe );
319 }
320 }
321
322 /***
323 * Return value of parameter with specified name as a float.
324 *
325 * @param name the name
326 * @param defaultValue the defaultValue if specified parameter
327 * does not exist or parameter can not be converted to
328 * the correct type
329 * @return the value
330 */
331 public float getParameterAsFloat( final String name,
332 final float defaultValue )
333 {
334 final String value = getParameter( name, null );
335 if( null == value )
336 {
337 return defaultValue;
338 }
339 else
340 {
341 try
342 {
343 return Float.parseFloat( value );
344 }
345 catch( final NumberFormatException nfe )
346 {
347 return defaultValue;
348 }
349 }
350 }
351
352 /***
353 * Return a Parameters object that represents a
354 * subset of parameters with specified prefix. The child
355 * parameters has a prefix with the separator ('.') appended.
356 * ie. if the prefix was "foo" then the parameter
357 * "foo.baz" would be included in child Parameters object
358 * using the key "baz".
359 *
360 * @param prefix the prefix
361 * @return the parameters object
362 */
363 public Parameters getChildParameters( final String prefix )
364 {
365 if( null == prefix )
366 {
367 throw new NullPointerException( "prefix" );
368 }
369 final String prefixAndSeparator = prefix + SEPARATOR;
370 final int length = prefix.length() + 1;
371 final String child = prefixedName( prefix );
372 final DefaultParameters parameters = new DefaultParameters( child );
373 final Iterator iterator = getParameters().keySet().iterator();
374 while( iterator.hasNext() )
375 {
376 final String key = (String)iterator.next();
377 if( key.startsWith( prefixAndSeparator ) )
378 {
379 final String value = getParameter( key, null );
380 final String newKey = key.substring( length );
381 parameters.setParameter( newKey, value );
382 }
383 }
384
385 parameters.makeReadOnly();
386 getChildren().add( parameters );
387 return parameters;
388 }
389
390 /***
391 * Return name that may be prefixed with full property
392 * name unless prefix is empty.
393 *
394 * @param name the name
395 * @return the name with prefix decorated
396 */
397 private String prefixedName( final String name )
398 {
399 if( getPrefix().equals( EMPTY_PREFIX ) )
400 {
401 return name;
402 }
403 else
404 {
405 return getPrefix() + SEPARATOR + name;
406 }
407 }
408
409 /***
410 * Mark the resource and all child parameter
411 * objects as read only.
412 */
413 public void makeReadOnly()
414 {
415 super.makeReadOnly();
416 final Iterator iterator = getChildren().iterator();
417 while( iterator.hasNext() )
418 {
419 final Object child = iterator.next();
420 if( child instanceof Freezable )
421 {
422 ( (Freezable)child ).makeReadOnly();
423 }
424 }
425 }
426
427 /***
428 * Set parameter with specified name to specified value.
429 *
430 * @param name the parameter name
431 * @param value the parameter value
432 */
433 public void setParameter( final String name,
434 final String value )
435 {
436 if( null == name )
437 {
438 throw new NullPointerException( "name" );
439 }
440 if( null == value )
441 {
442 throw new NullPointerException( "value" );
443 }
444 checkWriteable();
445 getParameters().setProperty( name, value );
446 }
447
448 /***
449 * Return the backing properties object associated with parameters.
450 *
451 * @return the backing properties object associated with parameters.
452 */
453 protected final Properties getParameters()
454 {
455 return m_parameters;
456 }
457
458 /***
459 * Return the prefix associated with Parameters object.
460 *
461 * @return the prefix associated with Parameters object.
462 */
463 protected final String getPrefix()
464 {
465 return m_prefix;
466 }
467
468 /***
469 * Return the set of child parameter objects.
470 *
471 * @return the set of child parameter objects
472 */
473 protected final Set getChildren()
474 {
475 return m_children;
476 }
477 }
This page was automatically generated by Maven