KickJava   Java API By Example, From Geeks To Geeks.

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


1 //$Id: BasicLazyInitializer.java,v 1.11 2005/02/16 12:50:18 oneovthafew Exp $
2
package org.hibernate.proxy;
3
4 import java.io.Serializable JavaDoc;
5 import java.lang.reflect.Method JavaDoc;
6
7 import org.hibernate.engine.EntityKey;
8 import org.hibernate.engine.SessionImplementor;
9 import org.hibernate.type.AbstractComponentType;
10 import org.hibernate.util.MarkerObject;
11 import org.hibernate.util.ReflectHelper;
12
13 /**
14  * Lazy initializer for POJOs
15  * @author Gavin King
16  */

17 public abstract class BasicLazyInitializer extends AbstractLazyInitializer {
18
19     protected static final Object JavaDoc INVOKE_IMPLEMENTATION = new MarkerObject("INVOKE_IMPLEMENTATION");
20
21     protected Class JavaDoc persistentClass;
22     protected Method JavaDoc getIdentifierMethod;
23     protected Method JavaDoc setIdentifierMethod;
24     protected boolean overridesEquals;
25     private Object JavaDoc replacement;
26     protected AbstractComponentType componentIdType;
27
28     protected BasicLazyInitializer(
29             String JavaDoc entityName,
30             Class JavaDoc persistentClass,
31             Serializable JavaDoc id,
32             Method JavaDoc getIdentifierMethod,
33             Method JavaDoc setIdentifierMethod,
34             AbstractComponentType componentIdType,
35             SessionImplementor session) {
36         super(entityName, id, session);
37         this.persistentClass = persistentClass;
38         this.getIdentifierMethod = getIdentifierMethod;
39         this.setIdentifierMethod = setIdentifierMethod;
40         this.componentIdType = componentIdType;
41         overridesEquals = ReflectHelper.overridesEquals(persistentClass);
42     }
43
44     protected abstract Object JavaDoc serializableProxy();
45
46     protected final Object JavaDoc invoke(Method JavaDoc method, Object JavaDoc[] args, Object JavaDoc proxy) throws Throwable JavaDoc {
47
48         String JavaDoc methodName = method.getName();
49         int params = method.getParameterTypes().length;
50
51         if ( params==0 ) {
52
53             if ( "writeReplace".equals(methodName) ) {
54                 
55                 final Object JavaDoc target = getTarget();
56                 final SessionImplementor session = getSession();
57                 if ( target==null && session!=null ) {
58                     final EntityKey key = new EntityKey(
59                         getIdentifier(),
60                         session.getFactory().getEntityPersister( getEntityName() ),
61                         session.getEntityMode()
62                     );
63                     setTarget( session.getPersistenceContext().getEntity(key) );
64                 }
65                 if (target==null) {
66                     if (replacement==null) replacement = serializableProxy();
67                     return replacement;
68                 }
69                 else {
70                     return target;
71                 }
72                 /*if (replacement==null) replacement = serializableProxy();
73                 replacement.setSnapshot(snapshot);
74                 replacement.setTarget( (Serializable) target );
75                 return replacement;*/

76
77             }
78             else if ( !overridesEquals && "hashCode".equals(methodName) ) {
79                 return new Integer JavaDoc( System.identityHashCode(proxy) );
80             }
81             else if ( isUninitialized() && method.equals(getIdentifierMethod) ) {
82                 return getIdentifier();
83             }
84             else if ( "finalize".equals(methodName) ) {
85                 return null;
86             }
87             else if ( "getHibernateLazyInitializer".equals(methodName) ) {
88                 return this;
89             }
90
91         }
92         else if ( params==1 ) {
93
94             if ( !overridesEquals && "equals".equals(methodName) ) {
95                 return args[0]==proxy ? Boolean.TRUE : Boolean.FALSE;
96             }
97             else if ( method.equals(setIdentifierMethod) ) {
98                 initialize();
99                 setIdentifier( (Serializable JavaDoc) args[0] );
100                 return INVOKE_IMPLEMENTATION;
101             }
102
103         }
104         
105         //if it is a property of an embedded component, invoke on the "identifier"
106
if ( componentIdType!=null && componentIdType.isMethodOf(method) ) {
107             return method.invoke( getIdentifier(), args );
108         }
109         
110         // otherwise:
111
return INVOKE_IMPLEMENTATION;
112
113     }
114
115     public final Class JavaDoc getPersistentClass() {
116         return persistentClass;
117     }
118
119 }
120
Popular Tags