|
18 | 18 | */ |
19 | 19 | package ubic.basecode.util; |
20 | 20 |
|
21 | | -import java.util.Iterator; |
22 | | - |
23 | | -import org.apache.commons.configuration2.CompositeConfiguration; |
24 | | -import org.apache.commons.configuration2.PropertiesConfiguration; |
25 | | -import org.apache.commons.configuration2.SystemConfiguration; |
26 | | -import org.apache.commons.configuration2.ex.ConfigurationException; |
27 | | -import org.apache.commons.configuration2.io.FileHandler; |
28 | 21 | import org.slf4j.Logger; |
29 | 22 | import org.slf4j.LoggerFactory; |
30 | 23 |
|
| 24 | +import javax.annotation.Nullable; |
| 25 | +import java.io.IOException; |
| 26 | +import java.io.InputStream; |
| 27 | +import java.util.Properties; |
| 28 | + |
31 | 29 | /** |
32 | 30 | * Configuration of ontology services and other things. |
| 31 | + * <p> |
| 32 | + * Configurations are retrieved from three locations: properties set at runtime with {@link #setString(String, String)}, |
| 33 | + * system properties and a default properties file named {@code basecode.properties} at the root of the classpath in |
| 34 | + * that order. |
| 35 | + * <p> |
| 36 | + * Properties set via system properties must be prefixed with {@code basecode.} to be considered. |
| 37 | + * <p> |
| 38 | + * Properties set at runtime can be reset with {@link #reset()} and {@link #reset(String)}. |
33 | 39 | * |
34 | 40 | * @author paul |
35 | | - * |
36 | 41 | */ |
37 | 42 | public class Configuration { |
38 | 43 |
|
39 | | - private static CompositeConfiguration config; |
40 | | - |
41 | | - /** |
42 | | - * Name of the resource containing defaults |
43 | | - */ |
44 | | - private static final String DEFAULT_CONFIGURATION = "ontology.properties"; |
45 | | - |
46 | | - private static Logger log = LoggerFactory.getLogger( Configuration.class ); |
| 44 | + private static final Logger log = LoggerFactory.getLogger( Configuration.class ); |
47 | 45 |
|
48 | | - /** |
49 | | - * The name of the file users can use to customize. |
50 | | - */ |
51 | | - private static final String USER_CONFIGURATION = "basecode.properties"; |
| 46 | + private static final String SYSTEM_PROPERTY_PREFIX = "basecode."; |
| 47 | + private static final Properties defaultProps = new Properties(); |
| 48 | + private static final Properties props = new Properties(); |
52 | 49 |
|
53 | 50 | static { |
54 | | - |
55 | | - config = new CompositeConfiguration(); |
56 | | - config.addConfiguration( new SystemConfiguration() ); |
57 | | - |
58 | | - /* |
59 | | - * the order matters - first come, first serve. Items added later do not overwrite items defined earlier. Thus |
60 | | - * the user configuration has to be listed first. |
61 | | - */ |
62 | | - |
63 | | - try { |
64 | | - // purely for backwards compatibility, if the user hasn't set up ontology.properties. |
65 | | - PropertiesConfiguration pc = new PropertiesConfiguration(); |
66 | | - FileHandler handler = new FileHandler( pc ); |
67 | | - handler.setFileName( "Gemma.properties" ); |
68 | | - handler.load(); |
69 | | - config.addConfiguration( pc ); |
70 | | - } catch ( ConfigurationException e ) { |
| 51 | + try ( InputStream is = Configuration.class.getResourceAsStream( "/basecode.properties" ) ) { |
| 52 | + if ( is != null ) { |
| 53 | + defaultProps.load( is ); |
| 54 | + } else { |
| 55 | + log.warn( "No basecode.properties was found in the classpath, only system and manually set properties will be considered." ); |
| 56 | + } |
| 57 | + } catch ( IOException e ) { |
| 58 | + throw new RuntimeException( e ); |
71 | 59 | } |
| 60 | + } |
72 | 61 |
|
73 | | - try { |
74 | | - PropertiesConfiguration pc = new PropertiesConfiguration(); |
75 | | - FileHandler handler = new FileHandler( pc ); |
76 | | - handler.setFileName( USER_CONFIGURATION ); |
77 | | - handler.load(); |
78 | | - config.addConfiguration( pc ); |
79 | | - } catch ( ConfigurationException e ) { |
| 62 | + /** |
| 63 | + * Obtain a configuration value by key. |
| 64 | + */ |
| 65 | + @Nullable |
| 66 | + public static String getString( String key ) { |
| 67 | + String val = props.getProperty( key ); |
| 68 | + if ( val == null ) { |
| 69 | + val = System.getProperty( SYSTEM_PROPERTY_PREFIX + key ); |
80 | 70 | } |
81 | | - |
82 | | - try { |
83 | | - PropertiesConfiguration pc = new PropertiesConfiguration(); |
84 | | - FileHandler handler = new FileHandler( pc ); |
85 | | - handler.setFileName( DEFAULT_CONFIGURATION ); |
86 | | - handler.load(); |
87 | | - config.addConfiguration( pc ); |
88 | | - } catch ( ConfigurationException e ) { |
89 | | - log.error( DEFAULT_CONFIGURATION + " is missing, ontology loading may fail" ); |
| 71 | + if ( val == null ) { |
| 72 | + val = defaultProps.getProperty( key ); |
90 | 73 | } |
| 74 | + return val; |
| 75 | + } |
91 | 76 |
|
92 | | - // step through the result and do a final round of variable substitution |
93 | | - for ( Iterator<String> it = config.getKeys(); it.hasNext(); ) { |
94 | | - String key = it.next(); |
95 | | - String property = config.getString( key ); |
96 | | - if ( property != null && property.startsWith( "${" ) && property.endsWith( "}" ) ) { |
97 | | - String keyToSubstitute = property.substring( 2, property.length() - 1 ); |
98 | | - String valueToSubstitute = config.getString( keyToSubstitute ); |
99 | | - log.debug( key + "=" + property + " -> " + valueToSubstitute ); |
100 | | - config.setProperty( key, valueToSubstitute ); |
101 | | - } |
| 77 | + /** |
| 78 | + * Obtain a boolean configuration value by key. |
| 79 | + * |
| 80 | + * @see Boolean#parseBoolean(String) |
| 81 | + */ |
| 82 | + @Nullable |
| 83 | + public static Boolean getBoolean( String key ) { |
| 84 | + String val = getString( key ); |
| 85 | + if ( val != null ) { |
| 86 | + return Boolean.parseBoolean( val ); |
| 87 | + } else { |
| 88 | + return null; |
102 | 89 | } |
103 | | - |
104 | 90 | } |
105 | 91 |
|
106 | 92 | /** |
107 | | - * @param key |
108 | | - * @return |
| 93 | + * Set a configuration by key. |
109 | 94 | */ |
110 | | - public static boolean getBoolean( String key ) { |
111 | | - return config.getBoolean( key, false ); |
| 95 | + public static void setString( String key, String value ) { |
| 96 | + props.setProperty( key, value ); |
112 | 97 | } |
113 | 98 |
|
114 | 99 | /** |
115 | | - * @param key |
116 | | - * @return |
| 100 | + * Reset all configurations set at runtime. |
117 | 101 | */ |
118 | | - public static String getString( String key ) { |
119 | | - return config.getString( key ); |
| 102 | + public static void reset() { |
| 103 | + props.clear(); |
120 | 104 | } |
121 | 105 |
|
122 | 106 | /** |
123 | | - * @param key |
124 | | - * @return |
| 107 | + * Reset a specific configuration by key. |
125 | 108 | */ |
126 | | - public static void setString( String key, Object value ) { |
127 | | - config.setProperty( key, value ); |
| 109 | + public static void reset( String key ) { |
| 110 | + props.remove( key ); |
128 | 111 | } |
129 | 112 | } |
0 commit comments