All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.sap.cloud.mt.runtime.TenantAwareDataSource Maven / Gradle / Ivy

There is a newer version: 3.3.3
Show newest version
/******************************************************************************
 * © 2020 SAP SE or an SAP affiliate company. All rights reserved.            *
 ******************************************************************************/

package com.sap.cloud.mt.runtime;

import com.sap.cloud.mt.subscription.DataSourceInfo;
import com.sap.cloud.mt.subscription.exceptions.InternalError;
import com.sap.cloud.mt.subscription.exceptions.UnknownTenant;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import javax.sql.DataSource;
import java.io.PrintWriter;
import java.sql.Connection;
import java.sql.SQLException;
import java.sql.SQLFeatureNotSupportedException;
import java.util.function.BiFunction;
import java.util.function.Supplier;

public class TenantAwareDataSource implements DataSource {

	private static final Logger logger = LoggerFactory.getLogger(TenantAwareDataSource.class);
	private final Supplier tenantProvider;
	private final DataSourceLookup dataSourceLookup;
	//                       db driver,is one data source per db
	private final BiFunction connectionProviderFunction = new ConnectionProviderFunction();

	public TenantAwareDataSource(Supplier tenantProvider, DataSourceLookup dataSourceLookup) {
		this.tenantProvider = tenantProvider;
		this.dataSourceLookup = dataSourceLookup;
		if (this.tenantProvider == null) {
			throw new IllegalArgumentException("No tenant provider set");
		}
		if (this.dataSourceLookup == null) {
			throw new IllegalArgumentException("No data source lookup object set");
		}
	}

	public TenantAwareDataSource(TenantProvider tenantProvider, DataSourceLookup dataSourceLookup) {
		this((Supplier) tenantProvider, dataSourceLookup);
	}

	private DataSourceAndInfo getDataSourceAndInfo() throws InternalError, UnknownTenant {
		return dataSourceLookup.getDataSourceAndInfo(tenantProvider.get());
	}

	@Override
	public Connection getConnection() throws SQLException {
		ConnectionProvider connectionProvider = null;
		DataSourceAndInfo dsAndInfo = null;
		try {
			dsAndInfo = getDataSourceAndInfo();
		} catch(InternalError | UnknownTenant error) {
			logger.error("Could not get a data source because of error {}", error.getMessage());
			throw new SQLException(error);
		}
		DataSourceInfo info = dsAndInfo.getDataSourceInfo();
		connectionProvider = connectionProviderFunction.apply(info.getDriver(), dataSourceLookup.isOneDataSourcePerDb());
		if (connectionProvider == null) {
			throw new SQLException("No connection provider determined for driver " + info.getDriver() +
					"and flag oneDataSourcePerDb=" + dataSourceLookup.isOneDataSourcePerDb());
		}
		try {
			return connectionProvider.getConnection(tenantProvider.get(), dsAndInfo);
		} catch(SQLException e) {
			logger.debug("Could not retrieve a connection => fix data source");
			try {
				dataSourceLookup.fixDataSourceAfterCredentialChange(tenantProvider.get());
				return connectionProvider.getConnection(tenantProvider.get(), getDataSourceAndInfo());
			} catch(InternalError | UnknownTenant | SQLException error) {
				logger.error("Could not retrieve a connection because of error {}", error.getMessage());
				throw new SQLException(error);
			}
		}
	}

	@Override
	public Connection getConnection(String username, String password) throws SQLException {
		throw new UnsupportedOperationException("getConnection with username and password isn't supported");
	}

	@Override
	@SuppressWarnings("unchecked")
	public  T unwrap(Class iface) throws SQLException {
		if (iface.isInstance(this)) {
			return (T) this;
		}
		try {
			return getDataSourceAndInfo().getDataSource().unwrap(iface);
		} catch(InternalError | UnknownTenant | SQLException e) {
			logger.error("Could not get data source because of {}", e.getMessage());
			throw new SQLException(e);
		}
	}

	@Override
	public boolean isWrapperFor(Class iface) throws SQLException {
		try {
			return (iface.isInstance(this) || getDataSourceAndInfo().getDataSource().isWrapperFor(iface));
		} catch(InternalError | UnknownTenant | SQLException e) {
			logger.error("Could not get data source because of {}", e.getMessage());
			throw new SQLException(e);
		}
	}

	@Override
	public PrintWriter getLogWriter() throws SQLException {
		logger.error("Operation getLogWriter is not supported");
		throw new UnsupportedOperationException("Operation getLogWriter is not supported");
	}

	@Override
	public void setLogWriter(PrintWriter out) throws SQLException {
		logger.error("Operation setLogWriter is not supported");
		throw new UnsupportedOperationException("Operation setLogWriter is not supported");
	}

	@Override
	public void setLoginTimeout(int seconds) throws SQLException {
		logger.error("Operation setLoginTimeout is not supported");
		throw new UnsupportedOperationException("Operation setLoginTimeout is not supported");
	}

	@Override
	public int getLoginTimeout() throws SQLException {
		return 0;
	}

	@Override
	public java.util.logging.Logger getParentLogger() throws SQLFeatureNotSupportedException {
		return java.util.logging.Logger.getLogger(java.util.logging.Logger.GLOBAL_LOGGER_NAME);

	}

	public void deleteFromCache(String tenantId) {
		dataSourceLookup.deleteFromCache(tenantId);
	}

	DataSourceLookup getDataSourceLookup() {
		return dataSourceLookup;
	}

	public Supplier getTenantProvider() {
		return tenantProvider;
	}

	boolean doesTenantExist(String tenantId) {
		return dataSourceLookup.doesTenantExist(tenantId);
	}

	void fixDataSource(String tenantId) throws InternalError {
		dataSourceLookup.fixDataSourceAfterCredentialChange(tenantId);
	}
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy