Skip to content

Commit a86d704

Browse files
committed
DATAMONGO-1158 - Polishing.
MongoFactoryBean, MongoOptionsFactoryBean, MongoClientFactoryBean and MongoClientOptionsFactoryBean now extend AbstractFactoryBean to get a lot of the lifecycle callbacks without further code. Added non-null assertions to newly introduced methods on MongoOperations/MongoTemplate. Moved MongoClientVersion into util package. Introduced static imports for ReflectionUtils and MongoClientVersion for all references in the newly introduced Invoker types. Some formatting, JavaDoc polishes, suppress deprecation warnings. Added build profile for MongoDB Java driver 3.0 as well as the following snapshot. Original pull request: spring-projects#273.
1 parent 57ab27a commit a86d704

35 files changed

+368
-344
lines changed

pom.xml

+10-1
Original file line numberDiff line numberDiff line change
@@ -122,7 +122,16 @@
122122

123123
<profile>
124124

125-
<id>mongo-3-next</id>
125+
<id>mongo3</id>
126+
<properties>
127+
<mongo>3.0.0-beta3</mongo>
128+
</properties>
129+
130+
</profile>
131+
132+
<profile>
133+
134+
<id>mongo3-next</id>
126135
<properties>
127136
<mongo>3.0.0-SNAPSHOT</mongo>
128137
</properties>

spring-data-mongodb/src/main/java/org/springframework/data/mongodb/config/MongoClientParser.java

-1
Original file line numberDiff line numberDiff line change
@@ -82,5 +82,4 @@ public BeanDefinition parse(Element element, ParserContext parserContext) {
8282

8383
return mongoComponent.getBeanDefinition();
8484
}
85-
8685
}

spring-data-mongodb/src/main/java/org/springframework/data/mongodb/config/MongoCredentialPropertyEditor.java

+11-3
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,10 @@ public class MongoCredentialPropertyEditor extends PropertyEditorSupport {
3838
private static final String OPTIONS_DELIMINATOR = "?";
3939
private static final String OPTION_VALUE_DELIMINATOR = "&";
4040

41+
/*
42+
* (non-Javadoc)
43+
* @see java.beans.PropertyEditorSupport#setAsText(java.lang.String)
44+
*/
4145
@Override
4246
public void setAsText(String text) throws IllegalArgumentException {
4347

@@ -46,6 +50,7 @@ public void setAsText(String text) throws IllegalArgumentException {
4650
}
4751

4852
List<MongoCredential> credentials = new ArrayList<MongoCredential>();
53+
4954
for (String credentialString : text.split(",")) {
5055

5156
if (!text.contains(USERNAME_PASSWORD_DELIMINATOR) || !text.contains(DATABASE_DELIMINATOR)) {
@@ -57,8 +62,11 @@ public void setAsText(String text) throws IllegalArgumentException {
5762
Properties options = extractOptions(credentialString);
5863

5964
if (!options.isEmpty()) {
65+
6066
if (options.containsKey(AUTH_MECHANISM_KEY)) {
67+
6168
String authMechanism = options.getProperty(AUTH_MECHANISM_KEY);
69+
6270
if (MongoCredential.GSSAPI_MECHANISM.equals(authMechanism)) {
6371
credentials.add(MongoCredential.createGSSAPICredential(userNameAndPassword[0]));
6472
} else if (MongoCredential.MONGODB_CR_MECHANISM.equals(authMechanism)) {
@@ -86,14 +94,14 @@ public void setAsText(String text) throws IllegalArgumentException {
8694
setValue(credentials);
8795
}
8896

89-
private String[] extractUserNameAndPassword(String text) {
97+
private static String[] extractUserNameAndPassword(String text) {
9098

9199
int dbSeperationIndex = text.lastIndexOf(DATABASE_DELIMINATOR);
92100
String userNameAndPassword = text.substring(0, dbSeperationIndex);
93101
return userNameAndPassword.split(USERNAME_PASSWORD_DELIMINATOR);
94102
}
95103

96-
private String extractDB(String text) {
104+
private static String extractDB(String text) {
97105

98106
int dbSeperationIndex = text.lastIndexOf(DATABASE_DELIMINATOR);
99107

@@ -103,7 +111,7 @@ private String extractDB(String text) {
103111
return optionsSeperationIndex > -1 ? tmp.substring(0, optionsSeperationIndex) : tmp;
104112
}
105113

106-
private Properties extractOptions(String text) {
114+
private static Properties extractOptions(String text) {
107115

108116
int optionsSeperationIndex = text.lastIndexOf(OPTIONS_DELIMINATOR);
109117
int dbSeperationIndex = text.lastIndexOf(OPTIONS_DELIMINATOR);

spring-data-mongodb/src/main/java/org/springframework/data/mongodb/config/MongoParsingUtils.java

+12-7
Original file line numberDiff line numberDiff line change
@@ -37,11 +37,10 @@
3737
* @author Thomas Darimont
3838
* @author Christoph Strobl
3939
*/
40+
@SuppressWarnings("deprecation")
4041
abstract class MongoParsingUtils {
4142

42-
private MongoParsingUtils() {
43-
44-
}
43+
private MongoParsingUtils() {}
4544

4645
/**
4746
* Parses the mongo replica-set element.
@@ -56,12 +55,14 @@ static void parseReplicaSet(Element element, BeanDefinitionBuilder mongoBuilder)
5655
}
5756

5857
/**
59-
* Parses the mongo:options sub-element. Populates the given attribute factory with the proper attributes.
58+
* Parses the {@code mongo:options} sub-element. Populates the given attribute factory with the proper attributes.
6059
*
61-
* @return true if parsing actually occured, false otherwise
60+
* @return true if parsing actually occured, {@literal false} otherwise
6261
*/
6362
static boolean parseMongoOptions(Element element, BeanDefinitionBuilder mongoBuilder) {
63+
6464
Element optionsElement = DomUtils.getChildElementByTagName(element, "options");
65+
6566
if (optionsElement == null) {
6667
return false;
6768
}
@@ -90,14 +91,18 @@ static boolean parseMongoOptions(Element element, BeanDefinitionBuilder mongoBui
9091
}
9192

9293
/**
93-
* @param element
94-
* @param mongoClientBuilder
94+
* Parses the {@code mongo:client-options} sub-element. Populates the given attribute factory with the proper
95+
* attributes.
96+
*
97+
* @param element must not be {@literal null}.
98+
* @param mongoClientBuilder must not be {@literal null}.
9599
* @return
96100
* @since 1.7
97101
*/
98102
public static boolean parseMongoClientOptions(Element element, BeanDefinitionBuilder mongoClientBuilder) {
99103

100104
Element optionsElement = DomUtils.getChildElementByTagName(element, "client-options");
105+
101106
if (optionsElement == null) {
102107
return false;
103108
}

spring-data-mongodb/src/main/java/org/springframework/data/mongodb/config/ReadPreferencePropertyEditor.java

+5
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,10 @@
2727
*/
2828
public class ReadPreferencePropertyEditor extends PropertyEditorSupport {
2929

30+
/*
31+
* (non-Javadoc)
32+
* @see java.beans.PropertyEditorSupport#setAsText(java.lang.String)
33+
*/
3034
@Override
3135
public void setAsText(String readPreferenceString) throws IllegalArgumentException {
3236

@@ -35,6 +39,7 @@ public void setAsText(String readPreferenceString) throws IllegalArgumentExcepti
3539
}
3640

3741
ReadPreference preference = null;
42+
3843
try {
3944
preference = ReadPreference.valueOf(readPreferenceString);
4045
} catch (IllegalArgumentException ex) {

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

+2-2
Original file line numberDiff line numberDiff line change
@@ -52,8 +52,8 @@ public interface IndexOperations {
5252
/**
5353
* Clears all indices that have not yet been applied to this collection.
5454
*
55-
* @deprecated since 1.7. The mongo-java-driver version 3 does no longer support reseting the index cache.
56-
* @throws {@link UnsupportedOperationException} when used with mongo-java-driver version 3.
55+
* @deprecated since 1.7. The MongoDB Java driver version 3.0 does no longer support reseting the index cache.
56+
* @throws {@link UnsupportedOperationException} when used with MongoDB Java driver version 3.0.
5757
*/
5858
@Deprecated
5959
void resetIndexCache();

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

+55-52
Original file line numberDiff line numberDiff line change
@@ -20,9 +20,7 @@
2020
import java.util.Collections;
2121
import java.util.List;
2222

23-
import org.springframework.beans.factory.DisposableBean;
24-
import org.springframework.beans.factory.FactoryBean;
25-
import org.springframework.beans.factory.InitializingBean;
23+
import org.springframework.beans.factory.config.AbstractFactoryBean;
2624
import org.springframework.dao.DataAccessException;
2725
import org.springframework.dao.support.PersistenceExceptionTranslator;
2826
import org.springframework.util.CollectionUtils;
@@ -40,19 +38,17 @@
4038
* @author Christoph Strobl
4139
* @since 1.7
4240
*/
43-
public class MongoClientFactoryBean implements FactoryBean<Mongo>, InitializingBean, DisposableBean,
44-
PersistenceExceptionTranslator {
41+
public class MongoClientFactoryBean extends AbstractFactoryBean<Mongo> implements PersistenceExceptionTranslator {
4542

46-
private MongoClient mongo;
43+
private static final PersistenceExceptionTranslator DEFAULT_EXCEPTION_TRANSLATOR = new MongoExceptionTranslator();
4744

4845
private MongoClientOptions mongoClientOptions;
49-
5046
private String host;
5147
private Integer port;
5248
private List<ServerAddress> replicaSetSeeds;
5349
private List<MongoCredential> credentials;
5450

55-
private PersistenceExceptionTranslator exceptionTranslator = new MongoExceptionTranslator();
51+
private PersistenceExceptionTranslator exceptionTranslator = DEFAULT_EXCEPTION_TRANSLATOR;
5652

5753
/**
5854
* Set the {@link MongoClientOptions} to be used when creating {@link MongoClient}.
@@ -72,52 +68,40 @@ public void setCredentials(MongoCredential[] credentials) {
7268
this.credentials = filterNonNullElementsAsList(credentials);
7369
}
7470

71+
/**
72+
* Set the list of {@link ServerAddress} to build up a replica set for.
73+
*
74+
* @param replicaSetSeeds can be {@literal null}.
75+
*/
7576
public void setReplicaSetSeeds(ServerAddress[] replicaSetSeeds) {
7677
this.replicaSetSeeds = filterNonNullElementsAsList(replicaSetSeeds);
7778
}
7879

7980
/**
80-
* @param elements the elements to filter <T>
81-
* @return a new unmodifiable {@link List#} from the given elements without nulls
81+
* Configures the host to connect to.
82+
*
83+
* @param host
8284
*/
83-
private <T> List<T> filterNonNullElementsAsList(T[] elements) {
84-
85-
if (elements == null) {
86-
return Collections.emptyList();
87-
}
88-
89-
List<T> candidateElements = new ArrayList<T>();
90-
91-
for (T element : elements) {
92-
if (element != null) {
93-
candidateElements.add(element);
94-
}
95-
}
96-
97-
return Collections.unmodifiableList(candidateElements);
98-
}
99-
10085
public void setHost(String host) {
10186
this.host = host;
10287
}
10388

89+
/**
90+
* Configures the port to connect to.
91+
*
92+
* @param port
93+
*/
10494
public void setPort(int port) {
10595
this.port = port;
10696
}
10797

10898
/**
99+
* Configures the {@link PersistenceExceptionTranslator} to use.
100+
*
109101
* @param exceptionTranslator
110102
*/
111103
public void setExceptionTranslator(PersistenceExceptionTranslator exceptionTranslator) {
112-
this.exceptionTranslator = exceptionTranslator;
113-
}
114-
115-
/*
116-
* (non-Javadoc)
117-
* @see org.springframework.beans.factory.FactoryBean#getObject()
118-
*/
119-
public Mongo getObject() throws Exception {
120-
return mongo;
104+
this.exceptionTranslator = exceptionTranslator == null ? DEFAULT_EXCEPTION_TRANSLATOR : exceptionTranslator;
121105
}
122106

123107
/*
@@ -128,14 +112,6 @@ public Class<? extends Mongo> getObjectType() {
128112
return Mongo.class;
129113
}
130114

131-
/*
132-
* (non-Javadoc)
133-
* @see org.springframework.beans.factory.FactoryBean#isSingleton()
134-
*/
135-
public boolean isSingleton() {
136-
return true;
137-
}
138-
139115
/*
140116
* (non-Javadoc)
141117
* @see org.springframework.dao.support.PersistenceExceptionTranslator#translateExceptionIfPossible(java.lang.RuntimeException)
@@ -146,18 +122,29 @@ public DataAccessException translateExceptionIfPossible(RuntimeException ex) {
146122

147123
/*
148124
* (non-Javadoc)
149-
* @see org.springframework.beans.factory.InitializingBean#afterPropertiesSet()
125+
* @see org.springframework.beans.factory.config.AbstractFactoryBean#createInstance()
150126
*/
151-
public void afterPropertiesSet() throws Exception {
127+
@Override
128+
protected Mongo createInstance() throws Exception {
152129

153130
if (mongoClientOptions == null) {
154131
mongoClientOptions = MongoClientOptions.builder().build();
155132
}
133+
156134
if (credentials == null) {
157135
credentials = Collections.emptyList();
158136
}
159137

160-
this.mongo = createMongoClient();
138+
return createMongoClient();
139+
}
140+
141+
/*
142+
* (non-Javadoc)
143+
* @see org.springframework.beans.factory.config.AbstractFactoryBean#destroyInstance(java.lang.Object)
144+
*/
145+
@Override
146+
protected void destroyInstance(Mongo instance) throws Exception {
147+
instance.close();
161148
}
162149

163150
private MongoClient createMongoClient() throws UnknownHostException {
@@ -172,15 +159,31 @@ private MongoClient createMongoClient() throws UnknownHostException {
172159
private ServerAddress createConfiguredOrDefaultServerAddress() throws UnknownHostException {
173160

174161
ServerAddress defaultAddress = new ServerAddress();
162+
175163
return new ServerAddress(StringUtils.hasText(host) ? host : defaultAddress.getHost(),
176164
port != null ? port.intValue() : defaultAddress.getPort());
177165
}
178166

179-
/*
180-
* (non-Javadoc)
181-
* @see org.springframework.beans.factory.DisposableBean#destroy()
167+
/**
168+
* Returns the given array as {@link List} with all {@literal null} elements removed.
169+
*
170+
* @param elements the elements to filter <T>, can be {@literal null}.
171+
* @return a new unmodifiable {@link List#} from the given elements without {@literal null}s.
182172
*/
183-
public void destroy() throws Exception {
184-
this.mongo.close();
173+
private static <T> List<T> filterNonNullElementsAsList(T[] elements) {
174+
175+
if (elements == null) {
176+
return Collections.emptyList();
177+
}
178+
179+
List<T> candidateElements = new ArrayList<T>();
180+
181+
for (T element : elements) {
182+
if (element != null) {
183+
candidateElements.add(element);
184+
}
185+
}
186+
187+
return Collections.unmodifiableList(candidateElements);
185188
}
186189
}

0 commit comments

Comments
 (0)