From 2a64bcf9a7e616e9f7653d078caeaa4d343d97a2 Mon Sep 17 00:00:00 2001 From: Steve Ebersole Date: Mon, 21 Feb 2011 21:46:54 -0600 Subject: [PATCH] HHH-5943 - Make ServiceRegistry mutable --- .../internal/ServicesRegistryBootstrap.java | 40 --------------- .../org/hibernate/jmx/HibernateService.java | 6 +-- .../ServiceProxyFactoryFactoryImpl.java | 2 + .../javassist/ServiceProxyFactoryImpl.java | 2 + .../spi/proxy/ServiceProxyFactory.java | 10 ++++ .../spi/proxy/ServiceProxyFactoryFactory.java | 9 ++++ .../spi/proxy/ServiceProxyTargetSource.java | 3 +- .../TestServicesRegistryBootstrapping.java | 50 ------------------- .../test/common/ServiceRegistryHolder.java | 3 +- .../ServiceBootstrappingTest.java | 21 +++++++- .../ejb/EntityManagerFactoryImpl.java | 3 +- 11 files changed, 48 insertions(+), 101 deletions(-) delete mode 100644 hibernate-core/src/main/java/org/hibernate/cfg/internal/ServicesRegistryBootstrap.java delete mode 100644 hibernate-core/src/test/java/org/hibernate/test/cfg/internal/TestServicesRegistryBootstrapping.java rename hibernate-core/src/test/java/org/hibernate/test/{cfg/internal => service}/ServiceBootstrappingTest.java (73%) diff --git a/hibernate-core/src/main/java/org/hibernate/cfg/internal/ServicesRegistryBootstrap.java b/hibernate-core/src/main/java/org/hibernate/cfg/internal/ServicesRegistryBootstrap.java deleted file mode 100644 index 8453136043..0000000000 --- a/hibernate-core/src/main/java/org/hibernate/cfg/internal/ServicesRegistryBootstrap.java +++ /dev/null @@ -1,40 +0,0 @@ -/* - * Hibernate, Relational Persistence for Idiomatic Java - * - * Copyright (c) 2010, 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.cfg.internal; - -import org.hibernate.service.internal.ServiceRegistryImpl; -import org.hibernate.service.spi.StandardServiceInitiators; - -import java.util.Map; - -/** - * The standard bootstrap process for Hibernate services - * - * @author Steve Ebersole - */ -public class ServicesRegistryBootstrap { - public ServiceRegistryImpl initiateServicesRegistry(Map configurationValues) { - return new ServiceRegistryImpl( StandardServiceInitiators.LIST, configurationValues ); - } -} diff --git a/hibernate-core/src/main/java/org/hibernate/jmx/HibernateService.java b/hibernate-core/src/main/java/org/hibernate/jmx/HibernateService.java index aab2d2b83e..9b3396b638 100644 --- a/hibernate-core/src/main/java/org/hibernate/jmx/HibernateService.java +++ b/hibernate-core/src/main/java/org/hibernate/jmx/HibernateService.java @@ -12,7 +12,7 @@ import org.slf4j.LoggerFactory; import org.hibernate.HibernateException; import org.hibernate.SessionFactory; import org.hibernate.cfg.Environment; -import org.hibernate.cfg.internal.ServicesRegistryBootstrap; +import org.hibernate.service.internal.ServiceRegistryImpl; import org.hibernate.tool.hbm2ddl.SchemaExport; import org.hibernate.internal.util.jndi.JndiHelper; import org.hibernate.util.ExternalSessionFactoryConfig; @@ -61,9 +61,7 @@ public class HibernateService extends ExternalSessionFactoryConfig implements Hi SessionFactory buildSessionFactory() throws HibernateException { log.info( "starting service at JNDI name: " + boundName ); log.info( "service properties: " + properties ); - return buildConfiguration().buildSessionFactory( - new ServicesRegistryBootstrap().initiateServicesRegistry( properties ) - ); + return buildConfiguration().buildSessionFactory( new ServiceRegistryImpl( properties ) ); } protected Map getExtraProperties() { diff --git a/hibernate-core/src/main/java/org/hibernate/service/internal/proxy/javassist/ServiceProxyFactoryFactoryImpl.java b/hibernate-core/src/main/java/org/hibernate/service/internal/proxy/javassist/ServiceProxyFactoryFactoryImpl.java index e5100b3a51..4d7f46a583 100644 --- a/hibernate-core/src/main/java/org/hibernate/service/internal/proxy/javassist/ServiceProxyFactoryFactoryImpl.java +++ b/hibernate-core/src/main/java/org/hibernate/service/internal/proxy/javassist/ServiceProxyFactoryFactoryImpl.java @@ -28,6 +28,8 @@ import org.hibernate.service.spi.proxy.ServiceProxyFactoryFactory; import org.hibernate.service.spi.proxy.ServiceProxyTargetSource; /** + * Javassist-based implementation of a {@link ServiceProxyFactoryFactory} + * * @author Steve Ebersole */ public class ServiceProxyFactoryFactoryImpl implements ServiceProxyFactoryFactory { diff --git a/hibernate-core/src/main/java/org/hibernate/service/internal/proxy/javassist/ServiceProxyFactoryImpl.java b/hibernate-core/src/main/java/org/hibernate/service/internal/proxy/javassist/ServiceProxyFactoryImpl.java index 4e47b5b762..9d823a9a44 100644 --- a/hibernate-core/src/main/java/org/hibernate/service/internal/proxy/javassist/ServiceProxyFactoryImpl.java +++ b/hibernate-core/src/main/java/org/hibernate/service/internal/proxy/javassist/ServiceProxyFactoryImpl.java @@ -38,6 +38,8 @@ import java.lang.reflect.InvocationTargetException; import java.lang.reflect.Method; /** + * Javassist-based implementation of a {@link ServiceProxyFactory} + * * @author Steve Ebersole */ public class ServiceProxyFactoryImpl implements ServiceProxyFactory { diff --git a/hibernate-core/src/main/java/org/hibernate/service/spi/proxy/ServiceProxyFactory.java b/hibernate-core/src/main/java/org/hibernate/service/spi/proxy/ServiceProxyFactory.java index 455a8b6e73..cc90536ca7 100644 --- a/hibernate-core/src/main/java/org/hibernate/service/spi/proxy/ServiceProxyFactory.java +++ b/hibernate-core/src/main/java/org/hibernate/service/spi/proxy/ServiceProxyFactory.java @@ -26,8 +26,18 @@ package org.hibernate.service.spi.proxy; import org.hibernate.service.spi.Service; /** + * Contract for creating proxy instances for {@link Service} instances. + * * @author Steve Ebersole */ public interface ServiceProxyFactory { + /** + * Create a proxy for the given service role. + * + * @param serviceRole The service role for which to create a proxy. + * @param The type of the service + * + * @return The service proxy + */ public T makeProxy(Class serviceRole); } diff --git a/hibernate-core/src/main/java/org/hibernate/service/spi/proxy/ServiceProxyFactoryFactory.java b/hibernate-core/src/main/java/org/hibernate/service/spi/proxy/ServiceProxyFactoryFactory.java index a4972b56e9..ff638e4c01 100644 --- a/hibernate-core/src/main/java/org/hibernate/service/spi/proxy/ServiceProxyFactoryFactory.java +++ b/hibernate-core/src/main/java/org/hibernate/service/spi/proxy/ServiceProxyFactoryFactory.java @@ -24,8 +24,17 @@ package org.hibernate.service.spi.proxy; /** + * Contract for making a {@link ServiceProxyFactory}. + * * @author Steve Ebersole */ public interface ServiceProxyFactoryFactory { + /** + * Make an instance of the service proxy factory. + * + * @param registry The registry of actual service instances + * + * @return The created service proxy factory + */ public ServiceProxyFactory makeServiceProxyFactory(ServiceProxyTargetSource registry); } diff --git a/hibernate-core/src/main/java/org/hibernate/service/spi/proxy/ServiceProxyTargetSource.java b/hibernate-core/src/main/java/org/hibernate/service/spi/proxy/ServiceProxyTargetSource.java index 8c9d2a7003..75fe90d880 100644 --- a/hibernate-core/src/main/java/org/hibernate/service/spi/proxy/ServiceProxyTargetSource.java +++ b/hibernate-core/src/main/java/org/hibernate/service/spi/proxy/ServiceProxyTargetSource.java @@ -33,8 +33,7 @@ import org.hibernate.service.spi.ServiceRegistry; */ public interface ServiceProxyTargetSource extends ServiceRegistry { /** - * Retrieve a service by role. Unlike {@link org.hibernate.service.spi.ServiceRegistry#getService}, this version - * will never return a proxy. + * Retrieve a service by role. Unlike {@link ServiceRegistry#getService}, this version will never return a proxy. * * @param serviceRole The service role * @param The type of the service diff --git a/hibernate-core/src/test/java/org/hibernate/test/cfg/internal/TestServicesRegistryBootstrapping.java b/hibernate-core/src/test/java/org/hibernate/test/cfg/internal/TestServicesRegistryBootstrapping.java deleted file mode 100644 index 148cc2aed8..0000000000 --- a/hibernate-core/src/test/java/org/hibernate/test/cfg/internal/TestServicesRegistryBootstrapping.java +++ /dev/null @@ -1,50 +0,0 @@ -/* - * Hibernate, Relational Persistence for Idiomatic Java - * - * Copyright (c) 2010, 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.test.cfg.internal; - -import org.hibernate.cfg.internal.ServicesRegistryBootstrap; -import org.hibernate.engine.jdbc.spi.JdbcServices; -import org.hibernate.service.internal.ServiceRegistryImpl; -import org.hibernate.test.common.ConnectionProviderBuilder; -import org.hibernate.testing.junit.UnitTestCase; - -/** - * TODO : javadoc - * - * @author Steve Ebersole - */ -public class TestServicesRegistryBootstrapping extends UnitTestCase { - - public TestServicesRegistryBootstrapping(String string) { - super( string ); - } - - public void testBasicBootstrapping() { - ServiceRegistryImpl servicesRegistry = new ServicesRegistryBootstrap().initiateServicesRegistry( - ConnectionProviderBuilder.getConnectionProviderProperties() - ); - - assertNotNull( servicesRegistry.getService( JdbcServices.class ) ); - } -} diff --git a/hibernate-core/src/test/java/org/hibernate/test/common/ServiceRegistryHolder.java b/hibernate-core/src/test/java/org/hibernate/test/common/ServiceRegistryHolder.java index b631bff2f8..8bcce7839e 100644 --- a/hibernate-core/src/test/java/org/hibernate/test/common/ServiceRegistryHolder.java +++ b/hibernate-core/src/test/java/org/hibernate/test/common/ServiceRegistryHolder.java @@ -27,7 +27,6 @@ import java.util.Map; import java.util.Properties; import org.hibernate.cfg.Environment; -import org.hibernate.cfg.internal.ServicesRegistryBootstrap; import org.hibernate.engine.jdbc.internal.JdbcServicesImpl; import org.hibernate.engine.jdbc.spi.JdbcServices; import org.hibernate.internal.util.config.ConfigurationHelper; @@ -47,7 +46,7 @@ public class ServiceRegistryHolder { properties.putAll( props ); Environment.verifyProperties( properties ); ConfigurationHelper.resolvePlaceHolders( properties ); - serviceRegistry = new ServicesRegistryBootstrap().initiateServicesRegistry( properties ); + serviceRegistry = new ServiceRegistryImpl( properties ); properties.putAll( serviceRegistry.getService( JdbcServices.class ).getDialect().getDefaultProperties() ); } diff --git a/hibernate-core/src/test/java/org/hibernate/test/cfg/internal/ServiceBootstrappingTest.java b/hibernate-core/src/test/java/org/hibernate/test/service/ServiceBootstrappingTest.java similarity index 73% rename from hibernate-core/src/test/java/org/hibernate/test/cfg/internal/ServiceBootstrappingTest.java rename to hibernate-core/src/test/java/org/hibernate/test/service/ServiceBootstrappingTest.java index 334cf7e9e2..4c74540f05 100644 --- a/hibernate-core/src/test/java/org/hibernate/test/cfg/internal/ServiceBootstrappingTest.java +++ b/hibernate-core/src/test/java/org/hibernate/test/service/ServiceBootstrappingTest.java @@ -21,13 +21,15 @@ * 51 Franklin Street, Fifth Floor * Boston, MA 02110-1301 USA */ -package org.hibernate.test.cfg.internal; +package org.hibernate.test.service; import org.hibernate.cfg.Environment; import org.hibernate.dialect.H2Dialect; import org.hibernate.engine.jdbc.spi.JdbcServices; import org.hibernate.service.internal.ServiceRegistryImpl; import org.hibernate.service.jdbc.connections.internal.DriverManagerConnectionProviderImpl; +import org.hibernate.service.jdbc.connections.internal.UserSuppliedConnectionProviderImpl; +import org.hibernate.service.jdbc.connections.spi.ConnectionProvider; import org.hibernate.test.common.ConnectionProviderBuilder; import org.hibernate.testing.junit.UnitTestCase; @@ -67,4 +69,21 @@ public class ServiceBootstrappingTest extends UnitTestCase { serviceRegistry.destroy(); } + + public void testBuildWithServiceOverride() { + Properties props = ConnectionProviderBuilder.getConnectionProviderProperties(); + + ServiceRegistryImpl serviceRegistry = new ServiceRegistryImpl( props ); + JdbcServices jdbcServices = serviceRegistry.getService( JdbcServices.class ); + + assertTrue( jdbcServices.getDialect() instanceof H2Dialect ); + assertTrue( jdbcServices.getConnectionProvider().isUnwrappableAs( DriverManagerConnectionProviderImpl.class ) ); + + serviceRegistry.registerService( ConnectionProvider.class, new UserSuppliedConnectionProviderImpl() ); + + assertTrue( jdbcServices.getDialect() instanceof H2Dialect ); + assertTrue( jdbcServices.getConnectionProvider().isUnwrappableAs( UserSuppliedConnectionProviderImpl.class ) ); + + serviceRegistry.destroy(); + } } diff --git a/hibernate-entitymanager/src/main/java/org/hibernate/ejb/EntityManagerFactoryImpl.java b/hibernate-entitymanager/src/main/java/org/hibernate/ejb/EntityManagerFactoryImpl.java index 7558708a58..3c10361626 100755 --- a/hibernate-entitymanager/src/main/java/org/hibernate/ejb/EntityManagerFactoryImpl.java +++ b/hibernate-entitymanager/src/main/java/org/hibernate/ejb/EntityManagerFactoryImpl.java @@ -39,7 +39,6 @@ import javax.persistence.spi.LoadState; import org.hibernate.SessionFactory; import org.hibernate.Hibernate; import org.hibernate.EntityMode; -import org.hibernate.cfg.internal.ServicesRegistryBootstrap; import org.hibernate.metadata.ClassMetadata; import org.hibernate.engine.SessionFactoryImplementor; import org.hibernate.mapping.PersistentClass; @@ -83,7 +82,7 @@ public class EntityManagerFactoryImpl implements HibernateEntityManagerFactory { ); serviceRegistryProperties.putAll( cfg.getProperties() ); serviceRegistryProperties.putAll( connectionProviderInjectionData ); - this.serviceRegistry = new ServicesRegistryBootstrap().initiateServicesRegistry( serviceRegistryProperties ); + this.serviceRegistry = new ServiceRegistryImpl( serviceRegistryProperties ); this.sessionFactory = cfg.buildSessionFactory( serviceRegistry ); this.transactionType = transactionType; this.discardOnClose = discardOnClose;