/[xulu]/branches/1.8-gt2-2.6/src/appl/ext/XuluConfig.java
ViewVC logotype

Annotation of /branches/1.8-gt2-2.6/src/appl/ext/XuluConfig.java

Parent Directory Parent Directory | Revision Log Revision Log


Revision 60 - (hide annotations)
Sun Oct 4 16:54:52 2009 UTC (15 years, 2 months ago) by alfonx
File size: 14472 byte(s)
* organized imports
1 mojays 2 package appl.ext;
2    
3     import java.io.File;
4     import java.io.FileInputStream;
5     import java.io.FileNotFoundException;
6     import java.io.FileOutputStream;
7     import java.io.IOException;
8     import java.util.Enumeration;
9     import java.util.Properties;
10     import java.util.Set;
11     import java.util.TreeSet;
12    
13     /**
14     * Simple Property store, which is loaded at each Xulu start and saved at each
15     * Xulu exit. Every class or plugin can store and retrieve configuration
16     * information easy through a static method. To keep the property file readable
17     * and to avoid conflicts with other plugins please prefix the key-name with a
18     * unique name, which identifies your class, followed by a dot and the property.
19     * This is important for correct display in a User-GUI (which could be
20     * implemented). <br>
21     * <br>
22     * Keyformat: <code>domain.property</code> <br>
23     * Example key: <code>DiscoveryService.className</code> <br>
24     * <br>
25     * There could be no double entries.But you can save multiple values to one key
26     * using the {@link #setMultiProperty(String, String[]) setMultiProperty}
27     * method. <br>
28     * <b>These values are internally separated with ';', so see that you don't use
29     * ';' in your values of {@link #setMultiProperty(String, String[]) Multi
30     * Properties}</b> <br>
31     * <br>
32     * First the settings of the file "DefaultProperties" are loaded. After that the
33     * values are possibly overwritten by the loading of the file "XuluProperties"
34     * in which is intended to store a actual user configuration.
35     * <b>"DefaultPropoerties" should never be modified by the user.</b> The
36     * programmer should store all default values there. <br>
37     * <br>
38     * The user may change values through the {@link ConfigurationEditorGUI}.
39     *
40     * @see ConfigurationEditorGUI
41     * @see ConfigurationEditorEngine
42     * @author Dominik Appl
43     */
44     public class XuluConfig {
45    
46     private final String delimiter = ";";
47    
48     // File for default values
49     File defaultFile = new File("DefaultProperties");
50    
51     // File in which the props are stored
52     File file = new File("XuluProperties");
53    
54     Properties propStore = null;
55    
56     private static XuluConfig instance = null;
57    
58     private XuluConfig() {
59     this(null);
60     }
61    
62     private XuluConfig(File rootDir) {
63     if ( rootDir != null && rootDir.isDirectory() ) {
64     defaultFile = new File(rootDir, defaultFile.getName());
65     file = new File(rootDir, file.getName());
66     }
67     // try to read defaults
68     Properties defaults = new Properties();
69     try {
70     defaults.load(new FileInputStream(defaultFile));
71     } catch (Exception e) {
72     System.out
73     .println("Warning: defaults could not be loaded from file '"
74     + defaultFile + "'");
75     System.out.println(e.getMessage());
76     }
77     propStore = new Properties(defaults);
78     if (file.exists())
79     load();
80     else
81     store();
82     }
83    
84     public static XuluConfig getXuluConfig() {
85     return getXuluConfig(null);
86     }
87    
88     public static XuluConfig getXuluConfig(File rootDir) {
89     if (instance == null)
90     instance = new XuluConfig(rootDir);
91     return instance;
92     }
93    
94     /**
95     * Saves the configuration to the file system
96     */
97     public void store() {
98     try {
99     propStore.store(new FileOutputStream(file),
100     "Stored properties of the Xulu modelling platform");
101     } catch (FileNotFoundException e) {
102     System.err.println("Could not write PropertyStore!!!");
103     e.printStackTrace();
104     } catch (IOException e) {
105     System.err.println("Could not write PropertyStore!!!");
106     e.printStackTrace();
107     }
108    
109     }
110    
111     /**
112     * Sets the Property file. Notice that this has only an effect if you call
113     * load afterwards.
114     *
115     * @param file
116     */
117     public void setPropertyFile(File file) {
118     this.file = file;
119     }
120    
121     /**
122     * loads the Properties from the Propertyfile. No loaded values are deleted -
123     * only existing ones are overwritten;
124     */
125     public void load() {
126     try {
127     propStore.load(new FileInputStream(file));
128     } catch (FileNotFoundException e) {
129     System.err.println("Property file with name '" + file
130     + "' not found");
131     } catch (IOException e) {
132     // TODO Auto-generated catch block
133     e.printStackTrace();
134     }
135     }
136    
137     /**
138     * inserts the property value with the specified key into the
139     * {@link XuluConfig}
140     *
141     * @param key
142     * the key
143     * @param value
144     * the value associated with the key
145     */
146     public void setProperty(String key, String value) {
147     if (!key.contains("."))
148     System.out
149     .println("WARNING for key "
150     + key
151     + ": Propertykeys in XuluConfig should have at least one '.' ! ");
152     propStore.setProperty(key, value);
153     }
154    
155     /***************************************************************************
156     * @return the property associated with the provided key. If not found the
157     * defaults are searched. If still not found, null is returned.
158     * Gives out a warning to console if the key was not found
159     **************************************************************************/
160     public String getProperty(String key) {
161     String value = propStore.getProperty(key);
162     if (value == null)
163     System.err.println("Warning! Value for property '" + key
164     + "' was not found in property file!");
165     return value;
166     }
167    
168     /***************************************************************************
169     * Same as {@link #getProperty(String)}, but you can disable the warning
170     * message
171     *
172     * @param giveWarning
173     * if true, a warning is given to the console if the specified
174     * entry was not found
175     * @return the property associated with the provided key. If not found the
176     * defaults are searched. If still not found, null is returned.
177     * Gives out a warning to console if the key was not found
178     **************************************************************************/
179     public String getProperty(String key, boolean giveWarning) {
180     String value = propStore.getProperty(key);
181     if (value == null && giveWarning)
182     System.err.println("Warning! Value for property '" + key
183     + "' was not found in property file!");
184     return value;
185     }
186    
187     /***************************************************************************
188     *
189     * @return the property associated with the provided key as integer value.
190     * If not found the defaults are searched. If not found or the value
191     * is in a wrong format 0 is returned and a warning is given to the
192     * console
193     **************************************************************************/
194     public int getIntProperty(String key) {
195     String value = propStore.getProperty(key);
196     if (value == null) {
197     System.err.println("Error! Value for property '" + key
198     + "' was not found in property file!");
199     return 0;
200     }
201     int returnvalue = 0;
202     try {
203     returnvalue = Integer.valueOf(value);
204     } catch (java.lang.NumberFormatException e) {
205     System.err
206     .println("Error! Value for property '"
207     + key
208     + "' was not in the right format. Should be a Integer value! - Returning 0");
209     }
210     return returnvalue;
211     }
212    
213     /***************************************************************************
214     * @return the property associated with the provided key as double value. If
215     * not found the defaults are searched. If not found or the value is
216     * in a wrong format 0 is returned and a warning is given to the
217     * console
218     **************************************************************************/
219     public double getDoubleProperty(String key) {
220     String value = propStore.getProperty(key);
221     if (value == null) {
222     System.err.println("Error! Value for property '" + key
223     + "' was not found in property file!");
224     return 0;
225     }
226     double returnvalue = 0;
227     try {
228     returnvalue = Double.valueOf(value);
229     } catch (java.lang.NumberFormatException e) {
230     System.err
231     .println("Error! Value for property '"
232     + key
233     + "' was not in the right format. Should be a Integer value! - Returning 0");
234     }
235     return returnvalue;
236     }
237    
238     /**
239     * inserts the property value with the specified key into the
240     * {@link XuluConfig}
241     *
242     * @param key
243     * the key
244     * @param value
245     * the value associated with the key
246     */
247     public void setBooleanProperty(String key, boolean value) {
248     this.setProperty(key, String.valueOf(value));
249     }
250    
251     /**
252     * inserts the property value with the specified key into the
253     * {@link XuluConfig}
254     *
255     * @param key
256     * the key
257     * @param value
258     * the value associated with the key
259     */
260     public void setIntProperty(String key, int value) {
261     this.setProperty(key, String.valueOf(value));
262     }
263    
264     /***************************************************************************
265     * @return the property associated with the provided key as boolean value.
266     * If not found the defaults are searched. If still not found or
267     * null then false is returned. Gives out a warning to console if
268     * the key was not found
269     **************************************************************************/
270     public boolean getBooleanProperty(String key) {
271     String value = propStore.getProperty(key);
272     if (value == null) {
273     System.err.println("Error! Value for property '" + key
274     + "' was not found in property file!");
275     return false;
276     }
277     if(value.equals("1"))
278     return true;
279     boolean bool = false;
280     try {
281     bool = Boolean.valueOf(value);
282     } catch (Exception e) {
283     System.err.println("Error! Value for property '" + key
284     + "' was not a valid boolean value");
285     }
286     return bool;
287     }
288    
289     /**
290     * A multiproperty stores multiple values for one key. These values are
291     * internaly seperated with spaces, so see that you dont use spaces in your
292     * values.
293     *
294     * @param key
295     * the key
296     * @param values
297     * array of the values which should be associated with the key
298     */
299     public void setMultiProperty(String key, String values[]) {
300     if (!key.contains("."))
301     System.out
302     .println("WARNING for key "
303     + key
304     + ": Propertykeys in XuluConfig should have at least one '.' ! ");
305     String value = "";
306     if (values != null)
307     for (String string : values) {
308     value += (string + delimiter);
309     }
310     propStore.setProperty(key, value);
311     }
312    
313     /***************************************************************************
314     * A multiproperty stores multiple values for one key. These values are
315     * internaly seperated with ';', so do not use ';' in your values. if the
316     * key was not found it gives out a warning to console and a empty array is
317     * retured.
318     *
319     * @return the multiProperty associated with the provided key, which is in
320     * this case just a String array
321     **************************************************************************/
322    
323     public String[] getMultiProperty(String key) {
324     String value = propStore.getProperty(key);
325     if (value != null)
326     return value.split(delimiter);
327     System.err.println("Error! Value for property '" + key
328     + "' was not found in property file!");
329     return new String[0];
330     }
331    
332     /**
333     * removes the Property from the list
334     *
335     * @param key
336     */
337     public void removeProperty(String key) {
338     propStore.remove(key);
339     }
340    
341     /**
342     * removes all keys starting with the value given. USE WITH CAUTION! If you
343     * give a String "a" all entrys starting with "a" will be removed from the
344     * registry
345     *
346     * @param key
347     */
348     public void removeAll(String key) {
349     if (key == null)
350     return;
351    
352     Enumeration keys = propStore.propertyNames();
353     while (keys.hasMoreElements()) {
354     String propKey = (String) keys.nextElement();
355     if (propKey == null)
356     continue;
357     if (propKey.startsWith(key))
358     propStore.remove(propKey);
359     }
360     }
361    
362     /**
363     * Gets all prefixes. A Prefix is the string of all characters up to the
364     * first dot. A Suffix is the string of all characters following this dot.
365     *
366     * @return the prefixes
367     *
368     */
369     public Set<String> getPrefixes() {
370     TreeSet set = new TreeSet();
371     Enumeration keys = propStore.propertyNames();
372     while (keys.hasMoreElements()) {
373     String propKey = (String) keys.nextElement();
374     String[] strings = propKey.split("\\.");
375     set.add(strings[0]);
376     }
377     return set;
378     }
379    
380     /**
381     * Gets all suffixes for one prefix. A Prefix is the string of all
382     * characters up to the first dot. A Suffix is the string of all characters
383     * following this dot. The Strings are ordered alphabeticly
384     *
385     * @param prefix
386     * the prefix.
387     */
388     public Set<String> getSuffixesOfPrefix(String prefix) {
389     TreeSet set = new TreeSet();
390     Enumeration keys = propStore.propertyNames();
391     while (keys.hasMoreElements()) {
392     String propKey = (String) keys.nextElement();
393     String[] strings = propKey.split("\\.");
394     if (strings.length > 1 && strings[0].equals(prefix))
395     set.add(propKey.substring(strings[0].length() + 1));
396     }
397     return set;
398     }
399    
400     /**
401     * Gets all entries starting with the given {@link String}
402     *
403     * @param startingString
404     * the prefixstring
405     * @return all keys starting the given prefix
406     */
407     public Set<String> getKeysStartingWith(String startingString) {
408     TreeSet set = new TreeSet();
409     Enumeration keys = propStore.propertyNames();
410     while (keys.hasMoreElements()) {
411     String propKey = (String) keys.nextElement();
412     if (propKey.startsWith(startingString))
413     set.add(propKey);
414     }
415     return set;
416     }
417    
418     /**
419     * Just for testing.
420     *
421     * @deprecated
422     */
423     public static void main(String[] args) {
424     XuluConfig config = XuluConfig.getXuluConfig();
425     config.setProperty("atest2", "test3");
426     config.setProperty("test4.testproperty", "test");
427     String[] multiTest = { "value1", "value2", "value3" };
428     config.setMultiProperty("multiproperty.1", multiTest);
429     String[] multioutput = config.getMultiProperty("multiproperty.1");
430     for (String string : multioutput) {
431     System.out.println("Multivalue: " + string);
432     }
433     config.store();
434     config.setPropertyFile(new File("XuluTestfile"));
435     config.load();
436     String[] multiTest2 = { "value1", "value2", "value3" };
437     config.setMultiProperty("multiproperty.2", multiTest);
438     config.removeAll("multiproperty");
439     config.removeProperty("atest2");
440     config.store();
441     }
442     }

[email protected]
ViewVC Help
Powered by ViewVC 1.1.26