Search in sources :

Example 1 with MultiTenancyStrategy

use of org.hibernate.MultiTenancyStrategy in project hibernate-orm by hibernate.

the class ConnectionProviderInitiator method initiateService.

@Override
public ConnectionProvider initiateService(Map configurationValues, ServiceRegistryImplementor registry) {
    final MultiTenancyStrategy strategy = MultiTenancyStrategy.determineMultiTenancyStrategy(configurationValues);
    if (strategy == MultiTenancyStrategy.DATABASE || strategy == MultiTenancyStrategy.SCHEMA) {
        // nothing to do, but given the separate hierarchies have to handle this here.
        return null;
    }
    final StrategySelector strategySelector = registry.getService(StrategySelector.class);
    final Object explicitSetting = configurationValues.get(AvailableSettings.CONNECTION_PROVIDER);
    if (explicitSetting != null) {
        // if we are explicitly supplied a ConnectionProvider to use (in some form) -> use it..
        if (explicitSetting instanceof ConnectionProvider) {
            return (ConnectionProvider) explicitSetting;
        } else if (explicitSetting instanceof Class) {
            final Class providerClass = (Class) explicitSetting;
            LOG.instantiatingExplicitConnectionProvider(providerClass.getName());
            return instantiateExplicitConnectionProvider(providerClass);
        } else {
            String providerName = StringHelper.nullIfEmpty(explicitSetting.toString());
            if (providerName != null) {
                if (LEGACY_CONNECTION_PROVIDER_MAPPING.containsKey(providerName)) {
                    final String actualProviderName = LEGACY_CONNECTION_PROVIDER_MAPPING.get(providerName);
                    DeprecationLogger.DEPRECATION_LOGGER.connectionProviderClassDeprecated(providerName, actualProviderName);
                    providerName = actualProviderName;
                }
                LOG.instantiatingExplicitConnectionProvider(providerName);
                final Class providerClass = strategySelector.selectStrategyImplementor(ConnectionProvider.class, providerName);
                try {
                    return instantiateExplicitConnectionProvider(providerClass);
                } catch (Exception e) {
                    throw new HibernateException("Could not instantiate connection provider [" + providerName + "]", e);
                }
            }
        }
    }
    if (configurationValues.get(AvailableSettings.DATASOURCE) != null) {
        return new DatasourceConnectionProviderImpl();
    }
    ConnectionProvider connectionProvider = null;
    final Class<? extends ConnectionProvider> singleRegisteredProvider = getSingleRegisteredProvider(strategySelector);
    if (singleRegisteredProvider != null) {
        try {
            connectionProvider = singleRegisteredProvider.newInstance();
        } catch (IllegalAccessException | InstantiationException e) {
            throw new HibernateException("Could not instantiate singular-registered ConnectionProvider", e);
        }
    }
    if (connectionProvider == null) {
        if (c3p0ConfigDefined(configurationValues)) {
            connectionProvider = instantiateC3p0Provider(strategySelector);
        }
    }
    if (connectionProvider == null) {
        if (proxoolConfigDefined(configurationValues)) {
            connectionProvider = instantiateProxoolProvider(strategySelector);
        }
    }
    if (connectionProvider == null) {
        if (hikariConfigDefined(configurationValues)) {
            connectionProvider = instantiateHikariProvider(strategySelector);
        }
    }
    if (connectionProvider == null) {
        if (viburConfigDefined(configurationValues)) {
            connectionProvider = instantiateViburProvider(strategySelector);
        }
    }
    if (connectionProvider == null) {
        if (agroalConfigDefined(configurationValues)) {
            connectionProvider = instantiateAgroalProvider(strategySelector);
        }
    }
    if (connectionProvider == null) {
        if (configurationValues.get(AvailableSettings.URL) != null) {
            connectionProvider = new DriverManagerConnectionProviderImpl();
        }
    }
    if (connectionProvider == null) {
        LOG.noAppropriateConnectionProvider();
        connectionProvider = new UserSuppliedConnectionProviderImpl();
    }
    final Map injectionData = (Map) configurationValues.get(INJECTION_DATA);
    if (injectionData != null && injectionData.size() > 0) {
        final ConnectionProvider theConnectionProvider = connectionProvider;
        new BeanInfoHelper(connectionProvider.getClass()).applyToBeanInfo(connectionProvider, new BeanInfoHelper.BeanInfoDelegate() {

            public void processBeanInfo(BeanInfo beanInfo) throws Exception {
                final PropertyDescriptor[] descriptors = beanInfo.getPropertyDescriptors();
                for (PropertyDescriptor descriptor : descriptors) {
                    final String propertyName = descriptor.getName();
                    if (injectionData.containsKey(propertyName)) {
                        final Method method = descriptor.getWriteMethod();
                        method.invoke(theConnectionProvider, injectionData.get(propertyName));
                    }
                }
            }
        });
    }
    return connectionProvider;
}
Also used : PropertyDescriptor(java.beans.PropertyDescriptor) HibernateException(org.hibernate.HibernateException) BeanInfo(java.beans.BeanInfo) Method(java.lang.reflect.Method) StrategySelector(org.hibernate.boot.registry.selector.spi.StrategySelector) HibernateException(org.hibernate.HibernateException) ConnectionProvider(org.hibernate.engine.jdbc.connections.spi.ConnectionProvider) MultiTenancyStrategy(org.hibernate.MultiTenancyStrategy) BeanInfoHelper(org.hibernate.internal.util.beans.BeanInfoHelper) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) HashMap(java.util.HashMap) Map(java.util.Map)

Example 2 with MultiTenancyStrategy

use of org.hibernate.MultiTenancyStrategy in project hibernate-orm by hibernate.

the class MultiTenantConnectionProviderInitiator method initiateService.

@Override
@SuppressWarnings({ "unchecked" })
public MultiTenantConnectionProvider initiateService(Map configurationValues, ServiceRegistryImplementor registry) {
    final MultiTenancyStrategy strategy = MultiTenancyStrategy.determineMultiTenancyStrategy(configurationValues);
    if (!strategy.requiresMultiTenantConnectionProvider()) {
        // nothing to do, but given the separate hierarchies have to handle this here.
        return null;
    }
    final Object configValue = configurationValues.get(AvailableSettings.MULTI_TENANT_CONNECTION_PROVIDER);
    if (configValue == null) {
        // if they also specified the data source *name*, then lets assume they want
        // DataSourceBasedMultiTenantConnectionProviderImpl
        final Object dataSourceConfigValue = configurationValues.get(AvailableSettings.DATASOURCE);
        if (dataSourceConfigValue != null && String.class.isInstance(dataSourceConfigValue)) {
            return new DataSourceBasedMultiTenantConnectionProviderImpl();
        }
        return null;
    }
    if (MultiTenantConnectionProvider.class.isInstance(configValue)) {
        return (MultiTenantConnectionProvider) configValue;
    } else {
        final Class<MultiTenantConnectionProvider> implClass;
        if (Class.class.isInstance(configValue)) {
            implClass = (Class) configValue;
        } else {
            final String className = configValue.toString();
            final ClassLoaderService classLoaderService = registry.getService(ClassLoaderService.class);
            try {
                implClass = classLoaderService.classForName(className);
            } catch (ClassLoadingException cle) {
                log.warn("Unable to locate specified class [" + className + "]", cle);
                throw new ServiceException("Unable to locate specified multi-tenant connection provider [" + className + "]");
            }
        }
        try {
            return implClass.newInstance();
        } catch (Exception e) {
            log.warn("Unable to instantiate specified class [" + implClass.getName() + "]", e);
            throw new ServiceException("Unable to instantiate specified multi-tenant connection provider [" + implClass.getName() + "]");
        }
    }
}
Also used : MultiTenancyStrategy(org.hibernate.MultiTenancyStrategy) ServiceException(org.hibernate.service.spi.ServiceException) ClassLoadingException(org.hibernate.boot.registry.classloading.spi.ClassLoadingException) MultiTenantConnectionProvider(org.hibernate.engine.jdbc.connections.spi.MultiTenantConnectionProvider) DataSourceBasedMultiTenantConnectionProviderImpl(org.hibernate.engine.jdbc.connections.spi.DataSourceBasedMultiTenantConnectionProviderImpl) ClassLoadingException(org.hibernate.boot.registry.classloading.spi.ClassLoadingException) ServiceException(org.hibernate.service.spi.ServiceException) ClassLoaderService(org.hibernate.boot.registry.classloading.spi.ClassLoaderService)

Aggregations

MultiTenancyStrategy (org.hibernate.MultiTenancyStrategy)2 BeanInfo (java.beans.BeanInfo)1 PropertyDescriptor (java.beans.PropertyDescriptor)1 Method (java.lang.reflect.Method)1 HashMap (java.util.HashMap)1 Map (java.util.Map)1 ConcurrentHashMap (java.util.concurrent.ConcurrentHashMap)1 HibernateException (org.hibernate.HibernateException)1 ClassLoaderService (org.hibernate.boot.registry.classloading.spi.ClassLoaderService)1 ClassLoadingException (org.hibernate.boot.registry.classloading.spi.ClassLoadingException)1 StrategySelector (org.hibernate.boot.registry.selector.spi.StrategySelector)1 ConnectionProvider (org.hibernate.engine.jdbc.connections.spi.ConnectionProvider)1 DataSourceBasedMultiTenantConnectionProviderImpl (org.hibernate.engine.jdbc.connections.spi.DataSourceBasedMultiTenantConnectionProviderImpl)1 MultiTenantConnectionProvider (org.hibernate.engine.jdbc.connections.spi.MultiTenantConnectionProvider)1 BeanInfoHelper (org.hibernate.internal.util.beans.BeanInfoHelper)1 ServiceException (org.hibernate.service.spi.ServiceException)1