Skip to content

Commit 08d119f

Browse files
committed
Update to new configuration system
1 parent e4c234d commit 08d119f

39 files changed

+636
-767
lines changed

CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
1+
# Version 2.0.0 (2017-01-05)
2+
3+
* [brk] Update to new configuration system.
4+
15
# Version 1.1.0 (2016-04-26)
26

37
* [new] Automatically build indexes

core/pom.xml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
<parent>
1515
<groupId>org.seedstack.addons.mongodb</groupId>
1616
<artifactId>mongodb</artifactId>
17-
<version>1.1.0-SNAPSHOT</version>
17+
<version>2.0.0-SNAPSHOT</version>
1818
</parent>
1919

2020
<artifactId>mongodb-core</artifactId>
@@ -32,6 +32,7 @@
3232
<version>${mongodb.version}</version>
3333
<scope>provided</scope>
3434
</dependency>
35+
3536
<dependency>
3637
<groupId>org.seedstack.seed</groupId>
3738
<artifactId>seed-testing</artifactId>

core/src/it/resources/META-INF/configuration/seed.props

Lines changed: 0 additions & 30 deletions
This file was deleted.
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
#
2+
# Copyright (c) 2013-2016, The SeedStack authors <http://seedstack.org>
3+
#
4+
# This Source Code Form is subject to the terms of the Mozilla Public
5+
# License, v. 2.0. If a copy of the MPL was not distributed with this
6+
# file, You can obtain one at http://mozilla.org/MPL/2.0/.
7+
#
8+
9+
mongoDb:
10+
clients:
11+
client1:
12+
hosts: localhost
13+
options:
14+
connectionsPerHost: 50
15+
databases: db1
16+
client2:
17+
async: true
18+
hosts: localhost
19+
settings:
20+
connectionPool:
21+
maxSize: 50
22+
databases: db2
23+
client3:
24+
async: true
25+
hosts: localhost
26+
databases:
27+
db2: db3
28+
Lines changed: 109 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,109 @@
1+
/**
2+
* Copyright (c) 2013-2016, The SeedStack authors <http://seedstack.org>
3+
*
4+
* This Source Code Form is subject to the terms of the Mozilla Public
5+
* License, v. 2.0. If a copy of the MPL was not distributed with this
6+
* file, You can obtain one at http://mozilla.org/MPL/2.0/.
7+
*/
8+
package org.seedstack.mongodb;
9+
10+
import org.seedstack.coffig.Config;
11+
import org.seedstack.coffig.SingleValue;
12+
13+
import java.util.ArrayList;
14+
import java.util.Collections;
15+
import java.util.HashMap;
16+
import java.util.List;
17+
import java.util.Map;
18+
19+
@Config("mongoDb")
20+
public class MongoDbConfig {
21+
private Map<String, ClientConfig> clients = new HashMap<>();
22+
23+
public Map<String, ClientConfig> getClients() {
24+
return Collections.unmodifiableMap(clients);
25+
}
26+
27+
public MongoDbConfig addClient(String name, ClientConfig config) {
28+
this.clients.put(name, config);
29+
return this;
30+
}
31+
32+
public static class ClientConfig {
33+
private boolean async = false;
34+
@SingleValue
35+
private String uri;
36+
private List<String> hosts = new ArrayList<>();
37+
private List<String> credentials = new ArrayList<>();
38+
private Map<String, DatabaseConfig> databases = new HashMap<>();
39+
40+
public boolean isAsync() {
41+
return async;
42+
}
43+
44+
public ClientConfig setAsync(boolean async) {
45+
this.async = async;
46+
return this;
47+
}
48+
49+
public boolean isConfiguredByUri() {
50+
return uri != null;
51+
}
52+
53+
public String getUri() {
54+
return uri;
55+
}
56+
57+
public ClientConfig setUri(String uri) {
58+
if (!hosts.isEmpty()) {
59+
throw new IllegalStateException("Cannot set MongoDb URI, the client is already configured through hosts");
60+
}
61+
this.uri = uri;
62+
return this;
63+
}
64+
65+
public List<String> getHosts() {
66+
return Collections.unmodifiableList(hosts);
67+
}
68+
69+
public ClientConfig addHost(String host) {
70+
if (uri != null) {
71+
throw new IllegalStateException("Cannot add MongoDb host, the client is already configured through an URI");
72+
}
73+
this.hosts.add(host);
74+
return this;
75+
}
76+
77+
public List<String> getCredentials() {
78+
return Collections.unmodifiableList(credentials);
79+
}
80+
81+
public ClientConfig addCredential(String credential) {
82+
this.credentials.add(credential);
83+
return this;
84+
}
85+
86+
public Map<String, DatabaseConfig> getDatabases() {
87+
return Collections.unmodifiableMap(databases);
88+
}
89+
90+
public ClientConfig addDatabase(String name, DatabaseConfig databaseConfig) {
91+
this.databases.put(name, databaseConfig);
92+
return this;
93+
}
94+
95+
public static class DatabaseConfig {
96+
@SingleValue
97+
private String alias;
98+
99+
public String getAlias() {
100+
return alias;
101+
}
102+
103+
public DatabaseConfig setAlias(String alias) {
104+
this.alias = alias;
105+
return this;
106+
}
107+
}
108+
}
109+
}

core/src/main/java/org/seedstack/mongodb/morphia/internal/AbstractMongoDbManager.java renamed to core/src/main/java/org/seedstack/mongodb/internal/AbstractMongoDbManager.java

Lines changed: 30 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -5,13 +5,15 @@
55
* License, v. 2.0. If a copy of the MPL was not distributed with this
66
* file, You can obtain one at http://mozilla.org/MPL/2.0/.
77
*/
8-
package org.seedstack.mongodb.morphia.internal;
8+
package org.seedstack.mongodb.internal;
99

10+
import com.google.common.base.Preconditions;
1011
import com.google.inject.Module;
1112
import com.mongodb.AuthenticationMechanism;
1213
import com.mongodb.MongoCredential;
1314
import com.mongodb.ServerAddress;
14-
import org.apache.commons.configuration.Configuration;
15+
import org.seedstack.coffig.Coffig;
16+
import org.seedstack.mongodb.MongoDbConfig;
1517
import org.seedstack.seed.SeedException;
1618
import org.slf4j.Logger;
1719
import org.slf4j.LoggerFactory;
@@ -26,23 +28,19 @@
2628
abstract class AbstractMongoDbManager<C, D> implements MongoDbManager {
2729
private static final Logger LOGGER = LoggerFactory.getLogger(AbstractMongoDbManager.class);
2830

29-
private final Map<String, C> mongoClients = new HashMap<String, C>();
30-
private final Map<String, D> mongoDatabases = new HashMap<String, D>();
31+
private final Map<String, C> mongoClients = new HashMap<>();
32+
private final Map<String, D> mongoDatabases = new HashMap<>();
3133

3234
@Override
33-
public void registerClient(String clientName, Configuration clientConfiguration) {
35+
public void registerClient(String clientName, MongoDbConfig.ClientConfig clientConfig, Coffig coffig) {
3436
LOGGER.info("Creating MongoDB client {}", clientName);
35-
mongoClients.put(clientName, doCreateClient(clientConfiguration));
37+
mongoClients.put(clientName, doCreateClient(clientName, clientConfig, coffig));
3638
}
3739

3840
@Override
3941
public void registerDatabase(String clientName, String dbName, String alias) {
4042
C mongoClient = mongoClients.get(clientName);
41-
42-
if (mongoClient == null) {
43-
throw SeedException.createNew(MongoDbErrorCodes.UNKNOWN_CLIENT_SPECIFIED).put("clientName", clientName).put("dbName", dbName);
44-
}
45-
43+
Preconditions.checkNotNull(mongoClient, "Mongo client " + clientName + " is not registered");
4644
mongoDatabases.put(alias, doCreateDatabase(mongoClient, dbName));
4745
}
4846

@@ -67,19 +65,11 @@ public void shutdown() {
6765
@SuppressWarnings("unchecked")
6866
public Module getModule() {
6967
Type[] actualTypeArguments = ((ParameterizedType) getClass().getGenericSuperclass()).getActualTypeArguments();
70-
return new MongoDbModule<C, D>((Class<C>) actualTypeArguments[0], (Class<D>) actualTypeArguments[1], mongoClients, mongoDatabases);
71-
}
72-
73-
protected <T> T instanceFromClassName(Class<T> clazz, String className) {
74-
try {
75-
return Class.forName(className).asSubclass(clazz).newInstance();
76-
} catch (Exception e) {
77-
throw SeedException.wrap(e, MongoDbErrorCodes.UNABLE_TO_INSTANTIATE_CLASS).put("className", className);
78-
}
68+
return new MongoDbModule<>((Class<C>) actualTypeArguments[0], (Class<D>) actualTypeArguments[1], mongoClients, mongoDatabases);
7969
}
8070

81-
public List<ServerAddress> buildServerAddresses(String[] addresses) {
82-
List<ServerAddress> serverAddresses = new ArrayList<ServerAddress>();
71+
List<ServerAddress> buildServerAddresses(String clientName, List<String> addresses) {
72+
List<ServerAddress> serverAddresses = new ArrayList<>();
8373

8474
if (addresses != null) {
8575
for (String address : addresses) {
@@ -89,39 +79,45 @@ public List<ServerAddress> buildServerAddresses(String[] addresses) {
8979
} else if (split.length == 2) {
9080
serverAddresses.add(new ServerAddress(split[0], Integer.parseInt(split[1])));
9181
} else {
92-
throw SeedException.createNew(MongoDbErrorCodes.UNABLE_TO_PARSE_SERVER_ADDRESS).put("address", address);
82+
throw SeedException.createNew(MongoDbErrorCode.INVALID_SERVER_ADDRESS)
83+
.put("clientName", clientName)
84+
.put("address", address);
9385
}
9486
}
9587
}
9688

9789
return serverAddresses;
9890
}
9991

100-
protected List<MongoCredential> buildMongoCredentials(String[] credentials) {
101-
List<MongoCredential> mongoCredentials = new ArrayList<MongoCredential>();
92+
List<MongoCredential> buildMongoCredentials(String clientName, List<String> credentials) {
93+
List<MongoCredential> mongoCredentials = new ArrayList<>();
10294

10395
if (credentials != null) {
10496
for (String credential : credentials) {
10597
String[] elements = credential.split(":", 3);
10698
if (elements.length == 3) {
10799
String[] sourceElements = elements[0].split("/", 2);
108100
if (sourceElements.length == 2) {
109-
mongoCredentials.add(buildMongoCredential(elements[1], elements[2], sourceElements[1], sourceElements[0]));
101+
mongoCredentials.add(buildMongoCredential(clientName, elements[1], elements[2], sourceElements[1], sourceElements[0]));
110102
} else if (sourceElements.length == 1) {
111-
mongoCredentials.add(buildMongoCredential(elements[1], elements[2], sourceElements[0], null));
103+
mongoCredentials.add(buildMongoCredential(clientName, elements[1], elements[2], sourceElements[0], null));
112104
} else {
113-
throw SeedException.createNew(MongoDbErrorCodes.INVALID_CREDENTIAL_SYNTAX).put("credential", credential);
105+
throw SeedException.createNew(MongoDbErrorCode.INVALID_CREDENTIAL_SYNTAX)
106+
.put("credential", credential)
107+
.put("clientName", clientName);
114108
}
115109
} else {
116-
throw SeedException.createNew(MongoDbErrorCodes.INVALID_CREDENTIAL_SYNTAX).put("credential", credential);
110+
throw SeedException.createNew(MongoDbErrorCode.INVALID_CREDENTIAL_SYNTAX)
111+
.put("credential", credential)
112+
.put("clientName", clientName);
117113
}
118114
}
119115
}
120116

121117
return mongoCredentials;
122118
}
123119

124-
protected MongoCredential buildMongoCredential(String user, String password, String source, String mechanism) {
120+
MongoCredential buildMongoCredential(String clientName, String user, String password, String source, String mechanism) {
125121
if (mechanism != null) {
126122
AuthenticationMechanism authenticationMechanism = AuthenticationMechanism.fromMechanismName(mechanism);
127123
switch (authenticationMechanism) {
@@ -136,7 +132,9 @@ protected MongoCredential buildMongoCredential(String user, String password, Str
136132
case GSSAPI:
137133
return MongoCredential.createGSSAPICredential(user);
138134
default:
139-
throw SeedException.createNew(MongoDbErrorCodes.UNSUPPORTED_AUTHENTICATION_MECHANISM).put("mechanism", authenticationMechanism.getMechanismName());
135+
throw SeedException.createNew(MongoDbErrorCode.UNSUPPORTED_AUTHENTICATION_MECHANISM)
136+
.put("clientName", clientName)
137+
.put("mechanism", authenticationMechanism.getMechanismName());
140138
}
141139
} else {
142140
return MongoCredential.createCredential(
@@ -147,7 +145,7 @@ protected MongoCredential buildMongoCredential(String user, String password, Str
147145
}
148146
}
149147

150-
protected abstract C doCreateClient(Configuration clientConfiguration);
148+
protected abstract C doCreateClient(String clientName, MongoDbConfig.ClientConfig clientConfiguration, Coffig coffig);
151149

152150
protected abstract D doCreateDatabase(C client, String dbName);
153151

0 commit comments

Comments
 (0)