Skip to content

Commit ef9275a

Browse files
authored
Merge pull request #1736 from marklogic/feature/connection-string
MLE-19240 Allowing for host to override connection string
2 parents 728ef0c + 24fc77f commit ef9275a

File tree

2 files changed

+29
-12
lines changed

2 files changed

+29
-12
lines changed

marklogic-client-api/src/main/java/com/marklogic/client/impl/DatabaseClientPropertySource.java

Lines changed: 16 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,22 @@ public class DatabaseClientPropertySource {
3636

3737
static {
3838
connectionPropertyHandlers = new LinkedHashMap<>();
39+
40+
// Connection string is handled first so that certain properties can override it as needed. This is useful
41+
// for e.g. specifying a specific host in a cluster while reusing the rest of the connection string.
42+
connectionPropertyHandlers.put(PREFIX + "connectionString", (bean, value) -> {
43+
if (value instanceof String) {
44+
ConnectionString cs = new ConnectionString((String) value, "connection string");
45+
bean.setHost(cs.getHost());
46+
bean.setPort(cs.getPort());
47+
if (cs.getDatabase() != null && cs.getDatabase().trim().length() > 0) {
48+
bean.setDatabase(cs.getDatabase());
49+
}
50+
} else {
51+
throw new IllegalArgumentException("Connection string must be of type String");
52+
}
53+
});
54+
3955
connectionPropertyHandlers.put(PREFIX + "host", (bean, value) -> {
4056
if (value instanceof String) {
4157
bean.setHost((String) value);
@@ -59,18 +75,6 @@ public class DatabaseClientPropertySource {
5975
throw new IllegalArgumentException("Database must be of type String");
6076
}
6177
});
62-
connectionPropertyHandlers.put(PREFIX + "connectionString", (bean, value) -> {
63-
if (value instanceof String) {
64-
ConnectionString cs = new ConnectionString((String) value, "connection string");
65-
bean.setHost(cs.getHost());
66-
bean.setPort(cs.getPort());
67-
if (cs.getDatabase() != null && cs.getDatabase().trim().length() > 0) {
68-
bean.setDatabase(cs.getDatabase());
69-
}
70-
} else {
71-
throw new IllegalArgumentException("Connection string must be of type String");
72-
}
73-
});
7478
connectionPropertyHandlers.put(PREFIX + "basePath", (bean, value) -> {
7579
if (value instanceof String) {
7680
bean.setBasePath((String) value);

marklogic-client-api/src/test/java/com/marklogic/client/impl/DatabaseClientPropertySourceTest.java

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -196,6 +196,19 @@ void nonNumericPortInConnectionString() {
196196
assertEquals("Invalid value for connection string; port must be numeric, but was 'nonNumericPort'", ex.getMessage());
197197
}
198198

199+
@Test
200+
void hostTakesPrecedence() {
201+
props = new HashMap() {{
202+
put(PREFIX + "host", "somehost");
203+
put(PREFIX + "connectionString", "user:password@localhost:8000/Documents");
204+
}};
205+
206+
DatabaseClientFactory.Bean bean = buildBean();
207+
assertEquals("somehost", bean.getHost(), "This allows a user to use a connection string as a starting " +
208+
"point, and then override the host for a direct connection to a particular host in a cluster. This " +
209+
"capability is used by our Spark connector to support direct connections to multiple hosts.");
210+
}
211+
199212
private void useConnectionString(String connectionString) {
200213
props = new HashMap() {{
201214
put(PREFIX + "connectionString", connectionString);

0 commit comments

Comments
 (0)