Skip to content

Commit 5a7f0b4

Browse files
committed
Simplify how baseCode can is configured
Only read properties from 'basecode.properties', system properties starting with 'basecode.' and properties set at runtime.
1 parent 0e73768 commit 5a7f0b4

3 files changed

Lines changed: 68 additions & 106 deletions

File tree

src/ubic/basecode/util/Configuration.java

Lines changed: 59 additions & 76 deletions
Original file line numberDiff line numberDiff line change
@@ -18,112 +18,95 @@
1818
*/
1919
package ubic.basecode.util;
2020

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;
2821
import org.slf4j.Logger;
2922
import org.slf4j.LoggerFactory;
3023

24+
import javax.annotation.Nullable;
25+
import java.io.IOException;
26+
import java.io.InputStream;
27+
import java.util.Properties;
28+
3129
/**
3230
* 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)}.
3339
*
3440
* @author paul
35-
*
3641
*/
3742
public class Configuration {
3843

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 );
4745

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();
5249

5350
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 );
7159
}
60+
}
7261

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 );
8070
}
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 );
9073
}
74+
return val;
75+
}
9176

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;
10289
}
103-
10490
}
10591

10692
/**
107-
* @param key
108-
* @return
93+
* Set a configuration by key.
10994
*/
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 );
11297
}
11398

11499
/**
115-
* @param key
116-
* @return
100+
* Reset all configurations set at runtime.
117101
*/
118-
public static String getString( String key ) {
119-
return config.getString( key );
102+
public static void reset() {
103+
props.clear();
120104
}
121105

122106
/**
123-
* @param key
124-
* @return
107+
* Reset a specific configuration by key.
125108
*/
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 );
128111
}
129112
}

src/ubic/basecode/util/r/AbstractRClient.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -622,7 +622,7 @@ public List<?> listEval( Class<?> listEntryType, String command ) {
622622
public boolean loadLibrary( String libraryName ) {
623623
try {
624624

625-
String userLibPath = Configuration.getString( "basecode.rlibpath" );
625+
String userLibPath = Configuration.getString( "rlibpath" );
626626
if ( StringUtils.isNotBlank( userLibPath ) ) {
627627
voidEval( ".libPaths(" + userLibPath + ")" );
628628
}

src/ubic/basecode/util/r/RServeClient.java

Lines changed: 8 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -18,26 +18,21 @@
1818
*/
1919
package ubic.basecode.util.r;
2020

21-
import java.io.File;
22-
import java.io.IOException;
23-
import java.net.URL;
24-
import java.util.Iterator;
25-
import java.util.List;
26-
27-
import org.apache.commons.configuration2.PropertiesConfiguration;
28-
import org.apache.commons.configuration2.ex.ConfigurationException;
29-
import org.apache.commons.configuration2.io.FileHandler;
3021
import org.apache.commons.lang3.StringUtils;
3122
import org.rosuda.REngine.REXP;
3223
import org.rosuda.REngine.REXPMismatchException;
3324
import org.rosuda.REngine.REngineException;
3425
import org.rosuda.REngine.RList;
3526
import org.rosuda.REngine.Rserve.RConnection;
3627
import org.rosuda.REngine.Rserve.RserveException;
37-
3828
import ubic.basecode.dataStructure.matrix.DenseDoubleMatrix;
3929
import ubic.basecode.dataStructure.matrix.DoubleMatrix;
40-
import ubic.basecode.util.ConfigUtils;
30+
import ubic.basecode.util.Configuration;
31+
32+
import java.io.File;
33+
import java.io.IOException;
34+
import java.util.Iterator;
35+
import java.util.List;
4136

4237
/**
4338
* @author pavlidis
@@ -56,24 +51,8 @@ public class RServeClient extends AbstractRClient {
5651

5752
private final static String os = System.getProperty( "os.name" ).toLowerCase();
5853

59-
/**
60-
* @return
61-
* @throws ConfigurationException
62-
*/
63-
protected static String findRserveCommand() throws ConfigurationException {
64-
URL userSpecificConfigFileLocation = ConfigUtils.locate( "local.properties" );
65-
66-
PropertiesConfiguration userConfig = null;
67-
if ( userSpecificConfigFileLocation != null ) {
68-
userConfig = new PropertiesConfiguration();
69-
FileHandler handler = new FileHandler( userConfig );
70-
handler.setFileName( "local.properties" );
71-
handler.load();
72-
}
73-
String rserveExecutable = null;
74-
if ( userConfig != null ) {
75-
rserveExecutable = userConfig.getString( "rserve.start.command" );
76-
}
54+
protected static String findRserveCommand() {
55+
String rserveExecutable = Configuration.getString( "rserve.start.command" );
7756
if ( StringUtils.isBlank( rserveExecutable ) ) {
7857
log.info( "Rserve command not configured? Trying fallbacks" );
7958
if ( os.startsWith( "windows" ) ) { // lower cased

0 commit comments

Comments
 (0)