SimpleMongoClientDatabaseFactory.java

/*
 * Copyright 2018-present 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.data.mongodb.core;

import org.springframework.beans.factory.DisposableBean;
import org.springframework.data.mongodb.MongoClusterCapable;
import org.springframework.data.mongodb.MongoDatabaseFactory;

import com.mongodb.ClientSessionOptions;
import com.mongodb.ConnectionString;
import com.mongodb.client.ClientSession;
import com.mongodb.client.MongoClient;
import com.mongodb.client.MongoClients;
import com.mongodb.client.MongoCluster;
import com.mongodb.client.MongoDatabase;

/**
 * Factory to create {@link MongoDatabase} instances from a {@link MongoClient} instance.
 *
 * @author Christoph Strobl
 * @since 3.0
 */
public class SimpleMongoClientDatabaseFactory extends MongoDatabaseFactorySupport<MongoClient>
		implements MongoClusterCapable, DisposableBean {

	/**
	 * Creates a new {@link SimpleMongoClientDatabaseFactory} instance for the given {@code connectionString}. Using this
	 * constructor will create a new {@link MongoClient} instance that will be closed when calling {@link #destroy()}.
	 *
	 * @param connectionString connection coordinates for a database connection. Must contain a database name and must not
	 *          be {@literal null} or empty.
	 * @see <a href="https://docs.mongodb.com/manual/reference/connection-string/">MongoDB Connection String reference</a>
	 */
	public SimpleMongoClientDatabaseFactory(String connectionString) {
		this(new ConnectionString(connectionString));
	}

	/**
	 * Creates a new {@link SimpleMongoClientDatabaseFactory} instance from the given {@link MongoClient}. Using this
	 * constructor will create a new {@link MongoClient} instance that will be closed when calling {@link #destroy()}.
	 *
	 * @param connectionString connection coordinates for a database connection. Must contain also a database name and not
	 *          be {@literal null}.
	 */
	public SimpleMongoClientDatabaseFactory(ConnectionString connectionString) {
		this(MongoClients.create(connectionString), connectionString.getDatabase(), true);
	}

	/**
	 * Creates a new {@link SimpleMongoClientDatabaseFactory} instance from the given {@link MongoClient}. Note that the
	 * client will not be closed when calling {@link #destroy()} as we assume a managed client instance that we do not
	 * want to close on {@link #destroy()} meaning that you (or the application container) must dispose the client
	 * instance once it is no longer required for use.
	 *
	 * @param mongoClient must not be {@literal null}.
	 * @param databaseName must not be {@literal null} or empty.
	 */
	public SimpleMongoClientDatabaseFactory(MongoClient mongoClient, String databaseName) {
		this(mongoClient, databaseName, false);
	}

	/**
	 * Creates a new {@link SimpleMongoClientDatabaseFactory} instance from the given {@link MongoClient}.
	 *
	 * @param mongoClient must not be {@literal null}.
	 * @param databaseName must not be {@literal null} or empty.
	 * @param mongoInstanceCreated
	 */
	SimpleMongoClientDatabaseFactory(MongoClient mongoClient, String databaseName, boolean mongoInstanceCreated) {
		super(mongoClient, databaseName, mongoInstanceCreated, MongoExceptionTranslator.DEFAULT_EXCEPTION_TRANSLATOR);
	}

	@Override
	public ClientSession getSession(ClientSessionOptions options) {
		return getMongoCluster().startSession(options);
	}

	@Override
	public MongoDatabaseFactory withSession(ClientSession session) {
		return new ClientCapableSessionBoundDatabaseFactory(session, this);
	}

	@Override
	protected void closeClient() {
		getMongoCluster().close();
	}

	@Override
	protected MongoDatabase doGetMongoDatabase(String dbName) {
		return getMongoCluster().getDatabase(dbName);
	}

	static class ClientCapableSessionBoundDatabaseFactory extends ClientSessionBoundMongoDbFactory
			implements MongoClusterCapable {

		private final MongoClusterCapable delegate;

		/**
		 * Create a new session-bound factory that delegates to the given factory and exposes the cluster via
		 * {@link MongoClusterCapable}.
		 *
		 * @param session the {@link ClientSession} to bind; must not be {@literal null}.
		 * @param delegate the delegate factory; must not be {@literal null}.
		 */
		ClientCapableSessionBoundDatabaseFactory(ClientSession session, SimpleMongoClientDatabaseFactory delegate) {
			super(session, delegate);
			this.delegate = delegate;
		}

		private MongoCluster proxyMongoCluster(MongoCluster cluster) {
			return createProxyInstance(session(), cluster, MongoCluster.class);
		}

		@Override
		public MongoCluster getMongoCluster() {
			return proxyMongoCluster(delegate.getMongoCluster());
		}
	}

}