Coverage Report - net.sourceforge.cobertura.util.ConfigurationUtil
 
Classes in this File Line Coverage Branch Coverage Complexity
ConfigurationUtil
50%
14/28
33%
2/6
2.6
 
 1  
 /*
 2  
  * Cobertura - http://cobertura.sourceforge.net/
 3  
  *
 4  
  * Copyright (C) 2007 Joakim Erdfelt
 5  
  *
 6  
  * Cobertura is free software; you can redistribute it and/or modify
 7  
  * it under the terms of the GNU General Public License as published
 8  
  * by the Free Software Foundation; either version 2 of the License,
 9  
  * or (at your option) any later version.
 10  
  *
 11  
  * Cobertura is distributed in the hope that it will be useful, but
 12  
  * WITHOUT ANY WARRANTY; without even the implied warranty of
 13  
  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
 14  
  * General Public License for more details.
 15  
  *
 16  
  * You should have received a copy of the GNU General Public License
 17  
  * along with Cobertura; if not, write to the Free Software
 18  
  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
 19  
  * USA
 20  
  */
 21  
 
 22  
 package net.sourceforge.cobertura.util;
 23  
 
 24  
 import java.io.IOException;
 25  
 import java.io.InputStream;
 26  
 import java.net.URL;
 27  
 import java.util.Properties;
 28  
 
 29  
 /**
 30  
  * A Utility Class to load the configuration.
 31  
  * 
 32  
  * Checks for values using the following hierarchy.
 33  
  * 1) System Property matching key.
 34  
  * 2) cobertura.properties Resource Property matching key.
 35  
  * 3) hardcoded default value
 36  
  * 
 37  
  * @author Joakim Erdfelt
 38  
  */
 39  
 public class ConfigurationUtil
 40  
 {
 41  
     public static final String RESOURCE = "/cobertura.properties";
 42  
 
 43  
     private Properties props;
 44  
 
 45  
     public ConfigurationUtil()
 46  5
     {
 47  5
         init();
 48  5
     }
 49  
 
 50  
     public void init()
 51  
     {
 52  5
         props = new Properties();
 53  
 
 54  5
         URL url = this.getClass().getResource( RESOURCE );
 55  5
         if ( url == null )
 56  
         {
 57  5
             DEBUG( "Unable to find configuration resource in classpath of name " + RESOURCE + ", using empty configuration." );
 58  5
             return;
 59  
         }
 60  
 
 61  0
         InputStream is = null;
 62  
         try
 63  
         {
 64  0
             is = url.openStream();
 65  0
             props.load( is );
 66  
         }
 67  0
         catch ( IOException e )
 68  
         {
 69  0
             System.err.println( "ERROR: Unable to load configuration resource " + RESOURCE + " - " + e.getMessage() );
 70  
         }
 71  
         finally
 72  
         {
 73  0
             IOUtil.closeInputStream( is );
 74  0
         }
 75  0
     }
 76  
 
 77  
     public String getProperty( String key, String defvalue )
 78  
     {
 79  5
         String value = System.getProperty( key );
 80  5
         if ( value != null )
 81  
         {
 82  5
             DEBUG("Using system property value [" + value + "] for key [" + key + "]");
 83  5
             return value;
 84  
         }
 85  
 
 86  0
         value = props.getProperty( key );
 87  0
         if ( value != null )
 88  
         {
 89  0
             DEBUG("Using cobertura.properties value [" + value + "] for key [" + key + "]");
 90  0
             return value;
 91  
         }
 92  
 
 93  0
         DEBUG("Using default value [" + defvalue + "] for key [" + key + "]");
 94  0
         return defvalue;
 95  
     }
 96  
 
 97  
     public String getDatafile()
 98  
     {
 99  5
         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  10
     }
 113  
 }