@ -202,59 +202,69 @@ public class MongoProperties {
public MongoClient createMongoClient ( MongoClientOptions options ,
public MongoClient createMongoClient ( MongoClientOptions options ,
Environment environment ) throws UnknownHostException {
Environment environment ) throws UnknownHostException {
try {
try {
if ( hasCustomAddress ( ) | | hasCustomCredentials ( ) ) {
Integer embeddedPort = getEmbeddedPort ( environment ) ;
if ( this . uri ! = null ) {
if ( embeddedPort ! = null ) {
throw new IllegalStateException ( "Invalid mongo configuration, "
return createEmbeddedMongoClient ( options , embeddedPort ) ;
+ "either uri or host/port/credentials must be specified" ) ;
}
if ( options = = null ) {
options = MongoClientOptions . builder ( ) . build ( ) ;
}
List < MongoCredential > credentials = new ArrayList < MongoCredential > ( ) ;
if ( hasCustomCredentials ( ) ) {
String database = this . authenticationDatabase = = null
? getMongoClientDatabase ( ) : this . authenticationDatabase ;
credentials . add ( MongoCredential . createCredential ( this . username ,
database , this . password ) ) ;
}
String host = this . host = = null ? "localhost" : this . host ;
int port = determinePort ( environment ) ;
return new MongoClient (
Collections . singletonList ( new ServerAddress ( host , port ) ) ,
credentials , options ) ;
}
}
// The options and credentials are in the URI
return createNetworkMongoClient ( options ) ;
return new MongoClient ( new MongoClientURI ( determineUri ( ) , builder ( options ) ) ) ;
}
}
finally {
finally {
clearPassword ( ) ;
clearPassword ( ) ;
}
}
}
}
private boolean hasCustomAddress ( ) {
private Integer getEmbeddedPort ( Environment environment ) {
return this . host ! = null | | this . port ! = null ;
if ( environment ! = null ) {
String localPort = environment . getProperty ( "local.mongo.port" ) ;
if ( localPort ! = null ) {
return Integer . valueOf ( localPort ) ;
}
}
return null ;
}
}
private boolean hasCustomCredentials ( ) {
private MongoClient createEmbeddedMongoClient ( MongoClientOptions options , int port ) {
return this . username ! = null & & this . password ! = null ;
if ( options = = null ) {
options = MongoClientOptions . builder ( ) . build ( ) ;
}
String host = this . host = = null ? "localhost" : this . host ;
return new MongoClient (
Collections . singletonList ( new ServerAddress ( host , port ) ) ,
Collections . < MongoCredential > emptyList ( ) , options ) ;
}
}
private int determinePort ( Environment environment ) {
private MongoClient createNetworkMongoClient ( MongoClientOptions options ) {
if ( this . port = = null ) {
if ( hasCustomAddress ( ) | | hasCustomCredentials ( ) ) {
return DEFAULT_PORT ;
if ( this . uri ! = null ) {
}
throw new IllegalStateException ( "Invalid mongo configuration, "
if ( this . port = = 0 ) {
+ "either uri or host/port/credentials must be specified" ) ;
if ( environment ! = null ) {
}
String localPort = environment . getProperty ( "local.mongo.port" ) ;
if ( options = = null ) {
if ( localPort ! = null ) {
options = MongoClientOptions . builder ( ) . build ( ) ;
return Integer . valueOf ( localPort ) ;
}
}
List < MongoCredential > credentials = new ArrayList < MongoCredential > ( ) ;
if ( hasCustomCredentials ( ) ) {
String database = this . authenticationDatabase = = null
? getMongoClientDatabase ( ) : this . authenticationDatabase ;
credentials . add ( MongoCredential . createCredential ( this . username ,
database , this . password ) ) ;
}
}
throw new IllegalStateException (
String host = this . host = = null ? "localhost" : this . host ;
"spring.data.mongodb.port=0 and no local mongo port configuration "
int port = this . port ! = null ? this . port : DEFAULT_PORT ;
+ "is available" ) ;
return new MongoClient (
Collections . singletonList ( new ServerAddress ( host , port ) ) ,
credentials , options ) ;
}
}
return this . port ;
// The options and credentials are in the URI
return new MongoClient ( new MongoClientURI ( determineUri ( ) , builder ( options ) ) ) ;
}
private boolean hasCustomAddress ( ) {
return this . host ! = null | | this . port ! = null ;
}
private boolean hasCustomCredentials ( ) {
return this . username ! = null & & this . password ! = null ;
}
}
private Builder builder ( MongoClientOptions options ) {
private Builder builder ( MongoClientOptions options ) {