From 489ee4a734364db37a25a890760fa4a543867d6c Mon Sep 17 00:00:00 2001 From: Brett Meyer Date: Wed, 1 May 2013 14:10:53 -0400 Subject: [PATCH] HHH-7943 Added support for ehcache and infinispan strategies and OSGi services. Numerous ehcache and infinispan CL fixes. --- .../internal/RegionFactoryInitiator.java | 46 +++++++++++++------ .../org/hibernate/cfg/SettingsFactory.java | 37 +-------------- .../ehcache/AbstractEhcacheRegionFactory.java | 9 ++++ .../cache/ehcache/EhCacheRegionFactory.java | 8 +--- .../StrategyRegistrationProviderImpl.java | 2 + .../OSGI-INF/blueprint/blueprint.xml | 10 ++++ .../infinispan/InfinispanRegionFactory.java | 15 +++++- .../StrategyRegistrationProviderImpl.java | 2 + ...ot.registry.selector.AvailabilityAnnouncer | 0 ...stry.selector.StrategyRegistrationProvider | 1 + .../OSGI-INF/blueprint/blueprint.xml | 10 ++++ 11 files changed, 81 insertions(+), 59 deletions(-) create mode 100644 hibernate-ehcache/src/main/resources/OSGI-INF/blueprint/blueprint.xml delete mode 100644 hibernate-infinispan/src/main/resources/META-INF/services/org.hibernate.boot.registry.selector.AvailabilityAnnouncer create mode 100644 hibernate-infinispan/src/main/resources/META-INF/services/org.hibernate.boot.registry.selector.StrategyRegistrationProvider create mode 100644 hibernate-infinispan/src/main/resources/OSGI-INF/blueprint/blueprint.xml diff --git a/hibernate-core/src/main/java/org/hibernate/cache/internal/RegionFactoryInitiator.java b/hibernate-core/src/main/java/org/hibernate/cache/internal/RegionFactoryInitiator.java index ffa33b8e72b8..4a934b6ad223 100644 --- a/hibernate-core/src/main/java/org/hibernate/cache/internal/RegionFactoryInitiator.java +++ b/hibernate-core/src/main/java/org/hibernate/cache/internal/RegionFactoryInitiator.java @@ -28,44 +28,62 @@ import org.hibernate.boot.registry.StandardServiceInitiator; import org.hibernate.boot.registry.selector.spi.StrategySelector; import org.hibernate.cache.spi.RegionFactory; +import org.hibernate.cfg.AvailableSettings; +import org.hibernate.internal.CoreMessageLogger; +import org.hibernate.internal.util.config.ConfigurationHelper; import org.hibernate.service.spi.ServiceRegistryImplementor; +import org.jboss.logging.Logger; /** * Initiator for the {@link RegionFactory} service. - * + * * @author Hardy Ferentschik + * @author Brett Meyer */ public class RegionFactoryInitiator implements StandardServiceInitiator { + + private static final CoreMessageLogger LOG = Logger.getMessageLogger( CoreMessageLogger.class, + RegionFactoryInitiator.class.getName() ); + /** * Singleton access */ public static final RegionFactoryInitiator INSTANCE = new RegionFactoryInitiator(); - /** - * Property name to use to configure the full qualified class name for the {@code RegionFactory} - */ - public static final String IMPL_NAME = "hibernate.cache.region.factory_class"; - @Override public Class getServiceInitiated() { return RegionFactory.class; } @Override - @SuppressWarnings( { "unchecked" }) + @SuppressWarnings({ "unchecked" }) public RegionFactory initiateService(Map configurationValues, ServiceRegistryImplementor registry) { - final Object setting = configurationValues.get( IMPL_NAME ); - return registry.getService( StrategySelector.class ).resolveDefaultableStrategy( - RegionFactory.class, - setting, - NoCachingRegionFactory.INSTANCE - ); + boolean useSecondLevelCache = ConfigurationHelper.getBoolean( AvailableSettings.USE_SECOND_LEVEL_CACHE, + configurationValues, true ); + boolean useQueryCache = ConfigurationHelper.getBoolean( AvailableSettings.USE_QUERY_CACHE, configurationValues ); + + RegionFactory regionFactory; + + // The cache provider is needed when we either have second-level cache enabled + // or query cache enabled. Note that useSecondLevelCache is enabled by default + if ( useSecondLevelCache || useQueryCache ) { + final Object setting = configurationValues.get( AvailableSettings.CACHE_REGION_FACTORY ); + regionFactory = registry.getService( StrategySelector.class ).resolveDefaultableStrategy( + RegionFactory.class, setting, NoCachingRegionFactory.INSTANCE ); + } + else { + regionFactory = NoCachingRegionFactory.INSTANCE; + } + + LOG.debugf( "Cache region factory : %s", regionFactory.getClass().getName() ); + + return regionFactory; } /** * Map legacy names unto the new corollary. * - * todo this shouldn't be public, nor really static. hack for org.hibernate.cfg.SettingsFactory.createRegionFactory() + * TODO: temporary hack for org.hibernate.cfg.SettingsFactory.createRegionFactory() * * @param name The (possibly legacy) factory name * diff --git a/hibernate-core/src/main/java/org/hibernate/cfg/SettingsFactory.java b/hibernate-core/src/main/java/org/hibernate/cfg/SettingsFactory.java index 67209c644b88..7219d08948cf 100644 --- a/hibernate-core/src/main/java/org/hibernate/cfg/SettingsFactory.java +++ b/hibernate-core/src/main/java/org/hibernate/cfg/SettingsFactory.java @@ -290,9 +290,7 @@ public Settings buildSettings(Properties props, ServiceRegistry serviceRegistry) settings.setQueryCacheFactory( createQueryCacheFactory( properties, serviceRegistry ) ); } - // The cache provider is needed when we either have second-level cache enabled - // or query cache enabled. Note that useSecondLevelCache is enabled by default - settings.setRegionFactory( createRegionFactory( properties, ( useSecondLevelCache || useQueryCache ), serviceRegistry ) ); + settings.setRegionFactory( serviceRegistry.getService( RegionFactory.class ) ); boolean useMinimalPuts = ConfigurationHelper.getBoolean( AvailableSettings.USE_MINIMAL_PUTS, properties, settings.getRegionFactory().isMinimalPutsEnabledByDefault() @@ -437,39 +435,6 @@ protected QueryCacheFactory createQueryCacheFactory(Properties properties, Servi throw new HibernateException( "could not instantiate QueryCacheFactory: " + queryCacheFactoryClassName, e ); } } - - private static RegionFactory createRegionFactory(Properties properties, boolean cachingEnabled, ServiceRegistry serviceRegistry) { - String regionFactoryClassName = RegionFactoryInitiator.mapLegacyNames( - ConfigurationHelper.getString( - AvailableSettings.CACHE_REGION_FACTORY, properties, null - ) - ); - if ( regionFactoryClassName == null || !cachingEnabled) { - regionFactoryClassName = DEF_CACHE_REG_FACTORY; - } - LOG.debugf( "Cache region factory : %s", regionFactoryClassName ); - try { - try { - return (RegionFactory) serviceRegistry.getService( ClassLoaderService.class ) - .classForName( regionFactoryClassName ) - .getConstructor( Properties.class ) - .newInstance( properties ); - } - catch ( NoSuchMethodException e ) { - // no constructor accepting Properties found, try no arg constructor - LOG.debugf( - "%s did not provide constructor accepting java.util.Properties; attempting no-arg constructor.", - regionFactoryClassName - ); - return (RegionFactory) serviceRegistry.getService( ClassLoaderService.class ) - .classForName( regionFactoryClassName ) - .newInstance(); - } - } - catch ( Exception e ) { - throw new HibernateException( "could not instantiate RegionFactory [" + regionFactoryClassName + "]", e ); - } - } //todo remove this once we move to new metamodel public static RegionFactory createRegionFactory(Properties properties, boolean cachingEnabled) { // todo : REMOVE! THIS IS TOTALLY A TEMPORARY HACK FOR org.hibernate.cfg.AnnotationBinder which will be going away diff --git a/hibernate-ehcache/src/main/java/org/hibernate/cache/ehcache/AbstractEhcacheRegionFactory.java b/hibernate-ehcache/src/main/java/org/hibernate/cache/ehcache/AbstractEhcacheRegionFactory.java index b12a88e188a3..b935309000d8 100644 --- a/hibernate-ehcache/src/main/java/org/hibernate/cache/ehcache/AbstractEhcacheRegionFactory.java +++ b/hibernate-ehcache/src/main/java/org/hibernate/cache/ehcache/AbstractEhcacheRegionFactory.java @@ -23,6 +23,7 @@ */ package org.hibernate.cache.ehcache; +import java.net.MalformedURLException; import java.net.URL; import java.util.Properties; @@ -205,6 +206,14 @@ protected URL loadResource(String configurationResourceName) { if ( url == null ) { url = AbstractEhcacheRegionFactory.class.getResource( configurationResourceName ); } + if ( url == null ) { + try { + url = new URL( configurationResourceName ); + } + catch ( MalformedURLException e ) { + // ignore + } + } } if ( LOG.isDebugEnabled() ) { LOG.debugf( diff --git a/hibernate-ehcache/src/main/java/org/hibernate/cache/ehcache/EhCacheRegionFactory.java b/hibernate-ehcache/src/main/java/org/hibernate/cache/ehcache/EhCacheRegionFactory.java index 6b28b6ee25ec..6dca8d5dae1c 100644 --- a/hibernate-ehcache/src/main/java/org/hibernate/cache/ehcache/EhCacheRegionFactory.java +++ b/hibernate-ehcache/src/main/java/org/hibernate/cache/ehcache/EhCacheRegionFactory.java @@ -89,13 +89,7 @@ public void start(Settings settings, Properties properties) throws CacheExceptio manager = new CacheManager( configuration ); } else { - URL url; - try { - url = new URL( configurationResourceName ); - } - catch (MalformedURLException e) { - url = loadResource( configurationResourceName ); - } + URL url = loadResource( configurationResourceName ); final Configuration configuration = HibernateEhcacheUtils.loadAndCorrectConfiguration( url ); manager = new CacheManager( configuration ); } diff --git a/hibernate-ehcache/src/main/java/org/hibernate/cache/ehcache/StrategyRegistrationProviderImpl.java b/hibernate-ehcache/src/main/java/org/hibernate/cache/ehcache/StrategyRegistrationProviderImpl.java index 3cedfe4f0a44..2c68ca1df824 100644 --- a/hibernate-ehcache/src/main/java/org/hibernate/cache/ehcache/StrategyRegistrationProviderImpl.java +++ b/hibernate-ehcache/src/main/java/org/hibernate/cache/ehcache/StrategyRegistrationProviderImpl.java @@ -48,6 +48,7 @@ public Iterable getStrategyRegistrations() { RegionFactory.class, EhCacheRegionFactory.class, "ehcache", + EhCacheRegionFactory.class.getName(), EhCacheRegionFactory.class.getSimpleName(), // legacy impl class name "org.hibernate.cache.EhCacheRegionFactory" @@ -59,6 +60,7 @@ public Iterable getStrategyRegistrations() { RegionFactory.class, SingletonEhCacheRegionFactory.class, "ehcache-singleton", + SingletonEhCacheRegionFactory.class.getName(), SingletonEhCacheRegionFactory.class.getSimpleName(), // legacy impl class name "org.hibernate.cache.SingletonEhCacheRegionFactory" diff --git a/hibernate-ehcache/src/main/resources/OSGI-INF/blueprint/blueprint.xml b/hibernate-ehcache/src/main/resources/OSGI-INF/blueprint/blueprint.xml new file mode 100644 index 000000000000..fbbb6739e628 --- /dev/null +++ b/hibernate-ehcache/src/main/resources/OSGI-INF/blueprint/blueprint.xml @@ -0,0 +1,10 @@ + + + + + + + + diff --git a/hibernate-infinispan/src/main/java/org/hibernate/cache/infinispan/InfinispanRegionFactory.java b/hibernate-infinispan/src/main/java/org/hibernate/cache/infinispan/InfinispanRegionFactory.java index 3a35625ec0f8..c1012cb7851e 100644 --- a/hibernate-infinispan/src/main/java/org/hibernate/cache/infinispan/InfinispanRegionFactory.java +++ b/hibernate-infinispan/src/main/java/org/hibernate/cache/infinispan/InfinispanRegionFactory.java @@ -1,5 +1,6 @@ package org.hibernate.cache.infinispan; +import java.io.FileNotFoundException; import java.io.IOException; import java.io.InputStream; import java.util.ArrayList; @@ -388,8 +389,18 @@ protected EmbeddedCacheManager createCacheManager(Properties properties) throws String configLoc = ConfigurationHelper.getString( INFINISPAN_CONFIG_RESOURCE_PROP, properties, DEF_INFINISPAN_CONFIG_RESOURCE); ClassLoader classLoader = ClassLoaderHelper.getContextClassLoader(); - InputStream is = FileLookupFactory.newInstance().lookupFileStrict( - configLoc, classLoader); + InputStream is; + try { + is = FileLookupFactory.newInstance().lookupFileStrict( + configLoc, classLoader); + } + catch ( FileNotFoundException e ) { + // In some environments (ex: OSGi), hibernate-infinispan may not + // be in the app CL. It's important to also try this CL. + classLoader = this.getClass().getClassLoader(); + is = FileLookupFactory.newInstance().lookupFileStrict( + configLoc, classLoader); + } ParserRegistry parserRegistry = new ParserRegistry(classLoader); ConfigurationBuilderHolder holder = parserRegistry.parse(is); diff --git a/hibernate-infinispan/src/main/java/org/hibernate/cache/infinispan/StrategyRegistrationProviderImpl.java b/hibernate-infinispan/src/main/java/org/hibernate/cache/infinispan/StrategyRegistrationProviderImpl.java index 611e0587e5db..8197d09ea345 100644 --- a/hibernate-infinispan/src/main/java/org/hibernate/cache/infinispan/StrategyRegistrationProviderImpl.java +++ b/hibernate-infinispan/src/main/java/org/hibernate/cache/infinispan/StrategyRegistrationProviderImpl.java @@ -47,6 +47,7 @@ public Iterable getStrategyRegistrations() { RegionFactory.class, InfinispanRegionFactory.class, "infinispan", + InfinispanRegionFactory.class.getName(), InfinispanRegionFactory.class.getSimpleName() ) ); @@ -56,6 +57,7 @@ public Iterable getStrategyRegistrations() { RegionFactory.class, JndiInfinispanRegionFactory.class, "infinispan-jndi", + JndiInfinispanRegionFactory.class.getName(), JndiInfinispanRegionFactory.class.getSimpleName() ) ); diff --git a/hibernate-infinispan/src/main/resources/META-INF/services/org.hibernate.boot.registry.selector.AvailabilityAnnouncer b/hibernate-infinispan/src/main/resources/META-INF/services/org.hibernate.boot.registry.selector.AvailabilityAnnouncer deleted file mode 100644 index e69de29bb2d1..000000000000 diff --git a/hibernate-infinispan/src/main/resources/META-INF/services/org.hibernate.boot.registry.selector.StrategyRegistrationProvider b/hibernate-infinispan/src/main/resources/META-INF/services/org.hibernate.boot.registry.selector.StrategyRegistrationProvider new file mode 100644 index 000000000000..b59a47b2c647 --- /dev/null +++ b/hibernate-infinispan/src/main/resources/META-INF/services/org.hibernate.boot.registry.selector.StrategyRegistrationProvider @@ -0,0 +1 @@ +org.hibernate.cache.infinispan.StrategyRegistrationProviderImpl \ No newline at end of file diff --git a/hibernate-infinispan/src/main/resources/OSGI-INF/blueprint/blueprint.xml b/hibernate-infinispan/src/main/resources/OSGI-INF/blueprint/blueprint.xml new file mode 100644 index 000000000000..ff3a39ab6998 --- /dev/null +++ b/hibernate-infinispan/src/main/resources/OSGI-INF/blueprint/blueprint.xml @@ -0,0 +1,10 @@ + + + + + + + +