com.mongodb.connection.SingleServerCluster Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of mongo-java-driver Show documentation
Show all versions of mongo-java-driver Show documentation
The MongoDB Java Driver uber-artifact, containing mongodb-driver, mongodb-driver-core, and bson
The newest version!
/*
* Copyright (c) 2008-2014 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.connection;
import com.mongodb.ServerAddress;
import com.mongodb.diagnostics.logging.Logger;
import com.mongodb.diagnostics.logging.Loggers;
import com.mongodb.event.ClusterListener;
import java.util.Arrays;
import java.util.Collections;
import static com.mongodb.assertions.Assertions.isTrue;
import static java.lang.String.format;
/**
* This class needs to be final because we are leaking a reference to "this" from the constructor
*/
final class SingleServerCluster extends BaseCluster {
private static final Logger LOGGER = Loggers.getLogger("cluster");
private final ClusterableServer server;
public SingleServerCluster(final ClusterId clusterId, final ClusterSettings settings, final ClusterableServerFactory serverFactory,
final ClusterListener clusterListener) {
super(clusterId, settings, serverFactory, clusterListener);
isTrue("one server in a direct cluster", settings.getHosts().size() == 1);
isTrue("connection mode is single", settings.getMode() == ClusterConnectionMode.SINGLE);
if (LOGGER.isInfoEnabled()) {
LOGGER.info(format("Cluster created with settings %s", settings.getShortDescription()));
}
// synchronized in the constructor because the change listener is re-entrant to this instance.
// In other words, we are leaking a reference to "this" from the constructor.
synchronized (this) {
this.server = createServer(settings.getHosts().get(0), new ChangeListener() {
@Override
public void stateChanged(final ChangeEvent event) {
ServerDescription descriptionToPublish = event.getNewValue();
if (event.getNewValue().isOk()) {
if (getSettings().getRequiredClusterType() != ClusterType.UNKNOWN
&& getSettings().getRequiredClusterType() != event.getNewValue().getClusterType()) {
descriptionToPublish = null;
} else if (getSettings().getRequiredClusterType() == ClusterType.REPLICA_SET
&& getSettings().getRequiredReplicaSetName() != null) {
if (!getSettings().getRequiredReplicaSetName().equals(event.getNewValue().getSetName())) {
descriptionToPublish = null;
}
}
}
publishDescription(descriptionToPublish);
}
});
publishDescription(server.getDescription());
}
}
@Override
protected void connect() {
server.connect();
}
private void publishDescription(final ServerDescription serverDescription) {
ClusterType clusterType = getSettings().getRequiredClusterType();
if (clusterType == ClusterType.UNKNOWN && serverDescription != null) {
clusterType = serverDescription.getClusterType();
}
ClusterDescription description = new ClusterDescription(ClusterConnectionMode.SINGLE, clusterType,
serverDescription == null ? Collections.emptyList()
: Arrays.asList(serverDescription));
updateDescription(description);
fireChangeEvent();
}
@Override
protected ClusterableServer getServer(final ServerAddress serverAddress) {
isTrue("open", !isClosed());
return server;
}
@Override
public void close() {
if (!isClosed()) {
server.close();
super.close();
}
}
}