|
17 | 17 | package com.mongodb.async.client;
|
18 | 18 |
|
19 | 19 | import com.mongodb.ConnectionString;
|
| 20 | +import com.mongodb.MongoClientException; |
20 | 21 | import com.mongodb.MongoDriverInformation;
|
21 |
| -import com.mongodb.connection.AsynchronousSocketChannelStreamFactory; |
| 22 | +import com.mongodb.MongoInternalException; |
| 23 | +import com.mongodb.connection.AsynchronousSocketChannelStreamFactoryFactory; |
22 | 24 | import com.mongodb.connection.Cluster;
|
23 | 25 | import com.mongodb.connection.DefaultClusterFactory;
|
24 |
| -import com.mongodb.connection.SocketSettings; |
25 | 26 | import com.mongodb.connection.StreamFactory;
|
26 | 27 | import com.mongodb.connection.StreamFactoryFactory;
|
| 28 | +import com.mongodb.connection.TlsChannelStreamFactoryFactory; |
27 | 29 | import com.mongodb.lang.Nullable;
|
28 | 30 | import org.bson.codecs.configuration.CodecRegistry;
|
29 | 31 |
|
|
38 | 40 | * @since 3.0
|
39 | 41 | * @deprecated Prefer the Reactive Streams-based asynchronous driver (mongodb-driver-reactivestreams artifactId)
|
40 | 42 | */
|
41 |
| -@SuppressWarnings("deprecation") |
42 | 43 | @Deprecated
|
43 | 44 | public final class MongoClients {
|
44 | 45 |
|
@@ -129,7 +130,6 @@ public static MongoClient create(final MongoClientSettings settings, @Nullable f
|
129 | 130 | * @throws IllegalArgumentException if the connection string's stream type is not one of "netty" or "nio2"
|
130 | 131 | * @see MongoClients#create(ConnectionString)
|
131 | 132 | */
|
132 |
| - @SuppressWarnings("deprecation") |
133 | 133 | public static MongoClient create(final ConnectionString connectionString,
|
134 | 134 | @Nullable final MongoDriverInformation mongoDriverInformation) {
|
135 | 135 |
|
@@ -158,34 +158,40 @@ public static MongoClient create(final com.mongodb.MongoClientSettings settings)
|
158 | 158 | * @return the client
|
159 | 159 | * @since 3.7
|
160 | 160 | */
|
161 |
| - @SuppressWarnings("deprecation") |
162 | 161 | public static MongoClient create(final com.mongodb.MongoClientSettings settings,
|
163 | 162 | @Nullable final MongoDriverInformation mongoDriverInformation) {
|
164 | 163 | return create(MongoClientSettings.createFromClientSettings(settings), mongoDriverInformation, null);
|
165 | 164 | }
|
166 | 165 |
|
167 |
| - @SuppressWarnings("deprecation") |
168 | 166 | private static MongoClient create(final MongoClientSettings settings,
|
169 | 167 | @Nullable final MongoDriverInformation mongoDriverInformation,
|
170 | 168 | @Nullable final String requestedStreamType) {
|
171 | 169 | String streamType = getStreamType(requestedStreamType);
|
172 |
| - if (isNetty(streamType) && settings.getStreamFactoryFactory() == null) { |
173 |
| - return NettyMongoClients.create(settings, mongoDriverInformation); |
| 170 | + if (settings.getStreamFactoryFactory() == null) { |
| 171 | + if (isNetty(streamType)) { |
| 172 | + return NettyMongoClients.create(settings, mongoDriverInformation); |
| 173 | + } else if (isNio(streamType)) { |
| 174 | + if (settings.getSslSettings().isEnabled()) { |
| 175 | + return createWithTlsChannel(settings, mongoDriverInformation); |
| 176 | + } else { |
| 177 | + return createWithAsynchronousSocketChannel(settings, mongoDriverInformation); |
| 178 | + } |
| 179 | + } else { |
| 180 | + throw new IllegalArgumentException("Unsupported stream type: " + streamType); |
| 181 | + } |
174 | 182 | } else {
|
175 |
| - return new MongoClientImpl(settings, createCluster(settings, mongoDriverInformation, |
176 |
| - getStreamFactory(settings, streamType, false), getStreamFactory(settings, streamType, true)), (Closeable) null); |
| 183 | + return createMongoClient(settings, mongoDriverInformation, getStreamFactory(settings, false), |
| 184 | + getStreamFactory(settings, true), null); |
177 | 185 | }
|
178 | 186 | }
|
179 | 187 |
|
180 |
| - @SuppressWarnings("deprecation") |
181 | 188 | static MongoClient createMongoClient(final MongoClientSettings settings, @Nullable final MongoDriverInformation mongoDriverInformation,
|
182 | 189 | final StreamFactory streamFactory, final StreamFactory heartbeatStreamFactory,
|
183 | 190 | @Nullable final Closeable externalResourceCloser) {
|
184 | 191 | return new MongoClientImpl(settings, createCluster(settings, mongoDriverInformation, streamFactory, heartbeatStreamFactory),
|
185 | 192 | externalResourceCloser);
|
186 | 193 | }
|
187 | 194 |
|
188 |
| - @SuppressWarnings("deprecation") |
189 | 195 | private static Cluster createCluster(final MongoClientSettings settings, @Nullable final MongoDriverInformation mongoDriverInformation,
|
190 | 196 | final StreamFactory streamFactory, final StreamFactory heartbeatStreamFactory) {
|
191 | 197 | notNull("settings", settings);
|
@@ -218,25 +224,57 @@ public static CodecRegistry getDefaultCodecRegistry() {
|
218 | 224 | return com.mongodb.MongoClientSettings.getDefaultCodecRegistry();
|
219 | 225 | }
|
220 | 226 |
|
221 |
| - @SuppressWarnings("deprecation") |
222 |
| - private static StreamFactory getStreamFactory(final MongoClientSettings settings, final String streamType, |
223 |
| - final boolean isHeartbeat) { |
| 227 | + |
| 228 | + private static MongoClient createWithTlsChannel(final MongoClientSettings settings, |
| 229 | + @Nullable final MongoDriverInformation mongoDriverInformation) { |
| 230 | + if (!isJava8()) { |
| 231 | + throw new MongoClientException("TLS is only supported natively with Java 8 and above. Please use Netty instead"); |
| 232 | + } |
| 233 | + final TlsChannelStreamFactoryFactory streamFactoryFactory = new TlsChannelStreamFactoryFactory(); |
| 234 | + StreamFactory streamFactory = streamFactoryFactory.create(settings.getSocketSettings(), settings.getSslSettings()); |
| 235 | + StreamFactory heartbeatStreamFactory = streamFactoryFactory.create(settings.getHeartbeatSocketSettings(), |
| 236 | + settings.getSslSettings()); |
| 237 | + return createMongoClient(settings, mongoDriverInformation, streamFactory, heartbeatStreamFactory, |
| 238 | + new Closeable() { |
| 239 | + @Override |
| 240 | + public void close() { |
| 241 | + streamFactoryFactory.close(); |
| 242 | + } |
| 243 | + }); |
| 244 | + } |
| 245 | + |
| 246 | + private static boolean isJava8() { |
| 247 | + try { |
| 248 | + Class.forName("java.time.Instant"); |
| 249 | + return true; |
| 250 | + } catch (ClassNotFoundException e) { |
| 251 | + return false; |
| 252 | + } |
| 253 | + } |
| 254 | + |
| 255 | + private static MongoClient createWithAsynchronousSocketChannel(final MongoClientSettings settings, |
| 256 | + @Nullable final MongoDriverInformation mongoDriverInformation) { |
| 257 | + StreamFactoryFactory streamFactoryFactory = new AsynchronousSocketChannelStreamFactoryFactory(); |
| 258 | + StreamFactory streamFactory = streamFactoryFactory.create(settings.getSocketSettings(), settings.getSslSettings()); |
| 259 | + StreamFactory heartbeatStreamFactory = streamFactoryFactory.create(settings.getHeartbeatSocketSettings(), |
| 260 | + settings.getSslSettings()); |
| 261 | + return createMongoClient(settings, mongoDriverInformation, streamFactory, heartbeatStreamFactory, null); |
| 262 | + } |
| 263 | + |
| 264 | + private static StreamFactory getStreamFactory(final MongoClientSettings settings, final boolean isHeartbeat) { |
224 | 265 | StreamFactoryFactory streamFactoryFactory = settings.getStreamFactoryFactory();
|
225 |
| - SocketSettings socketSettings = isHeartbeat ? settings.getHeartbeatSocketSettings() : settings.getSocketSettings(); |
226 |
| - if (streamFactoryFactory != null) { |
227 |
| - return streamFactoryFactory.create(socketSettings, settings.getSslSettings()); |
228 |
| - } else if (isNio2(streamType)) { |
229 |
| - return new AsynchronousSocketChannelStreamFactory(socketSettings, settings.getSslSettings()); |
230 |
| - } else { |
231 |
| - throw new IllegalArgumentException("Unsupported stream type: " + streamType); |
| 266 | + if (streamFactoryFactory == null) { |
| 267 | + throw new MongoInternalException("should not happen"); |
232 | 268 | }
|
| 269 | + return streamFactoryFactory.create(isHeartbeat ? settings.getHeartbeatSocketSettings() : settings.getSocketSettings(), |
| 270 | + settings.getSslSettings()); |
233 | 271 | }
|
234 | 272 |
|
235 | 273 | private static boolean isNetty(final String streamType) {
|
236 | 274 | return streamType.toLowerCase().equals("netty");
|
237 | 275 | }
|
238 | 276 |
|
239 |
| - private static boolean isNio2(final String streamType) { |
| 277 | + private static boolean isNio(final String streamType) { |
240 | 278 | return streamType.toLowerCase().equals("nio2");
|
241 | 279 | }
|
242 | 280 |
|
|
0 commit comments