Skip to content

Commit 3ec7901

Browse files
DATAMONGO-1158 - Polishing Round I
* protected reflective invokers by moving them around. * undo static imports. * add usage notes to reference documentation.
1 parent 7c1f8b7 commit 3ec7901

25 files changed

+253
-137
lines changed

spring-data-mongodb/src/main/java/org/springframework/data/mongodb/MongoClientVersion.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
*/
1616
package org.springframework.data.mongodb;
1717

18-
import static org.springframework.util.ClassUtils.*;
18+
import org.springframework.util.ClassUtils;
1919

2020
/**
2121
* {@link MongoClientVersion} holds information about the used mongo-java client and is used to distinguish between
@@ -26,9 +26,9 @@
2626
*/
2727
public class MongoClientVersion {
2828

29-
private static final boolean IS_MONGO_30 = isPresent("com.mongodb.binding.SingleServerBinding",
29+
private static final boolean IS_MONGO_30 = ClassUtils.isPresent("com.mongodb.binding.SingleServerBinding",
3030
MongoClientVersion.class.getClassLoader());
31-
private static final boolean IS_ASYNC_CLIENT = isPresent("com.mongodb.async.client.MongoClient",
31+
private static final boolean IS_ASYNC_CLIENT = ClassUtils.isPresent("com.mongodb.async.client.MongoClient",
3232
MongoClientVersion.class.getClassLoader());
3333

3434
/**

spring-data-mongodb/src/main/java/org/springframework/data/mongodb/core/DefaultIndexOperations.java

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,6 @@
2323
import java.util.List;
2424

2525
import org.springframework.dao.DataAccessException;
26-
import org.springframework.data.mongodb.ReflectiveDBCollectionInvoker;
2726
import org.springframework.data.mongodb.core.index.IndexDefinition;
2827
import org.springframework.data.mongodb.core.index.IndexField;
2928
import org.springframework.data.mongodb.core.index.IndexInfo;

spring-data-mongodb/src/main/java/org/springframework/data/mongodb/core/MongoDbUtils.java

Lines changed: 9 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -15,14 +15,12 @@
1515
*/
1616
package org.springframework.data.mongodb.core;
1717

18-
import static org.springframework.data.mongodb.MongoClientVersion.*;
19-
import static org.springframework.data.mongodb.ReflectiveDbInvoker.*;
20-
import static org.springframework.util.Assert.*;
21-
2218
import org.slf4j.Logger;
2319
import org.slf4j.LoggerFactory;
2420
import org.springframework.data.authentication.UserCredentials;
21+
import org.springframework.data.mongodb.MongoClientVersion;
2522
import org.springframework.transaction.support.TransactionSynchronizationManager;
23+
import org.springframework.util.Assert;
2624

2725
import com.mongodb.DB;
2826
import com.mongodb.Mongo;
@@ -90,10 +88,10 @@ public static DB getDB(Mongo mongo, String databaseName, UserCredentials credent
9088
public static DB getDB(Mongo mongo, String databaseName, UserCredentials credentials,
9189
String authenticationDatabaseName) {
9290

93-
notNull(mongo, "No Mongo instance specified!");
94-
hasText(databaseName, "Database name must be given!");
95-
notNull(credentials, "Credentials must not be null, use UserCredentials.NO_CREDENTIALS!");
96-
hasText(authenticationDatabaseName, "Authentication database name must not be null or empty!");
91+
Assert.notNull(mongo, "No Mongo instance specified!");
92+
Assert.hasText(databaseName, "Database name must be given!");
93+
Assert.notNull(credentials, "Credentials must not be null, use UserCredentials.NO_CREDENTIALS!");
94+
Assert.hasText(authenticationDatabaseName, "Authentication database name must not be null or empty!");
9795

9896
return doGetDB(mongo, databaseName, credentials, true, authenticationDatabaseName);
9997
}
@@ -128,7 +126,7 @@ private static DB doGetDB(Mongo mongo, String databaseName, UserCredentials cred
128126
DB db = mongo.getDB(databaseName);
129127

130128
if (requiresAuthDbAuthentication(credentials)) {
131-
authenticate(mongo, db, credentials, authenticationDatabaseName);
129+
ReflectiveDbInvoker.authenticate(mongo, db, credentials, authenticationDatabaseName);
132130
}
133131

134132
// TX sync active, bind new database to thread
@@ -195,7 +193,7 @@ public static void closeDB(DB db) {
195193
if (db != null) {
196194
LOGGER.debug("Closing Mongo DB object");
197195
try {
198-
requestDone(db);
196+
ReflectiveDbInvoker.requestDone(db);
199197
} catch (Throwable ex) {
200198
LOGGER.debug("Unexpected exception on closing Mongo DB object", ex);
201199
}
@@ -215,7 +213,7 @@ private static boolean requiresAuthDbAuthentication(UserCredentials credentials)
215213
return false;
216214
}
217215

218-
return isMongo3Driver() ? false : true;
216+
return !MongoClientVersion.isMongo3Driver();
219217
}
220218

221219
}

spring-data-mongodb/src/main/java/org/springframework/data/mongodb/core/MongoExceptionTranslator.java

Lines changed: 12 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -15,9 +15,7 @@
1515
*/
1616
package org.springframework.data.mongodb.core;
1717

18-
import static java.util.Arrays.*;
19-
import static org.springframework.util.ClassUtils.*;
20-
18+
import java.util.Arrays;
2119
import java.util.HashSet;
2220
import java.util.Set;
2321

@@ -29,6 +27,7 @@
2927
import org.springframework.dao.InvalidDataAccessResourceUsageException;
3028
import org.springframework.dao.support.PersistenceExceptionTranslator;
3129
import org.springframework.data.mongodb.UncategorizedMongoDbException;
30+
import org.springframework.util.ClassUtils;
3231

3332
import com.mongodb.MongoException;
3433

@@ -43,16 +42,18 @@
4342
*/
4443
public class MongoExceptionTranslator implements PersistenceExceptionTranslator {
4544

46-
private static final Set<String> DULICATE_KEY_EXCEPTIONS = new HashSet<String>(asList("MongoException.DuplicateKey",
47-
"DuplicateKeyException"));
45+
private static final Set<String> DULICATE_KEY_EXCEPTIONS = new HashSet<String>(Arrays.asList(
46+
"MongoException.DuplicateKey", "DuplicateKeyException"));
4847

49-
private static final Set<String> RESOURCE_FAILURE_EXCEPTIONS = new HashSet<String>(asList("MongoException.Network",
50-
"MongoSocketException", "MongoException.CursorNotFound", "MongoCursorNotFoundException",
51-
"MongoServerSelectionException", "MongoTimeoutException"));
48+
private static final Set<String> RESOURCE_FAILURE_EXCEPTIONS = new HashSet<String>(Arrays.asList(
49+
"MongoException.Network", "MongoSocketException", "MongoException.CursorNotFound",
50+
"MongoCursorNotFoundException", "MongoServerSelectionException", "MongoTimeoutException"));
5251

53-
private static final Set<String> RESOURCE_USAGE_EXCEPTIONS = new HashSet<String>(asList("MongoInternalException"));
52+
private static final Set<String> RESOURCE_USAGE_EXCEPTIONS = new HashSet<String>(
53+
Arrays.asList("MongoInternalException"));
5454

55-
private static final Set<String> DATA_INTEGRETY_EXCEPTIONS = new HashSet<String>(asList("WriteConcernException"));
55+
private static final Set<String> DATA_INTEGRETY_EXCEPTIONS = new HashSet<String>(
56+
Arrays.asList("WriteConcernException"));
5657

5758
/*
5859
* (non-Javadoc)
@@ -62,7 +63,7 @@ public DataAccessException translateExceptionIfPossible(RuntimeException ex) {
6263

6364
// Check for well-known MongoException subclasses.
6465

65-
String exception = getShortName(getUserClass(ex.getClass()));
66+
String exception = ClassUtils.getShortName(ClassUtils.getUserClass(ex.getClass()));
6667

6768
if (DULICATE_KEY_EXCEPTIONS.contains(exception)) {
6869
return new DuplicateKeyException(ex.getMessage(), ex);

spring-data-mongodb/src/main/java/org/springframework/data/mongodb/core/MongoOptionsFactoryBean.java

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -15,14 +15,11 @@
1515
*/
1616
package org.springframework.data.mongodb.core;
1717

18-
import static org.springframework.data.mongodb.MongoClientVersion.*;
19-
import static org.springframework.data.mongodb.ReflectiveMongoOptionsInvoker.*;
20-
2118
import javax.net.ssl.SSLSocketFactory;
2219

2320
import org.springframework.beans.factory.FactoryBean;
2421
import org.springframework.beans.factory.InitializingBean;
25-
import org.springframework.data.mongodb.ReflectiveMongoOptionsInvoker;
22+
import org.springframework.data.mongodb.MongoClientVersion;
2623

2724
import com.mongodb.MongoOptions;
2825

@@ -52,9 +49,12 @@ public class MongoOptionsFactoryBean implements FactoryBean<MongoOptions>, Initi
5249
private int writeTimeout = DEFAULT_MONGO_OPTIONS.getWtimeout();
5350
private boolean writeFsync = DEFAULT_MONGO_OPTIONS.isFsync();
5451

55-
private boolean autoConnectRetry = !isMongo3Driver() ? getAutoConnectRetry(DEFAULT_MONGO_OPTIONS) : false;
56-
private long maxAutoConnectRetryTime = !isMongo3Driver() ? getMaxAutoConnectRetryTime(DEFAULT_MONGO_OPTIONS) : -1;
57-
private boolean slaveOk = !isMongo3Driver() ? getSlaveOk(DEFAULT_MONGO_OPTIONS) : false;
52+
private boolean autoConnectRetry = !MongoClientVersion.isMongo3Driver() ? ReflectiveMongoOptionsInvoker
53+
.getAutoConnectRetry(DEFAULT_MONGO_OPTIONS) : false;
54+
private long maxAutoConnectRetryTime = !MongoClientVersion.isMongo3Driver() ? ReflectiveMongoOptionsInvoker
55+
.getMaxAutoConnectRetryTime(DEFAULT_MONGO_OPTIONS) : -1;
56+
private boolean slaveOk = !MongoClientVersion.isMongo3Driver() ? ReflectiveMongoOptionsInvoker
57+
.getSlaveOk(DEFAULT_MONGO_OPTIONS) : false;
5858

5959
private boolean ssl;
6060
private SSLSocketFactory sslSocketFactory;
@@ -233,7 +233,7 @@ public void afterPropertiesSet() {
233233
options.setSocketFactory(sslSocketFactory != null ? sslSocketFactory : SSLSocketFactory.getDefault());
234234
}
235235

236-
if (!isMongo3Driver()) {
236+
if (!MongoClientVersion.isMongo3Driver()) {
237237

238238
ReflectiveMongoOptionsInvoker.setAutoConnectRetry(options, autoConnectRetry);
239239
ReflectiveMongoOptionsInvoker.setMaxAutoConnectRetryTime(options, maxAutoConnectRetryTime);

spring-data-mongodb/src/main/java/org/springframework/data/mongodb/core/MongoTemplate.java

Lines changed: 11 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -15,14 +15,8 @@
1515
*/
1616
package org.springframework.data.mongodb.core;
1717

18-
import static com.mongodb.ReadPreference.*;
19-
import static org.springframework.data.mongodb.MongoClientVersion.*;
20-
import static org.springframework.data.mongodb.ReflectiveDbInvoker.*;
21-
import static org.springframework.data.mongodb.ReflectiveMapReduceInvoker.*;
22-
import static org.springframework.data.mongodb.ReflectiveWriteResultInvoker.*;
2318
import static org.springframework.data.mongodb.core.query.Criteria.*;
2419
import static org.springframework.data.mongodb.core.query.SerializationUtils.*;
25-
import static org.springframework.util.ObjectUtils.*;
2620

2721
import java.io.IOException;
2822
import java.util.ArrayList;
@@ -65,6 +59,7 @@
6559
import org.springframework.data.mapping.context.MappingContext;
6660
import org.springframework.data.mapping.model.ConvertingPropertyAccessor;
6761
import org.springframework.data.mapping.model.MappingException;
62+
import org.springframework.data.mongodb.MongoClientVersion;
6863
import org.springframework.data.mongodb.MongoDbFactory;
6964
import org.springframework.data.mongodb.core.aggregation.Aggregation;
7065
import org.springframework.data.mongodb.core.aggregation.AggregationOperationContext;
@@ -104,6 +99,7 @@
10499
import org.springframework.jca.cci.core.ConnectionCallback;
105100
import org.springframework.util.Assert;
106101
import org.springframework.util.CollectionUtils;
102+
import org.springframework.util.ObjectUtils;
107103
import org.springframework.util.ResourceUtils;
108104
import org.springframework.util.StringUtils;
109105

@@ -345,7 +341,8 @@ public CommandResult doInDB(DB db) throws MongoException, DataAccessException {
345341
*/
346342
@Deprecated
347343
public CommandResult executeCommand(final DBObject command, final int options) {
348-
return executeCommand(command, (options & Bytes.QUERYOPTION_SLAVEOK) != 0 ? secondaryPreferred() : primary());
344+
return executeCommand(command, (options & Bytes.QUERYOPTION_SLAVEOK) != 0 ? ReadPreference.secondaryPreferred()
345+
: ReadPreference.primary());
349346
}
350347

351348
/*
@@ -444,10 +441,10 @@ public <T> T executeInSession(final DbCallback<T> action) {
444441
return execute(new DbCallback<T>() {
445442
public T doInDB(DB db) throws MongoException, DataAccessException {
446443
try {
447-
requestStart(db);
444+
ReflectiveDbInvoker.requestStart(db);
448445
return action.doInDB(db);
449446
} finally {
450-
requestDone(db);
447+
ReflectiveDbInvoker.requestDone(db);
451448
}
452449
}
453450
});
@@ -736,7 +733,8 @@ protected WriteConcern prepareWriteConcern(MongoAction mongoAction) {
736733

737734
WriteConcern wc = writeConcernResolver.resolve(mongoAction);
738735

739-
if (isMongo3Driver() && nullSafeEquals(WriteResultChecking.EXCEPTION, writeResultChecking)
736+
if (MongoClientVersion.isMongo3Driver()
737+
&& ObjectUtils.nullSafeEquals(WriteResultChecking.EXCEPTION, writeResultChecking)
740738
&& (wc == null || wc.getW() < 1)) {
741739
return WriteConcern.ACKNOWLEDGED;
742740
}
@@ -1061,7 +1059,7 @@ public WriteResult doInCollection(DBCollection collection) throws MongoException
10611059
: collection.update(queryObj, updateObj, upsert, multi, writeConcernToUse);
10621060

10631061
if (entity != null && entity.hasVersionProperty() && !multi) {
1064-
if (wasAcknowledged(writeResult) && writeResult.getN() == 0
1062+
if (ReflectiveWriteResultInvoker.wasAcknowledged(writeResult) && writeResult.getN() == 0
10651063
&& dbObjectContainsVersionProperty(queryObj, entity)) {
10661064
throw new OptimisticLockingFailureException("Optimistic lock exception on saving entity: "
10671065
+ updateObj.toMap().toString() + " to collection " + collectionName);
@@ -1532,7 +1530,7 @@ private void copyMapReduceOptionsToCommand(Query query, MapReduceOptions mapRedu
15321530
}
15331531
if (!mapReduceOptions.getExtraOptions().isEmpty()) {
15341532
for (Map.Entry<String, Object> entry : mapReduceOptions.getExtraOptions().entrySet()) {
1535-
addExtraOption(mapReduceCommand, entry.getKey(), entry.getValue());
1533+
ReflectiveMapReduceInvoker.addExtraOption(mapReduceCommand, entry.getKey(), entry.getValue());
15361534
}
15371535
}
15381536
if (mapReduceOptions.getFinalizeFunction() != null) {
@@ -1924,7 +1922,7 @@ protected void handleAnyWriteResultErrors(WriteResult writeResult, DBObject quer
19241922
return;
19251923
}
19261924

1927-
String error = getError(writeResult);
1925+
String error = ReflectiveWriteResultInvoker.getError(writeResult);
19281926

19291927
if (error == null) {
19301928
return;

spring-data-mongodb/src/main/java/org/springframework/data/mongodb/ReflectiveDBCollectionInvoker.java renamed to spring-data-mongodb/src/main/java/org/springframework/data/mongodb/core/ReflectiveDBCollectionInvoker.java

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,13 +13,15 @@
1313
* See the License for the specific language governing permissions and
1414
* limitations under the License.
1515
*/
16-
package org.springframework.data.mongodb;
16+
package org.springframework.data.mongodb.core;
1717

1818
import static org.springframework.data.mongodb.MongoClientVersion.*;
1919
import static org.springframework.util.ReflectionUtils.*;
2020

2121
import java.lang.reflect.Method;
2222

23+
import org.springframework.data.mongodb.MongoClientVersion;
24+
2325
import com.mongodb.DBCollection;
2426
import com.mongodb.DBObject;
2527

@@ -30,7 +32,7 @@
3032
* @author Christoph Strobl
3133
* @since 1.7
3234
*/
33-
public class ReflectiveDBCollectionInvoker {
35+
class ReflectiveDBCollectionInvoker {
3436

3537
private static final Method GEN_INDEX_NAME_METHOD;
3638
private static final Method RESET_INDEX_CHACHE_METHOD;

spring-data-mongodb/src/main/java/org/springframework/data/mongodb/ReflectiveDbInvoker.java renamed to spring-data-mongodb/src/main/java/org/springframework/data/mongodb/core/ReflectiveDbInvoker.java

Lines changed: 17 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -13,14 +13,14 @@
1313
* See the License for the specific language governing permissions and
1414
* limitations under the License.
1515
*/
16-
package org.springframework.data.mongodb;
17-
18-
import static org.springframework.data.mongodb.MongoClientVersion.*;
19-
import static org.springframework.util.ReflectionUtils.*;
16+
package org.springframework.data.mongodb.core;
2017

2118
import java.lang.reflect.Method;
2219

2320
import org.springframework.data.authentication.UserCredentials;
21+
import org.springframework.data.mongodb.CannotGetMongoDbConnectionException;
22+
import org.springframework.data.mongodb.MongoClientVersion;
23+
import org.springframework.util.ReflectionUtils;
2424

2525
import com.mongodb.DB;
2626
import com.mongodb.Mongo;
@@ -32,7 +32,7 @@
3232
* @author Christoph Strobl
3333
* @since 1.7
3434
*/
35-
public final class ReflectiveDbInvoker {
35+
final class ReflectiveDbInvoker {
3636

3737
private static final Method DB_IS_AUTHENTICATED_METHOD;
3838
private static final Method DB_AUTHENTICATE_METHOD;
@@ -42,11 +42,11 @@ public final class ReflectiveDbInvoker {
4242

4343
static {
4444

45-
DB_IS_AUTHENTICATED_METHOD = findMethod(DB.class, "isAuthenticated");
46-
DB_AUTHENTICATE_METHOD = findMethod(DB.class, "authenticate", String.class, char[].class);
47-
DB_REQUEST_DONE_METHOD = findMethod(DB.class, "requestDone");
48-
DB_ADD_USER_METHOD = findMethod(DB.class, "addUser", String.class, char[].class);
49-
DB_REQUEST_START_METHOD = findMethod(DB.class, "requestStart");
45+
DB_IS_AUTHENTICATED_METHOD = ReflectionUtils.findMethod(DB.class, "isAuthenticated");
46+
DB_AUTHENTICATE_METHOD = ReflectionUtils.findMethod(DB.class, "authenticate", String.class, char[].class);
47+
DB_REQUEST_DONE_METHOD = ReflectionUtils.findMethod(DB.class, "requestDone");
48+
DB_ADD_USER_METHOD = ReflectionUtils.findMethod(DB.class, "addUser", String.class, char[].class);
49+
DB_REQUEST_START_METHOD = ReflectionUtils.findMethod(DB.class, "requestStart");
5050
}
5151

5252
private ReflectiveDbInvoker() {}
@@ -67,13 +67,13 @@ public static void authenticate(Mongo mongo, DB db, UserCredentials credentials,
6767

6868
synchronized (authDb) {
6969

70-
Boolean isAuthenticated = (Boolean) invokeMethod(DB_IS_AUTHENTICATED_METHOD, authDb);
70+
Boolean isAuthenticated = (Boolean) ReflectionUtils.invokeMethod(DB_IS_AUTHENTICATED_METHOD, authDb);
7171
if (!isAuthenticated) {
7272

7373
String username = credentials.getUsername();
7474
String password = credentials.hasPassword() ? credentials.getPassword() : null;
7575

76-
Boolean authenticated = (Boolean) invokeMethod(DB_AUTHENTICATE_METHOD, authDb, username,
76+
Boolean authenticated = (Boolean) ReflectionUtils.invokeMethod(DB_AUTHENTICATE_METHOD, authDb, username,
7777
password == null ? null : password.toCharArray());
7878
if (!authenticated) {
7979
throw new CannotGetMongoDbConnectionException("Failed to authenticate to database [" + databaseName + "], "
@@ -91,11 +91,11 @@ public static void authenticate(Mongo mongo, DB db, UserCredentials credentials,
9191
*/
9292
public static void requestStart(DB db) {
9393

94-
if (isMongo3Driver()) {
94+
if (MongoClientVersion.isMongo3Driver()) {
9595
return;
9696
}
9797

98-
invokeMethod(DB_REQUEST_START_METHOD, db);
98+
ReflectionUtils.invokeMethod(DB_REQUEST_START_METHOD, db);
9999
}
100100

101101
/**
@@ -106,11 +106,11 @@ public static void requestStart(DB db) {
106106
*/
107107
public static void requestDone(DB db) {
108108

109-
if (isMongo3Driver()) {
109+
if (MongoClientVersion.isMongo3Driver()) {
110110
return;
111111
}
112112

113-
invokeMethod(DB_REQUEST_DONE_METHOD, db);
113+
ReflectionUtils.invokeMethod(DB_REQUEST_DONE_METHOD, db);
114114
}
115115

116116
/**
@@ -125,7 +125,7 @@ public static void addUser(DB db, String username, char[] password) {
125125
throw new UnsupportedOperationException("Please DB.command to call either the createUser or updateUser command");
126126
}
127127

128-
invokeMethod(DB_ADD_USER_METHOD, db, username, password);
128+
ReflectionUtils.invokeMethod(DB_ADD_USER_METHOD, db, username, password);
129129
}
130130

131131
}

0 commit comments

Comments
 (0)