Skip to content

Commit 236d8be

Browse files
committed
Deprecate Stream-related API
Adds: * TransportSettings abstract class * NettyTransportSettings subclass of TransportSettings * A new method on MongoClientSettings to configure TransportSettings Deprecates: * MongoClientSettings#streamFactoryFactory * NettyStreamFactoryFactory * NettyStreamFactory * AsynchronousSocketChannelStreamFactory * AsynchronousSocketChannelStreamFactoryFactory * BufferProvider * SocketStreamFactory * Stream * StreamFactory * StreamFactoryFactory * TlsChannelStreamFactoryFactory JAVA-5051
1 parent 90c7062 commit 236d8be

File tree

45 files changed

+615
-15
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

45 files changed

+615
-15
lines changed

driver-core/src/main/com/mongodb/MongoClientSettings.java

Lines changed: 45 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@
2727
import com.mongodb.connection.SocketSettings;
2828
import com.mongodb.connection.SslSettings;
2929
import com.mongodb.connection.StreamFactoryFactory;
30+
import com.mongodb.connection.TransportSettings;
3031
import com.mongodb.event.CommandListener;
3132
import com.mongodb.lang.Nullable;
3233
import com.mongodb.spi.dns.DnsClient;
@@ -62,6 +63,7 @@
6263
*
6364
* @since 3.7
6465
*/
66+
@SuppressWarnings("deprecation")
6567
@Immutable
6668
public final class MongoClientSettings {
6769
private static final CodecRegistry DEFAULT_CODEC_REGISTRY =
@@ -89,6 +91,7 @@ public final class MongoClientSettings {
8991
private final boolean retryReads;
9092
private final ReadConcern readConcern;
9193
private final MongoCredential credential;
94+
private final TransportSettings transportSettings;
9295
private final StreamFactoryFactory streamFactoryFactory;
9396
private final List<CommandListener> commandListeners;
9497
private final CodecRegistry codecRegistry;
@@ -209,6 +212,7 @@ public static final class Builder {
209212
private boolean retryReads = true;
210213
private ReadConcern readConcern = ReadConcern.DEFAULT;
211214
private CodecRegistry codecRegistry = MongoClientSettings.getDefaultCodecRegistry();
215+
private TransportSettings transportSettings;
212216
private StreamFactoryFactory streamFactoryFactory;
213217
private List<CommandListener> commandListeners = new ArrayList<>();
214218

@@ -252,6 +256,7 @@ private Builder(final MongoClientSettings settings) {
252256
serverApi = settings.getServerApi();
253257
dnsClient = settings.getDnsClient();
254258
inetAddressResolver = settings.getInetAddressResolver();
259+
transportSettings = settings.getTransportSettings();
255260
streamFactoryFactory = settings.getStreamFactoryFactory();
256261
autoEncryptionSettings = settings.getAutoEncryptionSettings();
257262
contextProvider = settings.getContextProvider();
@@ -490,12 +495,29 @@ public Builder codecRegistry(final CodecRegistry codecRegistry) {
490495
* @param streamFactoryFactory the stream factory factory
491496
* @return this
492497
* @see #getStreamFactoryFactory()
498+
* @deprecated Prefer {@link #transportSettings(TransportSettings)}
493499
*/
500+
@Deprecated
494501
public Builder streamFactoryFactory(final StreamFactoryFactory streamFactoryFactory) {
495502
this.streamFactoryFactory = notNull("streamFactoryFactory", streamFactoryFactory);
496503
return this;
497504
}
498505

506+
/**
507+
* Sets the {@link TransportSettings} to apply.
508+
*
509+
* <p>
510+
* If transport settings are applied, application of {@link #streamFactoryFactory} is ignored.
511+
* </p>
512+
*
513+
* @param transportSettings the transport settings
514+
* @return this
515+
*/
516+
public Builder transportSettings(final TransportSettings transportSettings) {
517+
this.transportSettings = notNull("transportSettings", transportSettings);
518+
return this;
519+
}
520+
499521
/**
500522
* Adds the given command listener.
501523
*
@@ -771,12 +793,27 @@ public CodecRegistry getCodecRegistry() {
771793
*
772794
* @return the stream factory factory
773795
* @see Builder#streamFactoryFactory(StreamFactoryFactory)
796+
* @deprecated Prefer {@link #getTransportSettings()}
774797
*/
798+
@Deprecated
775799
@Nullable
776800
public StreamFactoryFactory getStreamFactoryFactory() {
777801
return streamFactoryFactory;
778802
}
779803

804+
/**
805+
* Gets the settings for the underlying transport implementation
806+
*
807+
* @return the settings for the underlying transport implementation
808+
*
809+
* @since 4.11
810+
* @see Builder#transportSettings(TransportSettings)
811+
*/
812+
@Nullable
813+
public TransportSettings getTransportSettings() {
814+
return transportSettings;
815+
}
816+
780817
/**
781818
* Gets the list of added {@code CommandListener}.
782819
*
@@ -978,6 +1015,7 @@ public boolean equals(final Object o) {
9781015
&& Objects.equals(writeConcern, that.writeConcern)
9791016
&& Objects.equals(readConcern, that.readConcern)
9801017
&& Objects.equals(credential, that.credential)
1018+
&& Objects.equals(transportSettings, that.transportSettings)
9811019
&& Objects.equals(streamFactoryFactory, that.streamFactoryFactory)
9821020
&& Objects.equals(commandListeners, that.commandListeners)
9831021
&& Objects.equals(codecRegistry, that.codecRegistry)
@@ -1000,11 +1038,11 @@ public boolean equals(final Object o) {
10001038

10011039
@Override
10021040
public int hashCode() {
1003-
return Objects.hash(readPreference, writeConcern, retryWrites, retryReads, readConcern, credential, streamFactoryFactory,
1004-
commandListeners, codecRegistry, loggerSettings, clusterSettings, socketSettings, heartbeatSocketSettings,
1005-
connectionPoolSettings, serverSettings, sslSettings, applicationName, compressorList, uuidRepresentation, serverApi,
1006-
autoEncryptionSettings, heartbeatSocketTimeoutSetExplicitly, heartbeatConnectTimeoutSetExplicitly, dnsClient,
1007-
inetAddressResolver, contextProvider);
1041+
return Objects.hash(readPreference, writeConcern, retryWrites, retryReads, readConcern, credential, transportSettings,
1042+
streamFactoryFactory, commandListeners, codecRegistry, loggerSettings, clusterSettings, socketSettings,
1043+
heartbeatSocketSettings, connectionPoolSettings, serverSettings, sslSettings, applicationName, compressorList,
1044+
uuidRepresentation, serverApi, autoEncryptionSettings, heartbeatSocketTimeoutSetExplicitly,
1045+
heartbeatConnectTimeoutSetExplicitly, dnsClient, inetAddressResolver, contextProvider);
10081046
}
10091047

10101048
@Override
@@ -1016,6 +1054,7 @@ public String toString() {
10161054
+ ", retryReads=" + retryReads
10171055
+ ", readConcern=" + readConcern
10181056
+ ", credential=" + credential
1057+
+ ", transportSettings=" + transportSettings
10191058
+ ", streamFactoryFactory=" + streamFactoryFactory
10201059
+ ", commandListeners=" + commandListeners
10211060
+ ", codecRegistry=" + codecRegistry
@@ -1044,6 +1083,7 @@ private MongoClientSettings(final Builder builder) {
10441083
retryReads = builder.retryReads;
10451084
readConcern = builder.readConcern;
10461085
credential = builder.credential;
1086+
transportSettings = builder.transportSettings;
10471087
streamFactoryFactory = builder.streamFactoryFactory;
10481088
codecRegistry = builder.codecRegistry;
10491089
commandListeners = builder.commandListeners;

driver-core/src/main/com/mongodb/annotations/Evolving.java

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,6 @@
1818

1919
package com.mongodb.annotations;
2020

21-
import com.mongodb.connection.StreamFactoryFactory;
2221
import org.bson.conversions.Bson;
2322

2423
import java.lang.annotation.Documented;
@@ -51,11 +50,6 @@
5150
* <td>Not applicable.</td>
5251
* </tr>
5352
* <tr>
54-
* <td>Doing so allows customizing API behavior.</td>
55-
* <td>{@link StreamFactoryFactory}</td>
56-
* <td>Not applicable.</td>
57-
* </tr>
58-
* <tr>
5953
* <td>Doing so facilitates writing application unit tests by creating a fake implementation.</td>
6054
* <td>{@code com.mongodb.client.MongoClient}</td>
6155
* <td>Applicable.</td>

driver-core/src/main/com/mongodb/connection/AsynchronousSocketChannelStreamFactory.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,9 @@
2929
* Factory to create a Stream that's an AsynchronousSocketChannelStream. Throws an exception if SSL is enabled.
3030
*
3131
* @since 3.0
32+
* @deprecated There is no replacement for this class.
3233
*/
34+
@Deprecated
3335
public class AsynchronousSocketChannelStreamFactory implements StreamFactory {
3436
private final PowerOfTwoBufferPool bufferProvider = PowerOfTwoBufferPool.DEFAULT;
3537
private final SocketSettings settings;

driver-core/src/main/com/mongodb/connection/AsynchronousSocketChannelStreamFactoryFactory.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,9 @@
2323
*
2424
* @see java.nio.channels.AsynchronousSocketChannel
2525
* @since 3.1
26+
* @deprecated There is no replacement for this class.
2627
*/
28+
@Deprecated
2729
public final class AsynchronousSocketChannelStreamFactoryFactory implements StreamFactoryFactory {
2830
private final AsynchronousChannelGroup group;
2931

driver-core/src/main/com/mongodb/connection/BufferProvider.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,9 @@
2323
* A provider of instances of ByteBuf.
2424
*
2525
* @since 3.0
26+
* @deprecated There is no replacement for this interface.
2627
*/
28+
@Deprecated
2729
@ThreadSafe
2830
public interface BufferProvider {
2931
/**
Lines changed: 207 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,207 @@
1+
/*
2+
* Copyright 2008-present MongoDB, Inc.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package com.mongodb.connection;
18+
19+
import com.mongodb.annotations.Immutable;
20+
import com.mongodb.lang.Nullable;
21+
import io.netty.buffer.ByteBufAllocator;
22+
import io.netty.channel.EventLoopGroup;
23+
import io.netty.channel.socket.SocketChannel;
24+
import io.netty.handler.ssl.ReferenceCountedOpenSslClientContext;
25+
import io.netty.handler.ssl.SslContext;
26+
import io.netty.handler.ssl.SslContextBuilder;
27+
import io.netty.handler.ssl.SslProvider;
28+
29+
import java.security.Security;
30+
31+
import static com.mongodb.assertions.Assertions.isTrueArgument;
32+
import static com.mongodb.assertions.Assertions.notNull;
33+
34+
/**
35+
* A {@code TransportSettings} implementation for a <a href="http://netty.io/">Netty</a>-based transport implementation.
36+
*
37+
* @since 4.11
38+
*/
39+
@Immutable
40+
public final class NettyTransportSettings extends TransportSettings {
41+
42+
private final EventLoopGroup eventLoopGroup;
43+
private final Class<? extends SocketChannel> socketChannelClass;
44+
private final ByteBufAllocator allocator;
45+
private final SslContext sslContext;
46+
47+
/**
48+
* Gets a builder for an instance of {@code NettyStreamFactoryFactory}.
49+
* @return the builder
50+
*/
51+
static Builder builder() {
52+
return new Builder();
53+
}
54+
55+
/**
56+
* A builder for an instance of {@code NettyStreamFactoryFactory}.
57+
*/
58+
public static final class Builder {
59+
private ByteBufAllocator allocator;
60+
private Class<? extends SocketChannel> socketChannelClass;
61+
private EventLoopGroup eventLoopGroup;
62+
private SslContext sslContext;
63+
64+
private Builder() {
65+
}
66+
67+
/**
68+
* Sets the allocator.
69+
*
70+
* @param allocator the allocator to use for ByteBuf instances
71+
* @return this
72+
*/
73+
public Builder allocator(final ByteBufAllocator allocator) {
74+
this.allocator = notNull("allocator", allocator);
75+
return this;
76+
}
77+
78+
/**
79+
* Sets the socket channel class
80+
*
81+
* @param socketChannelClass the socket channel class
82+
* @return this
83+
*/
84+
public Builder socketChannelClass(final Class<? extends SocketChannel> socketChannelClass) {
85+
this.socketChannelClass = notNull("socketChannelClass", socketChannelClass);
86+
return this;
87+
}
88+
89+
/**
90+
* Sets the event loop group.
91+
*
92+
* <p>It is highly recommended to supply your own event loop group and manage its shutdown. Otherwise, the event
93+
* loop group created by default will not be shutdown properly.</p>
94+
*
95+
* @param eventLoopGroup the event loop group that all channels created by this factory will be a part of
96+
* @return this
97+
*/
98+
public Builder eventLoopGroup(final EventLoopGroup eventLoopGroup) {
99+
this.eventLoopGroup = notNull("eventLoopGroup", eventLoopGroup);
100+
return this;
101+
}
102+
103+
/**
104+
* Sets a {@linkplain SslContextBuilder#forClient() client-side} {@link SslContext io.netty.handler.ssl.SslContext},
105+
* which overrides the standard {@link SslSettings#getContext()}.
106+
* By default, it is {@code null} and {@link SslSettings#getContext()} is at play.
107+
* <p>
108+
* This option may be used as a convenient way to utilize
109+
* <a href="https://www.openssl.org/">OpenSSL</a> as an alternative to the TLS/SSL protocol implementation in a JDK.
110+
* To achieve this, specify {@link SslProvider#OPENSSL} TLS/SSL protocol provider via
111+
* {@link SslContextBuilder#sslProvider(SslProvider)}. Note that doing so adds a runtime dependency on
112+
* <a href="https://netty.io/wiki/forked-tomcat-native.html">netty-tcnative</a>, which you must satisfy.
113+
* <p>
114+
* Notes:
115+
* <ul>
116+
* <li>Netty {@link SslContext} may not examine some
117+
* {@linkplain Security security}/{@linkplain System#getProperties() system} properties that are used to
118+
* <a href="https://docs.oracle.com/javase/8/docs/technotes/guides/security/jsse/JSSERefGuide.html#Customization">
119+
* customize JSSE</a>. Therefore, instead of using them you may have to apply the equivalent configuration programmatically,
120+
* if both the {@link SslContextBuilder} and the TLS/SSL protocol provider of choice support it.
121+
* </li>
122+
* <li>Only {@link SslProvider#JDK} and {@link SslProvider#OPENSSL} TLS/SSL protocol providers are supported.
123+
* </li>
124+
* </ul>
125+
*
126+
* @param sslContext The Netty {@link SslContext}, which must be created via {@linkplain SslContextBuilder#forClient()}.
127+
* @return {@code this}.
128+
*/
129+
public Builder sslContext(final SslContext sslContext) {
130+
this.sslContext = notNull("sslContext", sslContext);
131+
isTrueArgument("sslContext must be client-side", sslContext.isClient());
132+
isTrueArgument("sslContext must use either SslProvider.JDK or SslProvider.OPENSSL TLS/SSL protocol provider",
133+
!(sslContext instanceof ReferenceCountedOpenSslClientContext));
134+
135+
return this;
136+
}
137+
138+
/**
139+
* Build an instance of {@code NettyStreamFactoryFactory}.
140+
* @return factory of the netty stream factory
141+
*/
142+
public NettyTransportSettings build() {
143+
return new NettyTransportSettings(this);
144+
}
145+
}
146+
147+
/**
148+
* Gets the event loop group.
149+
*
150+
* @return the event loop group
151+
* @see Builder#eventLoopGroup(EventLoopGroup)
152+
*/
153+
@Nullable
154+
public EventLoopGroup getEventLoopGroup() {
155+
return eventLoopGroup;
156+
}
157+
158+
/**
159+
* Gets the socket channel class.
160+
*
161+
* @return the socket channel class
162+
* @see Builder#socketChannelClass(Class)
163+
*/
164+
@Nullable
165+
public Class<? extends SocketChannel> getSocketChannelClass() {
166+
return socketChannelClass;
167+
}
168+
169+
/**
170+
* Gets the allocator.
171+
*
172+
* @return the allocator
173+
* @see Builder#allocator(ByteBufAllocator)
174+
*/
175+
@Nullable
176+
public ByteBufAllocator getAllocator() {
177+
return allocator;
178+
}
179+
180+
/**
181+
* Gets the SSL Context.
182+
*
183+
* @return the SSL context
184+
* @see Builder#sslContext(SslContext)
185+
*/
186+
@Nullable
187+
public SslContext getSslContext() {
188+
return sslContext;
189+
}
190+
191+
@Override
192+
public String toString() {
193+
return "NettyTransportSettings{"
194+
+ "eventLoopGroup=" + eventLoopGroup
195+
+ ", socketChannelClass=" + socketChannelClass
196+
+ ", allocator=" + allocator
197+
+ ", sslContext=" + sslContext
198+
+ '}';
199+
}
200+
201+
private NettyTransportSettings(final Builder builder) {
202+
allocator = builder.allocator;
203+
socketChannelClass = builder.socketChannelClass;
204+
eventLoopGroup = builder.eventLoopGroup;
205+
sslContext = builder.sslContext;
206+
}
207+
}

0 commit comments

Comments
 (0)