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

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

/******************************************************************************
 * © 2020 SAP SE or an SAP affiliate company. All rights reserved.            *
 ******************************************************************************/
package com.sap.cloud.mt.runtime;

import com.atomikos.jdbc.AtomikosDataSourceBean;
import com.sap.cloud.mt.runtime.DataPoolSettings.Parameter;
import com.sap.cloud.mt.subscription.DataSourceInfo;
import com.sap.cloud.mt.subscription.exceptions.InternalError;
import com.sap.db.jdbcext.XADataSourceSAP;
import org.h2.jdbcx.JdbcDataSource;

import javax.sql.DataSource;
import java.util.Map;
import java.util.Optional;
import java.util.Properties;
import java.util.stream.Stream;

public class DataSourceCreatorAtomikos extends DataSourceCreator {
    private final DataPoolSettings poolSettings;

    public DataSourceCreatorAtomikos(DataPoolSettings poolSettings) {
        this.poolSettings = poolSettings;
    }

    @Override
    protected DataSource build(DataSourceInfo info, EnvironmentAccess env) throws InternalError {
        AtomikosDataSourceBean atomikosDataSourceBean = new AtomikosDataSourceBean();
        if (info.getDriver().equals("com.sap.db.jdbc.Driver")) {
            XADataSourceSAP xaDataSourceSAP = new XADataSourceSAP();
            xaDataSourceSAP.setUrl(info.getUrl());
            xaDataSourceSAP.setUser(info.getUser());
            xaDataSourceSAP.setPassword(info.getPassword());
            xaDataSourceSAP.setSchema(info.getSchema());
            if (info.getUrl().contains("?encrypt=true")) {
                xaDataSourceSAP.setConnectionProperty("encrypt", "true");
            }
            setDriverProperties(env, xaDataSourceSAP);
            atomikosDataSourceBean.setXaDataSource(xaDataSourceSAP);
        } else if (info.getDriver().equals("org.h2.Driver")) {
            JdbcDataSource xaDataSourceH2 = new JdbcDataSource();
            xaDataSourceH2.setUrl(info.getUrl());
            xaDataSourceH2.setUser(info.getUser());
            xaDataSourceH2.setPassword(info.getPassword());
            atomikosDataSourceBean.setXaDataSource(xaDataSourceH2);
        } else {
            throw new InternalError("Driver " + info.getDriver() + " is not supported");
        }
        atomikosDataSourceBean.setUniqueResourceName(info.getTenantId() + info.getSchema());
        return atomikosDataSourceBean;
    }

    private void setDriverProperties(EnvironmentAccess env, XADataSourceSAP xaDataSourceSAP) throws InternalError {
        Optional parameter = poolSettings.getAtomikosParameters().findFirst();
        if (parameter.isPresent()) {
            String prefix = parameter.get().getPrefix();
            Map driverPropertiesFromEnv = env.getPropertiesForPrefix(prefix + ".xa-properties");
            if (driverPropertiesFromEnv != null && !driverPropertiesFromEnv.isEmpty()) {
                // Add database specific properties specified by the user
                Properties allDbProperties = xaDataSourceSAP.getProperties();
                if (allDbProperties == null) {
                    allDbProperties = new Properties();
                }
                allDbProperties.putAll(driverPropertiesFromEnv);
                xaDataSourceSAP.setProperties(allDbProperties);
            }
        } else {
            throw new InternalError("No prefix for atomikos parameters found");
        }
    }

    @Override
    protected Stream getPoolParameters() {
        return poolSettings.getAtomikosParameters();
    }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy