KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > ubermq > kernel > Configurator


1 package com.ubermq.kernel;
2
3 import java.io.*;
4 import java.util.*;
5
6 /**
7  * An extensible configurator, capable of reading various file
8  * formats of configuration files. This class cannot be instantiated,
9  * but servers and clients can setup the configurator via an InputStream.
10  * Subsequently, the properties are available to all classes within the JVM.
11  * Provides an abstraction layer for application level code to determine
12  * properties.
13  */

14 public class Configurator
15 {
16     private static Properties theProperties = new Properties();
17
18     private Configurator()
19     {
20     }
21
22     /**
23      * Read a Properties object from the input stream as specified
24      * by <code>Properties.load</code>.
25      */

26     public static void setup(InputStream is) throws IOException
27     {
28         Properties props = new Properties();
29         props.load(is);
30         setup(props);
31     }
32
33     /**
34      * Set the globally available set of properties to be
35      * equivalent to the input.
36      * @param props the properties object that will be cloned and used as the globally
37      * available property set.
38      */

39     public static void setup(Properties props)
40     {
41         theProperties = (Properties)props.clone();
42     }
43
44     /**
45      * @return the value corresponding to the named parameter, or null
46      * if the name is not present in the properties list.
47      */

48     public static String JavaDoc getProperty(String JavaDoc property)
49     {
50         return theProperties.getProperty(property);
51     }
52
53     /**
54      * @return the value corresponding to the named parameter. If the name
55      * is not specified, <code>defaultValue</code> is returned.
56      */

57     public static String JavaDoc getProperty(String JavaDoc property, String JavaDoc defaultValue)
58     {
59         return theProperties.getProperty(property, defaultValue);
60     }
61
62     /**
63      * Interprets a configured property setting as a comma-delimited list,
64      * and returns the members as an array of String.
65      *
66      * @param property the property value to retrieve
67      * @param defaultValue the default value.
68      * @return a String[] representing the contents.
69      */

70     public static String JavaDoc[] getPropertyList(String JavaDoc property, String JavaDoc defaultValue)
71     {
72         String JavaDoc value = getProperty(property, defaultValue);
73         if (value != null)
74         {
75             return convertFromPropertyList(value);
76         }
77         else
78         {
79             return new String JavaDoc[0];
80         }
81     }
82     
83     /**
84      * Converts an array of String into a comma-delimited property list
85      * that can be parsed by <code>convertFromPropertyList</code> or
86      * <code>getPropertyList</code>.
87      *
88      * @param values the values to convert.
89      * @return a String that is the array in comma-delimited form.
90      */

91     public static String JavaDoc convertToPropertyList(String JavaDoc[] values)
92     {
93         if (values.length <= 0)
94             return "";
95             
96         StringBuffer JavaDoc sb = new StringBuffer JavaDoc(values[0]);
97         for(int i=1;i < values.length;i++)
98         {
99             sb.append(",");
100             sb.append(values[i]);
101         }
102         return sb.toString();
103     }
104
105     /**
106      * Converts a comma-delimited property list created by <code>convertToPropertyList</code>
107      * into an String[].
108      *
109      * @param value the list to convert.
110      * @return a String[] representing the parsed values.
111      */

112     public static String JavaDoc[] convertFromPropertyList(String JavaDoc value)
113     {
114         StringTokenizer st = new StringTokenizer(value, ", ");
115         List l = new LinkedList();
116         while (st.hasMoreTokens())
117         {
118             l.add(st.nextToken());
119         }
120         return (String JavaDoc[])l.toArray(new String JavaDoc[l.size()]);
121     }
122
123     /**
124      * @return the property associated with the named property as an Object.
125      */

126     public static Object JavaDoc get(Object JavaDoc property)
127     {
128         return theProperties.get(property);
129     }
130
131     public static Properties getProperties()
132     {
133         return theProperties;
134     }
135
136 }
137
Popular Tags