20
20
import java .util .Collections ;
21
21
import java .util .List ;
22
22
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 ;
26
24
import org .springframework .dao .DataAccessException ;
27
25
import org .springframework .dao .support .PersistenceExceptionTranslator ;
28
26
import org .springframework .util .CollectionUtils ;
40
38
* @author Christoph Strobl
41
39
* @since 1.7
42
40
*/
43
- public class MongoClientFactoryBean implements FactoryBean <Mongo >, InitializingBean , DisposableBean ,
44
- PersistenceExceptionTranslator {
41
+ public class MongoClientFactoryBean extends AbstractFactoryBean <Mongo > implements PersistenceExceptionTranslator {
45
42
46
- private MongoClient mongo ;
43
+ private static final PersistenceExceptionTranslator DEFAULT_EXCEPTION_TRANSLATOR = new MongoExceptionTranslator () ;
47
44
48
45
private MongoClientOptions mongoClientOptions ;
49
-
50
46
private String host ;
51
47
private Integer port ;
52
48
private List <ServerAddress > replicaSetSeeds ;
53
49
private List <MongoCredential > credentials ;
54
50
55
- private PersistenceExceptionTranslator exceptionTranslator = new MongoExceptionTranslator () ;
51
+ private PersistenceExceptionTranslator exceptionTranslator = DEFAULT_EXCEPTION_TRANSLATOR ;
56
52
57
53
/**
58
54
* Set the {@link MongoClientOptions} to be used when creating {@link MongoClient}.
@@ -72,52 +68,40 @@ public void setCredentials(MongoCredential[] credentials) {
72
68
this .credentials = filterNonNullElementsAsList (credentials );
73
69
}
74
70
71
+ /**
72
+ * Set the list of {@link ServerAddress} to build up a replica set for.
73
+ *
74
+ * @param replicaSetSeeds can be {@literal null}.
75
+ */
75
76
public void setReplicaSetSeeds (ServerAddress [] replicaSetSeeds ) {
76
77
this .replicaSetSeeds = filterNonNullElementsAsList (replicaSetSeeds );
77
78
}
78
79
79
80
/**
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
82
84
*/
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
-
100
85
public void setHost (String host ) {
101
86
this .host = host ;
102
87
}
103
88
89
+ /**
90
+ * Configures the port to connect to.
91
+ *
92
+ * @param port
93
+ */
104
94
public void setPort (int port ) {
105
95
this .port = port ;
106
96
}
107
97
108
98
/**
99
+ * Configures the {@link PersistenceExceptionTranslator} to use.
100
+ *
109
101
* @param exceptionTranslator
110
102
*/
111
103
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 ;
121
105
}
122
106
123
107
/*
@@ -128,14 +112,6 @@ public Class<? extends Mongo> getObjectType() {
128
112
return Mongo .class ;
129
113
}
130
114
131
- /*
132
- * (non-Javadoc)
133
- * @see org.springframework.beans.factory.FactoryBean#isSingleton()
134
- */
135
- public boolean isSingleton () {
136
- return true ;
137
- }
138
-
139
115
/*
140
116
* (non-Javadoc)
141
117
* @see org.springframework.dao.support.PersistenceExceptionTranslator#translateExceptionIfPossible(java.lang.RuntimeException)
@@ -146,18 +122,29 @@ public DataAccessException translateExceptionIfPossible(RuntimeException ex) {
146
122
147
123
/*
148
124
* (non-Javadoc)
149
- * @see org.springframework.beans.factory.InitializingBean#afterPropertiesSet ()
125
+ * @see org.springframework.beans.factory.config.AbstractFactoryBean#createInstance ()
150
126
*/
151
- public void afterPropertiesSet () throws Exception {
127
+ @ Override
128
+ protected Mongo createInstance () throws Exception {
152
129
153
130
if (mongoClientOptions == null ) {
154
131
mongoClientOptions = MongoClientOptions .builder ().build ();
155
132
}
133
+
156
134
if (credentials == null ) {
157
135
credentials = Collections .emptyList ();
158
136
}
159
137
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 ();
161
148
}
162
149
163
150
private MongoClient createMongoClient () throws UnknownHostException {
@@ -172,15 +159,31 @@ private MongoClient createMongoClient() throws UnknownHostException {
172
159
private ServerAddress createConfiguredOrDefaultServerAddress () throws UnknownHostException {
173
160
174
161
ServerAddress defaultAddress = new ServerAddress ();
162
+
175
163
return new ServerAddress (StringUtils .hasText (host ) ? host : defaultAddress .getHost (),
176
164
port != null ? port .intValue () : defaultAddress .getPort ());
177
165
}
178
166
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.
182
172
*/
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 );
185
188
}
186
189
}
0 commit comments