diff --git a/hibernate-core/src/main/java/org/hibernate/service/BasicServiceRegistry.java b/hibernate-core/src/main/java/org/hibernate/service/BasicServiceRegistry.java index 56d66cda9d..dc6bf9aae0 100644 --- a/hibernate-core/src/main/java/org/hibernate/service/BasicServiceRegistry.java +++ b/hibernate-core/src/main/java/org/hibernate/service/BasicServiceRegistry.java @@ -23,11 +23,8 @@ */ package org.hibernate.service; -import org.hibernate.service.spi.BasicServiceInitiator; - /** * @author Steve Ebersole */ public interface BasicServiceRegistry extends ServiceRegistry { - public void registerServiceInitiator(BasicServiceInitiator initiator); } diff --git a/hibernate-core/src/main/java/org/hibernate/service/internal/AbstractServiceRegistryImpl.java b/hibernate-core/src/main/java/org/hibernate/service/internal/AbstractServiceRegistryImpl.java index 3bce73e8fd..55f7382f2e 100644 --- a/hibernate-core/src/main/java/org/hibernate/service/internal/AbstractServiceRegistryImpl.java +++ b/hibernate-core/src/main/java/org/hibernate/service/internal/AbstractServiceRegistryImpl.java @@ -31,32 +31,29 @@ import java.util.concurrent.ConcurrentHashMap; import org.jboss.logging.Logger; -import org.hibernate.HibernateException; import org.hibernate.internal.CoreMessageLogger; import org.hibernate.internal.util.collections.CollectionHelper; import org.hibernate.service.Service; import org.hibernate.service.ServiceRegistry; -import org.hibernate.service.internal.proxy.javassist.ServiceProxyFactoryFactoryImpl; +import org.hibernate.service.UnknownServiceException; import org.hibernate.service.jmx.spi.JmxService; import org.hibernate.service.spi.InjectService; import org.hibernate.service.spi.Manageable; +import org.hibernate.service.spi.ServiceBinding; +import org.hibernate.service.spi.ServiceException; +import org.hibernate.service.spi.ServiceInitiator; import org.hibernate.service.spi.ServiceRegistryImplementor; import org.hibernate.service.spi.Startable; import org.hibernate.service.spi.Stoppable; -import org.hibernate.service.UnknownServiceException; -import org.hibernate.service.spi.proxy.ServiceProxyFactory; /** * @author Steve Ebersole */ -public abstract class AbstractServiceRegistryImpl implements ServiceRegistryImplementor { +public abstract class AbstractServiceRegistryImpl implements ServiceRegistryImplementor, ServiceBinding.OwningRegistry { private static final CoreMessageLogger LOG = Logger.getMessageLogger( CoreMessageLogger.class, AbstractServiceRegistryImpl.class.getName() ); private final ServiceRegistryImplementor parent; - // for now just hard-code the javassist factory - private ServiceProxyFactory serviceProxyFactory = new ServiceProxyFactoryFactoryImpl().makeServiceProxyFactory( this ); - private ConcurrentHashMap serviceBindingMap; // IMPL NOTE : the list used for ordered destruction. Cannot used map above because we need to // iterate it in reverse order which is only available through ListIterator @@ -73,6 +70,20 @@ public abstract class AbstractServiceRegistryImpl implements ServiceRegistryImpl this.serviceList = CollectionHelper.arrayList( 20 ); } + @SuppressWarnings({ "unchecked" }) + protected void createServiceBinding(ServiceInitiator initiator) { + serviceBindingMap.put( initiator.getServiceInitiated(), new ServiceBinding( this, initiator ) ); + } + + protected void createServiceBinding(ProvidedService providedService) { + ServiceBinding binding = locateServiceBinding( providedService.getServiceRole(), false ); + if ( binding == null ) { + binding = new ServiceBinding( this, providedService.getServiceRole(), providedService.getService() ); + serviceBindingMap.put( providedService.getServiceRole(), binding ); + } + registerService( binding, providedService.getService() ); + } + @Override @SuppressWarnings( {"unchecked"}) public ServiceRegistry getParentServiceRegistry() { @@ -97,40 +108,33 @@ public abstract class AbstractServiceRegistryImpl implements ServiceRegistryImpl @Override public R getService(Class serviceRole) { - return locateOrCreateServiceBinding( serviceRole, true ).getProxy(); - } - - @SuppressWarnings({ "unchecked" }) - protected ServiceBinding locateOrCreateServiceBinding(Class serviceRole, boolean checkParent) { - ServiceBinding serviceBinding = locateServiceBinding( serviceRole, checkParent ); + final ServiceBinding serviceBinding = locateServiceBinding( serviceRole ); if ( serviceBinding == null ) { - createServiceBinding( serviceRole ); + throw new UnknownServiceException( serviceRole ); } - return serviceBinding; + + R service = serviceBinding.getService(); + if ( service == null ) { + service = initializeService( serviceBinding ); + } + + return service; } - protected ServiceBinding createServiceBinding(Class serviceRole) { - R proxy = serviceProxyFactory.makeProxy( serviceRole ); - ServiceBinding serviceBinding = new ServiceBinding( proxy ); - serviceBindingMap.put( serviceRole, serviceBinding ); - return serviceBinding; - } - - protected void registerService(Class serviceRole, R service) { - ServiceBinding serviceBinding = locateOrCreateServiceBinding( serviceRole, false ); - R priorServiceInstance = serviceBinding.getTarget(); - serviceBinding.setTarget( service ); + protected void registerService(ServiceBinding serviceBinding, R service) { + R priorServiceInstance = serviceBinding.getService(); + serviceBinding.setService( service ); if ( priorServiceInstance != null ) { serviceList.remove( priorServiceInstance ); } serviceList.add( service ); } - private R initializeService(Class serviceRole) { - LOG.trace("Initializing service [role=" + serviceRole.getName() + "]"); + private R initializeService(ServiceBinding serviceBinding) { + LOG.trace( "Initializing service [role=" + serviceBinding.getServiceRole().getName() + "]" ); // PHASE 1 : create service - R service = createService( serviceRole ); + R service = createService( serviceBinding ); if ( service == null ) { return null; } @@ -139,12 +143,34 @@ public abstract class AbstractServiceRegistryImpl implements ServiceRegistryImpl configureService( service ); // PHASE 3 : Start service - startService( service, serviceRole ); + startService( serviceBinding ); return service; } - protected abstract T createService(Class serviceRole); + @SuppressWarnings( {"unchecked"}) + protected R createService(ServiceBinding serviceBinding) { + final ServiceInitiator serviceInitiator = serviceBinding.getServiceInitiator(); + if ( serviceInitiator == null ) { + // this condition should never ever occur + throw new UnknownServiceException( serviceBinding.getServiceRole() ); + } + + try { + R service = serviceBinding.getServiceRegistry().initiateService( serviceInitiator ); + // IMPL NOTE : the register call here is important to avoid potential stack overflow issues + // from recursive calls through #configureService + registerService( serviceBinding, service ); + return service; + } + catch ( ServiceException e ) { + throw e; + } + catch ( Exception e ) { + throw new ServiceException( "Unable to create requested service [" + serviceBinding.getServiceRole().getName() + "]", e ); + } + } + protected abstract void configureService(T service); protected void applyInjections(T service) { @@ -197,36 +223,19 @@ public abstract class AbstractServiceRegistryImpl implements ServiceRegistryImpl } @SuppressWarnings({ "unchecked" }) - protected void startService(T service, Class serviceRole) { - if ( Startable.class.isInstance( service ) ) { - ( (Startable) service ).start(); + protected void startService(ServiceBinding serviceBinding) { + if ( Startable.class.isInstance( serviceBinding.getService() ) ) { + ( (Startable) serviceBinding.getService() ).start(); } - if ( Manageable.class.isInstance( service ) ) { - getService( JmxService.class ).registerService( (Manageable) service, serviceRole ); + if ( Manageable.class.isInstance( serviceBinding.getService() ) ) { + getService( JmxService.class ).registerService( + (Manageable) serviceBinding.getService(), + serviceBinding.getServiceRole() + ); } } - @Override - @SuppressWarnings( {"unchecked"}) - public R getServiceInternal(Class serviceRole) { - // this call comes from the binding proxy, we most definitely do not want to look up into the parent - // in this case! - ServiceBinding serviceBinding = locateServiceBinding( serviceRole, false ); - if ( serviceBinding == null ) { - throw new HibernateException( "Only proxies should invoke #getServiceInternal" ); - } - R service = serviceBinding.getTarget(); - if ( service == null ) { - service = initializeService( serviceRole ); - serviceBinding.setTarget( service ); - } - if ( service == null ) { - throw new UnknownServiceException( serviceRole ); - } - return service; - } - public void destroy() { ListIterator serviceIterator = serviceList.listIterator( serviceList.size() ); while ( serviceIterator.hasPrevious() ) { diff --git a/hibernate-core/src/main/java/org/hibernate/service/internal/BasicServiceRegistryImpl.java b/hibernate-core/src/main/java/org/hibernate/service/internal/BasicServiceRegistryImpl.java index 58c22ba0da..e34eb5aada 100644 --- a/hibernate-core/src/main/java/org/hibernate/service/internal/BasicServiceRegistryImpl.java +++ b/hibernate-core/src/main/java/org/hibernate/service/internal/BasicServiceRegistryImpl.java @@ -23,19 +23,14 @@ */ package org.hibernate.service.internal; -import java.util.HashMap; import java.util.List; import java.util.Map; -import org.jboss.logging.Logger; - -import org.hibernate.internal.CoreMessageLogger; import org.hibernate.service.BasicServiceRegistry; import org.hibernate.service.Service; -import org.hibernate.service.UnknownServiceException; import org.hibernate.service.spi.BasicServiceInitiator; import org.hibernate.service.spi.Configurable; -import org.hibernate.service.spi.ServiceException; +import org.hibernate.service.spi.ServiceInitiator; import org.hibernate.service.spi.ServiceRegistryAwareService; /** @@ -44,86 +39,33 @@ import org.hibernate.service.spi.ServiceRegistryAwareService; * @author Steve Ebersole */ public class BasicServiceRegistryImpl extends AbstractServiceRegistryImpl implements BasicServiceRegistry { - private static final CoreMessageLogger LOG = Logger.getMessageLogger(CoreMessageLogger.class, BasicServiceRegistryImpl.class.getName()); - private final Map serviceInitiatorMap; private final Map configurationValues; @SuppressWarnings( {"unchecked"}) public BasicServiceRegistryImpl( - List serviceInitiators, - List providedServices, - Map configurationValues) { + final List serviceInitiators, + final List providedServices, + final Map configurationValues) { super(); this.configurationValues = configurationValues; - this.serviceInitiatorMap = toMap( serviceInitiators ); - for ( BasicServiceInitiator initiator : serviceInitiatorMap.values() ) { - // create the bindings up front to help identify to which registry services belong - createServiceBinding( initiator.getServiceInitiated() ); + // process initiators + for ( ServiceInitiator initiator : serviceInitiators ) { + createServiceBinding( initiator ); } + // then, explicitly provided service instances for ( ProvidedService providedService : providedServices ) { - ServiceBinding binding = locateOrCreateServiceBinding( providedService.getServiceRole(), false ); - binding.setTarget( providedService.getService() ); - } - } - - /** - * We convert the incoming list of initiators to a map for 2 reasons:
    - *
  • to make it easier to look up the initiator we need for a given service role
  • - *
  • to make sure there is only one initiator for a given service role (last wins)
  • - *
- * - * @param serviceInitiators The list of individual initiators - * - * @return The map of initiators keyed by the service rle they initiate. - */ - private static Map toMap(List serviceInitiators) { - final Map result = new HashMap(); - for ( BasicServiceInitiator initiator : serviceInitiators ) { - result.put( initiator.getServiceInitiated(), initiator ); - } - return result; - } - - @Override - @SuppressWarnings( {"unchecked"}) - public void registerServiceInitiator(BasicServiceInitiator initiator) { - ServiceBinding serviceBinding = locateServiceBinding( initiator.getServiceInitiated(), false ); - if ( serviceBinding != null ) { - serviceBinding.setTarget( null ); - } - else { - createServiceBinding( initiator.getServiceInitiated() ); - } - final Object previous = serviceInitiatorMap.put( initiator.getServiceInitiated(), initiator ); - if ( previous != null ) { - LOG.debugf( "Over-wrote existing service initiator [role=%s]", initiator.getServiceInitiated().getName() ); + createServiceBinding( providedService ); } } @Override - @SuppressWarnings({ "unchecked" }) - protected T createService(Class serviceRole) { - BasicServiceInitiator initiator = serviceInitiatorMap.get( serviceRole ); - if ( initiator == null ) { - throw new UnknownServiceException( serviceRole ); - } - try { - T service = initiator.initiateService( configurationValues, this ); - // IMPL NOTE : the register call here is important to avoid potential stack overflow issues - // from recursive calls through #configureService - registerService( serviceRole, service ); - return service; - } - catch ( ServiceException e ) { - throw e; - } - catch ( Exception e ) { - throw new ServiceException( "Unable to create requested service [" + serviceRole.getName() + "]", e ); - } + public R initiateService(ServiceInitiator serviceInitiator) { + // todo : add check/error for unexpected initiator types? + return ( (BasicServiceInitiator) serviceInitiator ).initiateService( configurationValues, this ); } @Override diff --git a/hibernate-core/src/main/java/org/hibernate/service/internal/ProvidedService.java b/hibernate-core/src/main/java/org/hibernate/service/internal/ProvidedService.java index 74a2fd92dc..d0cf4d1348 100644 --- a/hibernate-core/src/main/java/org/hibernate/service/internal/ProvidedService.java +++ b/hibernate-core/src/main/java/org/hibernate/service/internal/ProvidedService.java @@ -28,20 +28,20 @@ package org.hibernate.service.internal; * * @author Steve Ebersole */ -public class ProvidedService { - private final Class serviceRole; - private final T service; +public class ProvidedService { + private final Class serviceRole; + private final R service; - public ProvidedService(Class serviceRole, T service) { + public ProvidedService(Class serviceRole, R service) { this.serviceRole = serviceRole; this.service = service; } - public Class getServiceRole() { + public Class getServiceRole() { return serviceRole; } - public T getService() { + public R getService() { return service; } } diff --git a/hibernate-core/src/main/java/org/hibernate/service/internal/SessionFactoryServiceRegistryImpl.java b/hibernate-core/src/main/java/org/hibernate/service/internal/SessionFactoryServiceRegistryImpl.java index 311f6b911b..45199c861d 100644 --- a/hibernate-core/src/main/java/org/hibernate/service/internal/SessionFactoryServiceRegistryImpl.java +++ b/hibernate-core/src/main/java/org/hibernate/service/internal/SessionFactoryServiceRegistryImpl.java @@ -23,19 +23,11 @@ */ package org.hibernate.service.internal; -import java.util.HashMap; -import java.util.List; -import java.util.Map; - -import org.jboss.logging.Logger; - -import org.hibernate.engine.spi.SessionFactoryImplementor; -import org.hibernate.internal.CoreMessageLogger; import org.hibernate.cfg.Configuration; +import org.hibernate.engine.spi.SessionFactoryImplementor; import org.hibernate.service.Service; import org.hibernate.service.StandardSessionFactoryServiceInitiators; -import org.hibernate.service.UnknownServiceException; -import org.hibernate.service.spi.ServiceException; +import org.hibernate.service.spi.ServiceInitiator; import org.hibernate.service.spi.ServiceRegistryAwareService; import org.hibernate.service.spi.ServiceRegistryImplementor; import org.hibernate.service.spi.SessionFactoryServiceInitiator; @@ -44,13 +36,7 @@ import org.hibernate.service.spi.SessionFactoryServiceRegistry; /** * @author Steve Ebersole */ -public class SessionFactoryServiceRegistryImpl - extends AbstractServiceRegistryImpl - implements SessionFactoryServiceRegistry { - - private static final CoreMessageLogger LOG = Logger.getMessageLogger( CoreMessageLogger.class, SessionFactoryServiceRegistryImpl.class.getName() ); - - private final Map serviceInitiatorMap; +public class SessionFactoryServiceRegistryImpl extends AbstractServiceRegistryImpl implements SessionFactoryServiceRegistry { // for now we need to hold on to the Configuration... :( private Configuration configuration; @@ -62,62 +48,21 @@ public class SessionFactoryServiceRegistryImpl SessionFactoryImplementor sessionFactory, Configuration configuration) { super( parent ); - // for now, just use the standard initiator list - this.serviceInitiatorMap = toMap( StandardSessionFactoryServiceInitiators.LIST ); this.sessionFactory = sessionFactory; this.configuration = configuration; - for ( SessionFactoryServiceInitiator initiator : serviceInitiatorMap.values() ) { + // for now, just use the standard initiator list + for ( SessionFactoryServiceInitiator initiator : StandardSessionFactoryServiceInitiators.LIST ) { // create the bindings up front to help identify to which registry services belong - createServiceBinding( initiator.getServiceInitiated() ); - } - } - - private static Map toMap(List serviceInitiators) { - final Map result = new HashMap(); - for ( SessionFactoryServiceInitiator initiator : serviceInitiators ) { - result.put( initiator.getServiceInitiated(), initiator ); - } - return result; - } - - @Override - @SuppressWarnings( {"unchecked"}) - public void registerServiceInitiator(SessionFactoryServiceInitiator initiator) { - ServiceBinding serviceBinding = locateServiceBinding( initiator.getServiceInitiated(), false ); - if ( serviceBinding != null ) { - serviceBinding.setTarget( null ); - } - else { - createServiceBinding( initiator.getServiceInitiated() ); - } - final Object previous = serviceInitiatorMap.put( initiator.getServiceInitiated(), initiator ); - if ( previous != null ) { - LOG.debugf( "Over-wrote existing service initiator [role=%s]", initiator.getServiceInitiated().getName() ); + createServiceBinding( initiator ); } } @Override - @SuppressWarnings({ "unchecked" }) - protected T createService(Class serviceRole) { - SessionFactoryServiceInitiator initiator = serviceInitiatorMap.get( serviceRole ); - if ( initiator == null ) { - throw new UnknownServiceException( serviceRole ); - } - try { - T service = initiator.initiateService( sessionFactory, configuration, this ); - // IMPL NOTE : the register call here is important to avoid potential stack overflow issues - // from recursive calls through #configureService - registerService( serviceRole, service ); - return service; - } - catch ( ServiceException e ) { - throw e; - } - catch ( Exception e ) { - throw new ServiceException( "Unable to create requested service [" + serviceRole.getName() + "]", e ); - } + public R initiateService(ServiceInitiator serviceInitiator) { + // todo : add check/error for unexpected initiator types? + return ( (SessionFactoryServiceInitiator) serviceInitiator ).initiateService( sessionFactory, configuration, this ); } @Override 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 deleted file mode 100644 index 4d7f46a583..0000000000 --- a/hibernate-core/src/main/java/org/hibernate/service/internal/proxy/javassist/ServiceProxyFactoryFactoryImpl.java +++ /dev/null @@ -1,40 +0,0 @@ -/* - * 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.service.internal.proxy.javassist; - -import org.hibernate.service.spi.proxy.ServiceProxyFactory; -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 { - @Override - public ServiceProxyFactory makeServiceProxyFactory(ServiceProxyTargetSource registry) { - return new ServiceProxyFactoryImpl( registry ); - } -} 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 deleted file mode 100644 index 9be3b2daa5..0000000000 --- a/hibernate-core/src/main/java/org/hibernate/service/internal/proxy/javassist/ServiceProxyFactoryImpl.java +++ /dev/null @@ -1,124 +0,0 @@ -/* - * 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.service.internal.proxy.javassist; - -import javassist.util.proxy.MethodFilter; -import javassist.util.proxy.MethodHandler; -import javassist.util.proxy.ProxyFactory; -import javassist.util.proxy.ProxyObject; - -import org.hibernate.service.internal.ServiceProxy; -import org.hibernate.service.internal.ServiceProxyGenerationException; -import org.hibernate.service.Service; -import org.hibernate.service.spi.proxy.ServiceProxyFactory; -import org.hibernate.service.spi.proxy.ServiceProxyTargetSource; - -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 { - private final ServiceProxyTargetSource serviceRegistry; - - public ServiceProxyFactoryImpl(ServiceProxyTargetSource serviceRegistry) { - this.serviceRegistry = serviceRegistry; - } - - private static final MethodFilter FINALIZE_FILTER = new MethodFilter() { - public boolean isHandled(Method m) { - // skip finalize methods - return !( m.getParameterTypes().length == 0 && m.getName().equals( "finalize" ) ); - } - }; - - @Override - @SuppressWarnings( {"unchecked"}) - public T makeProxy(Class serviceRole) { - try { - ProxyFactory factory = new ProxyFactory(); - factory.setFilter( FINALIZE_FILTER ); - - Class[] interfaces = new Class[2]; - interfaces[0] = serviceRole; - interfaces[1] = ServiceProxy.class; - factory.setInterfaces( interfaces ); - - Class proxyClass = factory.createClass(); - ProxyObject proxyObject = (ProxyObject) proxyClass.newInstance(); - proxyObject.setHandler( new ServiceProxyMethodInterceptor( (T)proxyObject, serviceRole, serviceRegistry ) ); - return (T) proxyObject; - } - catch (Exception e) { - throw new ServiceProxyGenerationException( "Unable to make service proxy", e ); - } - } - - private static class ServiceProxyMethodInterceptor implements MethodHandler { - private final T proxy; - private final Class serviceRole; - private final ServiceProxyTargetSource serviceRegistry; - - private ServiceProxyMethodInterceptor(T proxy, Class serviceRole, ServiceProxyTargetSource serviceRegistry) { - this.proxy = proxy; - this.serviceRole = serviceRole; - this.serviceRegistry = serviceRegistry; - } - - @Override - @SuppressWarnings( {"UnnecessaryBoxing"} ) - public Object invoke( - Object object, - Method method, - Method method1, - Object[] args) throws Exception { - String name = method.getName(); - if ( "toString".equals( name ) ) { - return serviceRole.getName() + "_$$_Proxy@" + System.identityHashCode( object ); - } - else if ( "equals".equals( name ) ) { - return proxy == object ? Boolean.TRUE : Boolean.FALSE; - } - else if ( "hashCode".equals( name ) ) { - return Integer.valueOf( System.identityHashCode( object ) ); - } - else if ( "getTargetInstance".equals( name ) && ServiceProxy.class.equals( method.getDeclaringClass() ) ) { - return serviceRegistry.getServiceInternal( serviceRole ); - } - else { - try { - T target = serviceRegistry.getServiceInternal( serviceRole ); - return method.invoke( target, args ); - } - catch (InvocationTargetException e) { - throw (Exception) e.getTargetException(); - } - } - } - } - -} diff --git a/hibernate-core/src/main/java/org/hibernate/service/spi/BasicServiceInitiator.java b/hibernate-core/src/main/java/org/hibernate/service/spi/BasicServiceInitiator.java index 1b9ed5fe15..2115ddee70 100644 --- a/hibernate-core/src/main/java/org/hibernate/service/spi/BasicServiceInitiator.java +++ b/hibernate-core/src/main/java/org/hibernate/service/spi/BasicServiceInitiator.java @@ -32,14 +32,7 @@ import org.hibernate.service.Service; * * @author Steve Ebersole */ -public interface BasicServiceInitiator { - /** - * Obtains the service role initiated by this initiator. Should be unique within a registry - * - * @return The service role. - */ - public Class getServiceInitiated(); - +public interface BasicServiceInitiator extends ServiceInitiator { /** * Initiates the managed service. * diff --git a/hibernate-core/src/main/java/org/hibernate/service/spi/ServiceBinding.java b/hibernate-core/src/main/java/org/hibernate/service/spi/ServiceBinding.java new file mode 100644 index 0000000000..8d754a2684 --- /dev/null +++ b/hibernate-core/src/main/java/org/hibernate/service/spi/ServiceBinding.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.service.spi; + +import org.jboss.logging.Logger; + +import org.hibernate.service.Service; + +/** + * Models a binding for a particular service + * @author Steve Ebersole + */ +public final class ServiceBinding { + private static final Logger log = Logger.getLogger( ServiceBinding.class ); + + public static interface OwningRegistry { + public R initiateService(ServiceInitiator serviceInitiator); + } + + private final OwningRegistry serviceRegistry; + private final Class serviceRole; + private final ServiceInitiator serviceInitiator; + private R service; + + public ServiceBinding(OwningRegistry serviceRegistry, Class serviceRole, R service) { + this.serviceRegistry = serviceRegistry; + this.serviceRole = serviceRole; + this.serviceInitiator = null; + this.service = service; + } + + public ServiceBinding(OwningRegistry serviceRegistry, ServiceInitiator serviceInitiator) { + this.serviceRegistry = serviceRegistry; + this.serviceRole = serviceInitiator.getServiceInitiated(); + this.serviceInitiator = serviceInitiator; + } + + public OwningRegistry getServiceRegistry() { + return serviceRegistry; + } + + public Class getServiceRole() { + return serviceRole; + } + + public ServiceInitiator getServiceInitiator() { + return serviceInitiator; + } + + public R getService() { + return service; + } + + public void setService(R service) { + if ( this.service != null ) { + log.debug( "Overriding existing service binding [" + serviceRole.getName() + "]" ); + } + this.service = service; + } +} 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/ServiceInitiator.java similarity index 73% rename from hibernate-core/src/main/java/org/hibernate/service/spi/proxy/ServiceProxyFactory.java rename to hibernate-core/src/main/java/org/hibernate/service/spi/ServiceInitiator.java index 9d81709a1e..40107caf06 100644 --- a/hibernate-core/src/main/java/org/hibernate/service/spi/proxy/ServiceProxyFactory.java +++ b/hibernate-core/src/main/java/org/hibernate/service/spi/ServiceInitiator.java @@ -21,23 +21,18 @@ * 51 Franklin Street, Fifth Floor * Boston, MA 02110-1301 USA */ -package org.hibernate.service.spi.proxy; +package org.hibernate.service.spi; import org.hibernate.service.Service; /** - * Contract for creating proxy instances for {@link Service} instances. - * * @author Steve Ebersole */ -public interface ServiceProxyFactory { +public interface ServiceInitiator { /** - * Create a proxy for the given service role. + * Obtains the service role initiated by this initiator. Should be unique within a registry * - * @param serviceRole The service role for which to create a proxy. - * @param The type of the service - * - * @return The service proxy + * @return The service role. */ - public T makeProxy(Class serviceRole); + public Class getServiceInitiated(); } diff --git a/hibernate-core/src/main/java/org/hibernate/service/spi/ServiceRegistryImplementor.java b/hibernate-core/src/main/java/org/hibernate/service/spi/ServiceRegistryImplementor.java index 70179376d5..e15d6153bb 100644 --- a/hibernate-core/src/main/java/org/hibernate/service/spi/ServiceRegistryImplementor.java +++ b/hibernate-core/src/main/java/org/hibernate/service/spi/ServiceRegistryImplementor.java @@ -25,34 +25,11 @@ package org.hibernate.service.spi; import org.hibernate.service.Service; import org.hibernate.service.ServiceRegistry; -import org.hibernate.service.spi.proxy.ServiceProxyTargetSource; /** * @author Steve Ebersole */ -public interface ServiceRegistryImplementor extends ServiceRegistry, ServiceProxyTargetSource { +public interface ServiceRegistryImplementor extends ServiceRegistry { public ServiceBinding locateServiceBinding(Class serviceRole); - public void destroy(); - - public final class ServiceBinding { - private final R proxy; - private R target; - - public ServiceBinding(R proxy) { - this.proxy = proxy; - } - - public R getProxy() { - return proxy; - } - - public R getTarget() { - return target; - } - - public void setTarget(R target) { - this.target = target; - } - } } diff --git a/hibernate-core/src/main/java/org/hibernate/service/spi/SessionFactoryServiceInitiator.java b/hibernate-core/src/main/java/org/hibernate/service/spi/SessionFactoryServiceInitiator.java index 5c119f38d3..8df4a41182 100644 --- a/hibernate-core/src/main/java/org/hibernate/service/spi/SessionFactoryServiceInitiator.java +++ b/hibernate-core/src/main/java/org/hibernate/service/spi/SessionFactoryServiceInitiator.java @@ -30,14 +30,7 @@ import org.hibernate.service.Service; /** * @author Steve Ebersole */ -public interface SessionFactoryServiceInitiator { - /** - * Obtains the service role initiated by this initiator. Should be unique within a registry - * - * @return The service role. - */ - public Class getServiceInitiated(); - +public interface SessionFactoryServiceInitiator extends ServiceInitiator{ /** * Initiates the managed service. *

diff --git a/hibernate-core/src/main/java/org/hibernate/service/spi/SessionFactoryServiceRegistry.java b/hibernate-core/src/main/java/org/hibernate/service/spi/SessionFactoryServiceRegistry.java index 32ebfe2dd1..e2775874b6 100644 --- a/hibernate-core/src/main/java/org/hibernate/service/spi/SessionFactoryServiceRegistry.java +++ b/hibernate-core/src/main/java/org/hibernate/service/spi/SessionFactoryServiceRegistry.java @@ -30,5 +30,4 @@ package org.hibernate.service.spi; * @author Steve Ebersole */ public interface SessionFactoryServiceRegistry extends ServiceRegistryImplementor { - public void registerServiceInitiator(SessionFactoryServiceInitiator initiator); } 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 deleted file mode 100644 index ff638e4c01..0000000000 --- a/hibernate-core/src/main/java/org/hibernate/service/spi/proxy/ServiceProxyFactoryFactory.java +++ /dev/null @@ -1,40 +0,0 @@ -/* - * 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.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 deleted file mode 100644 index 45419fa2f6..0000000000 --- a/hibernate-core/src/main/java/org/hibernate/service/spi/proxy/ServiceProxyTargetSource.java +++ /dev/null @@ -1,46 +0,0 @@ -/* - * 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.service.spi.proxy; - -import org.hibernate.service.Service; -import org.hibernate.service.ServiceRegistry; - -/** - * Additional contract for service proxies. This allows the proxies access to their actual service instances. - * - * @author Steve Ebersole - */ -public interface ServiceProxyTargetSource extends ServiceRegistry { - /** - * Retrieve a service by role. Unlike {@link ServiceRegistry#getService}, this version will never return a proxy. - * - * @param serviceRole The service role - * @param The service role type - * - * @return The requested service. - * - * @throws org.hibernate.service.UnknownServiceException Indicates the service was not known. - */ - public R getServiceInternal(Class serviceRole); -} diff --git a/hibernate-core/src/main/java/org/hibernate/stat/internal/StatisticsInitiator.java b/hibernate-core/src/main/java/org/hibernate/stat/internal/StatisticsInitiator.java index f01273d311..f6a81c9073 100644 --- a/hibernate-core/src/main/java/org/hibernate/stat/internal/StatisticsInitiator.java +++ b/hibernate-core/src/main/java/org/hibernate/stat/internal/StatisticsInitiator.java @@ -26,9 +26,9 @@ package org.hibernate.stat.internal; import org.jboss.logging.Logger; import org.hibernate.HibernateException; +import org.hibernate.cfg.Configuration; import org.hibernate.engine.spi.SessionFactoryImplementor; import org.hibernate.internal.CoreMessageLogger; -import org.hibernate.cfg.Configuration; import org.hibernate.service.classloading.spi.ClassLoaderService; import org.hibernate.service.spi.ServiceRegistryImplementor; import org.hibernate.service.spi.SessionFactoryServiceInitiator; @@ -90,7 +90,6 @@ public class StatisticsInitiator implements SessionFactoryServiceInitiator