Clover Coverage Report
Coverage timestamp: Sat Sep 18 2010 04:09:52 UTC
../../../../img/srcFileCovDistChart5.png 77% of files have more coverage
58   286   28   3.87
16   131   0.48   15
15     1.87  
1    
 
  ConfigAccess       Line # 40 58 28 50.6% 0.505618
 
  (6)
 
1    /* This file is part of the project "Hilbert II" - http://www.qedeq.org
2    *
3    * Copyright 2000-2010, Michael Meyling <mime@qedeq.org>.
4    *
5    * "Hilbert II" is free software; you can redistribute
6    * it and/or modify it under the terms of the GNU General Public
7    * License as published by the Free Software Foundation; either
8    * version 2 of the License, or (at your option) any later version.
9    *
10    * This program is distributed in the hope that it will be useful,
11    * but WITHOUT ANY WARRANTY; without even the implied warranty of
12    * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13    * GNU General Public License for more details.
14    */
15   
16    package org.qedeq.kernel.config;
17   
18    import java.io.File;
19    import java.io.FileInputStream;
20    import java.io.FileOutputStream;
21    import java.io.IOException;
22    import java.io.InputStream;
23    import java.io.OutputStream;
24    import java.util.ArrayList;
25    import java.util.Collections;
26    import java.util.Enumeration;
27    import java.util.List;
28    import java.util.Properties;
29   
30    import org.qedeq.base.io.IoUtility;
31   
32   
33    /**
34    * This class reads entries from property files. This class should not
35    * be used outside this package.
36    *
37    * @version $Revision: 1.6 $
38    * @author Michael Meyling
39    */
 
40    final class ConfigAccess {
41   
42    /** Config file. */
43    private final File configFile;
44   
45    /** Collector for properties. */
46    private Properties properties = new Properties();
47   
48    /** Config file description. */
49    private final String description;
50   
51    /**
52    * Get access for a config file.
53    *
54    * @param configFile Config file.
55    * @param description Config file description
56    * @throws IOException Config file couldn't be loaded.
57    */
 
58  54 toggle public ConfigAccess(final File configFile, final String description) throws IOException {
59  54 this.configFile = configFile;
60  54 this.description = description;
61  54 FileInputStream stream = null;
62  54 try {
63  54 stream = new FileInputStream(configFile);
64  54 load(stream);
65    } catch (IOException e) {
66  0 System.out.println("no config file found, using default values");
67    } finally {
68  54 IoUtility.close(stream);
69    }
70  54 setString("configFileLocation", configFile.getCanonicalPath());
71    }
72   
73    /**
74    * Get config file.
75    *
76    * @return Config file.
77    */
 
78  54 toggle public final File getConfigFile() {
79  54 return configFile;
80    }
81   
82    /**
83    * Get description for config file.
84    *
85    * @return Config file description.
86    */
 
87  54 toggle public final String getConfigDescription() {
88  54 return description;
89    }
90   
91    /**
92    * Get properties.
93    *
94    * @return properties.
95    */
 
96  927 toggle private final Properties getProperties() {
97  927 return properties;
98    }
99   
100    /**
101    * Load properties from stream. The properties are
102    * added to the previous ones.
103    *
104    * @param inStream load from this stream
105    * @throws IOException loading failed
106    */
 
107  54 toggle private final void load(final InputStream inStream) throws IOException {
108  54 getProperties().load(inStream);
109    }
110   
111    /**
112    * Store properties in config file.
113    *
114    * @throws IOException Saving failed.
115    */
 
116  54 toggle public final void store() throws IOException {
117  54 OutputStream out = null;
118  54 try {
119  54 out = new FileOutputStream(getConfigFile());
120  54 getProperties().store(out, getConfigDescription());
121    } finally {
122  54 if (out != null) {
123  54 try {
124  54 out.close();
125    } catch (IOException e) {
126  0 throw e;
127    } catch (Exception e) {
128  0 throw new IOException(e.toString());
129    }
130    }
131    }
132    }
133   
134    /**
135    * Return String property.
136    *
137    * @param name Get this property.
138    * @return String for looked property. <code>null</code>, if property is missing.
139    */
 
140  296 toggle public final String getString(final String name) {
141  296 return getProperties().getProperty(name);
142    }
143   
144    /**
145    * Return String property.
146    *
147    * @param name Look for this String property.
148    * @param defaultValue Return this value if property doesn't exist.
149    * @return Value of property. Equal to default value if parameter doesn't exist.
150    */
 
151  54 toggle public final String getString(final String name, final String defaultValue) {
152  54 final String value = getProperties().getProperty(name);
153  54 if (value == null) {
154  0 setString(name, defaultValue);
155  0 return defaultValue;
156    } else {
157  54 return value;
158    }
159    }
160   
161    /**
162    * Set String property.
163    *
164    * @param name Set this property.
165    * @param value Set property to this value.
166    */
 
167  262 toggle public final void setString(final String name, final String value) {
168  262 getProperties().setProperty(name, value);
169    }
170   
171    /**
172    * Get list of String properties with certain prefix.
173    * Example:
174    * <ul>
175    * <li>module1=bluebird</li>
176    * <li>module2=tiger</li>
177    * <li>module3=tulip</li>
178    * </ul>
179    * The sequence of resulting properties is sorted by their keys.
180    *
181    * @param namePrefix Prefix of seeked property name.
182    * @return List of key sorted string properties (maybe empty).
183    */
 
184  0 toggle public final String[] getStringProperties(final String namePrefix) {
185  0 final List list = new ArrayList();
186  0 final Enumeration keys = getProperties().keys();
187  0 final List keyList = Collections.list(keys);
188  0 Collections.sort(keyList);
189  0 for (int i = 0; i < keyList.size(); i++) {
190  0 final String key = (String) keyList.get(i);
191  0 if (key.startsWith(namePrefix)) {
192  0 list.add(getProperties().get(key));
193    }
194    }
195  0 return (String []) list.toArray(new String[list.size()]);
196    }
197   
198    /**
199    * Set int property.
200    *
201    * @param name Set this property.
202    * @param value Set property to this value.
203    */
 
204  0 toggle public final void setInteger(final String name, final int value) {
205  0 setString(name, "" + value);
206    }
207   
208   
209    /**
210    * Get int property.
211    *
212    * @param name look for this property
213    * @return property
214    * @throws IllegalArgumentException Property is no valid int value
215    * @throws NullPointerException Property doesn't exist
216    */
 
217  0 toggle public final int getInteger(final String name) {
218  0 final String intPropAsString = getProperties().getProperty(name);
219  0 if (intPropAsString != null) {
220  0 try {
221  0 return Integer.parseInt(intPropAsString);
222    } catch (NumberFormatException ex) {
223  0 throw new IllegalArgumentException(
224    "int property " + intPropAsString + " has invalid format");
225    }
226    } else {
227  0 throw new NullPointerException("property \"" + name + "\" not found");
228    }
229    }
230   
231    /**
232    * Return int property.
233    *
234    * @param name Look for this integer property.
235    * @param defaultValue Return this value if property doesn't exist.
236    * @return int value of property. Equal to default value if parameter doesn't exist.
237    * @throws IllegalArgumentException Property is no valid int value.
238    */
 
239  0 toggle public final int getInteger(final String name, final int defaultValue) {
240  0 final String intPropAsString = getProperties().getProperty(name);
241  0 if (intPropAsString != null) {
242  0 try {
243  0 return Integer.parseInt(intPropAsString);
244    } catch (NumberFormatException ex) {
245  0 throw new IllegalArgumentException(
246    "Integer-Property " + intPropAsString + " has invalid format");
247    }
248    } else {
249  0 setInteger(name, defaultValue);
250  0 return defaultValue;
251    }
252    }
253   
254    /**
255    * Remove property.
256    *
257    * @param name Property to delete.
258    */
 
259  0 toggle public final void removeProperty(final String name) {
260  0 getProperties().remove(name);
261    }
262   
263    /**
264    * Remove properties with certain prefix.
265    *
266    * Example:
267    * <ul>
268    * <li>module1=bluebird</li>
269    * <li>module2=tiger</li>
270    * <li>module3=tulip</li>
271    * </ul>
272    * Calling with value <code>module</code> deletes all.
273    *
274    * @param namePrefix Prefix of seeked property name.
275    */
 
276  54 toggle public final void removeProperties(final String namePrefix) {
277  54 final Enumeration keys = getProperties().keys();
278  315 while (keys.hasMoreElements()) {
279  261 final String key = (String) keys.nextElement();
280  261 if (key.startsWith(namePrefix)) {
281  153 getProperties().remove(key);
282    }
283    }
284    }
285   
286    }