Skip to content

Commit 88acc94

Browse files
committed
Add support for comma-delimited entries in configuration
1 parent 43636e5 commit 88acc94

3 files changed

Lines changed: 17 additions & 7 deletions

File tree

src/ubic/basecode/util/ConfigUtils.java

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,8 @@
2222
import org.apache.commons.configuration2.PropertiesConfiguration;
2323
import org.apache.commons.configuration2.builder.FileBasedBuilderParametersImpl;
2424
import org.apache.commons.configuration2.builder.FileBasedConfigurationBuilder;
25+
import org.apache.commons.configuration2.convert.DefaultListDelimiterHandler;
26+
import org.apache.commons.configuration2.convert.ListDelimiterHandler;
2527
import org.apache.commons.configuration2.ex.ConfigurationException;
2628
import org.apache.commons.configuration2.io.FileHandler;
2729
import org.apache.commons.configuration2.io.FileLocator;
@@ -36,6 +38,8 @@
3638
*/
3739
public class ConfigUtils {
3840

41+
private static final ListDelimiterHandler LIST_DELIMITER_HANDLER = new DefaultListDelimiterHandler( ',' );
42+
3943
/**
4044
* @param file
4145
* @return
@@ -51,7 +55,7 @@ public static FileBasedConfigurationBuilder<PropertiesConfiguration> getConfigBu
5155
}
5256
FileBasedConfigurationBuilder<PropertiesConfiguration> builder = new FileBasedConfigurationBuilder<PropertiesConfiguration>(
5357
PropertiesConfiguration.class );
54-
builder.configure( new FileBasedBuilderParametersImpl().setFile( file ) );
58+
builder.configure( new FileBasedBuilderParametersImpl().setFile( file ).setListDelimiterHandler( LIST_DELIMITER_HANDLER ) );
5559
return builder;
5660
}
5761

@@ -96,7 +100,7 @@ public static PropertiesConfiguration loadClasspathConfig( String name ) throws
96100
throw new ConfigurationException( "Couldn't locate: " + name );
97101
}
98102

99-
PropertiesConfiguration pc = new PropertiesConfiguration();
103+
PropertiesConfiguration pc = createConfiguration();
100104
FileHandler handler = new FileHandler( pc );
101105
handler.setURL( url );
102106
handler.load();
@@ -116,7 +120,7 @@ public static PropertiesConfiguration loadConfig( File file ) throws Configurati
116120
throw new ConfigurationException( "Couldn't create the file: " + e.getMessage() );
117121
}
118122
}
119-
PropertiesConfiguration pc = new PropertiesConfiguration();
123+
PropertiesConfiguration pc = createConfiguration();
120124
FileHandler handler = new FileHandler( pc );
121125
handler.setFile( file );
122126
handler.load();
@@ -187,4 +191,10 @@ public static URL locate( String name ) {
187191
FileLocator fl = FileLocatorUtils.fileLocator().fileName( name ).create();
188192
return FileLocatorUtils.locate( fl );
189193
}
194+
195+
private static PropertiesConfiguration createConfiguration() {
196+
PropertiesConfiguration pc = new PropertiesConfiguration();
197+
pc.setListDelimiterHandler( LIST_DELIMITER_HANDLER );
198+
return pc;
199+
}
190200
}

test/data/test.properties

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
11
foo=bar
22
ack=yup
33
tek=6
4+
list=1,2 ,3

test/ubic/basecode/util/ConfigUtilsTest.java

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -14,10 +14,6 @@
1414
*/
1515
package ubic.basecode.util;
1616

17-
import static org.junit.Assert.assertEquals;
18-
import static org.junit.Assert.assertNotNull;
19-
import static org.junit.Assert.assertTrue;
20-
2117
import java.io.File;
2218

2319
import org.apache.commons.configuration2.PropertiesConfiguration;
@@ -27,6 +23,8 @@
2723
import org.junit.Before;
2824
import org.junit.Test;
2925

26+
import static org.junit.Assert.*;
27+
3028
/**
3129
* @author Paul
3230
*
@@ -58,6 +56,7 @@ public void testGetConfigBuilderFile() throws Exception {
5856
testConfigPath ) );
5957
assertNotNull( config );
6058
assertEquals( "bar", config.getConfiguration().getProperty( "foo" ) );
59+
assertArrayEquals( new String[] { "1", "2", "3" }, config.getConfiguration().getStringArray( "list" ) );
6160
}
6261

6362
@Test

0 commit comments

Comments
 (0)