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    {
041        public static final String RESOURCE = "/cobertura.properties";
042    
043        private Properties props;
044    
045        public ConfigurationUtil()
046        {
047            init();
048        }
049    
050        public void init()
051        {
052            props = new Properties();
053    
054            URL url = this.getClass().getResource( RESOURCE );
055            if ( url == null )
056            {
057                DEBUG( "Unable to find configuration resource in classpath of name " + RESOURCE + ", using empty configuration." );
058                return;
059            }
060    
061            InputStream is = null;
062            try
063            {
064                is = url.openStream();
065                props.load( is );
066            }
067            catch ( IOException e )
068            {
069                System.err.println( "ERROR: Unable to load configuration resource " + RESOURCE + " - " + e.getMessage() );
070            }
071            finally
072            {
073                IOUtil.closeInputStream( is );
074            }
075        }
076    
077        public String getProperty( String key, String defvalue )
078        {
079            String value = System.getProperty( key );
080            if ( value != null )
081            {
082                DEBUG("Using system property value [" + value + "] for key [" + key + "]");
083                return value;
084            }
085    
086            value = props.getProperty( key );
087            if ( value != null )
088            {
089                DEBUG("Using cobertura.properties value [" + value + "] for key [" + key + "]");
090                return value;
091            }
092    
093            DEBUG("Using default value [" + defvalue + "] for key [" + key + "]");
094            return defvalue;
095        }
096    
097        public String getDatafile()
098        {
099            return getProperty( "net.sourceforge.cobertura.datafile", "cobertura.ser" );
100        }
101        
102        /**
103         * Poor mans debugging.
104         * Intentionally didn't use log4j, as we dont want to introduce that dependency on instrumented files.
105         */
106        private void DEBUG(String msg)
107        {
108            if(false)
109            {
110                System.out.println("[Cobertura:ConfigurationUtil] " + msg);
111            }
112        }
113    }