Skip to content

Commit

Permalink
HHH-7943 Added support for ehcache and infinispan strategies and OSGi
Browse files Browse the repository at this point in the history
services.  Numerous ehcache and infinispan CL fixes.
  • Loading branch information
brmeyer committed May 1, 2013
1 parent 9d13495 commit 489ee4a
Show file tree
Hide file tree
Showing 11 changed files with 81 additions and 59 deletions.
Expand Up @@ -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<RegionFactory> {

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<RegionFactory> 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
*
Expand Down
Expand Up @@ -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()
Expand Down Expand Up @@ -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
Expand Down
Expand Up @@ -23,6 +23,7 @@
*/
package org.hibernate.cache.ehcache;

import java.net.MalformedURLException;
import java.net.URL;
import java.util.Properties;

Expand Down Expand Up @@ -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(
Expand Down
Expand Up @@ -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 );
}
Expand Down
Expand Up @@ -48,6 +48,7 @@ public Iterable<StrategyRegistration> getStrategyRegistrations() {
RegionFactory.class,
EhCacheRegionFactory.class,
"ehcache",
EhCacheRegionFactory.class.getName(),
EhCacheRegionFactory.class.getSimpleName(),
// legacy impl class name
"org.hibernate.cache.EhCacheRegionFactory"
Expand All @@ -59,6 +60,7 @@ public Iterable<StrategyRegistration> getStrategyRegistrations() {
RegionFactory.class,
SingletonEhCacheRegionFactory.class,
"ehcache-singleton",
SingletonEhCacheRegionFactory.class.getName(),
SingletonEhCacheRegionFactory.class.getSimpleName(),
// legacy impl class name
"org.hibernate.cache.SingletonEhCacheRegionFactory"
Expand Down
@@ -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>
@@ -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;
Expand Down Expand Up @@ -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);

Expand Down
Expand Up @@ -47,6 +47,7 @@ public Iterable<StrategyRegistration> getStrategyRegistrations() {
RegionFactory.class,
InfinispanRegionFactory.class,
"infinispan",
InfinispanRegionFactory.class.getName(),
InfinispanRegionFactory.class.getSimpleName()
)
);
Expand All @@ -56,6 +57,7 @@ public Iterable<StrategyRegistration> getStrategyRegistrations() {
RegionFactory.class,
JndiInfinispanRegionFactory.class,
"infinispan-jndi",
JndiInfinispanRegionFactory.class.getName(),
JndiInfinispanRegionFactory.class.getSimpleName()
)
);
Expand Down
Empty file.
@@ -0,0 +1 @@
org.hibernate.cache.infinispan.StrategyRegistrationProviderImpl
@@ -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>

0 comments on commit 489ee4a

Please sign in to comment.