HHH-7943 Added support for ehcache and infinispan strategies and OSGi

services.  Numerous ehcache and infinispan CL fixes.
This commit is contained in:
Brett Meyer 2013-05-01 14:10:53 -04:00
parent 9d134955c4
commit 489ee4a734
11 changed files with 81 additions and 59 deletions

View File

@ -28,44 +28,62 @@ import java.util.Map;
import org.hibernate.boot.registry.StandardServiceInitiator; import org.hibernate.boot.registry.StandardServiceInitiator;
import org.hibernate.boot.registry.selector.spi.StrategySelector; import org.hibernate.boot.registry.selector.spi.StrategySelector;
import org.hibernate.cache.spi.RegionFactory; 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.hibernate.service.spi.ServiceRegistryImplementor;
import org.jboss.logging.Logger;
/** /**
* Initiator for the {@link RegionFactory} service. * Initiator for the {@link RegionFactory} service.
* *
* @author Hardy Ferentschik * @author Hardy Ferentschik
* @author Brett Meyer
*/ */
public class RegionFactoryInitiator implements StandardServiceInitiator<RegionFactory> { public class RegionFactoryInitiator implements StandardServiceInitiator<RegionFactory> {
private static final CoreMessageLogger LOG = Logger.getMessageLogger( CoreMessageLogger.class,
RegionFactoryInitiator.class.getName() );
/** /**
* Singleton access * Singleton access
*/ */
public static final RegionFactoryInitiator INSTANCE = new RegionFactoryInitiator(); 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 @Override
public Class<RegionFactory> getServiceInitiated() { public Class<RegionFactory> getServiceInitiated() {
return RegionFactory.class; return RegionFactory.class;
} }
@Override @Override
@SuppressWarnings( { "unchecked" }) @SuppressWarnings({ "unchecked" })
public RegionFactory initiateService(Map configurationValues, ServiceRegistryImplementor registry) { public RegionFactory initiateService(Map configurationValues, ServiceRegistryImplementor registry) {
final Object setting = configurationValues.get( IMPL_NAME ); boolean useSecondLevelCache = ConfigurationHelper.getBoolean( AvailableSettings.USE_SECOND_LEVEL_CACHE,
return registry.getService( StrategySelector.class ).resolveDefaultableStrategy( configurationValues, true );
RegionFactory.class, boolean useQueryCache = ConfigurationHelper.getBoolean( AvailableSettings.USE_QUERY_CACHE, configurationValues );
setting,
NoCachingRegionFactory.INSTANCE 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. * 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 * @param name The (possibly legacy) factory name
* *

View File

@ -290,9 +290,7 @@ public class SettingsFactory implements Serializable {
settings.setQueryCacheFactory( createQueryCacheFactory( properties, serviceRegistry ) ); settings.setQueryCacheFactory( createQueryCacheFactory( properties, serviceRegistry ) );
} }
// The cache provider is needed when we either have second-level cache enabled settings.setRegionFactory( serviceRegistry.getService( RegionFactory.class ) );
// or query cache enabled. Note that useSecondLevelCache is enabled by default
settings.setRegionFactory( createRegionFactory( properties, ( useSecondLevelCache || useQueryCache ), serviceRegistry ) );
boolean useMinimalPuts = ConfigurationHelper.getBoolean( boolean useMinimalPuts = ConfigurationHelper.getBoolean(
AvailableSettings.USE_MINIMAL_PUTS, properties, settings.getRegionFactory().isMinimalPutsEnabledByDefault() AvailableSettings.USE_MINIMAL_PUTS, properties, settings.getRegionFactory().isMinimalPutsEnabledByDefault()
@ -437,39 +435,6 @@ public class SettingsFactory implements Serializable {
throw new HibernateException( "could not instantiate QueryCacheFactory: " + queryCacheFactoryClassName, e ); 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 //todo remove this once we move to new metamodel
public static RegionFactory createRegionFactory(Properties properties, boolean cachingEnabled) { 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 // todo : REMOVE! THIS IS TOTALLY A TEMPORARY HACK FOR org.hibernate.cfg.AnnotationBinder which will be going away

View File

@ -23,6 +23,7 @@
*/ */
package org.hibernate.cache.ehcache; package org.hibernate.cache.ehcache;
import java.net.MalformedURLException;
import java.net.URL; import java.net.URL;
import java.util.Properties; import java.util.Properties;
@ -205,6 +206,14 @@ abstract class AbstractEhcacheRegionFactory implements RegionFactory {
if ( url == null ) { if ( url == null ) {
url = AbstractEhcacheRegionFactory.class.getResource( configurationResourceName ); url = AbstractEhcacheRegionFactory.class.getResource( configurationResourceName );
} }
if ( url == null ) {
try {
url = new URL( configurationResourceName );
}
catch ( MalformedURLException e ) {
// ignore
}
}
} }
if ( LOG.isDebugEnabled() ) { if ( LOG.isDebugEnabled() ) {
LOG.debugf( LOG.debugf(

View File

@ -89,13 +89,7 @@ public class EhCacheRegionFactory extends AbstractEhcacheRegionFactory {
manager = new CacheManager( configuration ); manager = new CacheManager( configuration );
} }
else { else {
URL url; URL url = loadResource( configurationResourceName );
try {
url = new URL( configurationResourceName );
}
catch (MalformedURLException e) {
url = loadResource( configurationResourceName );
}
final Configuration configuration = HibernateEhcacheUtils.loadAndCorrectConfiguration( url ); final Configuration configuration = HibernateEhcacheUtils.loadAndCorrectConfiguration( url );
manager = new CacheManager( configuration ); manager = new CacheManager( configuration );
} }

View File

@ -48,6 +48,7 @@ public class StrategyRegistrationProviderImpl implements StrategyRegistrationPro
RegionFactory.class, RegionFactory.class,
EhCacheRegionFactory.class, EhCacheRegionFactory.class,
"ehcache", "ehcache",
EhCacheRegionFactory.class.getName(),
EhCacheRegionFactory.class.getSimpleName(), EhCacheRegionFactory.class.getSimpleName(),
// legacy impl class name // legacy impl class name
"org.hibernate.cache.EhCacheRegionFactory" "org.hibernate.cache.EhCacheRegionFactory"
@ -59,6 +60,7 @@ public class StrategyRegistrationProviderImpl implements StrategyRegistrationPro
RegionFactory.class, RegionFactory.class,
SingletonEhCacheRegionFactory.class, SingletonEhCacheRegionFactory.class,
"ehcache-singleton", "ehcache-singleton",
SingletonEhCacheRegionFactory.class.getName(),
SingletonEhCacheRegionFactory.class.getSimpleName(), SingletonEhCacheRegionFactory.class.getSimpleName(),
// legacy impl class name // legacy impl class name
"org.hibernate.cache.SingletonEhCacheRegionFactory" "org.hibernate.cache.SingletonEhCacheRegionFactory"

View File

@ -0,0 +1,10 @@
<?xml version="1.0" encoding="UTF-8"?>
<blueprint default-activation="eager"
xmlns="http://www.osgi.org/xmlns/blueprint/v1.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<bean id="strategyRegistrationProvider" class="org.hibernate.cache.ehcache.StrategyRegistrationProviderImpl"/>
<service ref="strategyRegistrationProvider" interface="org.hibernate.boot.registry.selector.StrategyRegistrationProvider"/>
</blueprint>

View File

@ -1,5 +1,6 @@
package org.hibernate.cache.infinispan; package org.hibernate.cache.infinispan;
import java.io.FileNotFoundException;
import java.io.IOException; import java.io.IOException;
import java.io.InputStream; import java.io.InputStream;
import java.util.ArrayList; import java.util.ArrayList;
@ -388,8 +389,18 @@ public class InfinispanRegionFactory implements RegionFactory {
String configLoc = ConfigurationHelper.getString( String configLoc = ConfigurationHelper.getString(
INFINISPAN_CONFIG_RESOURCE_PROP, properties, DEF_INFINISPAN_CONFIG_RESOURCE); INFINISPAN_CONFIG_RESOURCE_PROP, properties, DEF_INFINISPAN_CONFIG_RESOURCE);
ClassLoader classLoader = ClassLoaderHelper.getContextClassLoader(); ClassLoader classLoader = ClassLoaderHelper.getContextClassLoader();
InputStream is = FileLookupFactory.newInstance().lookupFileStrict( InputStream is;
configLoc, classLoader); 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); ParserRegistry parserRegistry = new ParserRegistry(classLoader);
ConfigurationBuilderHolder holder = parserRegistry.parse(is); ConfigurationBuilderHolder holder = parserRegistry.parse(is);

View File

@ -47,6 +47,7 @@ public class StrategyRegistrationProviderImpl implements StrategyRegistrationPro
RegionFactory.class, RegionFactory.class,
InfinispanRegionFactory.class, InfinispanRegionFactory.class,
"infinispan", "infinispan",
InfinispanRegionFactory.class.getName(),
InfinispanRegionFactory.class.getSimpleName() InfinispanRegionFactory.class.getSimpleName()
) )
); );
@ -56,6 +57,7 @@ public class StrategyRegistrationProviderImpl implements StrategyRegistrationPro
RegionFactory.class, RegionFactory.class,
JndiInfinispanRegionFactory.class, JndiInfinispanRegionFactory.class,
"infinispan-jndi", "infinispan-jndi",
JndiInfinispanRegionFactory.class.getName(),
JndiInfinispanRegionFactory.class.getSimpleName() JndiInfinispanRegionFactory.class.getSimpleName()
) )
); );

View File

@ -0,0 +1 @@
org.hibernate.cache.infinispan.StrategyRegistrationProviderImpl

View File

@ -0,0 +1,10 @@
<?xml version="1.0" encoding="UTF-8"?>
<blueprint default-activation="eager"
xmlns="http://www.osgi.org/xmlns/blueprint/v1.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<bean id="strategyRegistrationProvider" class="org.hibernate.cache.infinispan.StrategyRegistrationProviderImpl"/>
<service ref="strategyRegistrationProvider" interface="org.hibernate.boot.registry.selector.StrategyRegistrationProvider"/>
</blueprint>