diff --git a/hibernate-core/src/main/java/org/hibernate/cache/internal/NoCachingRegionFactory.java b/hibernate-core/src/main/java/org/hibernate/cache/internal/NoCachingRegionFactory.java index 0fdaf19e20..de22ac93df 100644 --- a/hibernate-core/src/main/java/org/hibernate/cache/internal/NoCachingRegionFactory.java +++ b/hibernate-core/src/main/java/org/hibernate/cache/internal/NoCachingRegionFactory.java @@ -42,9 +42,7 @@ import org.hibernate.cfg.Settings; * @author Steve Ebersole */ public class NoCachingRegionFactory implements RegionFactory { - - - public NoCachingRegionFactory(Properties properties) { + public NoCachingRegionFactory() { } public void start(Settings settings, Properties properties) throws CacheException { 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 new file mode 100644 index 0000000000..f14dec3b3e --- /dev/null +++ b/hibernate-core/src/main/java/org/hibernate/cache/internal/RegionFactoryInitiator.java @@ -0,0 +1,81 @@ +/* + * Hibernate, Relational Persistence for Idiomatic Java + * + * Copyright (c) 2011, Red Hat Inc. or third-party contributors as + * indicated by the @author tags or express copyright attribution + * statements applied by the authors. All third-party contributions are + * distributed under license by Red Hat Inc. + * + * This copyrighted material is made available to anyone wishing to use, modify, + * copy, or redistribute it subject to the terms and conditions of the GNU + * Lesser General Public License, as published by the Free Software Foundation. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY + * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License + * for more details. + * + * You should have received a copy of the GNU Lesser General Public License + * along with this distribution; if not, write to: + * Free Software Foundation, Inc. + * 51 Franklin Street, Fifth Floor + * Boston, MA 02110-1301 USA + */ +package org.hibernate.cache.internal; + +import java.util.Map; + +import org.hibernate.cache.spi.RegionFactory; +import org.hibernate.service.classloading.spi.ClassLoaderService; +import org.hibernate.service.spi.BasicServiceInitiator; +import org.hibernate.service.spi.ServiceException; +import org.hibernate.service.spi.ServiceRegistryImplementor; + +/** + * Initiator for the {@link RegionFactory} service. + * + * @author Hardy Ferentschik + */ +public class RegionFactoryInitiator implements BasicServiceInitiator { + 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" }) + public RegionFactory initiateService(Map configurationValues, ServiceRegistryImplementor registry) { + final Object impl = configurationValues.get( IMPL_NAME ); + if ( impl == null ) { + return new NoCachingRegionFactory(); + } + + if ( getServiceInitiated().isInstance( impl ) ) { + return (RegionFactory) impl; + } + + Class customImplClass = null; + if ( Class.class.isInstance( impl ) ) { + customImplClass = (Class) impl; + } + else { + customImplClass = registry.getService( ClassLoaderService.class ).classForName( impl.toString() ); + } + + try { + return customImplClass.newInstance(); + } + catch ( Exception e ) { + throw new ServiceException( + "Could not initialize custom RegionFactory impl [" + customImplClass.getName() + "]", e + ); + } + } +} diff --git a/hibernate-core/src/main/java/org/hibernate/cache/spi/RegionFactory.java b/hibernate-core/src/main/java/org/hibernate/cache/spi/RegionFactory.java index be9fae261f..15df7d6f39 100644 --- a/hibernate-core/src/main/java/org/hibernate/cache/spi/RegionFactory.java +++ b/hibernate-core/src/main/java/org/hibernate/cache/spi/RegionFactory.java @@ -28,6 +28,7 @@ import java.util.Properties; import org.hibernate.cache.CacheException; import org.hibernate.cache.spi.access.AccessType; import org.hibernate.cfg.Settings; +import org.hibernate.service.Service; /** * Contract for building second level cache regions. @@ -37,16 +38,16 @@ import org.hibernate.cfg.Settings; *
  • MyRegionFactoryImpl()
  • * * Use the first when we need to read config properties prior to - * {@link #start} being called. For an example, have a look at + * {@link #start(Settings, Properties)} being called. For an example, have a look at * {@link org.hibernate.cache.internal.bridge.RegionFactoryCacheProviderBridge} - * where we need the properties in order to determine which legacy + * where we need the properties in order to determine which legacy * {@link CacheProvider} to use so that we can answer the * {@link #isMinimalPutsEnabledByDefault()} question for the * {@link org.hibernate.cfg.SettingsFactory}. * * @author Steve Ebersole */ -public interface RegionFactory { +public interface RegionFactory extends Service { /** * Lifecycle callback to perform any necessary initialization of the @@ -55,6 +56,7 @@ public interface RegionFactory { * * @param settings The settings in effect. * @param properties The defined cfg properties + * * @throws org.hibernate.cache.CacheException Indicates problems starting the L2 cache impl; * considered as a sign to stop {@link org.hibernate.SessionFactory} * building. @@ -73,7 +75,7 @@ public interface RegionFactory { * level cache implementation? * * @return True if "minimal puts" should be performed by default; false - * otherwise. + * otherwise. */ public boolean isMinimalPutsEnabledByDefault(); @@ -101,10 +103,13 @@ public interface RegionFactory { * @param regionName The name of the region. * @param properties Configuration properties. * @param metadata Information regarding the type of data to be cached + * * @return The built region + * * @throws CacheException Indicates problems building the region. */ - public EntityRegion buildEntityRegion(String regionName, Properties properties, CacheDataDescription metadata) throws CacheException; + public EntityRegion buildEntityRegion(String regionName, Properties properties, CacheDataDescription metadata) + throws CacheException; /** * Build a cache region specialized for storing collection data. @@ -112,17 +117,22 @@ public interface RegionFactory { * @param regionName The name of the region. * @param properties Configuration properties. * @param metadata Information regarding the type of data to be cached + * * @return The built region + * * @throws CacheException Indicates problems building the region. */ - public CollectionRegion buildCollectionRegion(String regionName, Properties properties, CacheDataDescription metadata) throws CacheException; + public CollectionRegion buildCollectionRegion(String regionName, Properties properties, CacheDataDescription metadata) + throws CacheException; /** * Build a cache region specialized for storing query results * * @param regionName The name of the region. * @param properties Configuration properties. + * * @return The built region + * * @throws CacheException Indicates problems building the region. */ public QueryResultsRegion buildQueryResultsRegion(String regionName, Properties properties) throws CacheException; @@ -132,7 +142,9 @@ public interface RegionFactory { * * @param regionName The name of the region. * @param properties Configuration properties. + * * @return The built region + * * @throws CacheException Indicates problems building the region. */ public TimestampsRegion buildTimestampsRegion(String regionName, Properties properties) throws CacheException; diff --git a/hibernate-core/src/main/java/org/hibernate/persister/spi/PersisterFactory.java b/hibernate-core/src/main/java/org/hibernate/persister/spi/PersisterFactory.java index 43c71945e5..01a043be59 100644 --- a/hibernate-core/src/main/java/org/hibernate/persister/spi/PersisterFactory.java +++ b/hibernate-core/src/main/java/org/hibernate/persister/spi/PersisterFactory.java @@ -42,7 +42,7 @@ import org.hibernate.service.Service; */ public interface PersisterFactory extends Service { - // TODO: is it really neceassry to provide Configuration to CollectionPersisters ? + // TODO: is it really necessary to provide Configuration to CollectionPersisters ? // Should it not be enough with associated class ? or why does EntityPersister's not get access to configuration ? // // The only reason I could see that Configuration gets passed to collection persisters diff --git a/hibernate-core/src/main/java/org/hibernate/service/StandardServiceInitiators.java b/hibernate-core/src/main/java/org/hibernate/service/StandardServiceInitiators.java index 24b3c21297..3aad4c47ad 100644 --- a/hibernate-core/src/main/java/org/hibernate/service/StandardServiceInitiators.java +++ b/hibernate-core/src/main/java/org/hibernate/service/StandardServiceInitiators.java @@ -23,13 +23,17 @@ */ package org.hibernate.service; +import java.util.ArrayList; +import java.util.List; + +import org.hibernate.cache.internal.RegionFactoryInitiator; import org.hibernate.engine.jdbc.batch.internal.BatchBuilderInitiator; import org.hibernate.engine.jdbc.internal.JdbcServicesInitiator; import org.hibernate.engine.transaction.internal.TransactionFactoryInitiator; +import org.hibernate.integrator.internal.IntegratorServiceInitiator; import org.hibernate.persister.internal.PersisterClassResolverInitiator; import org.hibernate.persister.internal.PersisterFactoryInitiator; import org.hibernate.service.classloading.internal.ClassLoaderServiceInitiator; -import org.hibernate.integrator.internal.IntegratorServiceInitiator; import org.hibernate.service.internal.SessionFactoryServiceRegistryFactoryInitiator; import org.hibernate.service.jdbc.connections.internal.ConnectionProviderInitiator; import org.hibernate.service.jdbc.connections.internal.MultiTenantConnectionProviderInitiator; @@ -40,9 +44,6 @@ import org.hibernate.service.jndi.internal.JndiServiceInitiator; import org.hibernate.service.jta.platform.internal.JtaPlatformInitiator; import org.hibernate.service.spi.BasicServiceInitiator; -import java.util.ArrayList; -import java.util.List; - /** * @author Steve Ebersole */ @@ -72,7 +73,8 @@ public class StandardServiceInitiators { serviceInitiators.add( SessionFactoryServiceRegistryFactoryInitiator.INSTANCE ); serviceInitiators.add( IntegratorServiceInitiator.INSTANCE ); + serviceInitiators.add( RegionFactoryInitiator.INSTANCE ); + return serviceInitiators; } - }