KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > opensymphony > webwork > config > Configuration


1 /*
2  * Copyright (c) 2002-2003 by OpenSymphony
3  * All rights reserved.
4  */

5 package com.opensymphony.webwork.config;
6
7 import com.opensymphony.xwork.ObjectFactory;
8 import org.apache.commons.logging.Log;
9 import org.apache.commons.logging.LogFactory;
10
11 import java.util.Iterator JavaDoc;
12 import java.util.Locale JavaDoc;
13 import java.util.StringTokenizer JavaDoc;
14
15
16 /**
17  * Handles all WebWork2 config properties. Implementation of this class is pluggable (the
18  * default implementation is {@link DefaultConfiguration}). This gives developers to ability to customize how
19  * WebWork2 properties are set and retrieved. As an example, a developer may wish to check a separate property
20  * store before delegating to the WebWork one. <p>
21  * <p/>
22  * Key methods: <ul>
23  * <p/>
24  * <li>{@link #getLocale()}</li>
25  * <li>{@link #getString(String)}</li>
26  * <li>{@link #set(String, Object)}</li>
27  * <li>{@link #list()}</li></ul>
28  * <p/>
29  * Key methods for subclassers: <ul>
30  * <p/>
31  * <li>{@link #getImpl(String)}</li>
32  * <li>{@link #setImpl(String, Object)}</li>
33  * <li>{@link #listImpl()}</li>
34  * <li>{@link #isSetImpl(String)}</li></ul>
35  *
36  * @author Rickard Öberg
37  * @author Jason Carreira
38  * @author Bill Lynch (docs)
39  */

40 public class Configuration {
41     //~ Static fields/initializers /////////////////////////////////////////////
42

43     static Configuration configurationImpl;
44     static Configuration defaultImpl;
45     static Locale JavaDoc locale; // Cached locale
46
private static final Log LOG = LogFactory.getLog(Configuration.class);
47
48     //~ Methods ////////////////////////////////////////////////////////////////
49

50     /**
51      * Sets the current configuration implementation. Can only be called once.
52      *
53      * @param config a Configuration implementation
54      * @throws IllegalStateException if an error occurs when setting the configuration implementation.
55      */

56     public static void setConfiguration(Configuration config) throws IllegalStateException JavaDoc {
57         configurationImpl = config;
58         locale = null; // Reset cached locale
59
}
60
61     /**
62      * Gets the current configuration implementation.
63      *
64      * @return the current configuration implementation.
65      */

66     public static Configuration getConfiguration() {
67         return (configurationImpl == null) ? getDefaultConfiguration() : configurationImpl;
68     }
69
70     /**
71      * Returns the WebWork2 locale. Keys off the property <tt>webwork.locale</tt> which should be set
72      * as the Java {@link java.util.Locale#toString() toString()} representation of a Locale object (i.e.,
73      * "en", "de_DE", "_GB", "en_US_WIN", "de__POSIX", "fr_MAC", etc). <p>
74      * <p/>
75      * If no locale is specified then the default VM locale is used ({@link java.util.Locale#getDefault()}).
76      *
77      * @return the WebWork2 locale if specified or the VM default locale.
78      */

79     public static Locale JavaDoc getLocale() {
80         if (locale == null) {
81             try {
82                 StringTokenizer JavaDoc localeTokens = new StringTokenizer JavaDoc(getString("webwork.locale"), "_");
83                 String JavaDoc lang = null;
84                 String JavaDoc country = null;
85
86                 if (localeTokens.hasMoreTokens()) {
87                     lang = localeTokens.nextToken();
88                 }
89
90                 if (localeTokens.hasMoreTokens()) {
91                     country = localeTokens.nextToken();
92                 }
93
94                 locale = new Locale JavaDoc(lang, country);
95             } catch (Throwable JavaDoc t) {
96                 // Default
97
LOG.warn("Setting locale to the default locale");
98                 locale = Locale.getDefault();
99             }
100         }
101
102         return locale;
103     }
104
105     /**
106      * Determines whether or not a value has been set. Useful for testing for the existance of parameter without
107      * throwing an IllegalArgumentException.
108      *
109      * @param name the name of the property to test.
110      * @return <tt>true</tt> if the property exists and has a value, <tt>false</tt> otherwise.
111      */

112     public static boolean isSet(String JavaDoc name) {
113         return getConfiguration().isSetImpl(name);
114     }
115
116     /**
117      * Returns a property as a String. This will throw an <tt>IllegalArgumentException</tt> if an error occurs
118      * while retrieveing the property or if the property doesn't exist.
119      *
120      * @param name the name of the property to get.
121      * @return the property as a String
122      * @throws IllegalArgumentException if an error occurs retrieveing the property or the property does not exist.
123      */

124     public static String JavaDoc getString(String JavaDoc name) throws IllegalArgumentException JavaDoc {
125         String JavaDoc val = get(name).toString();
126
127         return val;
128     }
129
130     /**
131      * Returns a property as an Object. This will throw an <tt>IllegalArgumentException</tt> if an error occurs
132      * while retrieveing the property or if the property doesn't exist.
133      *
134      * @param name the name of the property to get.
135      * @return the property as an Object.
136      * @throws IllegalArgumentException if an error occurs retrieveing the property or the property does not exist.
137      */

138     public static Object JavaDoc get(String JavaDoc name) throws IllegalArgumentException JavaDoc {
139         Object JavaDoc val = getConfiguration().getImpl(name);
140
141         return val;
142     }
143
144     /**
145      * Returns an Iterator of all properties names.
146      *
147      * @return an Iterator of all properties names.
148      */

149     public static Iterator JavaDoc list() {
150         return getConfiguration().listImpl();
151     }
152
153     /**
154      * Implementation of the {@link #isSet(String)} method.
155      *
156      * @see #isSet(String)
157      */

158     public boolean isSetImpl(String JavaDoc name) {
159         // this is dumb.. maybe it should just throw an unsupported op like the rest of the *Impl
160
// methods in this class.
161
return false;
162     }
163
164     /**
165      * Sets a property. Throws an exception if an error occurs when setting the property or if the
166      * Configuration implementation does not support setting properties.
167      *
168      * @param name the name of the property to set.
169      * @param value the property to set.
170      * @throws IllegalArgumentException if an error occurs when setting the property.
171      * @throws UnsupportedOperationException if the config implementation does not support setting properties.
172      */

173     public static void set(String JavaDoc name, Object JavaDoc value) throws IllegalArgumentException JavaDoc, UnsupportedOperationException JavaDoc {
174         getConfiguration().setImpl(name, value);
175     }
176
177     /**
178      * Implementation of the {@link #set(String, Object)} method.
179      *
180      * @see #set(String, Object)
181      */

182     public void setImpl(String JavaDoc name, Object JavaDoc value) throws IllegalArgumentException JavaDoc, UnsupportedOperationException JavaDoc {
183         throw new UnsupportedOperationException JavaDoc("This configuration does not support updating a setting");
184     }
185
186     /**
187      * Implementation of the {@link #get(String)} method.
188      *
189      * @see #get(String)
190      */

191     public Object JavaDoc getImpl(String JavaDoc aName) throws IllegalArgumentException JavaDoc {
192         return null;
193     }
194
195     /**
196      * Implementation of the {@link #list()} method.
197      *
198      * @see #list()
199      */

200     public Iterator JavaDoc listImpl() {
201         throw new UnsupportedOperationException JavaDoc("This configuration does not support listing the settings");
202     }
203
204     private static Configuration getDefaultConfiguration() {
205         if (defaultImpl == null) {
206             // Create bootstrap implementation
207
defaultImpl = new DefaultConfiguration();
208
209             // Create default implementation
210
try {
211                 String JavaDoc className = getString("webwork.configuration");
212
213                 if (!className.equals(defaultImpl.getClass().getName())) {
214                     try {
215                         defaultImpl = (Configuration) ObjectFactory.getObjectFactory().buildBean(Thread.currentThread().getContextClassLoader().loadClass(className));
216                     } catch (Exception JavaDoc e) {
217                         LOG.error("Could not instantiate configuration", e);
218                     }
219                 }
220             } catch (IllegalArgumentException JavaDoc ex) {
221                 // ignore
222
}
223         }
224
225         return defaultImpl;
226     }
227
228     public static void reset() {
229         defaultImpl = null;
230         configurationImpl = null;
231     }
232 }
233
Popular Tags