Skip to content

Commit 94c321f

Browse files
committed
Remove deprecated ServerAddress methods (#1224)
* ServerAddress#getSocketAddress * ServerAddress#getSocketAddresses * UnixServerAddress#getSocketAddress * UnixServerAddress#getUnixSocketAddress JAVA-4937
1 parent b1c89fb commit 94c321f

File tree

46 files changed

+354
-398
lines changed

Some content is hidden

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

46 files changed

+354
-398
lines changed

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

-41
Original file line numberDiff line numberDiff line change
@@ -22,9 +22,6 @@
2222
import java.io.Serializable;
2323
import java.net.InetAddress;
2424
import java.net.InetSocketAddress;
25-
import java.net.UnknownHostException;
26-
import java.util.ArrayList;
27-
import java.util.List;
2825

2926
/**
3027
* Represents the location of a Mongo server - i.e. server name and port number
@@ -184,44 +181,6 @@ public int getPort() {
184181
return port;
185182
}
186183

187-
/**
188-
* Gets the underlying socket address
189-
*
190-
* @return socket address
191-
* @deprecated Prefer {@link InetAddress#getByName(String)}
192-
*/
193-
@Deprecated
194-
public InetSocketAddress getSocketAddress() {
195-
try {
196-
return new InetSocketAddress(InetAddress.getByName(host), port);
197-
} catch (UnknownHostException e) {
198-
throw new MongoSocketException(e.getMessage(), this, e);
199-
}
200-
}
201-
202-
/**
203-
* Gets all underlying socket addresses
204-
*
205-
* @return array of socket addresses
206-
*
207-
* @since 3.9
208-
* @deprecated Prefer {@link InetAddress#getAllByName(String)}
209-
*/
210-
@Deprecated
211-
public List<InetSocketAddress> getSocketAddresses() {
212-
try {
213-
InetAddress[] inetAddresses = InetAddress.getAllByName(host);
214-
List<InetSocketAddress> inetSocketAddressList = new ArrayList<>();
215-
for (InetAddress inetAddress : inetAddresses) {
216-
inetSocketAddressList.add(new InetSocketAddress(inetAddress, port));
217-
}
218-
219-
return inetSocketAddressList;
220-
} catch (UnknownHostException e) {
221-
throw new MongoSocketException(e.getMessage(), this, e);
222-
}
223-
}
224-
225184
@Override
226185
public String toString() {
227186
return host + ":" + port;

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

-20
Original file line numberDiff line numberDiff line change
@@ -17,10 +17,6 @@
1717
package com.mongodb;
1818

1919
import com.mongodb.annotations.Immutable;
20-
import jnr.unixsocket.UnixSocketAddress;
21-
22-
import java.net.InetSocketAddress;
23-
import java.net.SocketAddress;
2420

2521
import static com.mongodb.assertions.Assertions.isTrueArgument;
2622
import static com.mongodb.assertions.Assertions.notNull;
@@ -44,22 +40,6 @@ public UnixServerAddress(final String path) {
4440
isTrueArgument("The path must end in .sock", path.endsWith(".sock"));
4541
}
4642

47-
@SuppressWarnings("deprecation")
48-
@Deprecated
49-
@Override
50-
public InetSocketAddress getSocketAddress() {
51-
throw new UnsupportedOperationException("Cannot return a InetSocketAddress from a UnixServerAddress");
52-
}
53-
54-
/**
55-
* @return the SocketAddress for the MongoD unix domain socket.
56-
* @deprecated Prefer {@link UnixSocketAddress#UnixSocketAddress(String)}
57-
*/
58-
@Deprecated
59-
public SocketAddress getUnixSocketAddress() {
60-
return new UnixSocketAddress(getHost());
61-
}
62-
6343
@Override
6444
public String toString() {
6545
return getHost();

driver-core/src/main/com/mongodb/internal/connection/AsynchronousSocketChannelStream.java

+7-4
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
import com.mongodb.connection.AsyncCompletionHandler;
2323
import com.mongodb.connection.SocketSettings;
2424
import com.mongodb.lang.Nullable;
25+
import com.mongodb.spi.dns.InetAddressResolver;
2526

2627
import java.io.IOException;
2728
import java.net.SocketAddress;
@@ -36,29 +37,31 @@
3637
import java.util.concurrent.atomic.AtomicReference;
3738

3839
import static com.mongodb.assertions.Assertions.isTrue;
40+
import static com.mongodb.internal.connection.ServerAddressHelper.getSocketAddresses;
3941

4042
/**
4143
* <p>This class is not part of the public API and may be removed or changed at any time</p>
4244
*/
4345
public final class AsynchronousSocketChannelStream extends AsynchronousChannelStream {
4446
private final ServerAddress serverAddress;
47+
private final InetAddressResolver inetAddressResolver;
4548
private final SocketSettings settings;
4649

47-
public AsynchronousSocketChannelStream(final ServerAddress serverAddress, final SocketSettings settings,
48-
final PowerOfTwoBufferPool bufferProvider) {
50+
public AsynchronousSocketChannelStream(final ServerAddress serverAddress, final InetAddressResolver inetAddressResolver,
51+
final SocketSettings settings, final PowerOfTwoBufferPool bufferProvider) {
4952
super(serverAddress, settings, bufferProvider);
5053
this.serverAddress = serverAddress;
54+
this.inetAddressResolver = inetAddressResolver;
5155
this.settings = settings;
5256
}
5357

54-
@SuppressWarnings("deprecation")
5558
@Override
5659
public void openAsync(final AsyncCompletionHandler<Void> handler) {
5760
isTrue("unopened", getChannel() == null);
5861
Queue<SocketAddress> socketAddressQueue;
5962

6063
try {
61-
socketAddressQueue = new LinkedList<>(serverAddress.getSocketAddresses());
64+
socketAddressQueue = new LinkedList<>(getSocketAddresses(serverAddress, inetAddressResolver));
6265
} catch (Throwable t) {
6366
handler.failed(t);
6467
return;

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

+6-2
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
import com.mongodb.ServerAddress;
2020
import com.mongodb.connection.SocketSettings;
2121
import com.mongodb.connection.SslSettings;
22+
import com.mongodb.spi.dns.InetAddressResolver;
2223

2324
import static com.mongodb.assertions.Assertions.assertFalse;
2425
import static com.mongodb.assertions.Assertions.notNull;
@@ -29,21 +30,24 @@
2930
public class AsynchronousSocketChannelStreamFactory implements StreamFactory {
3031
private final PowerOfTwoBufferPool bufferProvider = PowerOfTwoBufferPool.DEFAULT;
3132
private final SocketSettings settings;
33+
private final InetAddressResolver inetAddressResolver;
3234

3335
/**
3436
* Create a new factory with the default {@code BufferProvider} and {@code AsynchronousChannelGroup}.
3537
*
3638
* @param settings the settings for the connection to a MongoDB server
3739
* @param sslSettings the settings for connecting via SSL
3840
*/
39-
public AsynchronousSocketChannelStreamFactory(final SocketSettings settings, final SslSettings sslSettings) {
41+
public AsynchronousSocketChannelStreamFactory(final InetAddressResolver inetAddressResolver, final SocketSettings settings,
42+
final SslSettings sslSettings) {
4043
assertFalse(sslSettings.isEnabled());
44+
this.inetAddressResolver = inetAddressResolver;
4145
this.settings = notNull("settings", settings);
4246
}
4347

4448
@Override
4549
public Stream create(final ServerAddress serverAddress) {
46-
return new AsynchronousSocketChannelStream(serverAddress, settings, bufferProvider);
50+
return new AsynchronousSocketChannelStream(serverAddress, inetAddressResolver, settings, bufferProvider);
4751
}
4852

4953
}

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

+8-1
Original file line numberDiff line numberDiff line change
@@ -18,15 +18,22 @@
1818

1919
import com.mongodb.connection.SocketSettings;
2020
import com.mongodb.connection.SslSettings;
21+
import com.mongodb.spi.dns.InetAddressResolver;
2122

2223
/**
2324
* A {@code StreamFactoryFactory} implementation for AsynchronousSocketChannel-based streams.
2425
*
2526
* @see java.nio.channels.AsynchronousSocketChannel
2627
*/
2728
public final class AsynchronousSocketChannelStreamFactoryFactory implements StreamFactoryFactory {
29+
private final InetAddressResolver inetAddressResolver;
30+
31+
public AsynchronousSocketChannelStreamFactoryFactory(final InetAddressResolver inetAddressResolver) {
32+
this.inetAddressResolver = inetAddressResolver;
33+
}
34+
2835
@Override
2936
public StreamFactory create(final SocketSettings socketSettings, final SslSettings sslSettings) {
30-
return new AsynchronousSocketChannelStreamFactory(socketSettings, sslSettings);
37+
return new AsynchronousSocketChannelStreamFactory(inetAddressResolver, socketSettings, sslSettings);
3138
}
3239
}

driver-core/src/main/com/mongodb/internal/connection/DefaultClusterFactory.java

+3-4
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,6 @@
3636
import com.mongodb.internal.diagnostics.logging.Loggers;
3737
import com.mongodb.lang.Nullable;
3838
import com.mongodb.spi.dns.DnsClient;
39-
import com.mongodb.spi.dns.InetAddressResolver;
4039

4140
import java.util.List;
4241

@@ -68,7 +67,7 @@ public Cluster createCluster(final ClusterSettings originalClusterSettings, fina
6867
@Nullable final String applicationName,
6968
@Nullable final MongoDriverInformation mongoDriverInformation,
7069
final List<MongoCompressor> compressorList, @Nullable final ServerApi serverApi,
71-
@Nullable final DnsClient dnsClient, @Nullable final InetAddressResolver inetAddressResolver) {
70+
@Nullable final DnsClient dnsClient) {
7271

7372
detectAndLogClusterEnvironment(originalClusterSettings);
7473

@@ -104,14 +103,14 @@ public Cluster createCluster(final ClusterSettings originalClusterSettings, fina
104103
ClusterableServerFactory serverFactory = new LoadBalancedClusterableServerFactory(serverSettings,
105104
connectionPoolSettings, internalConnectionPoolSettings, streamFactory, credential, loggerSettings, commandListener,
106105
applicationName, mongoDriverInformation != null ? mongoDriverInformation : MongoDriverInformation.builder().build(),
107-
compressorList, serverApi, inetAddressResolver);
106+
compressorList, serverApi);
108107
return new LoadBalancedCluster(clusterId, clusterSettings, serverFactory, dnsSrvRecordMonitorFactory);
109108
} else {
110109
ClusterableServerFactory serverFactory = new DefaultClusterableServerFactory(serverSettings,
111110
connectionPoolSettings, internalConnectionPoolSettings,
112111
streamFactory, heartbeatStreamFactory, credential, loggerSettings, commandListener, applicationName,
113112
mongoDriverInformation != null ? mongoDriverInformation : MongoDriverInformation.builder().build(), compressorList,
114-
serverApi, inetAddressResolver);
113+
serverApi);
115114

116115
if (clusterSettings.getMode() == ClusterConnectionMode.SINGLE) {
117116
return new SingleServerCluster(clusterId, clusterSettings, serverFactory);

driver-core/src/main/com/mongodb/internal/connection/DefaultClusterableServerFactory.java

+3-8
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,6 @@
3030
import com.mongodb.event.ServerListener;
3131
import com.mongodb.internal.inject.SameObjectProvider;
3232
import com.mongodb.lang.Nullable;
33-
import com.mongodb.spi.dns.InetAddressResolver;
3433

3534
import java.util.List;
3635

@@ -54,8 +53,6 @@ public class DefaultClusterableServerFactory implements ClusterableServerFactory
5453
private final List<MongoCompressor> compressorList;
5554
@Nullable
5655
private final ServerApi serverApi;
57-
@Nullable
58-
private final InetAddressResolver inetAddressResolver;
5956

6057
public DefaultClusterableServerFactory(
6158
final ServerSettings serverSettings, final ConnectionPoolSettings connectionPoolSettings,
@@ -65,8 +62,7 @@ public DefaultClusterableServerFactory(
6562
final LoggerSettings loggerSettings,
6663
@Nullable final CommandListener commandListener,
6764
@Nullable final String applicationName, @Nullable final MongoDriverInformation mongoDriverInformation,
68-
final List<MongoCompressor> compressorList, @Nullable final ServerApi serverApi,
69-
@Nullable final InetAddressResolver inetAddressResolver) {
65+
final List<MongoCompressor> compressorList, @Nullable final ServerApi serverApi) {
7066
this.serverSettings = serverSettings;
7167
this.connectionPoolSettings = connectionPoolSettings;
7268
this.internalConnectionPoolSettings = internalConnectionPoolSettings;
@@ -79,7 +75,6 @@ public DefaultClusterableServerFactory(
7975
this.mongoDriverInformation = mongoDriverInformation;
8076
this.compressorList = compressorList;
8177
this.serverApi = serverApi;
82-
this.inetAddressResolver = inetAddressResolver;
8378
}
8479

8580
@Override
@@ -90,11 +85,11 @@ public ClusterableServer create(final Cluster cluster, final ServerAddress serve
9085
ServerMonitor serverMonitor = new DefaultServerMonitor(serverId, serverSettings, cluster.getClock(),
9186
// no credentials, compressor list, or command listener for the server monitor factory
9287
new InternalStreamConnectionFactory(clusterMode, true, heartbeatStreamFactory, null, applicationName,
93-
mongoDriverInformation, emptyList(), loggerSettings, null, serverApi, inetAddressResolver),
88+
mongoDriverInformation, emptyList(), loggerSettings, null, serverApi),
9489
clusterMode, serverApi, sdamProvider);
9590
ConnectionPool connectionPool = new DefaultConnectionPool(serverId,
9691
new InternalStreamConnectionFactory(clusterMode, streamFactory, credential, applicationName,
97-
mongoDriverInformation, compressorList, loggerSettings, commandListener, serverApi, inetAddressResolver),
92+
mongoDriverInformation, compressorList, loggerSettings, commandListener, serverApi),
9893
connectionPoolSettings, internalConnectionPoolSettings, sdamProvider);
9994
ServerListener serverListener = singleServerListener(serverSettings);
10095
SdamServerDescriptionManager sdam = new DefaultSdamServerDescriptionManager(cluster, serverId, serverListener, serverMonitor,
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
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.internal.connection;
18+
19+
import com.mongodb.spi.dns.InetAddressResolver;
20+
21+
import java.net.InetAddress;
22+
import java.net.UnknownHostException;
23+
import java.util.List;
24+
25+
import static java.util.Arrays.asList;
26+
27+
/**
28+
* <p>This class is not part of the public API and may be removed or changed at any time</p>
29+
*/
30+
public class DefaultInetAddressResolver implements InetAddressResolver {
31+
32+
@Override
33+
public List<InetAddress> lookupByName(final String host) throws UnknownHostException {
34+
return asList(InetAddress.getAllByName(host));
35+
}
36+
}

0 commit comments

Comments
 (0)