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

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

The newest version!
/*******************************************************************************
 *   © 2019-2024 SAP SE or an SAP affiliate company. All rights reserved.
 ******************************************************************************/

package com.sap.cloud.mt.runtime;

import com.sap.cloud.mt.subscription.InstanceLifecycleManager;

import java.util.HashMap;
import java.util.Locale;
import java.util.Map;

public class DataSourceLookupBuilder {
	private static final String TOMCAT = "tomcat";
	private static final String HIKARI = "hikari";
	private InstanceLifecycleManager instanceLifecycleManager;
	private EnvironmentAccess environmentAccess;
	private String poolProvider;
	private boolean oneDataSourcePerDb;
	private boolean registerJmx;
	private Map prefixToPoolType;

	private DataSourceLookupBuilder() {
	}

	public static DataSourceLookupBuilder create() {
		return new DataSourceLookupBuilder();
	}

	public DataSourceLookup build() {
		DataPoolSettings dataPoolSettings;
		if (prefixToPoolType != null && !prefixToPoolType.isEmpty()) {
			dataPoolSettings = new DataPoolSettings(prefixToPoolType);
		} else {
			Map config = new HashMap<>();
			config.put("spring.datasource.hikari", DataPoolSettings.ConnectionPoolType.HIKARI);
			config.put("spring.datasource.tomcat", DataPoolSettings.ConnectionPoolType.TOMCAT);
			dataPoolSettings = new DataPoolSettings(config);
		}
		if (poolProvider == null || poolProvider.isEmpty()) {
			poolProvider = HIKARI;
		}
		switch (poolProvider.toLowerCase(Locale.ENGLISH)) {
			case TOMCAT:
				return new TomcatDataSourceLookup(instanceLifecycleManager, environmentAccess, dataPoolSettings,
						oneDataSourcePerDb, registerJmx);
			case HIKARI:
				return new HikariDataSourceLookup(instanceLifecycleManager, environmentAccess, dataPoolSettings,
						oneDataSourcePerDb);
			default:
				throw new IllegalArgumentException("Pool provider " + poolProvider + " is not supported");
		}
	}

	public DataSourceLookupBuilder instanceLifecycleManager(InstanceLifecycleManager instanceLifecycleManager) {
		this.instanceLifecycleManager = instanceLifecycleManager;
		return this;
	}

	public DataSourceLookupBuilder environmentAccess(EnvironmentAccess environmentAccess) {
		this.environmentAccess = environmentAccess;
		return this;
	}

	public DataSourceLookupBuilder poolProvider(String poolProvider) {
		this.poolProvider = poolProvider;
		return this;
	}

	public DataSourceLookupBuilder oneDataSourcePerDb(boolean oneDataSourcePerDb) {
		this.oneDataSourcePerDb = oneDataSourcePerDb;
		return this;
	}

	public DataSourceLookupBuilder registerJmx(boolean registerJmx) {
		this.registerJmx = registerJmx;
		return this;
	}

	public DataSourceLookupBuilder prefixToPoolType(Map prefixToPoolType) {
		this.prefixToPoolType = prefixToPoolType;
		return this;
	}
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy