@@ -39,8 +39,8 @@ public class SimpleMongoDbFactory implements DisposableBean, MongoDbFactory {
39
39
40
40
private final Mongo mongo ;
41
41
private final String databaseName ;
42
- private String username ;
43
- private String password ;
42
+ private final boolean mongoInstanceCreated ;
43
+ private final UserCredentials credentials ;
44
44
private WriteConcern writeConcern ;
45
45
46
46
/**
@@ -50,25 +50,18 @@ public class SimpleMongoDbFactory implements DisposableBean, MongoDbFactory {
50
50
* @param databaseName database name, not be {@literal null}.
51
51
*/
52
52
public SimpleMongoDbFactory (Mongo mongo , String databaseName ) {
53
- Assert .notNull (mongo , "Mongo must not be null" );
54
- Assert .hasText (databaseName , "Database name must not be empty" );
55
- Assert .isTrue (databaseName .matches ("[\\ w-]+" ),
56
- "Database name must only contain letters, numbers, underscores and dashes!" );
57
- this .mongo = mongo ;
58
- this .databaseName = databaseName ;
53
+ this (mongo , databaseName , UserCredentials .NO_CREDENTIALS , false );
59
54
}
60
55
61
56
/**
62
57
* Create an instance of SimpleMongoDbFactory given the Mongo instance, database name, and username/password
63
58
*
64
59
* @param mongo Mongo instance, must not be {@literal null}.
65
60
* @param databaseName Database name, must not be {@literal null}.
66
- * @param userCredentials username and password must not be {@literal null} .
61
+ * @param credentials username and password.
67
62
*/
68
- public SimpleMongoDbFactory (Mongo mongo , String databaseName , UserCredentials userCredentials ) {
69
- this (mongo , databaseName );
70
- this .username = userCredentials .getUsername ();
71
- this .password = userCredentials .getPassword ();
63
+ public SimpleMongoDbFactory (Mongo mongo , String databaseName , UserCredentials credentials ) {
64
+ this (mongo , databaseName , credentials , false );
72
65
}
73
66
74
67
/**
@@ -80,7 +73,21 @@ public SimpleMongoDbFactory(Mongo mongo, String databaseName, UserCredentials us
80
73
* @see MongoURI
81
74
*/
82
75
public SimpleMongoDbFactory (MongoURI uri ) throws MongoException , UnknownHostException {
83
- this (new Mongo (uri ), uri .getDatabase (), new UserCredentials (uri .getUsername (), parseChars (uri .getPassword ())));
76
+ this (new Mongo (uri ), uri .getDatabase (), new UserCredentials (uri .getUsername (), parseChars (uri .getPassword ())), true );
77
+ }
78
+
79
+ private SimpleMongoDbFactory (Mongo mongo , String databaseName , UserCredentials credentials ,
80
+ boolean mongoInstanceCreated ) {
81
+
82
+ Assert .notNull (mongo , "Mongo must not be null" );
83
+ Assert .hasText (databaseName , "Database name must not be empty" );
84
+ Assert .isTrue (databaseName .matches ("[\\ w-]+" ),
85
+ "Database name must only contain letters, numbers, underscores and dashes!" );
86
+
87
+ this .mongo = mongo ;
88
+ this .databaseName = databaseName ;
89
+ this .mongoInstanceCreated = mongoInstanceCreated ;
90
+ this .credentials = credentials == null ? UserCredentials .NO_CREDENTIALS : credentials ;
84
91
}
85
92
86
93
/**
@@ -92,10 +99,6 @@ public void setWriteConcern(WriteConcern writeConcern) {
92
99
this .writeConcern = writeConcern ;
93
100
}
94
101
95
- public WriteConcern getWriteConcern () {
96
- return writeConcern ;
97
- }
98
-
99
102
/*
100
103
* (non-Javadoc)
101
104
* @see org.springframework.data.mongodb.MongoDbFactory#getDb()
@@ -112,7 +115,10 @@ public DB getDb(String dbName) throws DataAccessException {
112
115
113
116
Assert .hasText (dbName , "Database name must not be empty." );
114
117
115
- DB db = MongoDbUtils .getDB (mongo , dbName , username , password == null ? null : password .toCharArray ());
118
+ String username = credentials .getUsername ();
119
+ char [] password = credentials .hasPassword () ? credentials .getPassword ().toCharArray () : null ;
120
+
121
+ DB db = MongoDbUtils .getDB (mongo , dbName , username , password );
116
122
117
123
if (writeConcern != null ) {
118
124
db .setWriteConcern (writeConcern );
@@ -122,17 +128,17 @@ public DB getDb(String dbName) throws DataAccessException {
122
128
}
123
129
124
130
/**
125
- * Clean up the Mongo instance.
131
+ * Clean up the Mongo instance if it was created by the factory itself.
132
+ *
133
+ * @see DisposableBean#destroy()
126
134
*/
127
135
public void destroy () throws Exception {
128
- mongo .close ();
136
+ if (mongoInstanceCreated ) {
137
+ mongo .close ();
138
+ }
129
139
}
130
140
131
- public static String parseChars (char [] chars ) {
132
- if (chars == null ) {
133
- return null ;
134
- } else {
135
- return String .valueOf (chars );
136
- }
141
+ private static String parseChars (char [] chars ) {
142
+ return chars == null ? null : String .valueOf (chars );
137
143
}
138
144
}
0 commit comments