Skip to content

Remove deprecated ServerAddress methods #1224

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
Oct 21, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
41 changes: 0 additions & 41 deletions driver-core/src/main/com/mongodb/ServerAddress.java
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,6 @@
import java.io.Serializable;
import java.net.InetAddress;
import java.net.InetSocketAddress;
import java.net.UnknownHostException;
import java.util.ArrayList;
import java.util.List;

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

/**
* Gets the underlying socket address
*
* @return socket address
* @deprecated Prefer {@link InetAddress#getByName(String)}
*/
@Deprecated
public InetSocketAddress getSocketAddress() {
try {
return new InetSocketAddress(InetAddress.getByName(host), port);
} catch (UnknownHostException e) {
throw new MongoSocketException(e.getMessage(), this, e);
}
}

/**
* Gets all underlying socket addresses
*
* @return array of socket addresses
*
* @since 3.9
* @deprecated Prefer {@link InetAddress#getAllByName(String)}
*/
@Deprecated
public List<InetSocketAddress> getSocketAddresses() {
try {
InetAddress[] inetAddresses = InetAddress.getAllByName(host);
List<InetSocketAddress> inetSocketAddressList = new ArrayList<>();
for (InetAddress inetAddress : inetAddresses) {
inetSocketAddressList.add(new InetSocketAddress(inetAddress, port));
}

return inetSocketAddressList;
} catch (UnknownHostException e) {
throw new MongoSocketException(e.getMessage(), this, e);
}
}

@Override
public String toString() {
return host + ":" + port;
Expand Down
20 changes: 0 additions & 20 deletions driver-core/src/main/com/mongodb/UnixServerAddress.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,6 @@
package com.mongodb;

import com.mongodb.annotations.Immutable;
import jnr.unixsocket.UnixSocketAddress;

import java.net.InetSocketAddress;
import java.net.SocketAddress;

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

@SuppressWarnings("deprecation")
@Deprecated
@Override
public InetSocketAddress getSocketAddress() {
throw new UnsupportedOperationException("Cannot return a InetSocketAddress from a UnixServerAddress");
}

/**
* @return the SocketAddress for the MongoD unix domain socket.
* @deprecated Prefer {@link UnixSocketAddress#UnixSocketAddress(String)}
*/
@Deprecated
public SocketAddress getUnixSocketAddress() {
return new UnixSocketAddress(getHost());
}

@Override
public String toString() {
return getHost();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
import com.mongodb.connection.AsyncCompletionHandler;
import com.mongodb.connection.SocketSettings;
import com.mongodb.lang.Nullable;
import com.mongodb.spi.dns.InetAddressResolver;

import java.io.IOException;
import java.net.SocketAddress;
Expand All @@ -36,29 +37,31 @@
import java.util.concurrent.atomic.AtomicReference;

import static com.mongodb.assertions.Assertions.isTrue;
import static com.mongodb.internal.connection.ServerAddressHelper.getSocketAddresses;

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

public AsynchronousSocketChannelStream(final ServerAddress serverAddress, final SocketSettings settings,
final PowerOfTwoBufferPool bufferProvider) {
public AsynchronousSocketChannelStream(final ServerAddress serverAddress, final InetAddressResolver inetAddressResolver,
final SocketSettings settings, final PowerOfTwoBufferPool bufferProvider) {
super(serverAddress, settings, bufferProvider);
this.serverAddress = serverAddress;
this.inetAddressResolver = inetAddressResolver;
this.settings = settings;
}

@SuppressWarnings("deprecation")
@Override
public void openAsync(final AsyncCompletionHandler<Void> handler) {
isTrue("unopened", getChannel() == null);
Queue<SocketAddress> socketAddressQueue;

try {
socketAddressQueue = new LinkedList<>(serverAddress.getSocketAddresses());
socketAddressQueue = new LinkedList<>(getSocketAddresses(serverAddress, inetAddressResolver));
} catch (Throwable t) {
handler.failed(t);
return;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
import com.mongodb.ServerAddress;
import com.mongodb.connection.SocketSettings;
import com.mongodb.connection.SslSettings;
import com.mongodb.spi.dns.InetAddressResolver;

import static com.mongodb.assertions.Assertions.assertFalse;
import static com.mongodb.assertions.Assertions.notNull;
Expand All @@ -29,21 +30,24 @@
public class AsynchronousSocketChannelStreamFactory implements StreamFactory {
private final PowerOfTwoBufferPool bufferProvider = PowerOfTwoBufferPool.DEFAULT;
private final SocketSettings settings;
private final InetAddressResolver inetAddressResolver;

/**
* Create a new factory with the default {@code BufferProvider} and {@code AsynchronousChannelGroup}.
*
* @param settings the settings for the connection to a MongoDB server
* @param sslSettings the settings for connecting via SSL
*/
public AsynchronousSocketChannelStreamFactory(final SocketSettings settings, final SslSettings sslSettings) {
public AsynchronousSocketChannelStreamFactory(final InetAddressResolver inetAddressResolver, final SocketSettings settings,
final SslSettings sslSettings) {
assertFalse(sslSettings.isEnabled());
this.inetAddressResolver = inetAddressResolver;
this.settings = notNull("settings", settings);
}

@Override
public Stream create(final ServerAddress serverAddress) {
return new AsynchronousSocketChannelStream(serverAddress, settings, bufferProvider);
return new AsynchronousSocketChannelStream(serverAddress, inetAddressResolver, settings, bufferProvider);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -18,15 +18,22 @@

import com.mongodb.connection.SocketSettings;
import com.mongodb.connection.SslSettings;
import com.mongodb.spi.dns.InetAddressResolver;

/**
* A {@code StreamFactoryFactory} implementation for AsynchronousSocketChannel-based streams.
*
* @see java.nio.channels.AsynchronousSocketChannel
*/
public final class AsynchronousSocketChannelStreamFactoryFactory implements StreamFactoryFactory {
private final InetAddressResolver inetAddressResolver;

public AsynchronousSocketChannelStreamFactoryFactory(final InetAddressResolver inetAddressResolver) {
this.inetAddressResolver = inetAddressResolver;
}

@Override
public StreamFactory create(final SocketSettings socketSettings, final SslSettings sslSettings) {
return new AsynchronousSocketChannelStreamFactory(socketSettings, sslSettings);
return new AsynchronousSocketChannelStreamFactory(inetAddressResolver, socketSettings, sslSettings);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,6 @@
import com.mongodb.internal.diagnostics.logging.Loggers;
import com.mongodb.lang.Nullable;
import com.mongodb.spi.dns.DnsClient;
import com.mongodb.spi.dns.InetAddressResolver;

import java.util.List;

Expand Down Expand Up @@ -68,7 +67,7 @@ public Cluster createCluster(final ClusterSettings originalClusterSettings, fina
@Nullable final String applicationName,
@Nullable final MongoDriverInformation mongoDriverInformation,
final List<MongoCompressor> compressorList, @Nullable final ServerApi serverApi,
@Nullable final DnsClient dnsClient, @Nullable final InetAddressResolver inetAddressResolver) {
@Nullable final DnsClient dnsClient) {

detectAndLogClusterEnvironment(originalClusterSettings);

Expand Down Expand Up @@ -104,14 +103,14 @@ public Cluster createCluster(final ClusterSettings originalClusterSettings, fina
ClusterableServerFactory serverFactory = new LoadBalancedClusterableServerFactory(serverSettings,
connectionPoolSettings, internalConnectionPoolSettings, streamFactory, credential, loggerSettings, commandListener,
applicationName, mongoDriverInformation != null ? mongoDriverInformation : MongoDriverInformation.builder().build(),
compressorList, serverApi, inetAddressResolver);
compressorList, serverApi);
return new LoadBalancedCluster(clusterId, clusterSettings, serverFactory, dnsSrvRecordMonitorFactory);
} else {
ClusterableServerFactory serverFactory = new DefaultClusterableServerFactory(serverSettings,
connectionPoolSettings, internalConnectionPoolSettings,
streamFactory, heartbeatStreamFactory, credential, loggerSettings, commandListener, applicationName,
mongoDriverInformation != null ? mongoDriverInformation : MongoDriverInformation.builder().build(), compressorList,
serverApi, inetAddressResolver);
serverApi);

if (clusterSettings.getMode() == ClusterConnectionMode.SINGLE) {
return new SingleServerCluster(clusterId, clusterSettings, serverFactory);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,6 @@
import com.mongodb.event.ServerListener;
import com.mongodb.internal.inject.SameObjectProvider;
import com.mongodb.lang.Nullable;
import com.mongodb.spi.dns.InetAddressResolver;

import java.util.List;

Expand All @@ -54,8 +53,6 @@ public class DefaultClusterableServerFactory implements ClusterableServerFactory
private final List<MongoCompressor> compressorList;
@Nullable
private final ServerApi serverApi;
@Nullable
private final InetAddressResolver inetAddressResolver;

public DefaultClusterableServerFactory(
final ServerSettings serverSettings, final ConnectionPoolSettings connectionPoolSettings,
Expand All @@ -65,8 +62,7 @@ public DefaultClusterableServerFactory(
final LoggerSettings loggerSettings,
@Nullable final CommandListener commandListener,
@Nullable final String applicationName, @Nullable final MongoDriverInformation mongoDriverInformation,
final List<MongoCompressor> compressorList, @Nullable final ServerApi serverApi,
@Nullable final InetAddressResolver inetAddressResolver) {
final List<MongoCompressor> compressorList, @Nullable final ServerApi serverApi) {
this.serverSettings = serverSettings;
this.connectionPoolSettings = connectionPoolSettings;
this.internalConnectionPoolSettings = internalConnectionPoolSettings;
Expand All @@ -79,7 +75,6 @@ public DefaultClusterableServerFactory(
this.mongoDriverInformation = mongoDriverInformation;
this.compressorList = compressorList;
this.serverApi = serverApi;
this.inetAddressResolver = inetAddressResolver;
}

@Override
Expand All @@ -90,11 +85,11 @@ public ClusterableServer create(final Cluster cluster, final ServerAddress serve
ServerMonitor serverMonitor = new DefaultServerMonitor(serverId, serverSettings, cluster.getClock(),
// no credentials, compressor list, or command listener for the server monitor factory
new InternalStreamConnectionFactory(clusterMode, true, heartbeatStreamFactory, null, applicationName,
mongoDriverInformation, emptyList(), loggerSettings, null, serverApi, inetAddressResolver),
mongoDriverInformation, emptyList(), loggerSettings, null, serverApi),
clusterMode, serverApi, sdamProvider);
ConnectionPool connectionPool = new DefaultConnectionPool(serverId,
new InternalStreamConnectionFactory(clusterMode, streamFactory, credential, applicationName,
mongoDriverInformation, compressorList, loggerSettings, commandListener, serverApi, inetAddressResolver),
mongoDriverInformation, compressorList, loggerSettings, commandListener, serverApi),
connectionPoolSettings, internalConnectionPoolSettings, sdamProvider);
ServerListener serverListener = singleServerListener(serverSettings);
SdamServerDescriptionManager sdam = new DefaultSdamServerDescriptionManager(cluster, serverId, serverListener, serverMonitor,
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
/*
* Copyright 2008-present MongoDB, Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package com.mongodb.internal.connection;

import com.mongodb.spi.dns.InetAddressResolver;

import java.net.InetAddress;
import java.net.UnknownHostException;
import java.util.List;

import static java.util.Arrays.asList;

/**
* <p>This class is not part of the public API and may be removed or changed at any time</p>
*/
public class DefaultInetAddressResolver implements InetAddressResolver {

@Override
public List<InetAddress> lookupByName(final String host) throws UnknownHostException {
return asList(InetAddress.getAllByName(host));
}
}
Loading