001: /*
002: * Cobertura - http://cobertura.sourceforge.net/
003: *
004: * Copyright (C) 2007 Joakim Erdfelt
005: *
006: * Cobertura is free software; you can redistribute it and/or modify
007: * it under the terms of the GNU General Public License as published
008: * by the Free Software Foundation; either version 2 of the License,
009: * or (at your option) any later version.
010: *
011: * Cobertura is distributed in the hope that it will be useful, but
012: * WITHOUT ANY WARRANTY; without even the implied warranty of
013: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
014: * General Public License for more details.
015: *
016: * You should have received a copy of the GNU General Public License
017: * along with Cobertura; if not, write to the Free Software
018: * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
019: * USA
020: */
021:
022: package net.sourceforge.cobertura.util;
023:
024: import java.io.IOException;
025: import java.io.InputStream;
026: import java.net.URL;
027: import java.util.Properties;
028:
029: /**
030: * A Utility Class to load the configuration.
031: *
032: * Checks for values using the following hierarchy.
033: * 1) System Property matching key.
034: * 2) cobertura.properties Resource Property matching key.
035: * 3) hardcoded default value
036: *
037: * @author Joakim Erdfelt
038: */
039: public class ConfigurationUtil {
040: public static final String RESOURCE = "/cobertura.properties";
041:
042: private Properties props;
043:
044: public ConfigurationUtil() {
045: init();
046: }
047:
048: public void init() {
049: props = new Properties();
050:
051: URL url = this .getClass().getResource(RESOURCE);
052: if (url == null) {
053: DEBUG("Unable to find configuration resource in classpath of name "
054: + RESOURCE + ", using empty configuration.");
055: return;
056: }
057:
058: InputStream is = null;
059: try {
060: is = url.openStream();
061: props.load(is);
062: } catch (IOException e) {
063: System.err
064: .println("ERROR: Unable to load configuration resource "
065: + RESOURCE + " - " + e.getMessage());
066: } finally {
067: IOUtil.closeInputStream(is);
068: }
069: }
070:
071: public String getProperty(String key, String defvalue) {
072: String value = System.getProperty(key);
073: if (value != null) {
074: DEBUG("Using system property value [" + value
075: + "] for key [" + key + "]");
076: return value;
077: }
078:
079: value = props.getProperty(key);
080: if (value != null) {
081: DEBUG("Using cobertura.properties value [" + value
082: + "] for key [" + key + "]");
083: return value;
084: }
085:
086: DEBUG("Using default value [" + defvalue + "] for key [" + key
087: + "]");
088: return defvalue;
089: }
090:
091: public String getDatafile() {
092: return getProperty("net.sourceforge.cobertura.datafile",
093: "cobertura.ser");
094: }
095:
096: /**
097: * Poor mans debugging.
098: * Intentionally didn't use log4j, as we dont want to introduce that dependency on instrumented files.
099: */
100: private void DEBUG(String msg) {
101: if (false) {
102: System.out.println("[Cobertura:ConfigurationUtil] " + msg);
103: }
104: }
105: }
|