|
50 | 50 | import com.mongodb.operation.AsyncReadOperation;
|
51 | 51 | import com.mongodb.operation.AsyncWriteOperation;
|
52 | 52 | import com.mongodb.operation.BatchCursor;
|
| 53 | +import com.mongodb.operation.CommandReadOperation; |
53 | 54 | import com.mongodb.operation.CommandWriteOperation;
|
54 | 55 | import com.mongodb.operation.DropDatabaseOperation;
|
55 | 56 | import com.mongodb.operation.ReadOperation;
|
@@ -98,10 +99,6 @@ public final class ClusterFixture {
|
98 | 99 | private static Map<ReadPreference, AsyncReadWriteBinding> asyncBindingMap = new HashMap<ReadPreference, AsyncReadWriteBinding>();
|
99 | 100 |
|
100 | 101 | static {
|
101 |
| - String mongoURIProperty = System.getProperty(MONGODB_URI_SYSTEM_PROPERTY_NAME); |
102 |
| - String mongoURIString = mongoURIProperty == null || mongoURIProperty.isEmpty() |
103 |
| - ? DEFAULT_URI : mongoURIProperty; |
104 |
| - connectionString = new ConnectionString(mongoURIString); |
105 | 102 | Runtime.getRuntime().addShutdownHook(new ShutdownHook());
|
106 | 103 | }
|
107 | 104 |
|
@@ -188,7 +185,37 @@ public void run() {
|
188 | 185 | }
|
189 | 186 |
|
190 | 187 | public static synchronized ConnectionString getConnectionString() {
|
191 |
| - return connectionString; |
| 188 | + if (connectionString != null) { |
| 189 | + return connectionString; |
| 190 | + } |
| 191 | + |
| 192 | + String mongoURIProperty = System.getProperty(MONGODB_URI_SYSTEM_PROPERTY_NAME); |
| 193 | + if (mongoURIProperty != null && !mongoURIProperty.isEmpty()) { |
| 194 | + connectionString = new ConnectionString(mongoURIProperty); |
| 195 | + return connectionString; |
| 196 | + } |
| 197 | + |
| 198 | + // Figure out what the connection string should be |
| 199 | + Cluster cluster = createCluster(new ConnectionString(DEFAULT_URI), |
| 200 | + new SocketStreamFactory(SocketSettings.builder().build(), SslSettings.builder().build())); |
| 201 | + try { |
| 202 | + BsonDocument isMasterResult = new CommandReadOperation<BsonDocument>("admin", |
| 203 | + new BsonDocument("ismaster", new BsonInt32(1)), new BsonDocumentCodec()).execute(new ClusterBinding(cluster, |
| 204 | + ReadPreference.nearest(), ReadConcern.DEFAULT)); |
| 205 | + if (isMasterResult.containsKey("setName")) { |
| 206 | + connectionString = new ConnectionString(DEFAULT_URI + "/?replicaSet=" |
| 207 | + + isMasterResult.getString("setName").getValue()); |
| 208 | + } else { |
| 209 | + connectionString = new ConnectionString(DEFAULT_URI); |
| 210 | + ClusterFixture.cluster = cluster; |
| 211 | + } |
| 212 | + |
| 213 | + return connectionString; |
| 214 | + } finally { |
| 215 | + if (ClusterFixture.cluster == null) { |
| 216 | + cluster.close(); |
| 217 | + } |
| 218 | + } |
192 | 219 | }
|
193 | 220 |
|
194 | 221 | public static ReadWriteBinding getBinding(final Cluster cluster) {
|
@@ -263,15 +290,19 @@ public static synchronized Cluster getAsyncCluster() {
|
263 | 290 | return asyncCluster;
|
264 | 291 | }
|
265 | 292 |
|
266 |
| - @SuppressWarnings("deprecation") |
267 | 293 | public static Cluster createCluster(final StreamFactory streamFactory) {
|
268 |
| - return new DefaultClusterFactory().createCluster(ClusterSettings.builder().applyConnectionString(getConnectionString()).build(), |
| 294 | + return createCluster(getConnectionString(), streamFactory); |
| 295 | + } |
| 296 | + |
| 297 | + @SuppressWarnings("deprecation") |
| 298 | + private static Cluster createCluster(final ConnectionString connectionString, final StreamFactory streamFactory) { |
| 299 | + return new DefaultClusterFactory().createCluster(ClusterSettings.builder().applyConnectionString(connectionString).build(), |
269 | 300 | ServerSettings.builder().build(),
|
270 |
| - ConnectionPoolSettings.builder().applyConnectionString(getConnectionString()).build(), |
| 301 | + ConnectionPoolSettings.builder().applyConnectionString(connectionString).build(), |
271 | 302 | streamFactory,
|
272 |
| - new SocketStreamFactory(SocketSettings.builder().build(), getSslSettings()), |
273 |
| - getConnectionString().getCredentialList(), null, null, null, |
274 |
| - getConnectionString().getCompressorList()); |
| 303 | + new SocketStreamFactory(SocketSettings.builder().build(), getSslSettings(connectionString)), |
| 304 | + connectionString.getCredentialList(), null, null, null, |
| 305 | + connectionString.getCompressorList()); |
275 | 306 | }
|
276 | 307 |
|
277 | 308 | public static StreamFactory getAsyncStreamFactory() {
|
|
0 commit comments