Classes in this File | Line Coverage | Branch Coverage | Complexity | ||||
Configuration |
|
| 1.0;1 |
1 | /* $Id: Configuration.java 17819 2010-01-12 18:40:41Z linus $ | |
2 | ***************************************************************************** | |
3 | * Copyright (c) 2009 Contributors - see below | |
4 | * All rights reserved. This program and the accompanying materials | |
5 | * are made available under the terms of the Eclipse Public License v1.0 | |
6 | * which accompanies this distribution, and is available at | |
7 | * http://www.eclipse.org/legal/epl-v10.html | |
8 | * | |
9 | * Contributors: | |
10 | * linus | |
11 | ***************************************************************************** | |
12 | * | |
13 | * Some portions of this file was previously release using the BSD License: | |
14 | */ | |
15 | ||
16 | // Copyright (c) 1996-2007 The Regents of the University of California. All | |
17 | // Rights Reserved. Permission to use, copy, modify, and distribute this | |
18 | // software and its documentation without fee, and without a written | |
19 | // agreement is hereby granted, provided that the above copyright notice | |
20 | // and this paragraph appear in all copies. This software program and | |
21 | // documentation are copyrighted by The Regents of the University of | |
22 | // California. The software program and documentation are supplied "AS | |
23 | // IS", without any accompanying services from The Regents. The Regents | |
24 | // does not warrant that the operation of the program will be | |
25 | // uninterrupted or error-free. The end-user understands that the program | |
26 | // was developed for research purposes and is advised not to rely | |
27 | // exclusively on the program for any reason. IN NO EVENT SHALL THE | |
28 | // UNIVERSITY OF CALIFORNIA BE LIABLE TO ANY PARTY FOR DIRECT, INDIRECT, | |
29 | // SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES, INCLUDING LOST PROFITS, | |
30 | // ARISING OUT OF THE USE OF THIS SOFTWARE AND ITS DOCUMENTATION, EVEN IF | |
31 | // THE UNIVERSITY OF CALIFORNIA HAS BEEN ADVISED OF THE POSSIBILITY OF | |
32 | // SUCH DAMAGE. THE UNIVERSITY OF CALIFORNIA SPECIFICALLY DISCLAIMS ANY | |
33 | // WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF | |
34 | // MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. THE SOFTWARE | |
35 | // PROVIDED HEREUNDER IS ON AN "AS IS" BASIS, AND THE UNIVERSITY OF | |
36 | // CALIFORNIA HAS NO OBLIGATIONS TO PROVIDE MAINTENANCE, SUPPORT, | |
37 | // UPDATES, ENHANCEMENTS, OR MODIFICATIONS. | |
38 | ||
39 | package org.argouml.configuration; | |
40 | ||
41 | import java.beans.PropertyChangeListener; | |
42 | import java.io.File; | |
43 | import java.net.URL; | |
44 | ||
45 | ||
46 | /** | |
47 | * This class provides the core user configuration implementation | |
48 | * logic. All fancy handling and registry access occurs | |
49 | * behind the scenes. | |
50 | * | |
51 | * @stereotype utility | |
52 | * @author Thierry Lach | |
53 | * @since 0.9.4 | |
54 | */ | |
55 | public final class Configuration { | |
56 | ||
57 | //////////////////////////////////////////////////////////////// | |
58 | // Instance variables | |
59 | ||
60 | /** | |
61 | * Property to indicate configuration load from file. | |
62 | */ | |
63 | public static final String FILE_LOADED = "configuration.load.file"; | |
64 | ||
65 | /** | |
66 | * Property to indicate configuration load from url. | |
67 | */ | |
68 | public static final String URL_LOADED = "configuration.load.url"; | |
69 | ||
70 | /** | |
71 | * Property to indicate configuration save to file. | |
72 | */ | |
73 | public static final String FILE_SAVED = "configuration.save.file"; | |
74 | ||
75 | /** | |
76 | * Property to indicate configuration save to url. | |
77 | */ | |
78 | public static final String URL_SAVED = "configuration.save.url"; | |
79 | ||
80 | /** | |
81 | * The only occurance of the configuration handler. | |
82 | */ | |
83 | 900 | private static ConfigurationHandler config = |
84 | getFactory().getConfigurationHandler(); | |
85 | ||
86 | /** | |
87 | * Private constructor so it cannot be instantiated. | |
88 | */ | |
89 | 0 | private Configuration() { |
90 | // Don't allow instantiation | |
91 | 0 | } |
92 | ||
93 | /** | |
94 | * Returns the instance of the configuration singleton. | |
95 | * | |
96 | * @return the configuration handler | |
97 | */ | |
98 | public static ConfigurationHandler getConfigurationHandler() { | |
99 | 0 | return config; |
100 | } | |
101 | ||
102 | /** | |
103 | * Returns the configuration factory instance.<p> | |
104 | * | |
105 | * This is equivalent to ConfigurationFactory.getInstance() but | |
106 | * using Configuration.getFactory() is shorter to type and | |
107 | * allows us not to have to deal with ConfigurationFactory at | |
108 | * all if we don't need to modify or configure it. | |
109 | * | |
110 | * @return the configuration factory | |
111 | */ | |
112 | public static IConfigurationFactory getFactory() { | |
113 | 900 | return ConfigurationFactory.getInstance(); |
114 | } | |
115 | ||
116 | /** | |
117 | * Load the configuration from the default location. | |
118 | * | |
119 | * The configuration will be automatically loaded from the default | |
120 | * location the first time a value is queried or modified, if it | |
121 | * had not been previously loaded. Only the first load request | |
122 | * will be honored, so if the configuration is to be loaded from | |
123 | * a non-default location, load(name) must be used prior to any | |
124 | * other call. The configuration can be loaded only one time. | |
125 | * | |
126 | * Implementations must ignore load requests once a load is | |
127 | * already successful, and must return false for each of those | |
128 | * ignored requests. | |
129 | * | |
130 | * @return true if the load is successful, otherwise false | |
131 | */ | |
132 | public static boolean load() { | |
133 | 900 | return config.loadDefault(); |
134 | } | |
135 | ||
136 | /** | |
137 | * Load the configuration from a specified file. | |
138 | * | |
139 | * @param file the File to load | |
140 | * | |
141 | * @return true if the load is successful, otherwise false | |
142 | */ | |
143 | public static boolean load(File file) { | |
144 | 0 | return config.load(file); |
145 | } | |
146 | ||
147 | /** | |
148 | * Load the configuration from a specified url. | |
149 | * | |
150 | * @param url the URL to load | |
151 | * | |
152 | * @return true if the load is successful, otherwise false | |
153 | */ | |
154 | public static boolean load(URL url) { | |
155 | 0 | return config.load(url); |
156 | } | |
157 | ||
158 | /** | |
159 | * Save the configuration to the default location. | |
160 | * | |
161 | * Implementations do not have to handle this method. | |
162 | * If the method is not allowed or it fails, the implementation | |
163 | * must return false. | |
164 | * | |
165 | * @return true if the save is successful, otherwise false | |
166 | */ | |
167 | public static boolean save() { | |
168 | 0 | return Configuration.save(false); |
169 | } | |
170 | ||
171 | /** | |
172 | * Save the configuration to the default location. | |
173 | * | |
174 | * Implementations do not have to handle this method. | |
175 | * If the method is not allowed or it fails, the implementation | |
176 | * must return false. | |
177 | * | |
178 | * @param force the file to save even if it would not normally | |
179 | * be saved. | |
180 | * | |
181 | * @return true if the save is successful, otherwise false | |
182 | */ | |
183 | public static boolean save(boolean force) { | |
184 | 0 | return config.saveDefault(force); |
185 | } | |
186 | ||
187 | /** | |
188 | * Returns the string value of a configuration property. | |
189 | * | |
190 | * @param key the key to retrieve the value of | |
191 | * | |
192 | * @return the string value of the parameter if it exists, otherwise | |
193 | * a zero length string | |
194 | */ | |
195 | public static String getString(ConfigurationKey key) { | |
196 | 19563 | return getString(key, ""); |
197 | } | |
198 | ||
199 | /** | |
200 | * Returns the string value of a configuration property. | |
201 | * | |
202 | * @param key the key to retrieve the value of | |
203 | * @param defaultValue the value to return if the key does not exist | |
204 | * | |
205 | * @return the string value of the parameter if it exists, otherwise the | |
206 | * default value | |
207 | */ | |
208 | public static String getString(ConfigurationKey key, | |
209 | String defaultValue) { | |
210 | 92707 | return config.getString(key, defaultValue); |
211 | } | |
212 | ||
213 | /** | |
214 | * Returns the numeric value of a configuration property. | |
215 | * | |
216 | * @param key the key to retrieve the value of | |
217 | * | |
218 | * @return the string value of the parameter if it exists, otherwise zero | |
219 | */ | |
220 | public static int getInteger(ConfigurationKey key) { | |
221 | 993 | return getInteger(key, 0); |
222 | } | |
223 | ||
224 | /** | |
225 | * Returns the numeric value of a configuration property. | |
226 | * | |
227 | * @param key the key to retrieve the value of | |
228 | * @param defaultValue if the key is not found | |
229 | * | |
230 | * @return the string value of the parameter if it exists, | |
231 | * otherwise the default value | |
232 | */ | |
233 | public static double getDouble(ConfigurationKey key, | |
234 | double defaultValue) { | |
235 | 0 | return config.getDouble(key, defaultValue); |
236 | } | |
237 | ||
238 | /** | |
239 | * Returns the numeric value of a configuration property. | |
240 | * | |
241 | * @param key the key to retrieve the value of | |
242 | * | |
243 | * @return the string value of the parameter if it exists, otherwise zero | |
244 | */ | |
245 | public static double getDouble(ConfigurationKey key) { | |
246 | 0 | return getDouble(key, 0); |
247 | } | |
248 | ||
249 | /** | |
250 | * Returns the numeric value of a configuration property. | |
251 | * | |
252 | * @param key the key to retrieve the value of | |
253 | * @param defaultValue the value to return if the key does not exist | |
254 | * | |
255 | * @return the numeric value of the parameter if it exists, otherwise | |
256 | * the default value | |
257 | */ | |
258 | public static int getInteger(ConfigurationKey key, int defaultValue) { | |
259 | 11094 | return config.getInteger(key, defaultValue); |
260 | } | |
261 | ||
262 | /** | |
263 | * Returns the boolean value of a configuration property. | |
264 | * | |
265 | * @param key the key to retrieve the value of | |
266 | * | |
267 | * @return the boolean value of the parameter if it exists, otherwise false | |
268 | */ | |
269 | public static boolean getBoolean(ConfigurationKey key) { | |
270 | 5826 | return getBoolean(key, false); |
271 | } | |
272 | ||
273 | /** | |
274 | * Returns the boolean value of a configuration property. | |
275 | * | |
276 | * @param key the key to retrieve the value of | |
277 | * @param defaultValue the value to return if the key does not exist | |
278 | * | |
279 | * @return the boolean value of the parameter if it exists, otherwise | |
280 | * the default value | |
281 | */ | |
282 | public static boolean getBoolean(ConfigurationKey key, | |
283 | boolean defaultValue) { | |
284 | 148284 | return config.getBoolean(key, defaultValue); |
285 | } | |
286 | ||
287 | /** | |
288 | * Sets the string value of a configuration property. | |
289 | * | |
290 | * @param key the key to set | |
291 | * @param newValue the value to set the key to. | |
292 | */ | |
293 | public static void setString(ConfigurationKey key, String newValue) { | |
294 | 2731 | config.setString(key, newValue); |
295 | 2731 | } |
296 | ||
297 | /** | |
298 | * Sets the numeric value of a configuration property. | |
299 | * | |
300 | * @param key the key to set | |
301 | * @param newValue the value to set the key to. | |
302 | */ | |
303 | public static void setInteger(ConfigurationKey key, int newValue) { | |
304 | 1800 | config.setInteger(key, newValue); |
305 | 1800 | } |
306 | ||
307 | /** | |
308 | * Sets the numeric value of a configuration property. | |
309 | * | |
310 | * @param key the key to set | |
311 | * @param newValue the value to set the key to. | |
312 | */ | |
313 | public static void setDouble(ConfigurationKey key, double newValue) { | |
314 | 0 | config.setDouble(key, newValue); |
315 | 0 | } |
316 | ||
317 | /** | |
318 | * Sets the boolean value of a configuration property. | |
319 | * | |
320 | * @param key the key to set | |
321 | * @param newValue the value to set the key to. | |
322 | */ | |
323 | public static void setBoolean(ConfigurationKey key, | |
324 | boolean newValue) { | |
325 | 197650 | config.setBoolean(key, newValue); |
326 | 197650 | } |
327 | ||
328 | /** | |
329 | * Adds a property change listener. | |
330 | * | |
331 | * @param pcl The property change listener to add | |
332 | */ | |
333 | public static void addListener(PropertyChangeListener pcl) { | |
334 | 0 | config.addListener(pcl); |
335 | 0 | } |
336 | ||
337 | /** | |
338 | * Removes a property change listener. | |
339 | * | |
340 | * @param pcl The property change listener to remove | |
341 | */ | |
342 | public static void removeListener(PropertyChangeListener pcl) { | |
343 | 0 | config.removeListener(pcl); |
344 | 0 | } |
345 | ||
346 | /** | |
347 | * Adds a property change listener.Static for simplicity of use. | |
348 | * | |
349 | * @param key The key to listen for changes of | |
350 | * @param pcl The property change listener to add | |
351 | */ | |
352 | public static void addListener(ConfigurationKey key, | |
353 | PropertyChangeListener pcl) { | |
354 | 13500 | config.addListener(key, pcl); |
355 | 13500 | } |
356 | ||
357 | /** | |
358 | * Removes a property change listener. | |
359 | * | |
360 | * @param key The key to listen for changes of | |
361 | * @param pcl The property change listener to remove | |
362 | */ | |
363 | public static void removeListener(ConfigurationKey key, | |
364 | PropertyChangeListener pcl) { | |
365 | 0 | config.removeListener(key, pcl); |
366 | 0 | } |
367 | ||
368 | /** | |
369 | * @param key The key to remove. | |
370 | */ | |
371 | public static void removeKey(ConfigurationKey key) { | |
372 | 0 | config.remove(key.getKey()); |
373 | 0 | } |
374 | ||
375 | /** | |
376 | * Create a single component configuration key. | |
377 | * | |
378 | * @param k1 key component 1. | |
379 | * @return the new {@link ConfigurationKey}. | |
380 | */ | |
381 | public static ConfigurationKey makeKey(String k1) { | |
382 | 70200 | return new ConfigurationKeyImpl(k1); |
383 | } | |
384 | ||
385 | /** | |
386 | * Create a sub-component of an existing configuration key. | |
387 | * | |
388 | * @param ck existing key to extend. | |
389 | * @param k1 key component 1. | |
390 | * @return the new {@link ConfigurationKey}. | |
391 | */ | |
392 | public static ConfigurationKey makeKey(ConfigurationKey ck, String k1) { | |
393 | 0 | return new ConfigurationKeyImpl(ck, k1); |
394 | } | |
395 | ||
396 | /** | |
397 | * Create a two-component configuration key. | |
398 | * | |
399 | * @param k1 key component 1. | |
400 | * @param k2 key component 2. | |
401 | * @return the new {@link ConfigurationKey}. | |
402 | */ | |
403 | public static ConfigurationKey makeKey(String k1, String k2) { | |
404 | 27032 | return new ConfigurationKeyImpl(k1, k2); |
405 | } | |
406 | ||
407 | /** | |
408 | * Create a three-component configuration key. | |
409 | * | |
410 | * @param k1 key component 1. | |
411 | * @param k2 key component 2. | |
412 | * @param k3 key component 3. | |
413 | * @return the new {@link ConfigurationKey}. | |
414 | */ | |
415 | public static ConfigurationKey makeKey(String k1, String k2, String k3) { | |
416 | 330535 | return new ConfigurationKeyImpl(k1, k2, k3); |
417 | } | |
418 | ||
419 | /** | |
420 | * Create a four-component configuration key. | |
421 | * | |
422 | * @param k1 key component 1. | |
423 | * @param k2 key component 2. | |
424 | * @param k3 key component 3. | |
425 | * @param k4 key component 4. | |
426 | * @return the new {@link ConfigurationKey}. | |
427 | */ | |
428 | public static ConfigurationKey makeKey(String k1, String k2, | |
429 | String k3, String k4) { | |
430 | 2700 | return new ConfigurationKeyImpl(k1, k2, k3, k4); |
431 | } | |
432 | ||
433 | /** | |
434 | * Create a five-component configuration key. | |
435 | * | |
436 | * @param k1 key component 1. | |
437 | * @param k2 key component 2. | |
438 | * @param k3 key component 3. | |
439 | * @param k4 key component 4. | |
440 | * @param k5 key component 5. | |
441 | * @return the new {@link ConfigurationKey}. | |
442 | */ | |
443 | public static ConfigurationKey makeKey(String k1, String k2, | |
444 | String k3, String k4, | |
445 | String k5) { | |
446 | 0 | return new ConfigurationKeyImpl(k1, k2, k3, k4, k5); |
447 | } | |
448 | } | |
449 |