Skip to content

Commit 674af06

Browse files
committed
Take aliases into account when checking configuration
1 parent ef9fcdb commit 674af06

File tree

2 files changed

+37
-13
lines changed

2 files changed

+37
-13
lines changed

morphia/src/main/java/org/seedstack/mongodb/morphia/internal/MorphiaDatastoreImpl.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ class MorphiaDatastoreImpl implements MorphiaDatastore, Serializable {
2121
private String clientName;
2222
private String dbName;
2323

24-
public MorphiaDatastoreImpl(String clientName, String dbName) {
24+
MorphiaDatastoreImpl(String clientName, String dbName) {
2525
this.clientName = clientName;
2626
this.dbName = dbName;
2727
}

morphia/src/main/java/org/seedstack/mongodb/morphia/internal/MorphiaUtils.java

Lines changed: 36 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,6 @@
1212
import org.seedstack.seed.Application;
1313
import org.seedstack.seed.SeedException;
1414

15-
import java.util.Arrays;
16-
1715
public final class MorphiaUtils {
1816
private MorphiaUtils() {
1917

@@ -29,39 +27,65 @@ private MorphiaUtils() {
2927
public static MorphiaDatastore getMongoDatastore(Application application, Class<?> morphiaClass) {
3028
Configuration morphiaEntityConfiguration = application.getConfiguration(morphiaClass).subset("morphia");
3129
if (morphiaEntityConfiguration.isEmpty()) {
32-
throw SeedException.createNew(MorphiaErrorCodes.UNKNOW_DATASTORE_CONFIGURATION).put("aggregate",
33-
morphiaClass.getName());
30+
throw SeedException.createNew(MorphiaErrorCodes.UNKNOW_DATASTORE_CONFIGURATION)
31+
.put("aggregate", morphiaClass.getName());
3432
}
33+
3534
String clientName = morphiaEntityConfiguration.getString("clientName");
3635
String dbName = morphiaEntityConfiguration.getString("dbName");
3736
if (clientName == null) {
3837
throw SeedException.createNew(MorphiaErrorCodes.UNKNOW_DATASTORE_CLIENT)
39-
.put("aggregate", morphiaClass.getName()).put("clientName", clientName);
38+
.put("aggregate", morphiaClass.getName())
39+
.put("clientName", clientName);
4040
}
4141
if (dbName == null) {
4242
throw SeedException.createNew(MorphiaErrorCodes.UNKNOW_DATASTORE_DATABASE)
43-
.put("aggregate", morphiaClass.getName()).put("clientName", clientName).put("dbName", dbName);
43+
.put("aggregate", morphiaClass.getName())
44+
.put("clientName", clientName)
45+
.put("dbName", dbName);
4446
}
47+
4548
checkMongoClient(application.getConfiguration(), morphiaClass, clientName, dbName);
46-
MorphiaDatastore morphiaDatastore = new MorphiaDatastoreImpl(clientName, dbName);
47-
return morphiaDatastore;
49+
50+
return new MorphiaDatastoreImpl(clientName, dbName);
4851
}
4952

5053
private static void checkMongoClient(Configuration configuration, Class<?> mappedClass, String clientName, String dbName) {
5154
Configuration configurationClientMongodb = configuration.subset(MongoDbPlugin.CONFIGURATION_PREFIX + ".client." + clientName);
5255
if (configurationClientMongodb.isEmpty()) {
5356
throw SeedException.createNew(MongoDbErrorCodes.UNKNOWN_CLIENT_SPECIFIED)
54-
.put("aggregate", mappedClass.getName()).put("clientName", clientName).put("dbName", dbName);
57+
.put("aggregate", mappedClass.getName())
58+
.put("clientName", clientName)
59+
.put("dbName", dbName);
5560
}
61+
5662
boolean async = configurationClientMongodb.getBoolean("async", false);
5763
if (async) {
5864
throw SeedException.createNew(MorphiaErrorCodes.ERROR_ASYNC_CLIENT)
59-
.put("aggregate", mappedClass.getName()).put("clientName", clientName).put("dbName", dbName);
65+
.put("aggregate", mappedClass.getName())
66+
.put("clientName", clientName)
67+
.put("dbName", dbName);
6068
}
69+
6170
String[] dbNames = configurationClientMongodb.getStringArray("databases");
62-
if (dbNames != null && dbNames.length > 0 && !Arrays.asList(dbNames).contains(dbName)) {
71+
if (!dbOrAliasExists(dbName, configurationClientMongodb, dbNames)) {
6372
throw SeedException.createNew(MorphiaErrorCodes.UNKNOW_DATABASE_NAME)
64-
.put("aggregate", mappedClass.getName()).put("clientName", clientName).put("dbName", dbName);
73+
.put("aggregate", mappedClass.getName())
74+
.put("clientName", clientName)
75+
.put("dbName", dbName);
76+
}
77+
}
78+
79+
private static boolean dbOrAliasExists(String dbName, Configuration configurationClientMongodb, String[] dbNames) {
80+
boolean foundDb = false;
81+
if (dbNames != null) {
82+
for (String name : dbNames) {
83+
if (dbName.equals(configurationClientMongodb.getString(String.format("alias.%s", name), name))) {
84+
foundDb = true;
85+
break;
86+
}
87+
}
6588
}
89+
return foundDb;
6690
}
6791
}

0 commit comments

Comments
 (0)