Skip to content

Commit 1478688

Browse files
committed
Add DataSourceConfig.load() methods that take properties
The existing loadSettings() MUST use the "datasource" prefix and these newly added load() methods allow for other prefixes or no prefixes to be used when configuring from properties.
1 parent e59d1ab commit 1478688

5 files changed

Lines changed: 72 additions & 14 deletions

File tree

ebean-datasource-api/src/main/java/io/ebean/datasource/ConfigPropertiesHelper.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,8 @@ final class ConfigPropertiesHelper {
1515
* Construct with a prefix, serverName and properties.
1616
*/
1717
ConfigPropertiesHelper(String prefix, String poolName, Properties properties) {
18-
this.poolName = poolName;
1918
this.prefix = prefix;
19+
this.poolName = poolName;
2020
this.properties = properties;
2121
}
2222

ebean-datasource-api/src/main/java/io/ebean/datasource/DataSourceConfig.java

Lines changed: 27 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -861,16 +861,39 @@ public boolean useInitDatabase() {
861861
}
862862

863863
/**
864-
* Load the settings from the properties supplied.
864+
* Load the settings from the properties with no prefix on the property names.
865+
*
866+
* @param properties the properties to configure the dataSource
867+
*/
868+
public DataSourceConfig load(Properties properties) {
869+
return load(properties, null);
870+
}
871+
872+
/**
873+
* Load the settings from the properties with the given prefix on the property names.
874+
* <p>
875+
* For example, using a prefix of "my-db" then the username property key would be
876+
* "my-db.username".
877+
*
878+
* @param properties the properties to configure the dataSource
879+
* @param prefix the prefix of the property names.
880+
*/
881+
public DataSourceConfig load(Properties properties, String prefix) {
882+
loadSettings(new ConfigPropertiesHelper(prefix, null, properties));
883+
return this;
884+
}
885+
886+
/**
887+
* Load the settings from the properties with "datasource" prefix on the property names.
865888
* <p>
866-
* You can use this when you have your own properties to use for configuration.
889+
* For example, if the poolName is "hr" then the username property key would be:
890+
* "datasource.hr.username".
867891
*
868892
* @param properties the properties to configure the dataSource
869893
* @param poolName the name of the specific dataSource pool (optional)
870894
*/
871895
public DataSourceConfig loadSettings(Properties properties, String poolName) {
872-
ConfigPropertiesHelper dbProps = new ConfigPropertiesHelper("datasource", poolName, properties);
873-
loadSettings(dbProps);
896+
loadSettings(new ConfigPropertiesHelper("datasource", poolName, properties));
874897
return this;
875898
}
876899

ebean-datasource-api/src/test/java/io/ebean/datasource/DataSourceConfigTest.java

Lines changed: 30 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
package io.ebean.datasource;
22

3-
import org.assertj.core.api.AssertionsForClassTypes;
43
import org.junit.jupiter.api.Test;
54

65
import java.io.IOException;
@@ -49,22 +48,22 @@ public void parseCustom() {
4948
@Test
5049
public void isEmpty() {
5150
DataSourceConfig config = new DataSourceConfig();
52-
AssertionsForClassTypes.assertThat(config.isEmpty()).isTrue();
51+
assertThat(config.isEmpty()).isTrue();
5352

5453
config.setUrl("foo");
55-
AssertionsForClassTypes.assertThat(config.isEmpty()).isFalse();
54+
assertThat(config.isEmpty()).isFalse();
5655

5756
config = new DataSourceConfig();
5857
config.setUsername("foo");
59-
AssertionsForClassTypes.assertThat(config.isEmpty()).isFalse();
58+
assertThat(config.isEmpty()).isFalse();
6059

6160
config = new DataSourceConfig();
6261
config.setPassword("foo");
63-
AssertionsForClassTypes.assertThat(config.isEmpty()).isFalse();
62+
assertThat(config.isEmpty()).isFalse();
6463

6564
config = new DataSourceConfig();
6665
config.setDriver("foo");
67-
AssertionsForClassTypes.assertThat(config.isEmpty()).isFalse();
66+
assertThat(config.isEmpty()).isFalse();
6867
}
6968

7069
@Test
@@ -147,13 +146,35 @@ private DataSourceConfig create() {
147146

148147
@Test
149148
public void loadSettings() throws IOException {
149+
Properties props = new Properties();
150+
props.load(getClass().getResourceAsStream("/example.properties"));
150151

151-
DataSourceConfig config = new DataSourceConfig();
152+
var config = new DataSourceConfig().loadSettings(props, "foo");
153+
assertConfigValues(config);
154+
}
152155

156+
@Test
157+
public void load_prefix() throws IOException {
153158
Properties props = new Properties();
154-
props.load(getClass().getResourceAsStream("/example.properties"));
155-
config.loadSettings(props, "foo");
159+
props.load(getClass().getResourceAsStream("/example2.properties"));
160+
161+
var config = new DataSourceConfig().load(props, "bar");
162+
assertConfigValues(config);
163+
}
164+
165+
@Test
166+
public void load_noPrefix() throws IOException {
167+
Properties props = new Properties();
168+
props.load(getClass().getResourceAsStream("/example3.properties"));
169+
170+
var config = new DataSourceConfig().load(props);
171+
assertConfigValues(config);
172+
173+
var config2 = new DataSourceConfig().load(props, null);
174+
assertConfigValues(config2);
175+
}
156176

177+
private static void assertConfigValues(DataSourceConfig config) {
157178
assertThat(config.getReadOnlyUrl()).isEqualTo("myReadOnlyUrl");
158179
assertThat(config.getUrl()).isEqualTo("myUrl");
159180
assertThat(config.getUsername()).isEqualTo("myusername");
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
bar.username=myusername
2+
bar.password=mypassword
3+
bar.schema=myschema
4+
bar.url=myUrl
5+
bar.readOnlyUrl=myReadOnlyUrl
6+
bar.applicationName=myApp
7+
bar.clientInfo=ClientUser=ciu;ClientHostname=cih
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
username=myusername
2+
password=mypassword
3+
schema=myschema
4+
url=myUrl
5+
readOnlyUrl=myReadOnlyUrl
6+
applicationName=myApp
7+
clientInfo=ClientUser=ciu;ClientHostname=cih

0 commit comments

Comments
 (0)