KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > hibernate > intercept > FieldInterceptor


1 //$Id: FieldInterceptor.java,v 1.13 2005/07/12 20:12:55 oneovthafew Exp $
2
package org.hibernate.intercept;
3
4 import java.io.Serializable JavaDoc;
5 import java.util.Set JavaDoc;
6
7 import net.sf.cglib.transform.impl.InterceptFieldCallback;
8 import net.sf.cglib.transform.impl.InterceptFieldEnabled;
9
10 import org.hibernate.LazyInitializationException;
11 import org.hibernate.engine.SessionImplementor;
12 import org.hibernate.proxy.HibernateProxy;
13 import org.hibernate.proxy.LazyInitializer;
14
15 /**
16  * A field-level interceptor that initializes lazily fetched properties.
17  * This interceptor can be attached to classes instrumented by CGLIB.
18  * Note that this implementation assumes that the instance variable
19  * name is the same as the name of the persistent property that must
20  * be loaded.
21  *
22  * @author Gavin King
23  */

24 public final class FieldInterceptor implements InterceptFieldCallback, Serializable JavaDoc {
25
26     private transient SessionImplementor session;
27     private Set JavaDoc uninitializedFields;
28     private final String JavaDoc entityName;
29     private transient boolean initializing;
30     private boolean dirty;
31
32     private FieldInterceptor(SessionImplementor session, String JavaDoc entityName, Set JavaDoc uninitializedFields) {
33         this.session = session;
34         this.entityName = entityName;
35         this.uninitializedFields = uninitializedFields;
36     }
37
38     public void setSession(SessionImplementor session) {
39         this.session = session;
40     }
41
42     public boolean isInitialized() {
43         return uninitializedFields == null || uninitializedFields.size() == 0;
44     }
45
46     public boolean isInitialized(String JavaDoc field) {
47         return uninitializedFields == null || !uninitializedFields.contains( field );
48     }
49     
50     public void dirty() {
51         dirty = true;
52     }
53     
54     public boolean isDirty() {
55         return dirty;
56     }
57     
58     public void clearDirty() {
59         dirty = false;
60     }
61
62     private Object JavaDoc intercept(Object JavaDoc target, String JavaDoc fieldName, Object JavaDoc value) {
63         if ( initializing ) return value;
64
65         if ( uninitializedFields != null && uninitializedFields.contains( fieldName ) ) {
66             if ( session == null ) {
67                 throw new LazyInitializationException( "entity with lazy properties is not associated with a session" );
68             }
69             else if ( !session.isOpen() || !session.isConnected() ) {
70                 throw new LazyInitializationException( "session is not connected" );
71             }
72
73             final Object JavaDoc result;
74             initializing = true;
75             try {
76                 result = ( ( LazyPropertyInitializer ) session.getFactory()
77                         .getEntityPersister( entityName ) )
78                         .initializeLazyProperty( fieldName, target, session );
79             }
80             finally {
81                 initializing = false;
82             }
83             uninitializedFields = null; //let's assume that there is only one lazy fetch group, for now!
84
return result;
85         }
86         else {
87             return value;
88         }
89     }
90
91     public boolean readBoolean(Object JavaDoc target, String JavaDoc name, boolean oldValue) {
92         return ( ( Boolean JavaDoc ) intercept( target, name, oldValue ? Boolean.TRUE : Boolean.FALSE ) )
93                 .booleanValue();
94     }
95
96     public byte readByte(Object JavaDoc target, String JavaDoc name, byte oldValue) {
97         return ( ( Byte JavaDoc ) intercept( target, name, new Byte JavaDoc( oldValue ) ) ).byteValue();
98     }
99
100     public char readChar(Object JavaDoc target, String JavaDoc name, char oldValue) {
101         return ( ( Character JavaDoc ) intercept( target, name, new Character JavaDoc( oldValue ) ) )
102                 .charValue();
103     }
104
105     public double readDouble(Object JavaDoc target, String JavaDoc name, double oldValue) {
106         return ( ( Double JavaDoc ) intercept( target, name, new Double JavaDoc( oldValue ) ) )
107                 .doubleValue();
108     }
109
110     public float readFloat(Object JavaDoc target, String JavaDoc name, float oldValue) {
111         return ( ( Float JavaDoc ) intercept( target, name, new Float JavaDoc( oldValue ) ) )
112                 .floatValue();
113     }
114
115     public int readInt(Object JavaDoc target, String JavaDoc name, int oldValue) {
116         return ( ( Integer JavaDoc ) intercept( target, name, new Integer JavaDoc( oldValue ) ) )
117                 .intValue();
118     }
119
120     public long readLong(Object JavaDoc target, String JavaDoc name, long oldValue) {
121         return ( ( Long JavaDoc ) intercept( target, name, new Long JavaDoc( oldValue ) ) ).longValue();
122     }
123
124     public short readShort(Object JavaDoc target, String JavaDoc name, short oldValue) {
125         return ( ( Short JavaDoc ) intercept( target, name, new Short JavaDoc( oldValue ) ) )
126                 .shortValue();
127     }
128
129     public Object JavaDoc readObject(Object JavaDoc target, String JavaDoc name, Object JavaDoc oldValue) {
130         Object JavaDoc value = intercept( target, name, oldValue );
131         if (value instanceof HibernateProxy) {
132             LazyInitializer li = ( (HibernateProxy) value ).getHibernateLazyInitializer();
133             if ( li.isUnwrap() ) {
134                 value = li.getImplementation();
135             }
136         }
137         return value;
138     }
139
140     public boolean writeBoolean(Object JavaDoc target, String JavaDoc name, boolean oldValue, boolean newValue) {
141         dirty();
142         intercept( target, name, oldValue ? Boolean.TRUE : Boolean.FALSE );
143         return newValue;
144     }
145
146     public byte writeByte(Object JavaDoc target, String JavaDoc name, byte oldValue, byte newValue) {
147         dirty();
148         intercept( target, name, new Byte JavaDoc( oldValue ) );
149         return newValue;
150     }
151
152     public char writeChar(Object JavaDoc target, String JavaDoc name, char oldValue, char newValue) {
153         dirty();
154         intercept( target, name, new Character JavaDoc( oldValue ) );
155         return newValue;
156     }
157
158     public double writeDouble(Object JavaDoc target, String JavaDoc name, double oldValue, double newValue) {
159         dirty();
160         intercept( target, name, new Double JavaDoc( oldValue ) );
161         return newValue;
162     }
163
164     public float writeFloat(Object JavaDoc target, String JavaDoc name, float oldValue, float newValue) {
165         dirty();
166         intercept( target, name, new Float JavaDoc( oldValue ) );
167         return newValue;
168     }
169
170     public int writeInt(Object JavaDoc target, String JavaDoc name, int oldValue, int newValue) {
171         dirty();
172         intercept( target, name, new Integer JavaDoc( oldValue ) );
173         return newValue;
174     }
175
176     public long writeLong(Object JavaDoc target, String JavaDoc name, long oldValue, long newValue) {
177         dirty();
178         intercept( target, name, new Long JavaDoc( oldValue ) );
179         return newValue;
180     }
181
182     public short writeShort(Object JavaDoc target, String JavaDoc name, short oldValue, short newValue) {
183         dirty();
184         intercept( target, name, new Short JavaDoc( oldValue ) );
185         return newValue;
186     }
187
188     public Object JavaDoc writeObject(Object JavaDoc target, String JavaDoc name, Object JavaDoc oldValue, Object JavaDoc newValue) {
189         dirty();
190         intercept( target, name, oldValue );
191         return newValue;
192     }
193
194     public String JavaDoc toString() {
195         return "FieldInterceptor(" +
196             "entityName=" + entityName +
197             ",dirty=" + dirty +
198             ",uninitializedFields=" + uninitializedFields +
199             ')';
200     }
201
202     public static void clearDirty(Object JavaDoc entity) {
203         if ( hasInterceptor( entity ) ) {
204             getFieldInterceptor(entity).clearDirty();
205         }
206     }
207
208     public static boolean hasInterceptor(Object JavaDoc entity) {
209         return ( entity instanceof InterceptFieldEnabled ) &&
210             ( (InterceptFieldEnabled) entity ).getInterceptFieldCallback() != null;
211     }
212
213     public static FieldInterceptor getFieldInterceptor(Object JavaDoc entity) {
214         return (FieldInterceptor) ( (InterceptFieldEnabled) entity ).getInterceptFieldCallback();
215     }
216
217     public static FieldInterceptor initFieldInterceptor(Object JavaDoc entity, String JavaDoc entityName, SessionImplementor session, Set JavaDoc lazyProps) {
218         FieldInterceptor fieldInterceptor = new FieldInterceptor( session, entityName, lazyProps );
219         ( ( InterceptFieldEnabled ) entity ).setInterceptFieldCallback(fieldInterceptor);
220         return fieldInterceptor;
221     }
222 }
Popular Tags