Clover coverage report - QedeqKernelSe Coverage Report
Coverage timestamp: Do Mai 10 2007 03:16:40 CEST
file stats: LOC: 377   Methods: 19
NCLOC: 175   Classes: 1
 
 Source file Conditionals Statements Methods TOTAL
ConfigAccess.java 68,2% 72,2% 68,4% 70,8%
coverage coverage
 1    /* $Id: ConfigAccess.java,v 1.3 2007/05/10 00:37:53 m31 Exp $
 2    *
 3    * This file is part of the project "Hilbert II" - http://www.qedeq.org
 4    *
 5    * Copyright 2000-2007, 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    import org.qedeq.kernel.trace.Trace;
 33   
 34   
 35    /**
 36    * This class reads entries from property files. This class should not
 37    * be used outside this package.
 38    *
 39    * @version $Revision: 1.3 $
 40    * @author Michael Meyling
 41    */
 42    final class ConfigAccess {
 43   
 44    /** Config directory. */
 45    private final File configDirectory;
 46   
 47    /** Name of config file. */
 48    private final String configFileName;
 49   
 50    /** Collector for properties. */
 51    private Properties properties = new Properties();
 52   
 53    /** Config file description. */
 54    private final String description;
 55   
 56    /** Trace config write access? */
 57    private boolean noTrace = true;
 58   
 59    /**
 60    * Get access for a config file.
 61    *
 62    * @param configDirectory Directory location of config file.
 63    * @param configFileName Name of config file.
 64    * @param description Config file description
 65    * @throws IOException Config file couldn't be loaded.
 66    */
 67  3 public ConfigAccess(final File configDirectory, final String configFileName,
 68    final String description) throws IOException {
 69  3 this.configDirectory = configDirectory;
 70  3 this.configFileName = configFileName;
 71  3 this.description = description;
 72  3 try {
 73  3 load(new FileInputStream(new File(configDirectory, configFileName)));
 74    } catch (IOException e) {
 75  1 Trace.info(this, "ConfigAccess(File, String, String)",
 76    "no config file found, using default values");
 77    }
 78    /*
 79    try {
 80    load("/" + configFileName, ConfigAccess.class);
 81    } catch (MissingResourceException e) {
 82    throw new IOException(e.toString());
 83    }
 84    */
 85  3 setString("startDirectory", configDirectory.getCanonicalPath());
 86  3 setString("configFileLocation", new File(configDirectory, configFileName)
 87    .getCanonicalPath());
 88  3 noTrace = false;
 89    }
 90   
 91    /**
 92    * Get config directory.
 93    *
 94    * @return Start directory.
 95    */
 96  3 public final File getConfigDirectory() {
 97  3 return configDirectory;
 98    }
 99   
 100    /**
 101    * Get config file name.
 102    *
 103    * @return Config file name.
 104    */
 105  3 public final String getFileName() {
 106  3 return configFileName;
 107    }
 108   
 109    /**
 110    * Get config file.
 111    *
 112    * @return Config file.
 113    */
 114  3 public final File getConfigFile() {
 115  3 return new File(getConfigDirectory(), getFileName());
 116    }
 117   
 118    /**
 119    * Get description for config file.
 120    *
 121    * @return Config file description.
 122    */
 123  3 public final String getConfigDescription() {
 124  3 return description;
 125    }
 126   
 127    /**
 128    * Get properties.
 129    *
 130    * @return properties.
 131    */
 132  57 private final Properties getProperties() {
 133  57 return properties;
 134    }
 135   
 136    /**
 137    * Load properties from stream. The properties are
 138    * added to the previous ones.
 139    *
 140    * @param inStream load from this stream
 141    * @throws IOException loading failed
 142    */
 143  2 private final void load(final InputStream inStream) throws IOException {
 144  2 getProperties().load(inStream);
 145    }
 146   
 147    /**
 148    * Load properties from resource. The properties are added
 149    * to the previous ones.
 150    *
 151    * @param resource read from this resource.
 152    * @param resourceRequestor class, which requests the resource.
 153    * The resource is loaded relatively to this class.
 154    * @throws IOException Reading failed or resource was not found.
 155    */
 156  0 private final void load(final String resource, final Class resourceRequestor)
 157    throws IOException {
 158  0 final InputStream stream
 159    = resourceRequestor.getResourceAsStream(resource);
 160  0 if (stream == null) {
 161  0 throw new IOException("ressource was not found: " + resource);
 162    }
 163  0 try {
 164  0 getProperties().load(stream);
 165    } catch (IllegalArgumentException e) {
 166  0 throw new IOException(e.toString());
 167    }
 168    }
 169   
 170    /**
 171    * Store properties in config.
 172    *
 173    * @throws IOException Saving failed.
 174    */
 175  3 public final void store() throws IOException {
 176  3 store(getConfigFile(), getConfigDescription());
 177    }
 178   
 179    /**
 180    * Store properties in resource.
 181    *
 182    * @param resource save it here (must be different from <code>null</code>)
 183    * @param header config description
 184    * @throws IOException store failed
 185    */
 186  3 private final void store(final File resource, final String header)
 187    throws IOException {
 188   
 189  3 OutputStream stream = null;
 190  3 try {
 191  3 stream = new FileOutputStream(resource);
 192  3 getProperties().store(stream, header);
 193    } finally {
 194  3 if (stream != null) {
 195  3 try {
 196  3 stream.close();
 197    } catch (Exception e) {
 198    // ignore
 199    }
 200    }
 201    }
 202    }
 203   
 204    /**
 205    * Return String property.
 206    *
 207    * @param name Get this property.
 208    * @return String for looked property. <code>null</code>, if property is missing.
 209    */
 210  0 public final String getString(final String name) {
 211  0 return getProperties().getProperty(name);
 212    }
 213   
 214    /**
 215    * Return String property.
 216    *
 217    * @param name Look for this String property
 218    * @param defaultValue Return this value if property doesn't exist.
 219    * @return Value of property. Equal to default value if parameter doesn't exist.
 220    */
 221  3 public final String getString(final String name, final String defaultValue) {
 222  3 final String method = "getString(String, String)";
 223  3 try {
 224  3 Trace.begin(this, method);
 225  3 Trace.param(this, method, "name", name);
 226  3 Trace.param(this, method, "defaultValue", defaultValue);
 227  3 final String value = getProperties().getProperty(name);
 228  3 if (value == null) {
 229  1 setString(name, defaultValue);
 230  1 return defaultValue;
 231    } else {
 232  2 return value;
 233    }
 234    } finally {
 235  3 Trace.end(this, method);
 236    }
 237    }
 238   
 239   
 240    /**
 241    * Set String property.
 242    *
 243    * @param name Set this property.
 244    * @param value Set property to this value.
 245    */
 246  16 public final void setString(final String name, final String value) {
 247  16 final String method = "setString(String, String)";
 248  16 try {
 249  16 if (!noTrace) {
 250  10 Trace.begin(this, method);
 251  10 Trace.param(this, method, "name", name);
 252  10 Trace.param(this, method, "value", value);
 253    }
 254  16 getProperties().setProperty(name, value);
 255    } finally {
 256  16 if (!noTrace) {
 257  10 Trace.end(this, method);
 258    }
 259    }
 260    }
 261   
 262    /**
 263    * Get list of String properties with certain prefix.
 264    * Example:
 265    * <ul>
 266    * <li>module1=bluebird</li>
 267    * <li>module2=tiger</li>
 268    * <li>module3=tulip</li>
 269    * </ul>
 270    * The sequence of resulting properties is sorted by their keys.
 271    *
 272    * @param namePrefix Prefix of seeked property name.
 273    * @return List of key sorted string properties (maybe empty).
 274    */
 275  6 public final String[] getStringProperties(final String namePrefix) {
 276  6 final List list = new ArrayList();
 277  6 final Enumeration keys = getProperties().keys();
 278  6 final List keyList = Collections.list(keys);
 279  6 Collections.sort(keyList);
 280  6 for (int i = 0; i < keyList.size(); i++) {
 281  34 final String key = (String) keyList.get(i);
 282  34 if (key.startsWith(namePrefix)) {
 283  16 list.add(getProperties().get(key));
 284    }
 285    }
 286  6 return (String []) list.toArray(new String[] {});
 287    }
 288   
 289    /**
 290    * Set int property.
 291    *
 292    * @param name Set this property.
 293    * @param value Set property to this value.
 294    */
 295  0 public final void setInteger(final String name, final int value) {
 296  0 setString(name, "" + value);
 297    }
 298   
 299   
 300    /**
 301    * Get int property.
 302    *
 303    * @param name look for this property
 304    * @return property
 305    * @throws IllegalArgumentException Property is no valid int value
 306    * @throws NullPointerException Property doesn't exist
 307    */
 308  0 public final int getInteger(final String name) {
 309  0 final String intPropAsString = getProperties().getProperty(name);
 310  0 if (intPropAsString != null) {
 311  0 try {
 312  0 return Integer.parseInt(intPropAsString);
 313    } catch (NumberFormatException ex) {
 314  0 throw new IllegalArgumentException(
 315    "int property " + intPropAsString + " has invalid format");
 316    }
 317    } else {
 318  0 throw new NullPointerException("property \"" + name + "\" not found");
 319    }
 320    }
 321   
 322    /**
 323    * Return int property.
 324    *
 325    * @param name Look for this integer property.
 326    * @param defaultValue Return this value if property doesn't exist.
 327    * @return int value of property. Equal to default value if parameter doesn't exist.
 328    * @throws IllegalArgumentException Property is no valid int value.
 329    */
 330  0 public final int getInteger(final String name, final int defaultValue) {
 331  0 final String intPropAsString = getProperties().getProperty(name);
 332  0 if (intPropAsString != null) {
 333  0 try {
 334  0 return Integer.parseInt(intPropAsString);
 335    } catch (NumberFormatException ex) {
 336  0 throw new IllegalArgumentException(
 337    "Integer-Property " + intPropAsString + " has invalid format");
 338    }
 339    } else {
 340  0 setInteger(name, defaultValue);
 341  0 return defaultValue;
 342    }
 343    }
 344   
 345    /**
 346    * Remove property.
 347    *
 348    * @param name Property to delete.
 349    */
 350  0 public final void removeProperty(final String name) {
 351  0 getProperties().remove(name);
 352    }
 353   
 354    /**
 355    * Remove properties with certain prefix.
 356    *
 357    * Example:
 358    * <ul>
 359    * <li>module1=bluebird</li>
 360    * <li>module2=tiger</li>
 361    * <li>module3=tulip</li>
 362    * </ul>
 363    * Calling with value <code>module</code> deletes all.
 364    *
 365    * @param namePrefix Prefix of seeked property name.
 366    */
 367  3 public final void removeProperties(final String namePrefix) {
 368  3 final Enumeration keys = getProperties().keys();
 369  3 while (keys.hasMoreElements()) {
 370  17 final String key = (String) keys.nextElement();
 371  17 if (key.startsWith(namePrefix)) {
 372  8 getProperties().remove(key);
 373    }
 374    }
 375    }
 376   
 377    }