Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 4e13f6c

Browse files
committed
allow construct ArangoConfigProperties from java.util.Properties (DE-976)
1 parent 6e52fa0 commit 4e13f6c

File tree

4 files changed

+56
-12
lines changed

4 files changed

+56
-12
lines changed

‎core/src/main/java/com/arangodb/config/ArangoConfigProperties.java

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77

88
import java.util.List;
99
import java.util.Optional;
10+
import java.util.Properties;
1011

1112
public interface ArangoConfigProperties {
1213

@@ -34,6 +35,22 @@ static ArangoConfigProperties fromFile(final String fileName, final String prefi
3435
return new ArangoConfigPropertiesImpl(fileName, prefix);
3536
}
3637

38+
/**
39+
* Creates {@code ArangoConfigProperties} from Java properties ({@link java.util.Properties}).
40+
* Properties must be prefixed with @{code "arangodb"}, eg. @{code "arangodb.hosts=localhost:8529"}.
41+
*/
42+
static ArangoConfigProperties fromProperties(final Properties properties) {
43+
return new ArangoConfigPropertiesImpl(properties);
44+
}
45+
46+
/**
47+
* Creates {@code ArangoConfigProperties} from Java properties ({@link java.util.Properties}).
48+
* Properties must be prefixed with @{code prefix}, eg. @{code "<prefix>.hosts=localhost:8529"}.
49+
*/
50+
static ArangoConfigProperties fromProperties(final Properties properties, final String prefix) {
51+
return new ArangoConfigPropertiesImpl(properties, prefix);
52+
}
53+
3754
default Optional<List<HostDescription>> getHosts() {
3855
return Optional.empty();
3956
}

‎core/src/main/java/com/arangodb/internal/config/ArangoConfigPropertiesImpl.java

Lines changed: 19 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -31,29 +31,37 @@ public ArangoConfigPropertiesImpl(final String fileName) {
3131
this(fileName, DEFAULT_PREFIX);
3232
}
3333

34-
public ArangoConfigPropertiesImpl(final String fileName, final String prefix) {
35-
properties = initProperties(fileName);
36-
this.prefix = initPrefix(prefix);
34+
public ArangoConfigPropertiesImpl(final String fileName, final String prefix) {
35+
this(initProperties(fileName), prefix);
3736
}
3837

39-
private String initPrefix(String p) {
40-
if (p == null) {
41-
return "";
42-
} else {
43-
return p + ".";
44-
}
38+
public ArangoConfigPropertiesImpl(final Properties properties) {
39+
this(properties, DEFAULT_PREFIX);
40+
}
41+
42+
public ArangoConfigPropertiesImpl(final Properties properties, final String prefix) {
43+
this.properties = properties;
44+
this.prefix = initPrefix(prefix);
4545
}
4646

47-
private Properties initProperties(String fileName) {
47+
private staticProperties initProperties(String fileName) {
4848
Properties p = new Properties();
49-
try (InputStream is = getClass().getClassLoader().getResourceAsStream(fileName)) {
49+
try (InputStream is = ArangoConfigPropertiesImpl.class.getClassLoader().getResourceAsStream(fileName)) {
5050
p.load(is);
5151
} catch (Exception e) {
5252
throw ArangoDBException.of("Got exception while reading properties file " + fileName, e);
5353
}
5454
return p;
5555
}
5656

57+
private String initPrefix(String p) {
58+
if (p == null) {
59+
return "";
60+
} else {
61+
return p + ".";
62+
}
63+
}
64+
5765
private String getProperty(String key) {
5866
return properties.getProperty(prefix + key);
5967
}

‎test-functional/src/test/java/com/arangodb/ArangoDBTest.java

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -672,14 +672,27 @@ void loadproperties() {
672672

673673
@ParameterizedTest
674674
@MethodSource("arangos")
675-
void loadpropertiesWithPrefix() {
675+
void loadPropertiesWithPrefix() {
676676
ArangoDB adb = new ArangoDB.Builder()
677677
.loadProperties(ConfigUtils.loadConfig("arangodb-with-prefix.properties", "adb"))
678678
.build();
679679
adb.getVersion();
680680
adb.shutdown();
681681
}
682682

683+
@ParameterizedTest
684+
@MethodSource("arangos")
685+
void loadConfigFromPropertiesWithPrefix() {
686+
Properties props = new Properties();
687+
props.setProperty("adb.hosts", "172.28.0.1:8529");
688+
props.setProperty("adb.password", "test");
689+
ArangoDB adb = new ArangoDB.Builder()
690+
.loadProperties(ConfigUtils.loadConfig(props, "adb"))
691+
.build();
692+
adb.getVersion();
693+
adb.shutdown();
694+
}
695+
683696
@ParameterizedTest
684697
@MethodSource("arangos")
685698
void accessMultipleDatabases(ArangoDB arangoDB) {

‎test-functional/src/test/java/com/arangodb/config/ConfigUtils.java

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
package com.arangodb.config;
22

3+
import java.util.Properties;
4+
35
public class ConfigUtils {
46

57
public static ArangoConfigProperties loadConfig() {
@@ -14,4 +16,8 @@ public static ArangoConfigProperties loadConfig(final String location, final Str
1416
return ArangoConfigProperties.fromFile(location, prefix);
1517
}
1618

19+
public static ArangoConfigProperties loadConfig(final Properties properties, final String prefix) {
20+
return ArangoConfigProperties.fromProperties(properties, prefix);
21+
}
22+
1723
}

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /