package com.mongodb.binding;
import com.mongodb.ReadPreference;
import com.mongodb.ServerAddress;
import com.mongodb.async.SingleResultCallback;
import com.mongodb.connection.AsyncConnection;
import com.mongodb.connection.Cluster;
import com.mongodb.connection.Server;
import com.mongodb.connection.ServerDescription;
import com.mongodb.internal.binding.AbstractReferenceCounted;
import com.mongodb.internal.connection.NoOpSessionContext;
import com.mongodb.selector.ServerAddressSelector;
import com.mongodb.selector.ServerSelector;
import com.mongodb.session.SessionContext;
import static com.mongodb.assertions.Assertions.notNull;
@Deprecated
public class AsyncSingleServerBinding extends AbstractReferenceCounted implements AsyncReadWriteBinding {
private final Cluster cluster;
private final ServerAddress serverAddress;
private final ReadPreference readPreference;
public AsyncSingleServerBinding(final Cluster cluster, final ServerAddress serverAddress) {
this(cluster, serverAddress, ReadPreference.primary());
}
public AsyncSingleServerBinding(final Cluster cluster, final ServerAddress serverAddress, final ReadPreference readPreference) {
this.cluster = notNull("cluster", cluster);
this.serverAddress = notNull("serverAddress", serverAddress);
this.readPreference = notNull("readPreference", readPreference);
}
@Override
public ReadPreference getReadPreference() {
return readPreference;
}
@Override
public void getReadConnectionSource(final SingleResultCallback<AsyncConnectionSource> callback) {
getAsyncSingleServerBindingConnectionSource(new ServerAddressSelector(serverAddress), callback);
}
@Override
public void getWriteConnectionSource(final SingleResultCallback<AsyncConnectionSource> callback) {
getAsyncSingleServerBindingConnectionSource(new ServerAddressSelector(serverAddress), callback);
}
private void getAsyncSingleServerBindingConnectionSource(final ServerSelector serverSelector,
final SingleResultCallback<AsyncConnectionSource> callback) {
cluster.selectServerAsync(serverSelector, new SingleResultCallback<Server>() {
@Override
public void onResult(final Server result, final Throwable t) {
if (t != null) {
callback.onResult(null, t);
} else {
callback.onResult(new AsyncSingleServerBindingConnectionSource(result), null);
}
}
});
}
@Override
public SessionContext getSessionContext() {
return NoOpSessionContext.INSTANCE;
}
@Override
public AsyncSingleServerBinding retain() {
super.retain();
return this;
}
private final class AsyncSingleServerBindingConnectionSource extends AbstractReferenceCounted implements AsyncConnectionSource {
private final Server server;
private AsyncSingleServerBindingConnectionSource(final Server server) {
AsyncSingleServerBinding.this.retain();
this.server = server;
}
@Override
public ServerDescription getServerDescription() {
return server.getDescription();
}
@Override
public SessionContext getSessionContext() {
return NoOpSessionContext.INSTANCE;
}
@Override
public void getConnection(final SingleResultCallback<AsyncConnection> callback) {
server.getConnectionAsync(callback);
}
public AsyncConnectionSource retain() {
super.retain();
return this;
}
@Override
public void release() {
super.release();
if (super.getCount() == 0) {
AsyncSingleServerBinding.this.release();
}
}
}
}