KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > sync4j > framework > config > Configuration


1 /**
2  * Copyright (C) 2003-2005 Funambol
3  *
4  * This program is free software; you can redistribute it and/or modify
5  * it under the terms of the GNU General Public License as published by
6  * the Free Software Foundation; either version 2 of the License, or
7  * (at your option) any later version.
8  *
9  * This program is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12  * GNU General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with this program; if not, write to the Free Software
16  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
17  */

18
19
20 package sync4j.framework.config;
21
22 import java.io.InputStream JavaDoc;
23 import java.io.IOException JavaDoc;
24
25 import java.net.URL JavaDoc;
26
27 import java.util.Map JavaDoc;
28 import java.util.HashMap JavaDoc;
29 import java.util.Properties JavaDoc;
30 import java.util.Collections JavaDoc;
31
32 import sync4j.framework.tools.beans.BeanFactory;
33 import sync4j.framework.config.ConfigurationException;
34
35 import org.apache.commons.lang.StringUtils;
36
37 /**
38  * Incapsulates all the configuration information about the Sync4j Engine.
39  * Configuration parameters are stored as properties in a Map structure. The
40  * key is the name of the parameter and it is strucured in dotted separated
41  * namespaces. The value of the parameter is the value of the key and can be of
42  * any type. Accessor methods are provided to get the value of the parameter in
43  * a particular type (such as String, int, double, boolean, ...).<br>
44  * Access to getXXX() and setXXX() methods are synchronized.
45  * <p>
46  * This class follows the Singleton pattern, therefore it cannot be directly
47  * instantiated; an internal instance is created just once and returned at any
48  * getConfiguration() call.
49  *
50  * @author Stefano Fornari @ funambol
51  *
52  * @version $Id: Configuration.java,v 1.1 2005/05/16 17:32:54 nichele Exp $
53  */

54 public class Configuration
55 implements java.io.Serializable JavaDoc {
56
57     // --------------------------------------------------------------- Constants
58

59     // ------------------------------------------------------------ Private data
60

61     private final Map JavaDoc internalMap = Collections.synchronizedMap(new HashMap JavaDoc());
62
63     private static Configuration singleton;
64     private transient ClassLoader JavaDoc classLoader = null;
65
66     // -------------------------------------------------------------- properties
67

68     /** Getter for property classLoader.
69      * @return Value of property classLoader.
70      *
71      */

72     public ClassLoader JavaDoc getClassLoader() {
73         return classLoader;
74     }
75
76     /** Setter for property classLoader.
77      * @param classLoader New value of property classLoader.
78      *
79      */

80     public void setClassLoader(ClassLoader JavaDoc classLoader) {
81         this.classLoader = classLoader;
82     }
83
84     // ------------------------------------------------------------ Constructors
85

86     protected Configuration() {
87     }
88
89     public static synchronized Configuration getConfiguration() {
90         if (singleton == null) {
91             singleton = new Configuration();
92         }
93
94         return singleton;
95     }
96
97     // ---------------------------------------------------------- Public methods
98

99     public String JavaDoc getStringValue(String JavaDoc name, String JavaDoc def)
100     throws ConfigurationException {
101         String JavaDoc value = (String JavaDoc)internalMap.get(name);
102
103         return (value == null) ? def : value;
104     }
105
106     public String JavaDoc getStringValue(String JavaDoc name)
107     throws ConfigurationException {
108         String JavaDoc value = (String JavaDoc)internalMap.get(name);
109
110         if (value == null) {
111             throw new ConfigurationException("No " + name + " property defined");
112         }
113
114         return value;
115     }
116
117     /**
118      * Turns a character separated values property into an array of String
119      * objects given the separator. For example:
120      * <pre>
121      * one;two;trhee
122      * </pre>
123      * will result in
124      * <pre
125      * {"one", "two", "trhee"}
126      * </pre>
127      *
128      * @param name the name of the property
129      * @param separator the separator character
130      *
131      * @return an array of Strings
132      */

133     public String JavaDoc[] getStringArray(String JavaDoc name, char separator)
134     throws ConfigurationException {
135         String JavaDoc value = (String JavaDoc)internalMap.get(name);
136
137         if (value == null) {
138             throw new ConfigurationException("No " + name + " property defined");
139         }
140
141         //
142
// NOTE: String.split() is not used for performance reason. We do not
143
// need a regular expression here.
144
//
145
return StringUtils.split(value, String.valueOf(separator));
146     }
147
148     public int getIntValue(String JavaDoc name, int def)
149     throws ConfigurationException {
150         Integer JavaDoc value = (Integer JavaDoc)internalMap.get(name);
151
152         return (value == null) ? def : value.intValue();
153     }
154
155     public int getIntValue(String JavaDoc name)
156     throws ConfigurationException {
157         Integer JavaDoc value = (Integer JavaDoc)internalMap.get(name);
158
159         if (value == null) {
160             throw new ConfigurationException("No " + name + " property defined");
161         }
162
163         return value.intValue();
164     }
165
166     public boolean getBoolValue(String JavaDoc name, boolean def)
167     throws ConfigurationException {
168         Boolean JavaDoc value = (Boolean JavaDoc)internalMap.get(name);
169
170         return (value == null) ? def : value.booleanValue();
171     }
172
173     public boolean getBoolValue(String JavaDoc name)
174     throws ConfigurationException {
175         Boolean JavaDoc value = (Boolean JavaDoc)internalMap.get(name);
176
177         if (value == null) {
178             throw new ConfigurationException("No " + name + " property defined");
179         }
180
181         return value.booleanValue();
182     }
183
184     public Object JavaDoc getValue(String JavaDoc name, Object JavaDoc def)
185     throws ConfigurationException {
186         Object JavaDoc value = internalMap.get(name);
187
188         return (value == null) ? def : value;
189     }
190
191     public Object JavaDoc getValue(String JavaDoc name)
192     throws ConfigurationException {
193         Object JavaDoc value = internalMap.get(name);
194
195         if (value == null) {
196             throw new ConfigurationException("No " + name + " property defined");
197         }
198
199         return value;
200     }
201
202     /**
203      * It is supposed that the value of the parameter is a class name. It returns
204      * an instance of that class, created with <i>newInstance()</i>.
205      *
206      * @param name the name of the parameter
207      *
208      * @return an instance of the class specified by the parameter
209      *
210      * @throws ConfigurationException in case of errors
211      */

212     public Object JavaDoc getClassInstance(String JavaDoc name)
213     throws ConfigurationException {
214        String JavaDoc value = (String JavaDoc)internalMap.get(name);
215
216        if (value == null) {
217            throw new ConfigurationException("No " + name + " property defined");
218        }
219
220        try {
221            return Class.forName(value).newInstance();
222        } catch (Exception JavaDoc e) {
223            throw new ConfigurationException( "Error in creating an instance of "
224                                            + value
225                                            , e );
226        }
227     }
228
229     /**
230      * It is supposed that the value of the parameter is a bean name. It returns
231      * that bean, created with <i>BeanFactory.getBeanInstance()</i>.
232      *
233      * @param name the name of the parameter
234      *
235      * @return an instance of the class specified by the parameter
236      *
237      * @throws ConfigurationException in case of errors
238      */

239     public Object JavaDoc getBeanInstance(String JavaDoc name)
240     throws ConfigurationException {
241        String JavaDoc value = (String JavaDoc)internalMap.get(name);
242
243        if (value == null) {
244            throw new ConfigurationException("No " + name + " property defined");
245        }
246
247        try {
248            return BeanFactory.getBeanInstance(classLoader, value);
249        } catch (Exception JavaDoc e) {
250            throw new ConfigurationException( "Error in creating an instance of "
251                                            + value
252                                            , e );
253        }
254     }
255
256     /**
257      * Create and return an array of bean as specified by a list of bean names.
258      *
259      * @param name the name of the paramenter
260      * @param separator the separator character
261      *
262      */

263     public Object JavaDoc[] getBeanArray(String JavaDoc name, char separator)
264     throws ConfigurationException {
265         String JavaDoc[] values = getStringArray(name, separator);
266
267         Object JavaDoc[] ret = new Object JavaDoc[values.length];
268
269         int i = 0;
270         try {
271             for (; i<ret.length; ++i) {
272                 ret[i] = BeanFactory.getBeanInstance(classLoader, values[i]);
273             }
274         } catch (Exception JavaDoc e) {
275             throw new ConfigurationException ( "Error in creating the bean "
276                                              + values[i]
277                                              , e );
278         }
279
280         return ret;
281     }
282
283     /**
284      * Loads and instantiate a bean by its config name. In this case the bean
285      * is not looked up in the internal configuration map, but is created
286      * directly by the means of the BeanFactory. This call is a shortcut for:
287      * <code>
288      *
289      * ClassLoader cl = Configuration.getConfiguration().getClassLoader();
290      * Object o = BeanFactory.getBeanInstance(cl, beanName);
291      *
292      * </code>
293      *
294      * @param beanName the complete beanName
295      *
296      * @return the bean instance
297      *
298      * @throws ConfigurationException in case of instantiation error
299      */

300     public Object JavaDoc getBeanInstanceByName(String JavaDoc beanName) {
301         try {
302              return BeanFactory.getBeanInstance(classLoader, beanName);
303         } catch (Exception JavaDoc e) {
304              throw new ConfigurationException( "Error in creating an instance of "
305                                              + beanName
306                                              , e );
307         }
308     }
309
310     public synchronized void load(InputStream JavaDoc is) throws IOException JavaDoc {
311         Properties JavaDoc properties = new Properties JavaDoc();
312
313         properties.load(is);
314
315         internalMap.putAll(properties);
316     }
317
318     public synchronized void load(String JavaDoc uri)
319     throws IOException JavaDoc {
320         load(new URL JavaDoc(uri).openStream());
321     }
322
323     public String JavaDoc toString() {
324         StringBuffer JavaDoc sb = new StringBuffer JavaDoc();
325
326         sb.append(getClass().getName()).append(" - ");
327         sb.append(internalMap.toString()).append(" - ");
328         sb.append("classLoader: " + classLoader);
329
330         return sb.toString();
331     }
332
333 }
Popular Tags