Add ConnectionDetail support to Mongo auto-configuration
Update Mongo auto-configuration so that `MongoConnectionDetails` beans may be optionally used to provide connection details. See gh-34657 Co-Authored-By: Mortitz Halbritter <mkammerer@vmware.com> Co-Authored-By: Phillip Webb <pwebb@vmware.com>pull/34759/head
parent
042f0c8520
commit
2ef33dc81f
@ -0,0 +1,88 @@
|
||||
/*
|
||||
* Copyright 2012-2023 the original author or authors.
|
||||
*
|
||||
* 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
|
||||
*
|
||||
* https://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 org.springframework.boot.autoconfigure.mongo;
|
||||
|
||||
import com.mongodb.ConnectionString;
|
||||
|
||||
import org.springframework.boot.autoconfigure.service.connection.ConnectionDetails;
|
||||
|
||||
/**
|
||||
* Details required to establish a connection to a MongoDB service.
|
||||
*
|
||||
* @author Moritz Halbritter
|
||||
* @author Andy Wilkinson
|
||||
* @author Phillip Webb
|
||||
* @since 3.1.0
|
||||
*/
|
||||
public interface MongoConnectionDetails extends ConnectionDetails {
|
||||
|
||||
/**
|
||||
* The {@link ConnectionString} for MongoDB.
|
||||
* @return the connection string
|
||||
*/
|
||||
ConnectionString getConnectionString();
|
||||
|
||||
/**
|
||||
* GridFS configuration.
|
||||
* @return the GridFS configuration or {@code null}
|
||||
*/
|
||||
default GridFs getGridFs() {
|
||||
return null;
|
||||
}
|
||||
|
||||
/**
|
||||
* GridFS configuration.
|
||||
*/
|
||||
interface GridFs {
|
||||
|
||||
/**
|
||||
* GridFS database name.
|
||||
* @return the GridFS database name or {@code null}
|
||||
*/
|
||||
String getDatabase();
|
||||
|
||||
/**
|
||||
* GridFS bucket name.
|
||||
* @return the GridFS bucket name or {@code null}
|
||||
*/
|
||||
String getBucket();
|
||||
|
||||
/**
|
||||
* Factory method to create a new {@link GridFs} instance.
|
||||
* @param database the database
|
||||
* @param bucket the bucket name
|
||||
* @return a new {@link GridFs} instance
|
||||
*/
|
||||
static GridFs of(String database, String bucket) {
|
||||
return new GridFs() {
|
||||
|
||||
@Override
|
||||
public String getDatabase() {
|
||||
return database;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getBucket() {
|
||||
return bucket;
|
||||
}
|
||||
|
||||
};
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
}
|
@ -0,0 +1,82 @@
|
||||
/*
|
||||
* Copyright 2012-2023 the original author or authors.
|
||||
*
|
||||
* 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
|
||||
*
|
||||
* https://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 org.springframework.boot.autoconfigure.mongo;
|
||||
|
||||
import com.mongodb.ConnectionString;
|
||||
|
||||
/**
|
||||
* Adapts {@link MongoProperties} to {@link MongoConnectionDetails}.
|
||||
*
|
||||
* @author Moritz Halbritter
|
||||
* @author Andy Wilkinson
|
||||
* @author Phillip Webb
|
||||
* @since 3.1.0
|
||||
*/
|
||||
public class PropertiesMongoConnectionDetails implements MongoConnectionDetails {
|
||||
|
||||
private final MongoProperties properties;
|
||||
|
||||
public PropertiesMongoConnectionDetails(MongoProperties properties) {
|
||||
this.properties = properties;
|
||||
}
|
||||
|
||||
@Override
|
||||
public ConnectionString getConnectionString() {
|
||||
// mongodb://[username:password@]host1[:port1][,host2[:port2],...[,hostN[:portN]]][/[database.collection][?options]]
|
||||
if (this.properties.getUri() != null) {
|
||||
return new ConnectionString(this.properties.getUri());
|
||||
}
|
||||
StringBuilder builder = new StringBuilder("mongodb://");
|
||||
if (this.properties.getUsername() != null) {
|
||||
builder.append(this.properties.getUsername());
|
||||
builder.append(":");
|
||||
builder.append(this.properties.getPassword());
|
||||
builder.append("@");
|
||||
}
|
||||
builder.append((this.properties.getHost() != null) ? this.properties.getHost() : "localhost");
|
||||
if (this.properties.getPort() != null) {
|
||||
builder.append(":");
|
||||
builder.append(this.properties.getPort());
|
||||
}
|
||||
if (this.properties.getAdditionalHosts() != null) {
|
||||
builder.append(String.join(",", this.properties.getAdditionalHosts()));
|
||||
}
|
||||
if (this.properties.getMongoClientDatabase() != null || this.properties.getReplicaSetName() != null
|
||||
|| this.properties.getAuthenticationDatabase() != null) {
|
||||
builder.append("/");
|
||||
if (this.properties.getMongoClientDatabase() != null) {
|
||||
builder.append(this.properties.getMongoClientDatabase());
|
||||
}
|
||||
else if (this.properties.getAuthenticationDatabase() != null) {
|
||||
builder.append(this.properties.getAuthenticationDatabase());
|
||||
}
|
||||
if (this.properties.getReplicaSetName() != null) {
|
||||
builder.append("?");
|
||||
builder.append("repliceSet=");
|
||||
builder.append(this.properties.getReplicaSetName());
|
||||
}
|
||||
}
|
||||
return new ConnectionString(builder.toString());
|
||||
}
|
||||
|
||||
@Override
|
||||
public GridFs getGridFs() {
|
||||
return GridFs.of(PropertiesMongoConnectionDetails.this.properties.getGridfs().getDatabase(),
|
||||
PropertiesMongoConnectionDetails.this.properties.getGridfs().getBucket());
|
||||
}
|
||||
|
||||
}
|
@ -0,0 +1,68 @@
|
||||
/*
|
||||
* Copyright 2012-2023 the original author or authors.
|
||||
*
|
||||
* 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
|
||||
*
|
||||
* https://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 org.springframework.boot.autoconfigure.mongo;
|
||||
|
||||
import com.mongodb.ConnectionString;
|
||||
import com.mongodb.MongoClientSettings;
|
||||
import org.bson.UuidRepresentation;
|
||||
|
||||
import org.springframework.core.Ordered;
|
||||
|
||||
/**
|
||||
* A {@link MongoClientSettingsBuilderCustomizer} that applies standard settings to a
|
||||
* {@link MongoClientSettings}.
|
||||
*
|
||||
* @author Moritz Halbritter
|
||||
* @author Andy Wilkinson
|
||||
* @author Phillip Webb
|
||||
* @since 3.1.0
|
||||
*/
|
||||
public class StandardMongoClientSettingsBuilderCustomizer implements MongoClientSettingsBuilderCustomizer, Ordered {
|
||||
|
||||
private final ConnectionString connectionString;
|
||||
|
||||
private final UuidRepresentation uuidRepresentation;
|
||||
|
||||
private int order = 0;
|
||||
|
||||
public StandardMongoClientSettingsBuilderCustomizer(ConnectionString connectionString,
|
||||
UuidRepresentation uuidRepresentation) {
|
||||
this.connectionString = connectionString;
|
||||
this.uuidRepresentation = uuidRepresentation;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void customize(MongoClientSettings.Builder settingsBuilder) {
|
||||
settingsBuilder.uuidRepresentation(this.uuidRepresentation);
|
||||
settingsBuilder.applyConnectionString(this.connectionString);
|
||||
}
|
||||
|
||||
@Override
|
||||
public int getOrder() {
|
||||
return this.order;
|
||||
}
|
||||
|
||||
/**
|
||||
* Set the order value of this object.
|
||||
* @param order the new order value
|
||||
* @see #getOrder()
|
||||
*/
|
||||
public void setOrder(int order) {
|
||||
this.order = order;
|
||||
}
|
||||
|
||||
}
|
Loading…
Reference in New Issue