1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19 package ubic.basecode.util;
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 import org.slf4j.Logger;
29 import org.slf4j.LoggerFactory;
30
31
32
33
34
35
36
37 public class Configuration {
38
39 private static CompositeConfiguration config;
40
41
42
43
44 private static final String DEFAULT_CONFIGURATION = "ontology.properties";
45
46 private static Logger log = LoggerFactory.getLogger( Configuration.class );
47
48
49
50
51 private static final String USER_CONFIGURATION = "basecode.properties";
52
53 static {
54
55 config = new CompositeConfiguration();
56 config.addConfiguration( new SystemConfiguration() );
57
58
59
60
61
62
63 try {
64
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 ) {
71 }
72
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 ) {
80 }
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" );
90 }
91
92
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 }
102 }
103
104 }
105
106
107
108
109
110 public static boolean getBoolean( String key ) {
111 return config.getBoolean( key, false );
112 }
113
114
115
116
117
118 public static String getString( String key ) {
119 return config.getString( key );
120 }
121
122
123
124
125
126 public static void setString( String key, Object value ) {
127 config.setProperty( key, value );
128 }
129 }