Docjar: A Java Source and Docuemnt Enginecom.*    java.*    javax.*    org.*    all    new    plug-in

Quick Search    Search Deep

Source code: org/argouml/configuration/Configuration.java


1   // Copyright (c) 1996-2001 The Regents of the University of California. All
2   // Rights Reserved. Permission to use, copy, modify, and distribute this
3   // software and its documentation without fee, and without a written
4   // agreement is hereby granted, provided that the above copyright notice
5   // and this paragraph appear in all copies.  This software program and
6   // documentation are copyrighted by The Regents of the University of
7   // California. The software program and documentation are supplied "AS
8   // IS", without any accompanying services from The Regents. The Regents
9   // does not warrant that the operation of the program will be
10  // uninterrupted or error-free. The end-user understands that the program
11  // was developed for research purposes and is advised not to rely
12  // exclusively on the program for any reason.  IN NO EVENT SHALL THE
13  // UNIVERSITY OF CALIFORNIA BE LIABLE TO ANY PARTY FOR DIRECT, INDIRECT,
14  // SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES, INCLUDING LOST PROFITS,
15  // ARISING OUT OF THE USE OF THIS SOFTWARE AND ITS DOCUMENTATION, EVEN IF
16  // THE UNIVERSITY OF CALIFORNIA HAS BEEN ADVISED OF THE POSSIBILITY OF
17  // SUCH DAMAGE. THE UNIVERSITY OF CALIFORNIA SPECIFICALLY DISCLAIMS ANY
18  // WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
19  // MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. THE SOFTWARE
20  // PROVIDED HEREUNDER IS ON AN "AS IS" BASIS, AND THE UNIVERSITY OF
21  // CALIFORNIA HAS NO OBLIGATIONS TO PROVIDE MAINTENANCE, SUPPORT,
22  // UPDATES, ENHANCEMENTS, OR MODIFICATIONS.
23  
24  //
25  //  This code is originally from the open source UML editor argouml.
26  //  Information on argouml can be found at: http://argouml.tigris.org/
27  //
28  //  The original author is Thierry Lach  
29  //
30  //  Adopted by Matthieu Cormier on Fri June 27 2003.
31  
32  package org.argouml.configuration;
33  
34  import java.io.*;
35  import java.beans.*;
36  import java.util.*;
37  import java.net.*;
38  
39  
40  /**
41   *   This class provides the core user configuration implementation
42   *   logic.  All fancy handling and registry access occurs
43   *   behind the scenes.
44   *
45   *   @stereotype singleton
46   */
47  public class Configuration {
48  
49    ////////////////////////////////////////////////////////////////
50    // Instance variables
51  
52    /** Property to indicate configuration load from file
53     */
54    public final static String FILE_LOADED = "configuration.load.file";
55  
56    /** Property to indicate configuration load from url
57     */
58    public final static String URL_LOADED = "configuration.load.url";
59  
60    /** Property to indicate configuration save to file
61     */
62    public final static String FILE_SAVED = "configuration.save.file";
63  
64    /** Property to indicate configuration save to url
65     */
66    public final static String URL_SAVED = "configuration.save.url";
67  
68    /** The only occurance of the configuration handler.
69     */
70    private static ConfigurationHandler _config = null;
71  
72    private static Configuration SINGLETON = new Configuration();
73  
74    /** Private constructor so it cannot be instantiated.
75     */
76    private Configuration() {
77        _config = ConfigurationFactory.getInstance().getConfigurationHandler();
78    }
79  
80    /** Returns the instance of the configuration singleton.
81     *
82     * @return the configuration handler
83     */
84    public static ConfigurationHandler getConfigurationHandler()
85    {
86        return _config;
87    }
88  
89    /** Returns the configuration factory instance.
90     *
91     *  This is equivalent to ConfigurationFactory.getInstance() but
92     *  using Configuration.getFactory() is shorter to type and
93     *  allows us not to have to deal with ConfigurationFactory at
94     *  all if we don't need to modify or configure it.
95     *
96     * @return the configuration factory
97     */
98  
99    public static final ConfigurationFactory getFactory() {
100       return ConfigurationFactory.getInstance();
101   }
102 
103   /** Load the configuration from the default location.
104    *
105    *  The configuration will be automatically loaded from the default
106    *  location the first time a value is queried or modified, if it
107    *  had not been previously loaded.  Only the first load request
108    *  will be honored, so if the configuration is to be loaded from
109    *  a non-default location, load(name) must be used prior to any
110    *  other call.  The configuration can be loaded only one time.
111    *
112    *  Implementations must ignore load requests once a load is 
113    *  already successful, and must return false for each of those
114    *  ignored requests.
115    *
116    * @return true if the load is successful, otherwise false
117    */
118   public static final boolean load() {
119       return _config.loadDefault();
120   }
121 
122   /** Load the configuration from a specified file
123    *
124    * @param file the File to load
125    *
126    * @return true if the load is successful, otherwise false
127    */
128   public static final boolean load(File file) {
129       return _config.load(file);
130   }
131 
132   /** Load the configuration from a specified url
133    *
134    * @param url the URL to load
135    *
136    * @return true if the load is successful, otherwise false
137    */
138   public static final boolean load(URL url) {
139       return _config.load(url);
140   }
141 
142   /** Save the configuration to the default location.
143    *
144    *  Implementations do not have to handle this method.
145    *  If the method is not allowed or it fails, the implementation
146    *  must return false.
147    *
148    * @return true if the save is successful, otherwise false
149    */
150   public static final boolean save() {
151       return Configuration.save(false);
152   }
153 
154   /** Save the configuration to the default location.
155    *
156    *  Implementations do not have to handle this method.
157    *  If the method is not allowed or it fails, the implementation
158    *  must return false.
159    *
160    * @return true if the save is successful, otherwise false
161    */
162   public static final boolean save(boolean force) {
163       return _config.saveDefault(force);
164   }
165 
166   /** Returns the string value of a configuration property.
167    *
168    * @param key the key to retrieve the value of
169    *
170    * @return the string value of the parameter if it exists, otherwise 
171    * a zero length string
172    */
173   public static String getString(ConfigurationKey key) {
174       return getString(key, "");
175   }
176 
177   /** Returns the string value of a configuration property.
178    *
179    * @param key the key to retrieve the value of
180    * @param defaultValue the value to return if the key does not exist
181    *
182    * @return the string value of the parameter if it exists, otherwise the
183    *   default value
184    */
185   public static final String getString(ConfigurationKey key, String defaultValue) {
186       return _config.getString(key, defaultValue);
187   }
188 
189   /** Returns the numeric value of a configuration property.
190    *
191    * @param key the key to retrieve the value of
192    *
193    * @return the string value of the parameter if it exists, otherwise zero
194    */
195   public static final int getInteger(ConfigurationKey key) {
196       return getInteger(key, 0);
197   }
198 
199   /** Returns the numeric value of a configuration property.
200    *
201    * @param key the key to retrieve the value of
202    *
203    * @return the string value of the parameter if it exists,
204    *         otherwise the default value
205    */
206   public static final double getDouble(ConfigurationKey key,
207                                        double defaultValue) {
208       return _config.getDouble(key, defaultValue);
209   }
210 
211   /** Returns the numeric value of a configuration property.
212    *
213    * @param key the key to retrieve the value of
214    *
215    * @return the string value of the parameter if it exists, otherwise zero
216    */
217   public static final double getDouble(ConfigurationKey key) {
218       return getDouble(key, 0);
219   }
220 
221   /** Returns the numeric value of a configuration property.
222    *
223    * @param key the key to retrieve the value of
224    * @param defaultValue the value to return if the key does not exist
225    *
226    * @return the numeric value of the parameter if it exists, otherwise
227    *  the default value
228    */
229   public static final int getInteger(ConfigurationKey key, int defaultValue) {
230       return _config.getInteger(key, defaultValue);
231   }
232 
233   /** Returns the boolean value of a configuration property.
234    *
235    * @param key the key to retrieve the value of
236    *
237    * @return the boolean value of the parameter if it exists, otherwise false
238    */
239   public static final boolean getBoolean(ConfigurationKey key) {
240       return getBoolean(key, false);
241   }
242 
243   /** Returns the boolean value of a configuration property.
244    *
245    * @param key the key to retrieve the value of
246    * @param defaultValue the value to return if the key does not exist
247    *
248    * @return the boolean value of the parameter if it exists, otherwise
249    *  the default value
250    */
251   public static final boolean getBoolean(ConfigurationKey key, boolean defaultValue) {
252       return _config.getBoolean(key, defaultValue);
253   }
254 
255   /** Sets the string value of a configuration property.
256    *
257    * @param key the key to set
258    * @param newValue the value to set the key to.
259    */
260   public static final void setString(ConfigurationKey key, String newValue) {
261       _config.setString(key, newValue);
262   }
263 
264   /** Sets the numeric value of a configuration property.
265    *
266    * @param key the key to set
267    * @param newValue the value to set the key to.
268    */
269   public static final void setInteger(ConfigurationKey key, int newValue) {
270       _config.setInteger(key, newValue);
271   }
272 
273   /** Sets the numeric value of a configuration property.
274    *
275    * @param key the key to set
276    * @param newValue the value to set the key to.
277    */
278   public static final void setDouble(ConfigurationKey key, double newValue) {
279       _config.setDouble(key, newValue);
280   }
281   /** Sets the boolean value of a configuration property.
282    *
283    * @param key the key to set
284    * @param newValue the value to set the key to.
285    */
286   public static final void setBoolean(ConfigurationKey key, boolean newValue) {
287       _config.setBoolean(key, newValue);
288   }
289 
290   /** Adds a property change listener.
291    *
292    * @param pcl The property change listener to add
293    */
294   public static final void addListener(PropertyChangeListener pcl) {
295       _config.addListener(pcl);
296   }
297 
298   /** Removes a property change listener.
299    *
300    * @param pcl The property change listener to remove
301    */
302   public static final void removeListener(PropertyChangeListener pcl) {
303       _config.removeListener(pcl);
304   }
305 
306   /** Adds a property change listener.Static for simplicity of use.
307    *
308    * @param key The key to listen for changes of
309    * @param pcl The property change listener to add
310    */
311   public static final void addListener(ConfigurationKey key, PropertyChangeListener pcl) {
312       _config.addListener(key, pcl);
313   }
314 
315   /** Removes a property change listener.
316    *
317    * @param key The key to listen for changes of
318    * @param pcl The property change listener to remove
319    */
320   public static final void removeListener(ConfigurationKey key, PropertyChangeListener pcl) {
321       _config.removeListener(key, pcl);
322   }
323 
324   /** Create a single component configuration key.
325    */
326   public static ConfigurationKey makeKey(String k1) {
327       return new ConfigurationKeyImpl(k1);
328   }
329 
330   /** Create a sub-component of an existing configuration key.
331    */
332   public static ConfigurationKey makeKey(ConfigurationKey ck, String k1) {
333       return new ConfigurationKeyImpl(ck, k1);
334   }
335 
336   /** Create a two-component configuration key.
337    */
338   public static ConfigurationKey makeKey(String k1, String k2) {
339       return new ConfigurationKeyImpl(k1, k2);
340   }
341 
342   /** Create a three-component configuration key.
343    */
344   public static ConfigurationKey makeKey(String k1, String k2, String k3) {
345       return new ConfigurationKeyImpl(k1, k2, k3);
346   }
347 
348   /** Create a four-component configuration key.
349    */
350   public static ConfigurationKey makeKey(String k1, String k2, String k3, String k4) {
351       return new ConfigurationKeyImpl(k1, k2, k3, k4);
352   }
353 
354   /** Create a five-component configuration key.
355    */
356   public static ConfigurationKey makeKey(String k1, String k2, String k3, String k4, String k5) {
357       return new ConfigurationKeyImpl(k1, k2, k3, k4, k5);
358   }
359 
360 }
361