KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > hibernate > proxy > CGLIBLazyInitializer


1 // $Id: CGLIBLazyInitializer.java,v 1.12 2005/06/05 05:24:44 oneovthafew Exp $
2
package org.hibernate.proxy;
3
4 import java.io.Serializable JavaDoc;
5 import java.lang.reflect.Method JavaDoc;
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 /**
19  * A <tt>LazyInitializer</tt> implemented using the CGLIB bytecode generation library
20  */

21 public final class CGLIBLazyInitializer extends BasicLazyInitializer implements MethodInterceptor {
22
23     private Class JavaDoc[] interfaces;
24     private boolean constructed = false;
25
26     static HibernateProxy getProxy(final String JavaDoc entityName, final Class JavaDoc persistentClass,
27             final Class JavaDoc[] interfaces, final Method JavaDoc getIdentifierMethod,
28             final Method JavaDoc setIdentifierMethod, AbstractComponentType componentIdType,
29             final Serializable JavaDoc id, final SessionImplementor session) throws HibernateException {
30         // note: interfaces is assumed to already contain HibernateProxy.class
31

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 JavaDoc 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 JavaDoc 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 JavaDoc factory, final String JavaDoc entityName,
58             final Class JavaDoc persistentClass, final Class JavaDoc[] interfaces,
59             final Method JavaDoc getIdentifierMethod, final Method JavaDoc setIdentifierMethod,
60             AbstractComponentType componentIdType, final Serializable JavaDoc 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 JavaDoc 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 JavaDoc getProxyFactory(Class JavaDoc persistentClass, Class JavaDoc[] interfaces)
88             throws HibernateException {
89         // note: interfaces is assumed to already contain HibernateProxy.class
90

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 JavaDoc 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 JavaDoc entityName, final Class JavaDoc persistentClass,
111             final Class JavaDoc[] interfaces, final Serializable JavaDoc id, final Method JavaDoc getIdentifierMethod,
112             final Method JavaDoc 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 JavaDoc intercept(final Object JavaDoc proxy, final Method JavaDoc method, final Object JavaDoc[] args,
126             final MethodProxy methodProxy) throws Throwable JavaDoc {
127         if ( constructed ) {
128             
129             Object JavaDoc result = invoke( method, args, proxy );
130             if ( result == INVOKE_IMPLEMENTATION ) {
131                 Object JavaDoc target = getImplementation();
132                 final Object JavaDoc 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             // while constructor is running
150
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 JavaDoc 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