Search in sources :

Example 1 with StrategyRegistrationProvider

use of org.hibernate.boot.registry.selector.StrategyRegistrationProvider in project hibernate-orm by hibernate.

the class OsgiSessionFactoryService method buildSessionFactory.

private Object buildSessionFactory(Bundle requestingBundle, OsgiClassLoader osgiClassLoader) {
    final BootstrapServiceRegistryBuilder bsrBuilder = new BootstrapServiceRegistryBuilder();
    bsrBuilder.applyClassLoaderService(new OSGiClassLoaderServiceImpl(osgiClassLoader, osgiServiceUtil));
    final Integrator[] integrators = osgiServiceUtil.getServiceImpls(Integrator.class);
    for (Integrator integrator : integrators) {
        bsrBuilder.applyIntegrator(integrator);
    }
    final StrategyRegistrationProvider[] strategyRegistrationProviders = osgiServiceUtil.getServiceImpls(StrategyRegistrationProvider.class);
    for (StrategyRegistrationProvider strategyRegistrationProvider : strategyRegistrationProviders) {
        bsrBuilder.applyStrategySelectors(strategyRegistrationProvider);
    }
    final BootstrapServiceRegistry bsr = bsrBuilder.build();
    final StandardServiceRegistryBuilder ssrBuilder = new StandardServiceRegistryBuilder(bsr);
    // Allow bundles to put the config file somewhere other than the root level.
    final BundleWiring bundleWiring = (BundleWiring) requestingBundle.adapt(BundleWiring.class);
    final Collection<String> cfgResources = bundleWiring.listResources("/", "hibernate.cfg.xml", BundleWiring.LISTRESOURCES_RECURSE);
    if (cfgResources.size() == 0) {
        ssrBuilder.configure();
    } else {
        if (cfgResources.size() > 1) {
            LOG.warn("Multiple hibernate.cfg.xml files found in the persistence bundle.  Using the first one discovered.");
        }
        String cfgResource = "/" + cfgResources.iterator().next();
        ssrBuilder.configure(cfgResource);
    }
    ssrBuilder.applySetting(AvailableSettings.JTA_PLATFORM, osgiJtaPlatform);
    final StandardServiceRegistry ssr = ssrBuilder.build();
    final MetadataBuilder metadataBuilder = new MetadataSources(ssr).getMetadataBuilder();
    final TypeContributor[] typeContributors = osgiServiceUtil.getServiceImpls(TypeContributor.class);
    for (TypeContributor typeContributor : typeContributors) {
        metadataBuilder.applyTypes(typeContributor);
    }
    return metadataBuilder.build().buildSessionFactory();
}
Also used : BootstrapServiceRegistryBuilder(org.hibernate.boot.registry.BootstrapServiceRegistryBuilder) StandardServiceRegistryBuilder(org.hibernate.boot.registry.StandardServiceRegistryBuilder) MetadataBuilder(org.hibernate.boot.MetadataBuilder) BundleWiring(org.osgi.framework.wiring.BundleWiring) MetadataSources(org.hibernate.boot.MetadataSources) BootstrapServiceRegistry(org.hibernate.boot.registry.BootstrapServiceRegistry) TypeContributor(org.hibernate.boot.model.TypeContributor) StrategyRegistrationProvider(org.hibernate.boot.registry.selector.StrategyRegistrationProvider) Integrator(org.hibernate.integrator.spi.Integrator) StandardServiceRegistry(org.hibernate.boot.registry.StandardServiceRegistry)

Example 2 with StrategyRegistrationProvider

use of org.hibernate.boot.registry.selector.StrategyRegistrationProvider in project hibernate-orm by hibernate.

the class StrategySelectorBuilder method buildSelector.

/**
	 * Builds the selector.
	 *
	 * @param classLoaderService The class loading service used to (attempt to) resolve any un-registered
	 * strategy implementations.
	 *
	 * @return The selector.
	 */
public StrategySelector buildSelector(ClassLoaderService classLoaderService) {
    final StrategySelectorImpl strategySelector = new StrategySelectorImpl(classLoaderService);
    // build the baseline...
    addDialects(strategySelector);
    addJtaPlatforms(strategySelector);
    addTransactionCoordinatorBuilders(strategySelector);
    addMultiTableBulkIdStrategies(strategySelector);
    addEntityCopyObserverStrategies(strategySelector);
    addImplicitNamingStrategies(strategySelector);
    addCacheKeysFactories(strategySelector);
    // apply auto-discovered registrations
    for (StrategyRegistrationProvider provider : classLoaderService.loadJavaServices(StrategyRegistrationProvider.class)) {
        for (StrategyRegistration discoveredStrategyRegistration : provider.getStrategyRegistrations()) {
            applyFromStrategyRegistration(strategySelector, discoveredStrategyRegistration);
        }
    }
    // apply customizations
    for (StrategyRegistration explicitStrategyRegistration : explicitStrategyRegistrations) {
        applyFromStrategyRegistration(strategySelector, explicitStrategyRegistration);
    }
    return strategySelector;
}
Also used : StrategyRegistrationProvider(org.hibernate.boot.registry.selector.StrategyRegistrationProvider) StrategyRegistration(org.hibernate.boot.registry.selector.StrategyRegistration)

Example 3 with StrategyRegistrationProvider

use of org.hibernate.boot.registry.selector.StrategyRegistrationProvider in project hibernate-orm by hibernate.

the class EntityManagerFactoryBuilderImpl method buildBootstrapServiceRegistry.

/**
	 * Builds the {@link BootstrapServiceRegistry} used to eventually build the {@link org.hibernate.boot.registry.StandardServiceRegistryBuilder}; mainly
	 * used here during instantiation to define class-loading behavior.
	 *
	 * @param integrationSettings Any integration settings passed by the EE container or SE application
	 *
	 * @return The built BootstrapServiceRegistry
	 */
private BootstrapServiceRegistry buildBootstrapServiceRegistry(Map integrationSettings, ClassLoader providedClassLoader, ClassLoaderService providedClassLoaderService) {
    final BootstrapServiceRegistryBuilder bsrBuilder = new BootstrapServiceRegistryBuilder();
    bsrBuilder.applyIntegrator(new JpaIntegrator());
    final IntegratorProvider integratorProvider = (IntegratorProvider) integrationSettings.get(INTEGRATOR_PROVIDER);
    if (integratorProvider != null) {
        for (Integrator integrator : integratorProvider.getIntegrators()) {
            bsrBuilder.applyIntegrator(integrator);
        }
    }
    final StrategyRegistrationProviderList strategyRegistrationProviderList = (StrategyRegistrationProviderList) integrationSettings.get(STRATEGY_REGISTRATION_PROVIDERS);
    if (strategyRegistrationProviderList != null) {
        for (StrategyRegistrationProvider strategyRegistrationProvider : strategyRegistrationProviderList.getStrategyRegistrationProviders()) {
            bsrBuilder.applyStrategySelectors(strategyRegistrationProvider);
        }
    }
    if (providedClassLoaderService != null) {
        bsrBuilder.applyClassLoaderService(providedClassLoaderService);
    } else {
        if (persistenceUnit.getClassLoader() != null) {
            bsrBuilder.applyClassLoader(persistenceUnit.getClassLoader());
        }
        if (providedClassLoader != null) {
            bsrBuilder.applyClassLoader(providedClassLoader);
        }
        final ClassLoader appClassLoader = (ClassLoader) integrationSettings.get(org.hibernate.cfg.AvailableSettings.APP_CLASSLOADER);
        if (appClassLoader != null) {
            LOG.debugf("Found use of deprecated `%s` setting; use `%s` instead.", org.hibernate.cfg.AvailableSettings.APP_CLASSLOADER, org.hibernate.cfg.AvailableSettings.CLASSLOADERS);
        }
        final Object classLoadersSetting = integrationSettings.get(org.hibernate.cfg.AvailableSettings.CLASSLOADERS);
        if (classLoadersSetting != null) {
            if (java.util.Collection.class.isInstance(classLoadersSetting)) {
                for (ClassLoader classLoader : (java.util.Collection<ClassLoader>) classLoadersSetting) {
                    bsrBuilder.applyClassLoader(classLoader);
                }
            } else if (classLoadersSetting.getClass().isArray()) {
                for (ClassLoader classLoader : (ClassLoader[]) classLoadersSetting) {
                    bsrBuilder.applyClassLoader(classLoader);
                }
            } else if (ClassLoader.class.isInstance(classLoadersSetting)) {
                bsrBuilder.applyClassLoader((ClassLoader) classLoadersSetting);
            }
        }
        //configurationValues not assigned yet, using directly the properties of the PU
        Properties puProperties = persistenceUnit.getProperties();
        if (puProperties != null) {
            final String tcclLookupPrecedence = puProperties.getProperty(org.hibernate.cfg.AvailableSettings.TC_CLASSLOADER);
            if (tcclLookupPrecedence != null) {
                bsrBuilder.applyTcclLookupPrecedence(TcclLookupPrecedence.valueOf(tcclLookupPrecedence.toUpperCase(Locale.ROOT)));
            }
        }
    }
    return bsrBuilder.build();
}
Also used : BootstrapServiceRegistryBuilder(org.hibernate.boot.registry.BootstrapServiceRegistryBuilder) Properties(java.util.Properties) StrategyRegistrationProviderList(org.hibernate.jpa.boot.spi.StrategyRegistrationProviderList) StrategyRegistrationProvider(org.hibernate.boot.registry.selector.StrategyRegistrationProvider) JpaIntegrator(org.hibernate.jpa.event.spi.JpaIntegrator) BeanValidationIntegrator(org.hibernate.cfg.beanvalidation.BeanValidationIntegrator) Integrator(org.hibernate.integrator.spi.Integrator) JpaIntegrator(org.hibernate.jpa.event.spi.JpaIntegrator) IntegratorProvider(org.hibernate.jpa.boot.spi.IntegratorProvider)

Example 4 with StrategyRegistrationProvider

use of org.hibernate.boot.registry.selector.StrategyRegistrationProvider in project hibernate-orm by hibernate.

the class OsgiPersistenceProvider method generateSettings.

@SuppressWarnings("unchecked")
private Map generateSettings(Map properties) {
    final Map settings = new HashMap();
    if (properties != null) {
        settings.putAll(properties);
    }
    settings.put(AvailableSettings.JTA_PLATFORM, osgiJtaPlatform);
    final Integrator[] integrators = osgiServiceUtil.getServiceImpls(Integrator.class);
    final IntegratorProvider integratorProvider = new IntegratorProvider() {

        @Override
        public List<Integrator> getIntegrators() {
            return Arrays.asList(integrators);
        }
    };
    settings.put(EntityManagerFactoryBuilderImpl.INTEGRATOR_PROVIDER, integratorProvider);
    final StrategyRegistrationProvider[] strategyRegistrationProviders = osgiServiceUtil.getServiceImpls(StrategyRegistrationProvider.class);
    final StrategyRegistrationProviderList strategyRegistrationProviderList = new StrategyRegistrationProviderList() {

        @Override
        public List<StrategyRegistrationProvider> getStrategyRegistrationProviders() {
            return Arrays.asList(strategyRegistrationProviders);
        }
    };
    settings.put(EntityManagerFactoryBuilderImpl.STRATEGY_REGISTRATION_PROVIDERS, strategyRegistrationProviderList);
    final TypeContributor[] typeContributors = osgiServiceUtil.getServiceImpls(TypeContributor.class);
    final TypeContributorList typeContributorList = new TypeContributorList() {

        @Override
        public List<TypeContributor> getTypeContributors() {
            return Arrays.asList(typeContributors);
        }
    };
    settings.put(EntityManagerFactoryBuilderImpl.TYPE_CONTRIBUTORS, typeContributorList);
    return settings;
}
Also used : StrategyRegistrationProvider(org.hibernate.boot.registry.selector.StrategyRegistrationProvider) HashMap(java.util.HashMap) Integrator(org.hibernate.integrator.spi.Integrator) TypeContributorList(org.hibernate.jpa.boot.spi.TypeContributorList) HashMap(java.util.HashMap) Map(java.util.Map) IntegratorProvider(org.hibernate.jpa.boot.spi.IntegratorProvider) StrategyRegistrationProviderList(org.hibernate.jpa.boot.spi.StrategyRegistrationProviderList) TypeContributor(org.hibernate.boot.model.TypeContributor)

Aggregations

StrategyRegistrationProvider (org.hibernate.boot.registry.selector.StrategyRegistrationProvider)4 Integrator (org.hibernate.integrator.spi.Integrator)3 TypeContributor (org.hibernate.boot.model.TypeContributor)2 BootstrapServiceRegistryBuilder (org.hibernate.boot.registry.BootstrapServiceRegistryBuilder)2 IntegratorProvider (org.hibernate.jpa.boot.spi.IntegratorProvider)2 StrategyRegistrationProviderList (org.hibernate.jpa.boot.spi.StrategyRegistrationProviderList)2 HashMap (java.util.HashMap)1 Map (java.util.Map)1 Properties (java.util.Properties)1 MetadataBuilder (org.hibernate.boot.MetadataBuilder)1 MetadataSources (org.hibernate.boot.MetadataSources)1 BootstrapServiceRegistry (org.hibernate.boot.registry.BootstrapServiceRegistry)1 StandardServiceRegistry (org.hibernate.boot.registry.StandardServiceRegistry)1 StandardServiceRegistryBuilder (org.hibernate.boot.registry.StandardServiceRegistryBuilder)1 StrategyRegistration (org.hibernate.boot.registry.selector.StrategyRegistration)1 BeanValidationIntegrator (org.hibernate.cfg.beanvalidation.BeanValidationIntegrator)1 TypeContributorList (org.hibernate.jpa.boot.spi.TypeContributorList)1 JpaIntegrator (org.hibernate.jpa.event.spi.JpaIntegrator)1 BundleWiring (org.osgi.framework.wiring.BundleWiring)1