View Javadoc

1   /***
2    * Redistribution and use of this software and associated documentation
3    * ("Software"), with or without modification, are permitted provided
4    * that the following conditions are met:
5    *
6    * 1. Redistributions of source code must retain copyright
7    *    statements and notices.  Redistributions must also contain a
8    *    copy of this document.
9    *
10   * 2. Redistributions in binary form must reproduce the
11   *    above copyright notice, this list of conditions and the
12   *    following disclaimer in the documentation and/or other
13   *    materials provided with the distribution.
14   *
15   * 3. The name "Exolab" must not be used to endorse or promote
16   *    products derived from this Software without prior written
17   *    permission of Exoffice Technologies.  For written permission,
18   *    please contact info@exolab.org.
19   *
20   * 4. Products derived from this Software may not be called "Exolab"
21   *    nor may "Exolab" appear in their names without prior written
22   *    permission of Exoffice Technologies. Exolab is a registered
23   *    trademark of Exoffice Technologies.
24   *
25   * 5. Due credit should be given to the Exolab Project
26   *    (http://www.exolab.org/).
27   *
28   * THIS SOFTWARE IS PROVIDED BY EXOFFICE TECHNOLOGIES AND CONTRIBUTORS
29   * ``AS IS'' AND ANY EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT
30   * NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
31   * FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL
32   * EXOFFICE TECHNOLOGIES OR ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
33   * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
34   * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
35   * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
36   * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
37   * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
38   * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
39   * OF THE POSSIBILITY OF SUCH DAMAGE.
40   *
41   * Copyright 2005 (C) Exoffice Technologies Inc. All Rights Reserved.
42   *
43   * $Id: Properties.java,v 1.2 2005/05/03 13:45:59 tanderson Exp $
44   */
45  
46  package org.exolab.jms.net.util;
47  
48  import java.util.Map;
49  import java.util.HashMap;
50  
51  import org.exolab.jms.net.connector.ResourceException;
52  import org.exolab.jms.net.uri.InvalidURIException;
53  import org.exolab.jms.net.uri.URI;
54  import org.exolab.jms.net.uri.URIHelper;
55  
56  
57  /***
58   * Helper class for manipulating string property maps.
59   *
60   * @author <a href="mailto:tma@netspace.net.au">Tim Anderson</a>
61   * @version $Revision: 1.2 $ $Date: 2005/05/03 13:45:59 $
62   */
63  public final class Properties {
64  
65      /***
66       * The properties.
67       */
68      private final Map _properties;
69  
70      /***
71       * The property prefix. If non-null, this is prepended to unqualified
72       * property names.
73       */
74      private final String _prefix;
75  
76  
77      /***
78       * Construct a new <code>Properties</code>.
79       *
80       * @param prefix     the property name prefix. If non-null, this is
81       *                   prepended to unqualified property names.
82       */
83      public Properties(String prefix) {
84          this(null, prefix);
85      }
86  
87      /***
88       * Construct a new <code>Properties</code>.
89       *
90       * @param properties the properties to use. May be <code>null</code>
91       * @param prefix     the property name prefix. If non-null, this is
92       *                   prepended to property names before performing lookups
93       */
94      public Properties(Map properties, String prefix) {
95          _properties = (properties != null) ? properties : new HashMap();
96          _prefix = prefix;
97      }
98  
99      /***
100      * Adds a property to the underlying map.
101      * If the property already exists, it will be replaced.
102      *
103      * @param name  the property name
104      * @param value the property value. May be <code>null</code>
105      */
106     public void set(String name, String value) {
107         _properties.put(getName(name), value);
108     }
109 
110     /***
111      * Adds a property to the underlying map, iff its value is non-null.
112      * If the non-null, and the property already exists, it will be replaced.
113      *
114      * @param name  the property name
115      * @param value the property value
116      */
117     public void setNonNull(String name, String value) {
118         if (value != null) {
119             _properties.put(getName(name), value);
120         }
121     }
122 
123     /***
124      * Adds a boolean property to the underlying map, as a String.
125      * If the property already exists, it will be replaced.
126      *
127      * @param name  the property name
128      * @param value the property value
129      */
130     public void set(String name, boolean value) {
131         Boolean bool = (value) ? Boolean.TRUE : Boolean.FALSE;
132         set(name, bool.toString());
133     }
134 
135     /***
136      * Adds an integer property to the underlying map, as a String.
137      * If the property already exists, it will be replaced.
138      *
139      * @param name  the property name
140      * @param value the property value
141      */
142     public void set(String name, int value) {
143         set(name, Integer.toString(value));
144     }
145 
146     /***
147      * Adds an object property to the underlying map, as a String.
148      * If the property already exists, it will be replaced.
149      *
150      * @param name  the property name
151      * @param value the property value
152      */
153     public void set(String name, Object value) {
154         if (value != null) {
155             set(name, value.toString());
156         } else {
157             set(name, null);
158         }
159     }
160 
161     /***
162      * Adds an object property to the underlying map, as a String,
163      * iff its value is non-null.
164      * If non-null, and the property already exists, it will be replaced.
165      *
166      * @param name  the property name
167      * @param value the property value
168      */
169     public void setNonNull(String name, Object value) {
170         if (value != null) {
171             set(name, value.toString());
172         }
173     }
174 
175     /***
176      * Returns the value of a property.
177      *
178      * @param name the property name
179      * @return the corresponding value, or <code>null</code> if none can be
180      *         found
181      * @throws ResourceException if the property isn't a string
182      */
183     public String get(String name) throws ResourceException {
184         Object result = null;
185         name = getName(name);
186         result = _properties.get(name);
187         if (result != null && !(result instanceof String)) {
188             throw new ResourceException("Invalid type for property=" + name);
189         }
190         return (String) result;
191     }
192 
193     /***
194      * Returns the value of a boolean property.
195      *
196      * @param name         the property name
197      * @param defaultValue the value to return if the property doesn't exist.
198      * @return the corresponding value, or <code>defaultValue</code> if none can
199      *         be  found
200      * @throws ResourceException if the property isn't a valid boolean
201      */
202     public boolean getBoolean(String name, boolean defaultValue)
203             throws ResourceException {
204         boolean result = defaultValue;
205         String value = get(name);
206         if (value != null) {
207             if (value.equalsIgnoreCase("true")) {
208                 result = true;
209             } else if (value.equalsIgnoreCase("false")) {
210                 result = false;
211             } else {
212                 throw new ResourceException("Invalid boolean for property="
213                                             + getName(name)
214                                             + ": " + value);
215             }
216         }
217         return result;
218     }
219 
220     /***
221      * Returns the value of an integer property.
222      *
223      * @param name         the property name
224      * @param defaultValue the value to return if the property doesn't exist.
225      * @return the corresponding value, or <code>defaultValue</code> if none can
226      *         be  found
227      * @throws ResourceException if the property isn't a valid integer
228      */
229     public int getInt(String name, int defaultValue)
230             throws ResourceException {
231         int result = defaultValue;
232         String value = get(name);
233         if (value != null) {
234             try {
235                 result = Integer.parseInt(value);
236             } catch (NumberFormatException exception) {
237                 throw new ResourceException("Invalid int for property="
238                                             + getName(name)
239                                             + ": " + value);
240             }
241         }
242         return result;
243     }
244 
245     /***
246      * Returns the value of an URI property.
247      *
248      * @param name the property name
249      * @return the corresponding URI, or <code>null</code> if none can be found
250      * @throws ResourceException if the URI is invalid
251      */
252     public URI getURI(String name) throws ResourceException {
253         URI result = null;
254         String uri = get(name);
255         if (uri != null) {
256             try {
257                 result = URIHelper.parse(uri);
258             } catch (InvalidURIException exception) {
259                 throw new ResourceException("Invalid URI for property="
260                                             + getName(name)
261                                             + ": " + uri);
262             }
263         }
264         return result;
265     }
266 
267     /***
268      * Returns the underlying properties.
269      *
270      * @return the underlying properties
271      */
272     public Map getProperties() {
273         return _properties;
274     }
275 
276     /***
277      * Prepends the supplied name with the property prefix, if it is
278      * unqualified (i.e, contains no "."). If the prefix is null, returns the
279      * name unchanged.
280      *
281      * @param name the property name
282      * @return the fully qualified property name.
283      */
284     private String getName(String name) {
285         String result;
286         if (_prefix != null && name.indexOf('.') == -1) {
287             result = _prefix + name;
288         } else {
289             result = name;
290         }
291         return result;
292     }
293 
294 }