HHH-7492 - fixing test failures caused by some configuration is not applied to metamodel

This commit is contained in:
Strong Liu 2012-07-31 15:08:19 +08:00
parent 5562a981ac
commit 9632e010e8
7 changed files with 104 additions and 85 deletions

View File

@ -43,6 +43,7 @@ import org.hibernate.cfg.Settings;
* @author Steve Ebersole
*/
public class NoCachingRegionFactory implements RegionFactory {
public static RegionFactory INSTANCE = new NoCachingRegionFactory();
public NoCachingRegionFactory() {
}

View File

@ -25,8 +25,16 @@ package org.hibernate.cache.internal;
import java.util.Map;
import org.jboss.logging.Logger;
import org.hibernate.cache.spi.RegionFactory;
import org.hibernate.cfg.AvailableSettings;
import org.hibernate.cfg.Environment;
import org.hibernate.internal.CoreMessageLogger;
import org.hibernate.internal.util.config.ConfigurationHelper;
import org.hibernate.service.classloading.spi.ClassLoaderService;
import org.hibernate.service.config.spi.ConfigurationService;
import org.hibernate.service.config.spi.StandardConverters;
import org.hibernate.service.spi.BasicServiceInitiator;
import org.hibernate.service.spi.ServiceException;
import org.hibernate.service.spi.ServiceRegistryImplementor;
@ -38,6 +46,11 @@ import org.hibernate.service.spi.ServiceRegistryImplementor;
*/
public class RegionFactoryInitiator implements BasicServiceInitiator<RegionFactory> {
public static final RegionFactoryInitiator INSTANCE = new RegionFactoryInitiator();
public static final String DEF_CACHE_REG_FACTORY = NoCachingRegionFactory.class.getName();
private static final CoreMessageLogger LOG = Logger.getMessageLogger(
CoreMessageLogger.class,
RegionFactoryInitiator.class.getName()
);
/**
* Property name to use to configure the full qualified class name for the {@code RegionFactory}
@ -53,10 +66,19 @@ public class RegionFactoryInitiator implements BasicServiceInitiator<RegionFacto
@SuppressWarnings( { "unchecked" })
public RegionFactory initiateService(Map configurationValues, ServiceRegistryImplementor registry) {
final Object impl = configurationValues.get( IMPL_NAME );
if ( impl == null ) {
return new NoCachingRegionFactory();
boolean isCacheEnabled = isCacheEnabled(registry);
if(!isCacheEnabled){
LOG.debugf( "Second level cache has been disabled, so using % as cache region factory", NoCachingRegionFactory.class.getName() );
return NoCachingRegionFactory.INSTANCE;
}
if ( impl == null ) {
LOG.debugf(
"No 'hibernate.cache.region.factory_class' is provided, so using %s as default",
NoCachingRegionFactory.class.getName()
);
return NoCachingRegionFactory.INSTANCE;
}
LOG.debugf( "Cache region factory : %s", impl.toString() );
if ( getServiceInitiated().isInstance( impl ) ) {
return (RegionFactory) impl;
}
@ -80,6 +102,20 @@ public class RegionFactoryInitiator implements BasicServiceInitiator<RegionFacto
}
}
private static boolean isCacheEnabled(ServiceRegistryImplementor serviceRegistry) {
final ConfigurationService configurationService = serviceRegistry.getService( ConfigurationService.class );
final boolean useSecondLevelCache = configurationService.getSetting(
AvailableSettings.USE_SECOND_LEVEL_CACHE,
StandardConverters.BOOLEAN,
true
);
final boolean useQueryCache = configurationService.getSetting(
AvailableSettings.USE_QUERY_CACHE,
StandardConverters.BOOLEAN
);
return useSecondLevelCache || useQueryCache;
}
// todo this shouldn't be public (nor really static):
// hack for org.hibernate.cfg.SettingsFactory.createRegionFactory()
public static String mapLegacyNames(final String name) {

View File

@ -248,7 +248,7 @@ public class SettingsFactory implements Serializable {
// 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(
Environment.USE_MINIMAL_PUTS, properties, settings.getRegionFactory().isMinimalPutsEnabledByDefault()
@ -371,38 +371,6 @@ public class SettingsFactory implements Serializable {
}
}
private static RegionFactory createRegionFactory(Properties properties, boolean cachingEnabled, ServiceRegistry serviceRegistry) {
String regionFactoryClassName = RegionFactoryInitiator.mapLegacyNames(
ConfigurationHelper.getString(
Environment.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

View File

@ -45,6 +45,10 @@ import org.hibernate.cache.spi.TimestampsRegion;
import org.hibernate.cache.spi.access.AccessType;
import org.hibernate.cfg.Settings;
import org.hibernate.internal.util.config.ConfigurationHelper;
import org.hibernate.service.config.spi.ConfigurationService;
import org.hibernate.service.config.spi.StandardConverters;
import org.hibernate.service.spi.ServiceRegistryAwareService;
import org.hibernate.service.spi.ServiceRegistryImplementor;
/**
* A {@link RegionFactory} for <a href="http://www.jboss.org/infinispan">Infinispan</a>-backed cache
@ -54,7 +58,7 @@ import org.hibernate.internal.util.config.ConfigurationHelper;
* @author Galder Zamarreño
* @since 3.5
*/
public class InfinispanRegionFactory implements RegionFactory {
public class InfinispanRegionFactory implements RegionFactory, ServiceRegistryAwareService {
private static final Log log = LogFactory.getLog(InfinispanRegionFactory.class);
@ -168,6 +172,8 @@ public class InfinispanRegionFactory implements RegionFactory {
private EmbeddedCacheManager manager;
protected ServiceRegistryImplementor serviceRegistry;
private final Map<String, TypeOverrides> typeOverrides = new HashMap<String, TypeOverrides>();
private final Set<String> definedConfigurations = new HashSet<String>();
@ -297,12 +303,13 @@ public class InfinispanRegionFactory implements RegionFactory {
/**
* {@inheritDoc}
*/
@Override
public void start(Settings settings, Properties properties) throws CacheException {
log.debug("Starting Infinispan region factory");
try {
transactionManagerlookup = createTransactionManagerLookup(settings, properties);
transactionManager = transactionManagerlookup.getTransactionManager();
manager = createCacheManager(properties);
manager = createCacheManager();
initGenericDataTypeOverrides();
Enumeration keys = properties.propertyNames();
while (keys.hasMoreElements()) {
@ -325,9 +332,16 @@ public class InfinispanRegionFactory implements RegionFactory {
return new HibernateTransactionManagerLookup(settings, properties);
}
/**
@Override
public void injectServices(ServiceRegistryImplementor serviceRegistry) {
this.serviceRegistry = serviceRegistry;
}
/**
* {@inheritDoc}
*/
@Override
public void stop() {
log.debug("Stop region factory");
stopCacheRegions();
@ -359,19 +373,28 @@ public class InfinispanRegionFactory implements RegionFactory {
return Collections.unmodifiableSet(definedConfigurations);
}
protected EmbeddedCacheManager createCacheManager(Properties properties) throws CacheException {
try {
String configLoc = ConfigurationHelper.getString(INFINISPAN_CONFIG_RESOURCE_PROP, properties, DEF_INFINISPAN_CONFIG_RESOURCE);
EmbeddedCacheManager manager = new DefaultCacheManager(configLoc, false);
String globalStats = extractProperty(INFINISPAN_GLOBAL_STATISTICS_PROP, properties);
if (globalStats != null) {
manager.getGlobalConfiguration().setExposeGlobalJmxStatistics(Boolean.parseBoolean(globalStats));
}
manager.start();
return manager;
} catch (IOException e) {
throw new CacheException("Unable to create default cache manager", e);
}
protected EmbeddedCacheManager createCacheManager() throws CacheException {
try {
ConfigurationService configurationService = serviceRegistry.getService( ConfigurationService.class );
String configLoc = configurationService.getSetting(
INFINISPAN_CONFIG_RESOURCE_PROP,
StandardConverters.STRING, DEF_INFINISPAN_CONFIG_RESOURCE
);
EmbeddedCacheManager manager = new DefaultCacheManager( configLoc, false );
Boolean globalStats = configurationService.getSetting(
INFINISPAN_GLOBAL_STATISTICS_PROP,
StandardConverters.BOOLEAN,
false
);
if ( globalStats ) {
manager.getGlobalConfiguration().fluent().globalJmxStatistics();
}
manager.start();
return manager;
}
catch ( IOException e ) {
throw new CacheException( "Unable to create default cache manager", e );
}
}
private void startRegion(BaseRegion region, String regionName) {

View File

@ -33,6 +33,11 @@ import org.infinispan.util.logging.LogFactory;
import org.hibernate.cache.CacheException;
import org.hibernate.internal.util.config.ConfigurationHelper;
import org.hibernate.internal.util.jndi.JndiHelper;
import org.hibernate.service.config.spi.ConfigurationService;
import org.hibernate.service.config.spi.StandardConverters;
import org.hibernate.service.jndi.spi.JndiService;
import org.hibernate.service.spi.ServiceRegistryAwareService;
import org.hibernate.service.spi.ServiceRegistryImplementor;
/**
* A {@link org.hibernate.cache.spi.RegionFactory} for <a href="http://www.jboss.org/infinispan">Infinispan</a>-backed cache
@ -42,9 +47,6 @@ import org.hibernate.internal.util.jndi.JndiHelper;
* @since 3.5
*/
public class JndiInfinispanRegionFactory extends InfinispanRegionFactory {
private static final Log log = LogFactory.getLog(JndiInfinispanRegionFactory.class);
/**
* Specifies the JNDI name under which the {@link EmbeddedCacheManager} to use is bound.
* There is no default value -- the user must specify the property.
@ -59,33 +61,18 @@ public class JndiInfinispanRegionFactory extends InfinispanRegionFactory {
super(props);
}
@Override
protected EmbeddedCacheManager createCacheManager(Properties properties) throws CacheException {
String name = ConfigurationHelper.getString(CACHE_MANAGER_RESOURCE_PROP, properties, null);
if (name == null)
throw new CacheException("Configuration property " + CACHE_MANAGER_RESOURCE_PROP + " not set");
return locateCacheManager(name, JndiHelper.extractJndiProperties(properties));
}
private EmbeddedCacheManager locateCacheManager(String jndiNamespace, Properties jndiProperties) {
Context ctx = null;
try {
ctx = new InitialContext(jndiProperties);
return (EmbeddedCacheManager) ctx.lookup(jndiNamespace);
} catch (NamingException ne) {
String msg = "Unable to retrieve CacheManager from JNDI [" + jndiNamespace + "]";
log.info(msg, ne);
throw new CacheException( msg );
} finally {
if (ctx != null) {
try {
ctx.close();
} catch( NamingException ne ) {
log.info("Unable to release initial context", ne);
}
}
}
}
@Override
protected EmbeddedCacheManager createCacheManager() throws CacheException {
String name = serviceRegistry.getService( ConfigurationService.class ).getSetting(
CACHE_MANAGER_RESOURCE_PROP,
StandardConverters.STRING
);
if ( name == null ) {
throw new CacheException( "Configuration property " + CACHE_MANAGER_RESOURCE_PROP + " not set" );
}
JndiService jndiService = serviceRegistry.getService( JndiService.class );
return (EmbeddedCacheManager) jndiService.locate( name );
}
@Override
public void stop() {

View File

@ -548,11 +548,11 @@ public class InfinispanRegionFactoryTestCase {
}
@Override
protected EmbeddedCacheManager createCacheManager(Properties properties) throws CacheException {
protected EmbeddedCacheManager createCacheManager() throws CacheException {
if (manager != null)
return manager;
else
return super.createCacheManager(properties);
return super.createCacheManager();
}
};
factory.start(null, p);

View File

@ -85,10 +85,14 @@ public class JndiRegionFactoryTestCase extends SingleNodeTestCase {
}
@Override
protected void afterConstructAndConfigureMetadata(MetadataImplementor metadataImplementor) {
afterConfigurationBuilt( null, null );
bindToJndi();
}
@Override
public void afterConfigurationBuilt(Mappings mappings, Dialect dialect) {
bindToJndi();
}
private void bindToJndi(){
if ( bindToJndi ) {
try {
// Create an in-memory jndi