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