HHH-6237 - Remove Service proxy code
This commit is contained in:
parent
dcc3546378
commit
5385cc5cc0
|
@ -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);
|
||||
}
|
||||
|
|
|
@ -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<Class,ServiceBinding> 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 <R extends Service> void createServiceBinding(ServiceInitiator<R> initiator) {
|
||||
serviceBindingMap.put( initiator.getServiceInitiated(), new ServiceBinding( this, initiator ) );
|
||||
}
|
||||
|
||||
protected <R extends Service> void createServiceBinding(ProvidedService<R> providedService) {
|
||||
ServiceBinding<R> binding = locateServiceBinding( providedService.getServiceRole(), false );
|
||||
if ( binding == null ) {
|
||||
binding = new ServiceBinding<R>( 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 extends Service> R getService(Class<R> serviceRole) {
|
||||
return locateOrCreateServiceBinding( serviceRole, true ).getProxy();
|
||||
}
|
||||
|
||||
@SuppressWarnings({ "unchecked" })
|
||||
protected <R extends Service> ServiceBinding<R> locateOrCreateServiceBinding(Class<R> serviceRole, boolean checkParent) {
|
||||
ServiceBinding<R> serviceBinding = locateServiceBinding( serviceRole, checkParent );
|
||||
final ServiceBinding<R> serviceBinding = locateServiceBinding( serviceRole );
|
||||
if ( serviceBinding == null ) {
|
||||
createServiceBinding( serviceRole );
|
||||
}
|
||||
return serviceBinding;
|
||||
throw new UnknownServiceException( serviceRole );
|
||||
}
|
||||
|
||||
protected <R extends Service> ServiceBinding<R> createServiceBinding(Class<R> serviceRole) {
|
||||
R proxy = serviceProxyFactory.makeProxy( serviceRole );
|
||||
ServiceBinding<R> serviceBinding = new ServiceBinding<R>( proxy );
|
||||
serviceBindingMap.put( serviceRole, serviceBinding );
|
||||
return serviceBinding;
|
||||
R service = serviceBinding.getService();
|
||||
if ( service == null ) {
|
||||
service = initializeService( serviceBinding );
|
||||
}
|
||||
|
||||
protected <R extends Service> void registerService(Class<R> serviceRole, R service) {
|
||||
ServiceBinding<R> serviceBinding = locateOrCreateServiceBinding( serviceRole, false );
|
||||
R priorServiceInstance = serviceBinding.getTarget();
|
||||
serviceBinding.setTarget( service );
|
||||
return service;
|
||||
}
|
||||
|
||||
protected <R extends Service> void registerService(ServiceBinding<R> serviceBinding, R service) {
|
||||
R priorServiceInstance = serviceBinding.getService();
|
||||
serviceBinding.setService( service );
|
||||
if ( priorServiceInstance != null ) {
|
||||
serviceList.remove( priorServiceInstance );
|
||||
}
|
||||
serviceList.add( service );
|
||||
}
|
||||
|
||||
private <R extends Service> R initializeService(Class<R> serviceRole) {
|
||||
LOG.trace("Initializing service [role=" + serviceRole.getName() + "]");
|
||||
private <R extends Service> R initializeService(ServiceBinding<R> 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 extends Service> T createService(Class<T> serviceRole);
|
||||
@SuppressWarnings( {"unchecked"})
|
||||
protected <R extends Service> R createService(ServiceBinding<R> serviceBinding) {
|
||||
final ServiceInitiator<R> 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 <T extends Service> void configureService(T service);
|
||||
|
||||
protected <T extends Service> void applyInjections(T service) {
|
||||
|
@ -197,36 +223,19 @@ public abstract class AbstractServiceRegistryImpl implements ServiceRegistryImpl
|
|||
}
|
||||
|
||||
@SuppressWarnings({ "unchecked" })
|
||||
protected <T extends Service> void startService(T service, Class serviceRole) {
|
||||
if ( Startable.class.isInstance( service ) ) {
|
||||
( (Startable) service ).start();
|
||||
protected <R extends Service> void startService(ServiceBinding<R> 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 extends Service> R getServiceInternal(Class<R> serviceRole) {
|
||||
// this call comes from the binding proxy, we most definitely do not want to look up into the parent
|
||||
// in this case!
|
||||
ServiceBinding<R> 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<Service> serviceIterator = serviceList.listIterator( serviceList.size() );
|
||||
while ( serviceIterator.hasPrevious() ) {
|
||||
|
|
|
@ -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<Class,BasicServiceInitiator> serviceInitiatorMap;
|
||||
private final Map configurationValues;
|
||||
|
||||
@SuppressWarnings( {"unchecked"})
|
||||
public BasicServiceRegistryImpl(
|
||||
List<BasicServiceInitiator> serviceInitiators,
|
||||
List<ProvidedService> providedServices,
|
||||
Map configurationValues) {
|
||||
final List<BasicServiceInitiator> serviceInitiators,
|
||||
final List<ProvidedService> 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:<ul>
|
||||
* <li>to make it easier to look up the initiator we need for a given service role</li>
|
||||
* <li>to make sure there is only one initiator for a given service role (last wins)</li>
|
||||
* </ul>
|
||||
*
|
||||
* @param serviceInitiators The list of individual initiators
|
||||
*
|
||||
* @return The map of initiators keyed by the service rle they initiate.
|
||||
*/
|
||||
private static Map<Class, BasicServiceInitiator> toMap(List<BasicServiceInitiator> serviceInitiators) {
|
||||
final Map<Class, BasicServiceInitiator> result = new HashMap<Class, BasicServiceInitiator>();
|
||||
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 extends Service> T createService(Class<T> serviceRole) {
|
||||
BasicServiceInitiator<T> 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 extends Service> R initiateService(ServiceInitiator<R> serviceInitiator) {
|
||||
// todo : add check/error for unexpected initiator types?
|
||||
return ( (BasicServiceInitiator<R>) serviceInitiator ).initiateService( configurationValues, this );
|
||||
}
|
||||
|
||||
@Override
|
||||
|
|
|
@ -28,20 +28,20 @@ package org.hibernate.service.internal;
|
|||
*
|
||||
* @author Steve Ebersole
|
||||
*/
|
||||
public class ProvidedService<T> {
|
||||
private final Class<T> serviceRole;
|
||||
private final T service;
|
||||
public class ProvidedService<R> {
|
||||
private final Class<R> serviceRole;
|
||||
private final R service;
|
||||
|
||||
public ProvidedService(Class<T> serviceRole, T service) {
|
||||
public ProvidedService(Class<R> serviceRole, R service) {
|
||||
this.serviceRole = serviceRole;
|
||||
this.service = service;
|
||||
}
|
||||
|
||||
public Class<T> getServiceRole() {
|
||||
public Class<R> getServiceRole() {
|
||||
return serviceRole;
|
||||
}
|
||||
|
||||
public T getService() {
|
||||
public R getService() {
|
||||
return service;
|
||||
}
|
||||
}
|
||||
|
|
|
@ -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<Class,SessionFactoryServiceInitiator> 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<Class, SessionFactoryServiceInitiator> toMap(List<SessionFactoryServiceInitiator> serviceInitiators) {
|
||||
final Map<Class, SessionFactoryServiceInitiator> result = new HashMap<Class, SessionFactoryServiceInitiator>();
|
||||
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 extends Service> T createService(Class<T> serviceRole) {
|
||||
SessionFactoryServiceInitiator<T> 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 extends Service> R initiateService(ServiceInitiator<R> serviceInitiator) {
|
||||
// todo : add check/error for unexpected initiator types?
|
||||
return ( (SessionFactoryServiceInitiator<R>) serviceInitiator ).initiateService( sessionFactory, configuration, this );
|
||||
}
|
||||
|
||||
@Override
|
||||
|
|
|
@ -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 );
|
||||
}
|
||||
}
|
|
@ -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 extends Service> T makeProxy(Class<T> 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>( (T)proxyObject, serviceRole, serviceRegistry ) );
|
||||
return (T) proxyObject;
|
||||
}
|
||||
catch (Exception e) {
|
||||
throw new ServiceProxyGenerationException( "Unable to make service proxy", e );
|
||||
}
|
||||
}
|
||||
|
||||
private static class ServiceProxyMethodInterceptor<T extends Service> implements MethodHandler {
|
||||
private final T proxy;
|
||||
private final Class<T> serviceRole;
|
||||
private final ServiceProxyTargetSource serviceRegistry;
|
||||
|
||||
private ServiceProxyMethodInterceptor(T proxy, Class<T> 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();
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
}
|
|
@ -32,14 +32,7 @@ import org.hibernate.service.Service;
|
|||
*
|
||||
* @author Steve Ebersole
|
||||
*/
|
||||
public interface BasicServiceInitiator<R extends Service> {
|
||||
/**
|
||||
* Obtains the service role initiated by this initiator. Should be unique within a registry
|
||||
*
|
||||
* @return The service role.
|
||||
*/
|
||||
public Class<R> getServiceInitiated();
|
||||
|
||||
public interface BasicServiceInitiator<R extends Service> extends ServiceInitiator<R> {
|
||||
/**
|
||||
* Initiates the managed service.
|
||||
*
|
||||
|
|
|
@ -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<R extends Service> {
|
||||
private static final Logger log = Logger.getLogger( ServiceBinding.class );
|
||||
|
||||
public static interface OwningRegistry {
|
||||
public <R extends Service> R initiateService(ServiceInitiator<R> serviceInitiator);
|
||||
}
|
||||
|
||||
private final OwningRegistry serviceRegistry;
|
||||
private final Class<R> serviceRole;
|
||||
private final ServiceInitiator<R> serviceInitiator;
|
||||
private R service;
|
||||
|
||||
public ServiceBinding(OwningRegistry serviceRegistry, Class<R> serviceRole, R service) {
|
||||
this.serviceRegistry = serviceRegistry;
|
||||
this.serviceRole = serviceRole;
|
||||
this.serviceInitiator = null;
|
||||
this.service = service;
|
||||
}
|
||||
|
||||
public ServiceBinding(OwningRegistry serviceRegistry, ServiceInitiator<R> serviceInitiator) {
|
||||
this.serviceRegistry = serviceRegistry;
|
||||
this.serviceRole = serviceInitiator.getServiceInitiated();
|
||||
this.serviceInitiator = serviceInitiator;
|
||||
}
|
||||
|
||||
public OwningRegistry getServiceRegistry() {
|
||||
return serviceRegistry;
|
||||
}
|
||||
|
||||
public Class<R> getServiceRole() {
|
||||
return serviceRole;
|
||||
}
|
||||
|
||||
public ServiceInitiator<R> 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;
|
||||
}
|
||||
}
|
|
@ -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<R extends Service> {
|
||||
/**
|
||||
* 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 <T> The type of the service
|
||||
*
|
||||
* @return The service proxy
|
||||
* @return The service role.
|
||||
*/
|
||||
public <T extends Service> T makeProxy(Class<T> serviceRole);
|
||||
public Class<R> getServiceInitiated();
|
||||
}
|
|
@ -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 <R extends Service> ServiceBinding<R> locateServiceBinding(Class<R> serviceRole);
|
||||
|
||||
public void destroy();
|
||||
|
||||
public final class ServiceBinding<R> {
|
||||
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;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -30,14 +30,7 @@ import org.hibernate.service.Service;
|
|||
/**
|
||||
* @author Steve Ebersole
|
||||
*/
|
||||
public interface SessionFactoryServiceInitiator<R extends Service> {
|
||||
/**
|
||||
* Obtains the service role initiated by this initiator. Should be unique within a registry
|
||||
*
|
||||
* @return The service role.
|
||||
*/
|
||||
public Class<R> getServiceInitiated();
|
||||
|
||||
public interface SessionFactoryServiceInitiator<R extends Service> extends ServiceInitiator<R>{
|
||||
/**
|
||||
* Initiates the managed service.
|
||||
* <p/>
|
||||
|
|
|
@ -30,5 +30,4 @@ package org.hibernate.service.spi;
|
|||
* @author Steve Ebersole
|
||||
*/
|
||||
public interface SessionFactoryServiceRegistry extends ServiceRegistryImplementor {
|
||||
public void registerServiceInitiator(SessionFactoryServiceInitiator initiator);
|
||||
}
|
||||
|
|
|
@ -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);
|
||||
}
|
|
@ -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 <R> The service role type
|
||||
*
|
||||
* @return The requested service.
|
||||
*
|
||||
* @throws org.hibernate.service.UnknownServiceException Indicates the service was not known.
|
||||
*/
|
||||
public <R extends Service> R getServiceInternal(Class<R> serviceRole);
|
||||
}
|
|
@ -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<Stati
|
|||
statistics.setStatisticsEnabled( enabled );
|
||||
LOG.debugf( "Statistics initialized [enabled=%s]", enabled );
|
||||
return statistics;
|
||||
|
||||
}
|
||||
|
||||
private static StatisticsFactory DEFAULT_STATS_BUILDER = new StatisticsFactory() {
|
||||
|
|
|
@ -112,7 +112,7 @@ public class ManagedDrivingTest extends BaseUnitTestCase {
|
|||
fail( "incorrect exception type : SQLException" );
|
||||
}
|
||||
|
||||
JtaPlatform instance = ( (ServiceProxy) serviceRegistry.getService( JtaPlatform.class ) ).getTargetInstance();
|
||||
JtaPlatform instance = serviceRegistry.getService( JtaPlatform.class );
|
||||
TransactionManager transactionManager = instance.retrieveTransactionManager();
|
||||
|
||||
// start the cmt
|
||||
|
|
Loading…
Reference in New Issue