HHH-5943 - Make ServiceRegistry mutable

This commit is contained in:
Steve Ebersole 2011-02-21 21:46:54 -06:00 committed by JPAV
parent 72a94161e2
commit 8a530db254
11 changed files with 49 additions and 99 deletions

View File

@ -1,39 +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 java.util.Map;
import org.hibernate.service.internal.ServiceRegistryImpl;
import org.hibernate.service.spi.StandardServiceInitiators;
/**
* The standard bootstrap process for Hibernate services
*
* @author Steve Ebersole
*/
public class ServicesRegistryBootstrap {
public ServiceRegistryImpl initiateServicesRegistry(Map configurationValues) {
return new ServiceRegistryImpl( StandardServiceInitiators.LIST, configurationValues );
}
}

View File

@ -1,5 +1,6 @@
//$Id: HibernateService.java 6100 2005-03-17 10:48:03Z turin42 $
package org.hibernate.jmx;
import java.util.Map;
import java.util.Properties;
import javax.naming.InitialContext;
@ -7,8 +8,8 @@ import org.hibernate.HibernateException;
import org.hibernate.HibernateLogger;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.Environment;
import org.hibernate.cfg.internal.ServicesRegistryBootstrap;
import org.hibernate.internal.util.jndi.JndiHelper;
import org.hibernate.service.internal.ServiceRegistryImpl;
import org.hibernate.tool.hbm2ddl.SchemaExport;
import org.hibernate.util.ExternalSessionFactoryConfig;
import org.jboss.logging.Logger;
@ -57,9 +58,7 @@ public class HibernateService extends ExternalSessionFactoryConfig implements Hi
SessionFactory buildSessionFactory() throws HibernateException {
LOG.startingServiceAtJndiName(boundName);
LOG.serviceProperties(properties);
return buildConfiguration().buildSessionFactory(
new ServicesRegistryBootstrap().initiateServicesRegistry( properties )
);
return buildConfiguration().buildSessionFactory(new ServiceRegistryImpl(properties));
}
@Override

View File

@ -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 {

View File

@ -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 {

View File

@ -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 <T> The type of the service
*
* @return The service proxy
*/
public <T extends Service> T makeProxy(Class<T> serviceRole);
}

View File

@ -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);
}

View File

@ -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 <T> The type of the service

View File

@ -1,49 +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 ) );
}
}

View File

@ -26,7 +26,6 @@ package org.hibernate.test.common;
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;
@ -46,7 +45,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() );
}

View File

@ -21,7 +21,7 @@
* 51 Franklin Street, Fifth Floor
* Boston, MA 02110-1301 USA
*/
package org.hibernate.test.cfg.internal;
package org.hibernate.test.service;
import java.util.Properties;
import org.hibernate.cfg.Environment;
@ -29,6 +29,8 @@ 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;
@ -66,4 +68,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();
}
}

View File

@ -38,7 +38,6 @@ import org.hibernate.EntityMode;
import org.hibernate.Hibernate;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration;
import org.hibernate.cfg.internal.ServicesRegistryBootstrap;
import org.hibernate.ejb.criteria.CriteriaBuilderImpl;
import org.hibernate.ejb.metamodel.MetamodelImpl;
import org.hibernate.ejb.util.PersistenceUtilHelper;
@ -81,7 +80,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;