1 package org.hibernate.proxy; 3 4 import java.io.Serializable ; 5 import java.lang.reflect.Method ; 6 7 import net.sf.cglib.proxy.Enhancer; 8 import net.sf.cglib.proxy.Factory; 9 import net.sf.cglib.proxy.MethodInterceptor; 10 import net.sf.cglib.proxy.MethodProxy; 11 import org.hibernate.HibernateException; 12 import org.hibernate.engine.SessionImplementor; 13 import org.hibernate.type.AbstractComponentType; 14 import org.hibernate.util.ReflectHelper; 15 16 import org.apache.commons.logging.LogFactory; 17 18 21 public final class CGLIBLazyInitializer extends BasicLazyInitializer implements MethodInterceptor { 22 23 private Class [] interfaces; 24 private boolean constructed = false; 25 26 static HibernateProxy getProxy(final String entityName, final Class persistentClass, 27 final Class [] interfaces, final Method getIdentifierMethod, 28 final Method setIdentifierMethod, AbstractComponentType componentIdType, 29 final Serializable id, final SessionImplementor session) throws HibernateException { 30 32 try { 33 final CGLIBLazyInitializer instance = new CGLIBLazyInitializer( 34 entityName, 35 persistentClass, 36 interfaces, 37 id, 38 getIdentifierMethod, 39 setIdentifierMethod, 40 componentIdType, 41 session 42 ); 43 44 Class concreteClass = interfaces.length == 1 ? persistentClass : null; 45 final HibernateProxy proxy = (HibernateProxy) Enhancer.create(concreteClass, interfaces, instance); 46 47 instance.constructed = true; 48 return proxy; 49 } 50 catch (Throwable t) { 51 LogFactory.getLog( BasicLazyInitializer.class ) 52 .error( "CGLIB Enhancement failed: " + entityName, t ); 53 throw new HibernateException( "CGLIB Enhancement failed: " + entityName, t ); 54 } 55 } 56 57 public static HibernateProxy getProxy(final Class factory, final String entityName, 58 final Class persistentClass, final Class [] interfaces, 59 final Method getIdentifierMethod, final Method setIdentifierMethod, 60 AbstractComponentType componentIdType, final Serializable id, 61 final SessionImplementor session) throws HibernateException { 62 63 final CGLIBLazyInitializer instance = new CGLIBLazyInitializer( 64 entityName, 65 persistentClass, 66 interfaces, 67 id, 68 getIdentifierMethod, 69 setIdentifierMethod, 70 componentIdType, 71 session 72 ); 73 74 final HibernateProxy proxy; 75 try { 76 proxy = (HibernateProxy) factory.newInstance(); 77 } 78 catch (Exception e) { 79 throw new HibernateException( "CGLIB Enhancement failed: " + persistentClass.getName(), e ); 80 } 81 ( (Factory) proxy ).setCallback( 0, instance ); 82 instance.constructed = true; 83 84 return proxy; 85 } 86 87 public static Class getProxyFactory(Class persistentClass, Class [] interfaces) 88 throws HibernateException { 89 91 try { 92 93 Enhancer en = new Enhancer(); 94 en.setUseCache( false ); 95 en.setInterceptDuringConstruction( false ); 96 en.setCallbackType( MethodInterceptor.class ); 97 en.setSuperclass( interfaces.length == 1 ? persistentClass : null ); 98 en.setInterfaces( interfaces ); 99 100 return en.createClass(); 101 102 } 103 catch (Throwable t) { 104 LogFactory.getLog( BasicLazyInitializer.class ) 105 .error( "CGLIB Enhancement failed: " + persistentClass.getName(), t ); 106 throw new HibernateException( "CGLIB Enhancement failed: " + persistentClass.getName(), t ); 107 } 108 } 109 110 private CGLIBLazyInitializer(final String entityName, final Class persistentClass, 111 final Class [] interfaces, final Serializable id, final Method getIdentifierMethod, 112 final Method setIdentifierMethod, AbstractComponentType componentIdType, 113 final SessionImplementor session) { 114 super( 115 entityName, 116 persistentClass, 117 id, 118 getIdentifierMethod, 119 setIdentifierMethod, 120 componentIdType, 121 session ); 122 this.interfaces = interfaces; 123 } 124 125 public Object intercept(final Object proxy, final Method method, final Object [] args, 126 final MethodProxy methodProxy) throws Throwable { 127 if ( constructed ) { 128 129 Object result = invoke( method, args, proxy ); 130 if ( result == INVOKE_IMPLEMENTATION ) { 131 Object target = getImplementation(); 132 final Object returnValue; 133 if ( ReflectHelper.isPublic( persistentClass, method ) ) { 134 returnValue = methodProxy.invoke( target, args ); 135 } 136 else { 137 if ( !method.isAccessible() ) method.setAccessible( true ); 138 returnValue = method.invoke( target, args ); 139 } 140 return returnValue == target ? proxy : returnValue; 141 } 142 else { 143 return result; 144 } 145 146 } 147 else { 148 149 if ( method.getName().equals( "getHibernateLazyInitializer" ) ) { 151 return this; 152 } 153 else { 154 return methodProxy.invokeSuper( proxy, args ); 155 } 156 157 } 158 } 159 160 protected Object serializableProxy() { 161 return new SerializableProxy( 162 getEntityName(), 163 persistentClass, 164 interfaces, 165 getIdentifier(), 166 getIdentifierMethod, 167 setIdentifierMethod, 168 componentIdType 169 ); 170 } 171 172 } 173 | Popular Tags |