HHH-9838 - Leverage ClassLoaderService during JavassistLazyInitializer#getProxyFactory

This commit is contained in:
Steve Ebersole 2015-06-05 13:03:44 -05:00
parent 5c8d1ac500
commit 5b1da92498
11 changed files with 300 additions and 175 deletions

View File

@ -17,6 +17,7 @@ import org.hibernate.AssertionFailure;
import org.hibernate.HibernateException;
import org.hibernate.bytecode.spi.BasicProxyFactory;
import org.hibernate.bytecode.spi.ProxyFactoryFactory;
import org.hibernate.engine.spi.SessionFactoryImplementor;
import org.hibernate.proxy.ProxyFactory;
import org.hibernate.proxy.pojo.javassist.JavassistProxyFactory;
@ -32,7 +33,8 @@ public class ProxyFactoryFactoryImpl implements ProxyFactoryFactory {
*
* @return a new Javassist-based proxy factory.
*/
public ProxyFactory buildProxyFactory() {
@Override
public ProxyFactory buildProxyFactory(SessionFactoryImplementor sessionFactory) {
return new JavassistProxyFactory();
}
@ -44,6 +46,7 @@ public class ProxyFactoryFactoryImpl implements ProxyFactoryFactory {
*
* @return The constructed BasicProxyFactoryImpl
*/
@Override
public BasicProxyFactory buildBasicProxyFactory(Class superClass, Class[] interfaces) {
return new BasicProxyFactoryImpl( superClass, interfaces );
}

View File

@ -6,7 +6,11 @@
*/
package org.hibernate.bytecode.spi;
import org.hibernate.SessionFactory;
import org.hibernate.engine.spi.SessionFactoryImplementor;
import org.hibernate.internal.SessionFactoryImpl;
import org.hibernate.proxy.ProxyFactory;
import org.hibernate.service.ServiceRegistry;
/**
* An interface for factories of {@link ProxyFactory proxy factory} instances.
@ -23,7 +27,7 @@ public interface ProxyFactoryFactory {
*
* @return The lazy-load proxy factory.
*/
public ProxyFactory buildProxyFactory();
public ProxyFactory buildProxyFactory(SessionFactoryImplementor sessionFactory);
/**
* Build a proxy factory for basic proxy concerns. The return

View File

@ -382,6 +382,11 @@ public class SessionFactoryDelegatingImpl implements SessionFactoryImplementor,
return delegate.locateEntityPersister( byName );
}
@Override
public DeserializationResolver getDeserializationResolver() {
return delegate.getDeserializationResolver();
}
@Override
public IdentifierGeneratorFactory getIdentifierGeneratorFactory() {
return delegate.getIdentifierGeneratorFactory();

View File

@ -6,6 +6,7 @@
*/
package org.hibernate.engine.spi;
import java.io.Serializable;
import java.util.Map;
import java.util.Properties;
import java.util.Set;
@ -302,4 +303,13 @@ public interface SessionFactoryImplementor extends Mapping, SessionFactory {
* @throws HibernateException If a matching EntityPersister cannot be located
*/
EntityPersister locateEntityPersister(String byName);
/**
* Contract for resolving this SessionFactory on deserialization
*/
interface DeserializationResolver<T extends SessionFactoryImplementor> extends Serializable {
T resolve();
}
DeserializationResolver getDeserializationResolver();
}

View File

@ -796,6 +796,16 @@ public final class SessionFactoryImpl implements SessionFactoryImplementor {
return entityPersister;
}
@Override
public DeserializationResolver getDeserializationResolver() {
return new DeserializationResolver() {
@Override
public SessionFactoryImplementor resolve() {
return (SessionFactoryImplementor) SessionFactoryRegistry.INSTANCE.findSessionFactory( uuid, name );
}
};
}
@Override
public Map<String, CollectionPersister> getCollectionPersisters() {
return collectionPersisters;

View File

@ -17,9 +17,11 @@ import javax.naming.event.NamingExceptionEvent;
import javax.naming.spi.ObjectFactory;
import org.hibernate.SessionFactory;
import org.hibernate.annotations.common.util.StringHelper;
import org.hibernate.engine.jndi.JndiException;
import org.hibernate.engine.jndi.JndiNameException;
import org.hibernate.engine.jndi.spi.JndiService;
import org.hibernate.engine.spi.SessionFactoryImplementor;
/**
* A registry of all {@link SessionFactory} instances for the same classloader as this class.
@ -158,6 +160,14 @@ public class SessionFactoryRegistry {
return sessionFactory;
}
public SessionFactory findSessionFactory(String uuid, String name) {
SessionFactory sessionFactory = getSessionFactory( uuid );
if ( sessionFactory == null && StringHelper.isNotEmpty( name ) ) {
sessionFactory = getNamedSessionFactory( name );
}
return sessionFactory;
}
/**
* Does this registry currently contain registrations?
*

View File

@ -47,7 +47,7 @@ public interface ProxyFactory {
public void postInstantiate(
String entityName,
Class persistentClass,
Set interfaces,
Set<Class> interfaces,
Method getIdentifierMethod,
Method setIdentifierMethod,
CompositeType componentIdType) throws HibernateException;

View File

@ -10,144 +10,44 @@ import java.io.Serializable;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import javassist.util.proxy.MethodFilter;
import javassist.util.proxy.MethodHandler;
import javassist.util.proxy.Proxy;
import javassist.util.proxy.ProxyFactory;
import org.hibernate.HibernateException;
import org.hibernate.engine.spi.SessionImplementor;
import org.hibernate.internal.CoreLogging;
import org.hibernate.internal.CoreMessageLogger;
import org.hibernate.internal.util.ReflectHelper;
import org.hibernate.proxy.HibernateProxy;
import org.hibernate.proxy.pojo.BasicLazyInitializer;
import org.hibernate.type.CompositeType;
import static org.hibernate.internal.CoreLogging.messageLogger;
/**
* A Javassist-based lazy initializer proxy.
*
* @author Muga Nishizawa
*/
public class JavassistLazyInitializer extends BasicLazyInitializer implements MethodHandler {
private static final CoreMessageLogger LOG = CoreLogging.messageLogger( JavassistLazyInitializer.class );
private static final CoreMessageLogger LOG = messageLogger( JavassistLazyInitializer.class );
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" ) );
}
};
private final Class[] interfaces;
private Class[] interfaces;
private boolean constructed;
private JavassistLazyInitializer(
final String entityName,
final Class persistentClass,
final Class[] interfaces,
final Serializable id,
final Method getIdentifierMethod,
final Method setIdentifierMethod,
final CompositeType componentIdType,
final SessionImplementor session,
final boolean overridesEquals) {
public JavassistLazyInitializer(
String entityName,
Class persistentClass,
Class[] interfaces,
Serializable id,
Method getIdentifierMethod,
Method setIdentifierMethod,
CompositeType componentIdType,
SessionImplementor session,
boolean overridesEquals) {
super( entityName, persistentClass, id, getIdentifierMethod, setIdentifierMethod, componentIdType, session, overridesEquals );
this.interfaces = interfaces;
}
public static HibernateProxy getProxy(
final String entityName,
final Class persistentClass,
final Class[] interfaces,
final Method getIdentifierMethod,
final Method setIdentifierMethod,
CompositeType componentIdType,
final Serializable id,
final SessionImplementor session) throws HibernateException {
// note: interface is assumed to already contain HibernateProxy.class
try {
final JavassistLazyInitializer instance = new JavassistLazyInitializer(
entityName,
persistentClass,
interfaces,
id,
getIdentifierMethod,
setIdentifierMethod,
componentIdType,
session,
ReflectHelper.overridesEquals(persistentClass)
);
ProxyFactory factory = new ProxyFactory();
factory.setSuperclass( interfaces.length == 1 ? persistentClass : null );
factory.setInterfaces( interfaces );
factory.setFilter( FINALIZE_FILTER );
Class cl = factory.createClass();
final HibernateProxy proxy = ( HibernateProxy ) cl.newInstance();
( ( Proxy ) proxy ).setHandler( instance );
instance.constructed = true;
return proxy;
}
catch ( Throwable t ) {
LOG.error(LOG.javassistEnhancementFailed(entityName), t);
throw new HibernateException(LOG.javassistEnhancementFailed(entityName), t);
}
}
public static HibernateProxy getProxy(
final Class factory,
final String entityName,
final Class persistentClass,
final Class[] interfaces,
final Method getIdentifierMethod,
final Method setIdentifierMethod,
final CompositeType componentIdType,
final Serializable id,
final SessionImplementor session,
final boolean classOverridesEquals) throws HibernateException {
final JavassistLazyInitializer instance = new JavassistLazyInitializer(
entityName,
persistentClass,
interfaces, id,
getIdentifierMethod,
setIdentifierMethod,
componentIdType,
session,
classOverridesEquals
);
final HibernateProxy proxy;
try {
proxy = ( HibernateProxy ) factory.newInstance();
}
catch ( Exception e ) {
throw new HibernateException(
"Javassist Enhancement failed: "
+ persistentClass.getName(), e
);
}
( ( Proxy ) proxy ).setHandler( instance );
instance.constructed = true;
return proxy;
}
public static Class getProxyFactory(
Class persistentClass,
Class[] interfaces) throws HibernateException {
// note: interfaces is assumed to already contain HibernateProxy.class
try {
ProxyFactory factory = new ProxyFactory();
factory.setSuperclass( interfaces.length == 1 ? persistentClass : null );
factory.setInterfaces( interfaces );
factory.setFilter( FINALIZE_FILTER );
return factory.createClass();
}
catch ( Throwable t ) {
LOG.error(LOG.javassistEnhancementFailed(persistentClass.getName()), t);
throw new HibernateException(LOG.javassistEnhancementFailed(persistentClass.getName()), t);
}
protected void constructed() {
constructed = true;
}
@Override

View File

@ -5,68 +5,211 @@
* See the lgpl.txt file in the root directory or <http://www.gnu.org/licenses/lgpl-2.1.html>.
*/
package org.hibernate.proxy.pojo.javassist;
import java.io.Serializable;
import java.lang.reflect.Method;
import java.util.Locale;
import java.util.Set;
import javassist.util.proxy.MethodFilter;
import javassist.util.proxy.Proxy;
import org.hibernate.HibernateException;
import org.hibernate.engine.spi.SessionImplementor;
import org.hibernate.internal.CoreMessageLogger;
import org.hibernate.internal.util.ReflectHelper;
import org.hibernate.internal.util.collections.ArrayHelper;
import org.hibernate.proxy.HibernateProxy;
import org.hibernate.proxy.ProxyFactory;
import org.hibernate.type.CompositeType;
import static org.hibernate.internal.CoreLogging.messageLogger;
/**
* A {@link ProxyFactory} implementation for producing Javassist-based proxies.
*
* @author Muga Nishizawa
*/
public class JavassistProxyFactory implements ProxyFactory, Serializable {
private static final CoreMessageLogger LOG = messageLogger( JavassistProxyFactory.class );
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" ) );
}
};
protected static final Class[] NO_CLASSES = new Class[0];
private Class persistentClass;
private String entityName;
private Class[] interfaces;
private Method getIdentifierMethod;
private Method setIdentifierMethod;
private CompositeType componentIdType;
private Class factory;
private boolean overridesEquals;
private Class proxyClass;
public JavassistProxyFactory() {
}
@Override
public void postInstantiate(
final String entityName,
final Class persistentClass,
final Set interfaces,
final Set<Class> interfaces,
final Method getIdentifierMethod,
final Method setIdentifierMethod,
CompositeType componentIdType) throws HibernateException {
this.entityName = entityName;
this.persistentClass = persistentClass;
this.interfaces = (Class[]) interfaces.toArray(NO_CLASSES);
this.interfaces = toArray( interfaces );
this.getIdentifierMethod = getIdentifierMethod;
this.setIdentifierMethod = setIdentifierMethod;
this.componentIdType = componentIdType;
this.factory = JavassistLazyInitializer.getProxyFactory( persistentClass, this.interfaces );
this.overridesEquals = ReflectHelper.overridesEquals(persistentClass);
this.overridesEquals = ReflectHelper.overridesEquals( persistentClass );
this.proxyClass = buildJavassistProxyFactory().createClass();
}
private Class[] toArray(Set<Class> interfaces) {
if ( interfaces == null ) {
return ArrayHelper.EMPTY_CLASS_ARRAY;
}
return interfaces.toArray( new Class[interfaces.size()] );
}
private javassist.util.proxy.ProxyFactory buildJavassistProxyFactory() {
return buildJavassistProxyFactory(
persistentClass,
interfaces
);
}
public static javassist.util.proxy.ProxyFactory buildJavassistProxyFactory(
final Class persistentClass,
final Class[] interfaces) {
javassist.util.proxy.ProxyFactory factory = new javassist.util.proxy.ProxyFactory() {
@Override
protected ClassLoader getClassLoader() {
return persistentClass.getClassLoader();
}
};
factory.setSuperclass( interfaces.length == 1 ? persistentClass : null );
factory.setInterfaces( interfaces );
factory.setFilter( FINALIZE_FILTER );
return factory;
}
@Override
public HibernateProxy getProxy(
Serializable id,
SessionImplementor session) throws HibernateException {
return JavassistLazyInitializer.getProxy(
factory,
final JavassistLazyInitializer initializer = new JavassistLazyInitializer(
entityName,
persistentClass,
interfaces,
id,
getIdentifierMethod,
setIdentifierMethod,
componentIdType,
id,
session,
overridesEquals
);
try {
final HibernateProxy proxy = (HibernateProxy) proxyClass.newInstance();
( (Proxy) proxy ).setHandler( initializer );
initializer.constructed();
return proxy;
}
catch (Throwable t) {
LOG.error( LOG.javassistEnhancementFailed( entityName ), t );
throw new HibernateException( LOG.javassistEnhancementFailed( entityName ), t );
}
}
public static HibernateProxy deserializeProxy(SerializableProxy serializableProxy) {
final JavassistLazyInitializer initializer = new JavassistLazyInitializer(
serializableProxy.getEntityName(),
serializableProxy.getPersistentClass(),
serializableProxy.getInterfaces(),
serializableProxy.getId(),
resolveIdGetterMethod( serializableProxy ),
resolveIdSetterMethod( serializableProxy ),
serializableProxy.getComponentIdType(),
null,
ReflectHelper.overridesEquals( serializableProxy.getPersistentClass() )
);
final javassist.util.proxy.ProxyFactory factory = buildJavassistProxyFactory(
serializableProxy.getPersistentClass(),
serializableProxy.getInterfaces()
);
// note: interface is assumed to already contain HibernateProxy.class
try {
final Class proxyClass = factory.createClass();
final HibernateProxy proxy = ( HibernateProxy ) proxyClass.newInstance();
( (Proxy) proxy ).setHandler( initializer );
initializer.constructed();
return proxy;
}
catch ( Throwable t ) {
final String message = LOG.javassistEnhancementFailed( serializableProxy.getEntityName() );
LOG.error( message, t );
throw new HibernateException( message, t );
}
}
@SuppressWarnings("unchecked")
private static Method resolveIdGetterMethod(SerializableProxy serializableProxy) {
if ( serializableProxy.getIdentifierGetterMethodName() == null ) {
return null;
}
try {
return serializableProxy.getIdentifierGetterMethodClass().getDeclaredMethod( serializableProxy.getIdentifierGetterMethodName() );
}
catch (NoSuchMethodException e) {
throw new HibernateException(
String.format(
Locale.ENGLISH,
"Unable to deserialize proxy [%s, %s]; could not locate id getter method [%s] on entity class [%s]",
serializableProxy.getEntityName(),
serializableProxy.getId(),
serializableProxy.getIdentifierGetterMethodName(),
serializableProxy.getIdentifierGetterMethodClass()
)
);
}
}
@SuppressWarnings("unchecked")
private static Method resolveIdSetterMethod(SerializableProxy serializableProxy) {
if ( serializableProxy.getIdentifierSetterMethodName() == null ) {
return null;
}
try {
return serializableProxy.getIdentifierSetterMethodClass().getDeclaredMethod(
serializableProxy.getIdentifierSetterMethodName(),
serializableProxy.getIdentifierSetterMethodParams()
);
}
catch (NoSuchMethodException e) {
throw new HibernateException(
String.format(
Locale.ENGLISH,
"Unable to deserialize proxy [%s, %s]; could not locate id setter method [%s] on entity class [%s]",
serializableProxy.getEntityName(),
serializableProxy.getId(),
serializableProxy.getIdentifierSetterMethodName(),
serializableProxy.getIdentifierSetterMethodClass()
)
);
}
}
}

View File

@ -5,10 +5,10 @@
* See the lgpl.txt file in the root directory or <http://www.gnu.org/licenses/lgpl-2.1.html>.
*/
package org.hibernate.proxy.pojo.javassist;
import java.io.Serializable;
import java.lang.reflect.Method;
import org.hibernate.HibernateException;
import org.hibernate.proxy.AbstractSerializableProxy;
import org.hibernate.proxy.HibernateProxy;
import org.hibernate.type.CompositeType;
@ -17,64 +17,104 @@ import org.hibernate.type.CompositeType;
* Serializable placeholder for Javassist proxies
*/
public final class SerializableProxy extends AbstractSerializableProxy {
private final Class persistentClass;
private final Class[] interfaces;
private Class persistentClass;
private Class[] interfaces;
private Class getIdentifierMethodClass;
private Class setIdentifierMethodClass;
private String getIdentifierMethodName;
private String setIdentifierMethodName;
private Class[] setIdentifierMethodParams;
private CompositeType componentIdType;
private final String identifierGetterMethodName;
private final Class identifierGetterMethodClass;
public SerializableProxy() {
}
private final String identifierSetterMethodName;
private final Class identifierSetterMethodClass;
private final Class[] identifierSetterMethodParams;
private final CompositeType componentIdType;
public SerializableProxy(
final String entityName,
final Class persistentClass,
final Class[] interfaces,
final Serializable id,
final Boolean readOnly,
final Method getIdentifierMethod,
final Method setIdentifierMethod,
String entityName,
Class persistentClass,
Class[] interfaces,
Serializable id,
Boolean readOnly,
Method getIdentifierMethod,
Method setIdentifierMethod,
CompositeType componentIdType) {
super( entityName, id, readOnly );
this.persistentClass = persistentClass;
this.interfaces = interfaces;
if (getIdentifierMethod!=null) {
getIdentifierMethodClass = getIdentifierMethod.getDeclaringClass();
getIdentifierMethodName = getIdentifierMethod.getName();
if ( getIdentifierMethod != null ) {
identifierGetterMethodName = getIdentifierMethod.getName();
identifierGetterMethodClass = getIdentifierMethod.getDeclaringClass();
}
if (setIdentifierMethod!=null) {
setIdentifierMethodClass = setIdentifierMethod.getDeclaringClass();
setIdentifierMethodName = setIdentifierMethod.getName();
setIdentifierMethodParams = setIdentifierMethod.getParameterTypes();
else {
identifierGetterMethodName = null;
identifierGetterMethodClass = null;
}
if ( setIdentifierMethod != null ) {
identifierSetterMethodName = setIdentifierMethod.getName();
identifierSetterMethodClass = setIdentifierMethod.getDeclaringClass();
identifierSetterMethodParams = setIdentifierMethod.getParameterTypes();
}
else {
identifierSetterMethodName = null;
identifierSetterMethodClass = null;
identifierSetterMethodParams = null;
}
this.componentIdType = componentIdType;
}
@Override
protected String getEntityName() {
return super.getEntityName();
}
@Override
protected Serializable getId() {
return super.getId();
}
protected Class getPersistentClass() {
return persistentClass;
}
protected Class[] getInterfaces() {
return interfaces;
}
protected String getIdentifierGetterMethodName() {
return identifierGetterMethodName;
}
protected Class getIdentifierGetterMethodClass() {
return identifierGetterMethodClass;
}
protected String getIdentifierSetterMethodName() {
return identifierSetterMethodName;
}
protected Class getIdentifierSetterMethodClass() {
return identifierSetterMethodClass;
}
protected Class[] getIdentifierSetterMethodParams() {
return identifierSetterMethodParams;
}
protected CompositeType getComponentIdType() {
return componentIdType;
}
/**
* Deserialization hook. This method is called by JDK deserialization. We use this hook
* to replace the serial form with a live form.
*
* @return The live form.
*/
private Object readResolve() {
try {
HibernateProxy proxy = JavassistLazyInitializer.getProxy(
getEntityName(),
persistentClass,
interfaces,
getIdentifierMethodName==null
? null
: getIdentifierMethodClass.getDeclaredMethod( getIdentifierMethodName, (Class[]) null ),
setIdentifierMethodName==null
? null
: setIdentifierMethodClass.getDeclaredMethod(setIdentifierMethodName, setIdentifierMethodParams),
componentIdType,
getId(),
null
);
setReadOnlyBeforeAttachedToSession( ( JavassistLazyInitializer ) proxy.getHibernateLazyInitializer() );
return proxy;
}
catch (NoSuchMethodException nsme) {
throw new HibernateException("could not create proxy for entity: " + getEntityName(), nsme);
}
HibernateProxy proxy = JavassistProxyFactory.deserializeProxy( this );
setReadOnlyBeforeAttachedToSession( ( JavassistLazyInitializer ) proxy.getHibernateLazyInitializer() );
return proxy;
}
}

View File

@ -190,7 +190,7 @@ public class PojoEntityTuplizer extends AbstractEntityTuplizer {
Getter idGetter,
Setter idSetter) {
// TODO : YUCK!!! fix after HHH-1907 is complete
return Environment.getBytecodeProvider().getProxyFactoryFactory().buildProxyFactory();
return Environment.getBytecodeProvider().getProxyFactoryFactory().buildProxyFactory( getFactory() );
// return getFactory().getSettings().getBytecodeProvider().getProxyFactoryFactory().buildProxyFactory();
}