Permalink
Browse files

JAVA-691: Took MongoAuthority and MongoCredentialsStore private (or r…

…ather, package-protected).
  • Loading branch information...
1 parent e671135 commit 1f07df644efb743004a755e4f263ce3bedbdd01f @jyemin jyemin committed Jan 23, 2013
@@ -805,7 +805,7 @@ public int getOptions(){
public abstract void cleanCursors( boolean force );
MongoCredentials getAuthenticationCredentials() {
- return getMongo().getCredentialsStore().get(getName());
+ return getMongo().getAuthority().getCredentialsStore().get(getName());
}
final Mongo _mongo;
@@ -327,11 +327,11 @@ CommandResult authenticate(Mongo mongo, final MongoCredentials credentials) {
void checkAuth(Mongo mongo) throws IOException {
// get the difference between the set of credentialed databases and the set of authenticated databases on this connection
- Set<String> unauthenticatedDatabases = new HashSet<String>(mongo.getCredentialsStore().getDatabases());
+ Set<String> unauthenticatedDatabases = new HashSet<String>(mongo.getAuthority().getCredentialsStore().getDatabases());
unauthenticatedDatabases.removeAll(authenticatedDatabases);
for (String databaseName : unauthenticatedDatabases) {
- authenticate(mongo, mongo.getCredentialsStore().get(databaseName));
+ authenticate(mongo, mongo.getAuthority().getCredentialsStore().get(databaseName));
}
}
@@ -31,21 +31,17 @@
static Logger _logger = Logger.getLogger( Bytes.LOGGER.getName() + ".tcp" );
/**
- * @param m
+ * @param mongo the Mongo instance
* @throws MongoException
*/
- public DBTCPConnector( Mongo m ) {
- _mongo = m;
- _portHolder = new DBPortPool.Holder( m._options );
- MongoAuthority.Type type = m.getAuthority().getType();
- if (type == MongoAuthority.Type.DIRECT) {
- setMasterAddress(m.getAuthority().getServerAddress());
- } else if (type == MongoAuthority.Type.REPLICA_SET) {
- _connectionStatus = new ReplicaSetStatus(m, m.getAuthority().getServerAddresses());
- } else if (type == MongoAuthority.Type.MONGOS_SET) {
- _connectionStatus = new MongosStatus(m, m.getAuthority().getServerAddresses());
- } else if (type == MongoAuthority.Type.DYNAMIC_SET) {
- _connectionStatus = new DynamicConnectionStatus(m, m.getAuthority().getServerAddresses());
+ public DBTCPConnector( Mongo mongo ) {
+ _mongo = mongo;
+ _portHolder = new DBPortPool.Holder( mongo._options );
+ MongoAuthority.Type type = mongo.getAuthority().getType();
+ if (type == MongoAuthority.Type.Direct) {
+ setMasterAddress(mongo.getAuthority().getServerAddresses().get(0));
+ } else if (type == MongoAuthority.Type.Set) {
+ _connectionStatus = new DynamicConnectionStatus(mongo, mongo.getAuthority().getServerAddresses());
} else {
throw new IllegalArgumentException("Unsupported authority type: " + type);
}
@@ -607,7 +603,7 @@ public CommandResult authenticate(MongoCredentials credentials) {
try {
CommandResult result = port.authenticate(_mongo, credentials);
- _mongo.getCredentialsStore().add(credentials);
+ _mongo.getAuthority().getCredentialsStore().add(credentials);
return result;
} finally {
mp.done(port);
@@ -297,12 +297,7 @@ public Mongo( MongoURI uri ) throws UnknownHostException {
_options = options;
_applyMongoOptions();
- if ( authority.isDirect() ){
- _connector = new DBTCPConnector( this );
- }
- else {
- _connector = new DBTCPConnector( this );
- }
+ _connector = new DBTCPConnector( this );
_connector.start();
if (_options.cursorFinalizerEnabled) {
@@ -547,15 +542,6 @@ public int getOptions(){
}
/**
- * Gets the credentials store.
- *
- * @return the credentials store.
- */
- public MongoCredentialsStore getCredentialsStore() {
- return _authority.getCredentialsStore();
- }
-
- /**
* Helper method for setting up MongoOptions at instantiation
* so that any options which affect this connection can be set.
*/
@@ -776,7 +762,7 @@ public String toString() {
* @return the authority
*/
- public MongoAuthority getAuthority() {
+ MongoAuthority getAuthority() {
return _authority;
}
}
@@ -20,6 +20,7 @@
import org.bson.util.annotations.Immutable;
import java.util.ArrayList;
+import java.util.Arrays;
import java.util.Collections;
import java.util.List;
@@ -32,35 +33,17 @@
* @since 2.11.0
*/
@Immutable
-public class MongoAuthority {
- private final ServerAddress serverAddress;
- private final MongoCredentialsStore credentialsStore;
- private final List<ServerAddress> serverAddresses;
+class MongoAuthority {
private final Type type;
+ private final List<ServerAddress> serverAddresses;
+ private final MongoCredentialsStore credentialsStore;
/**
* Enumeration of the connection types.
*/
- public enum Type {
- /**
- *
- */
- DIRECT,
-
- /**
- *
- */
- REPLICA_SET,
-
- /**
- *
- */
- MONGOS_SET,
-
- /**
- *
- */
- DYNAMIC_SET
+ enum Type {
+ Direct,
+ Set
}
/**
@@ -97,64 +80,6 @@ public static MongoAuthority direct(ServerAddress serverAddress, MongoCredential
* @param serverAddresses
* @return
*/
- public static MongoAuthority replicaSet(List<ServerAddress> serverAddresses) {
- return replicaSet(serverAddresses, (MongoCredentials) null);
- }
-
- /**
- *
- * @param serverAddresses
- * @param credentials
- * @return
- */
- public static MongoAuthority replicaSet(List<ServerAddress> serverAddresses, MongoCredentials credentials) {
- return replicaSet(serverAddresses, new MongoCredentialsStore(credentials));
- }
-
- /**
- *
- * @param serverAddresses
- * @param credentialsStore
- * @return
- */
- public static MongoAuthority replicaSet(List<ServerAddress> serverAddresses, MongoCredentialsStore credentialsStore) {
- return new MongoAuthority(serverAddresses, Type.REPLICA_SET, credentialsStore);
- }
-
- /**
- *
- * @param serverAddresses
- * @return
- */
- public static MongoAuthority mongosSet(List<ServerAddress> serverAddresses) {
- return mongosSet(serverAddresses, (MongoCredentials) null);
- }
-
- /**
- *
- * @param serverAddresses
- * @param credentials
- * @return
- */
- public static MongoAuthority mongosSet(List<ServerAddress> serverAddresses, MongoCredentials credentials) {
- return mongosSet(serverAddresses, new MongoCredentialsStore(credentials));
- }
-
- /**
- *
- * @param serverAddresses
- * @param credentials
- * @return
- */
- public static MongoAuthority mongosSet(List<ServerAddress> serverAddresses, MongoCredentialsStore credentials) {
- return new MongoAuthority(serverAddresses, Type.MONGOS_SET, credentials);
- }
-
- /**
- *
- * @param serverAddresses
- * @return
- */
public static MongoAuthority dynamicSet(List<ServerAddress> serverAddresses) {
return dynamicSet(serverAddresses, (MongoCredentials) null);
}
@@ -176,7 +101,7 @@ public static MongoAuthority dynamicSet(List<ServerAddress> serverAddresses, Mon
* @return
*/
public static MongoAuthority dynamicSet(List<ServerAddress> serverAddresses, MongoCredentialsStore credentialsStore) {
- return new MongoAuthority(serverAddresses, Type.DYNAMIC_SET, credentialsStore);
+ return new MongoAuthority(serverAddresses, Type.Set, credentialsStore);
}
/**
@@ -194,10 +119,9 @@ private MongoAuthority(final ServerAddress serverAddress, MongoCredentialsStore
throw new IllegalArgumentException("credentialsStore can not be null");
}
- this.serverAddress = serverAddress;
+ this.serverAddresses = Arrays.asList(serverAddress);
this.credentialsStore = credentialsStore;
- this.serverAddresses = null;
- this.type = Type.DIRECT;
+ this.type = Type.Direct;
}
/**
@@ -220,40 +144,19 @@ private MongoAuthority(final List<ServerAddress> serverAddresses, Type type, Mon
throw new IllegalArgumentException("type can not be null");
}
- if (type == Type.DIRECT) {
- throw new IllegalArgumentException("type can not be DIRECT with a list of server addresses");
+ if (type == Type.Direct) {
+ throw new IllegalArgumentException("type can not be Direct with a list of server addresses");
}
this.type = type;
this.serverAddresses = new ArrayList<ServerAddress>(serverAddresses);
this.credentialsStore = credentialsStore;
- this.serverAddress = null;
- }
-
- /**
- * Returns whether this is a direct connection to a single server.
- *
- * @return true if a direct connection
- */
- public boolean isDirect() {
- return serverAddress != null;
- }
-
- /**
- * Returns the single server address of a direction connection, or null if it's not a direction connection.
- *
- * @return the server address
- * @see com.mongodb.MongoAuthority#isDirect()
- */
- public ServerAddress getServerAddress() {
- return serverAddress;
}
/**
- * Returns the list of server addresses if this is not a direction connection. or null if it is.
+ * Returns the list of server addresses.
*
* @return the server address list
- * @see com.mongodb.MongoAuthority#isDirect()
*/
public List<ServerAddress> getServerAddresses() {
return serverAddresses == null ? null : Collections.unmodifiableList(serverAddresses);
@@ -286,31 +189,26 @@ public boolean equals(final Object o) {
final MongoAuthority that = (MongoAuthority) o;
if (!credentialsStore.equals(that.credentialsStore)) return false;
- if (serverAddress != null ? !serverAddress.equals(that.serverAddress) : that.serverAddress != null)
- return false;
- if (serverAddresses != null ? !serverAddresses.equals(that.serverAddresses) : that.serverAddresses != null)
- return false;
+ if (!serverAddresses.equals(that.serverAddresses)) return false;
if (type != that.type) return false;
return true;
}
@Override
public int hashCode() {
- int result = serverAddress != null ? serverAddress.hashCode() : 0;
- result = 31 * result + credentialsStore.hashCode();
- result = 31 * result + (serverAddresses != null ? serverAddresses.hashCode() : 0);
+ int result = credentialsStore.hashCode();
+ result = 31 * result + serverAddresses.hashCode();
result = 31 * result + type.hashCode();
return result;
}
@Override
public String toString() {
return "MongoAuthority{" +
- "serverAddress=" + serverAddress +
- ", credentialsStore=" + credentialsStore +
+ "type=" + type +
", serverAddresses=" + serverAddresses +
- ", type=" + type +
+ ", credentials=" + credentialsStore +
'}';
}
}
Oops, something went wrong.

0 comments on commit 1f07df6

Please sign in to comment.