KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jboss > util > property > PropertyManager


1 /*
2   * JBoss, Home of Professional Open Source
3   * Copyright 2005, JBoss Inc., and individual contributors as indicated
4   * by the @authors tag. See the copyright.txt in the distribution for a
5   * full listing of individual contributors.
6   *
7   * This is free software; you can redistribute it and/or modify it
8   * under the terms of the GNU Lesser General Public License as
9   * published by the Free Software Foundation; either version 2.1 of
10   * the License, or (at your option) any later version.
11   *
12   * This software is distributed in the hope that it will be useful,
13   * but WITHOUT ANY WARRANTY; without even the implied warranty of
14   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15   * Lesser General Public License for more details.
16   *
17   * You should have received a copy of the GNU Lesser General Public
18   * License along with this software; if not, write to the Free
19   * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
20   * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
21   */

22 package org.jboss.util.property;
23
24 import java.security.AccessController JavaDoc;
25 import java.security.PrivilegedAction JavaDoc;
26 import java.util.Map JavaDoc;
27 import java.util.Iterator JavaDoc;
28
29 import java.io.IOException JavaDoc;
30
31 import org.jboss.util.ThrowableHandler;
32
33 /**
34  * A more robust replacement of <tt>java.lang.System</tt> for property
35  * access.
36  *
37  * @version <tt>$Revision: 1958 $</tt>
38  * @author <a HREF="mailto:jason@planet57.com">Jason Dillon</a>
39  * @author <a HREF="mailto:adrian@jboss.com">Adrian Brock</a>
40  */

41 public final class PropertyManager
42 {
43    /** Property reader list property name */
44    public static final String JavaDoc READER_PROPERTY_NAME = "org.jboss.util.property.reader";
45
46    /** Token which specifies the default property reader */
47    public static final String JavaDoc DEFAULT_PROPERTY_READER_TOKEN = "DEFAULT";
48
49    /** The default property reader name array */
50    private static final String JavaDoc[] DEFAULT_PROPERTY_READERS = { DEFAULT_PROPERTY_READER_TOKEN };
51
52    /** Default property container */
53    private static PropertyMap props;
54
55    /**
56     * Do not allow instantiation of this class.
57     */

58    private PropertyManager()
59    {
60    }
61
62    /**
63     * Initialize the property system.
64     */

65    static
66    {
67       // construct default property container and initialze from system props
68
props = new PropertyMap();
69       PrivilegedAction JavaDoc action = new PrivilegedAction JavaDoc()
70       {
71          public Object JavaDoc run()
72          {
73             props.putAll(System.getProperties());
74
75             // replace system props to enable notifications via System.setProperty()
76
System.setProperties(props);
77
78             // load properties from initial property readers
79
String JavaDoc[] readerNames = getArrayProperty(READER_PROPERTY_NAME, DEFAULT_PROPERTY_READERS);
80
81             // construct each source and read its properties
82
for (int i = 0; i < readerNames.length; i++)
83             {
84                try
85                {
86                   if (readerNames[i].equals(DEFAULT_PROPERTY_READER_TOKEN))
87                   {
88                      load(new DefaultPropertyReader());
89                   }
90                   else
91                   {
92                      load(readerNames[i]);
93                   }
94                }
95                catch (IOException JavaDoc e)
96                {
97                   ThrowableHandler.add(e);
98                }
99             }
100             return null;
101          }
102       };
103       AccessController.doPrivileged(action);
104    }
105
106    /**
107     * Get the default <tt>PropertyMap</tt>.
108     *
109     * @return Default <tt>PropertyMap</tt>.
110     */

111    public static PropertyMap getDefaultPropertyMap()
112    {
113       SecurityManager JavaDoc sm = System.getSecurityManager();
114       if (sm != null)
115          sm.checkPropertiesAccess();
116       return props;
117    }
118
119    /**
120     * Add a property listener.
121     *
122     * @param listener Property listener to add.
123     */

124    public static void addPropertyListener(final PropertyListener listener)
125    {
126       SecurityManager JavaDoc sm = System.getSecurityManager();
127       if (sm != null)
128          sm.checkPropertiesAccess();
129       props.addPropertyListener(listener);
130    }
131
132    /**
133     * Add an array of property listeners.
134     *
135     * @param listeners Array of property listeners to add.
136     */

137    public static void addPropertyListeners(final PropertyListener[] listeners)
138    {
139       SecurityManager JavaDoc sm = System.getSecurityManager();
140       if (sm != null)
141          sm.checkPropertiesAccess();
142       props.addPropertyListeners(listeners);
143    }
144
145    /**
146     * Remove a property listener.
147     *
148     * @param listener Property listener to remove.
149     * @return True if listener was removed.
150     */

151    public static boolean removePropertyListener(final PropertyListener listener)
152    {
153       return props.removePropertyListener(listener);
154    }
155
156    /**
157     * Load properties from a map.
158     *
159     * @param prefix Prefix to append to all map keys (or <tt>null</tt>).
160     * @param map Map containing properties to load.
161     */

162    public static void load(final String JavaDoc prefix, final Map JavaDoc map) throws PropertyException
163    {
164       SecurityManager JavaDoc sm = System.getSecurityManager();
165       if (sm != null)
166          sm.checkPropertiesAccess();
167       props.load(prefix, map);
168    }
169
170    /**
171     * Load properties from a map.
172     *
173     * @param map Map containing properties to load.
174     */

175    public static void load(final Map JavaDoc map) throws PropertyException, IOException JavaDoc
176    {
177       SecurityManager JavaDoc sm = System.getSecurityManager();
178       if (sm != null)
179          sm.checkPropertiesAccess();
180       props.load(map);
181    }
182
183    /**
184     * Load properties from a <tt>PropertyReader</tt>.
185     *
186     * @param reader <tt>PropertyReader</tt> to read properties from.
187     */

188    public static void load(final PropertyReader reader) throws PropertyException, IOException JavaDoc
189    {
190       SecurityManager JavaDoc sm = System.getSecurityManager();
191       if (sm != null)
192          sm.checkPropertiesAccess();
193       props.load(reader);
194    }
195
196    /**
197     * Load properties from a <tt>PropertyReader</tt> specifed by the
198     * given class name.
199     *
200     * @param classname Class name of a <tt>PropertyReader</tt> to
201     * read from.
202     */

203    public static void load(final String JavaDoc classname) throws PropertyException, IOException JavaDoc
204    {
205       SecurityManager JavaDoc sm = System.getSecurityManager();
206       if (sm != null)
207          sm.checkPropertiesAccess();
208       props.load(classname);
209    }
210
211    /**
212     * Set a property.
213     *
214     * @param name Property name.
215     * @param value Property value.
216     * @return Previous property value or <tt>null</tt>.
217     */

218    public static String JavaDoc setProperty(final String JavaDoc name, final String JavaDoc value)
219    {
220       SecurityManager JavaDoc sm = System.getSecurityManager();
221       if (sm != null)
222          sm.checkPropertyAccess(name);
223       return (String JavaDoc) props.setProperty(name, value);
224    }
225
226    /**
227     * Remove a property.
228     *
229     * @param name Property name.
230     * @return Removed property value or <tt>null</tt>.
231     */

232    public static String JavaDoc removeProperty(final String JavaDoc name)
233    {
234       SecurityManager JavaDoc sm = System.getSecurityManager();
235       if (sm != null)
236          sm.checkPropertyAccess(name);
237       return props.removeProperty(name);
238    }
239
240    /**
241     * Get a property.
242     *
243     * @param name Property name.
244     * @param defaultValue Default property value.
245     * @return Property value or default.
246     */

247    public static String JavaDoc getProperty(final String JavaDoc name, final String JavaDoc defaultValue)
248    {
249       SecurityManager JavaDoc sm = System.getSecurityManager();
250       if (sm != null)
251          sm.checkPropertyAccess(name);
252       return props.getProperty(name, defaultValue);
253    }
254
255    /**
256     * Get a property.
257     *
258     * @param name Property name.
259     * @return Property value or <tt>null</tt>.
260     */

261    public static String JavaDoc getProperty(final String JavaDoc name)
262    {
263       SecurityManager JavaDoc sm = System.getSecurityManager();
264       if (sm != null)
265          sm.checkPropertyAccess(name);
266       return props.getProperty(name);
267    }
268
269    /**
270     * Get an array style property.
271     *
272     * @param base Base property name.
273     * @param defaultValues Default property values.
274     * @return Array of property values or default.
275     */

276    public static String JavaDoc[] getArrayProperty(final String JavaDoc base, final String JavaDoc[] defaultValues)
277    {
278       SecurityManager JavaDoc sm = System.getSecurityManager();
279       if (sm != null)
280          sm.checkPropertiesAccess();
281       return props.getArrayProperty(base, defaultValues);
282    }
283
284    /**
285     * Get an array style property.
286     *
287     * @param name Property name.
288     * @return Array of property values or empty array.
289     */

290    public static String JavaDoc[] getArrayProperty(final String JavaDoc name)
291    {
292       SecurityManager JavaDoc sm = System.getSecurityManager();
293       if (sm != null)
294          sm.checkPropertyAccess(name);
295       return props.getArrayProperty(name);
296    }
297
298    /**
299     * Return an iterator over all contained property names.
300     *
301     * @return Property name iterator.
302     */

303    public static Iterator JavaDoc names()
304    {
305       SecurityManager JavaDoc sm = System.getSecurityManager();
306       if (sm != null)
307          sm.checkPropertiesAccess();
308       return props.names();
309    }
310
311    /**
312     * Check if this map contains a given property.
313     *
314     * @param name Property name.
315     * @return True if contains property.
316     */

317    public static boolean containsProperty(final String JavaDoc name)
318    {
319       SecurityManager JavaDoc sm = System.getSecurityManager();
320       if (sm != null)
321          sm.checkPropertyAccess(name);
322       return props.containsProperty(name);
323    }
324
325    /**
326     * Get a property group for the given property base.
327     *
328     * @param basename Base property name.
329     * @return Property group.
330     */

331    public static PropertyGroup getPropertyGroup(final String JavaDoc basename)
332    {
333       SecurityManager JavaDoc sm = System.getSecurityManager();
334       if (sm != null)
335          sm.checkPropertiesAccess();
336       return props.getPropertyGroup(basename);
337    }
338
339    /**
340     * Get a property group for the given property base at the given index.
341     *
342     * @param basename Base property name.
343     * @param index Array property index.
344     * @return Property group.
345     */

346    public static PropertyGroup getPropertyGroup(final String JavaDoc basename, final int index)
347    {
348       SecurityManager JavaDoc sm = System.getSecurityManager();
349       if (sm != null)
350          sm.checkPropertiesAccess();
351       return props.getPropertyGroup(basename, index);
352    }
353 }
354
Popular Tags