| 1 | |
|
| 2 | |
|
| 3 | |
|
| 4 | |
|
| 5 | |
|
| 6 | |
|
| 7 | |
|
| 8 | |
|
| 9 | |
|
| 10 | |
|
| 11 | |
|
| 12 | |
|
| 13 | |
|
| 14 | |
|
| 15 | |
|
| 16 | |
|
| 17 | |
|
| 18 | |
|
| 19 | |
|
| 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 | |
|
| 31 | |
|
| 32 | |
|
| 33 | |
|
| 34 | |
|
| 35 | |
|
| 36 | |
|
| 37 | |
|
| 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 | |
|
| 104 | |
|
| 105 | |
|
| 106 | |
private void DEBUG(String msg) |
| 107 | |
{ |
| 108 | |
if(false) |
| 109 | |
{ |
| 110 | |
System.out.println("[Cobertura:ConfigurationUtil] " + msg); |
| 111 | |
} |
| 112 | 10 | } |
| 113 | |
} |