Search in sources :

Example 26 with ClassLoaderService

use of org.hibernate.boot.registry.classloading.spi.ClassLoaderService in project hibernate-orm by hibernate.

the class StatisticsInitiator method initiateServiceInternal.

private StatisticsImplementor initiateServiceInternal(SessionFactoryImplementor sessionFactory, Object configValue, ServiceRegistryImplementor registry) {
    StatisticsFactory statisticsFactory;
    if (configValue == null) {
        statisticsFactory = DEFAULT_STATS_BUILDER;
    } else if (StatisticsFactory.class.isInstance(configValue)) {
        statisticsFactory = (StatisticsFactory) configValue;
    } else {
        // assume it names the factory class
        final ClassLoaderService classLoaderService = registry.getService(ClassLoaderService.class);
        try {
            statisticsFactory = (StatisticsFactory) classLoaderService.classForName(configValue.toString()).newInstance();
        } catch (HibernateException e) {
            throw e;
        } catch (Exception e) {
            throw new HibernateException("Unable to instantiate specified StatisticsFactory implementation [" + configValue.toString() + "]", e);
        }
    }
    StatisticsImplementor statistics = statisticsFactory.buildStatistics(sessionFactory);
    final boolean enabled = sessionFactory.getSettings().isStatisticsEnabled();
    statistics.setStatisticsEnabled(enabled);
    LOG.debugf("Statistics initialized [enabled=%s]", enabled);
    return statistics;
}
Also used : HibernateException(org.hibernate.HibernateException) StatisticsImplementor(org.hibernate.stat.spi.StatisticsImplementor) StatisticsFactory(org.hibernate.stat.spi.StatisticsFactory) HibernateException(org.hibernate.HibernateException) ClassLoaderService(org.hibernate.boot.registry.classloading.spi.ClassLoaderService)

Example 27 with ClassLoaderService

use of org.hibernate.boot.registry.classloading.spi.ClassLoaderService in project hibernate-orm by hibernate.

the class DriverManagerConnectionProviderImpl method loadDriverIfPossible.

private Driver loadDriverIfPossible(String driverClassName) {
    if (driverClassName == null) {
        log.debug("No driver class specified");
        return null;
    }
    if (serviceRegistry != null) {
        final ClassLoaderService classLoaderService = serviceRegistry.getService(ClassLoaderService.class);
        final Class<Driver> driverClass = classLoaderService.classForName(driverClassName);
        try {
            return driverClass.newInstance();
        } catch (Exception e) {
            throw new ServiceException("Specified JDBC Driver " + driverClassName + " could not be loaded", e);
        }
    }
    try {
        return (Driver) Class.forName(driverClassName).newInstance();
    } catch (Exception e1) {
        throw new ServiceException("Specified JDBC Driver " + driverClassName + " could not be loaded", e1);
    }
}
Also used : ServiceException(org.hibernate.service.spi.ServiceException) Driver(java.sql.Driver) UnknownUnwrapTypeException(org.hibernate.service.UnknownUnwrapTypeException) SQLException(java.sql.SQLException) HibernateException(org.hibernate.HibernateException) ServiceException(org.hibernate.service.spi.ServiceException) ClassLoaderService(org.hibernate.boot.registry.classloading.spi.ClassLoaderService)

Example 28 with ClassLoaderService

use of org.hibernate.boot.registry.classloading.spi.ClassLoaderService 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 == MultiTenancyStrategy.NONE || strategy == MultiTenancyStrategy.DISCRIMINATOR) {
        // 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)

Example 29 with ClassLoaderService

use of org.hibernate.boot.registry.classloading.spi.ClassLoaderService in project hibernate-orm by hibernate.

the class DefaultIdentifierGeneratorFactory method getIdentifierGeneratorClass.

@Override
public Class getIdentifierGeneratorClass(String strategy) {
    if ("hilo".equals(strategy)) {
        throw new UnsupportedOperationException("Support for 'hilo' generator has been removed");
    }
    String resolvedStrategy = "native".equals(strategy) ? getDialect().getNativeIdentifierGeneratorStrategy() : strategy;
    Class generatorClass = generatorStrategyToClassNameMap.get(resolvedStrategy);
    try {
        if (generatorClass == null) {
            final ClassLoaderService cls = serviceRegistry.getService(ClassLoaderService.class);
            generatorClass = cls.classForName(resolvedStrategy);
        }
    } catch (ClassLoadingException e) {
        throw new MappingException(String.format("Could not interpret id generator strategy [%s]", strategy));
    }
    return generatorClass;
}
Also used : ClassLoadingException(org.hibernate.boot.registry.classloading.spi.ClassLoadingException) ClassLoaderService(org.hibernate.boot.registry.classloading.spi.ClassLoaderService) MappingException(org.hibernate.MappingException)

Aggregations

ClassLoaderService (org.hibernate.boot.registry.classloading.spi.ClassLoaderService)29 HibernateException (org.hibernate.HibernateException)8 MappingException (org.hibernate.MappingException)6 ClassLoadingException (org.hibernate.boot.registry.classloading.spi.ClassLoadingException)6 HashSet (java.util.HashSet)3 ServiceRegistry (org.hibernate.service.ServiceRegistry)3 IOException (java.io.IOException)2 SQLException (java.sql.SQLException)2 Set (java.util.Set)2 AttributeConverter (javax.persistence.AttributeConverter)2 ClassLoaderAccessImpl (org.hibernate.boot.internal.ClassLoaderAccessImpl)2 ClassLoaderAccess (org.hibernate.boot.spi.ClassLoaderAccess)2 ServiceException (org.hibernate.service.spi.ServiceException)2 BasicTypeRegistry (org.hibernate.type.BasicTypeRegistry)2 File (java.io.File)1 InputStream (java.io.InputStream)1 InvalidObjectException (java.io.InvalidObjectException)1 OutputStreamWriter (java.io.OutputStreamWriter)1 Annotation (java.lang.annotation.Annotation)1 InvocationTargetException (java.lang.reflect.InvocationTargetException)1