1 package org.hibernate.intercept; 3 4 import java.io.Serializable ; 5 import java.util.Set ; 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 24 public final class FieldInterceptor implements InterceptFieldCallback, Serializable { 25 26 private transient SessionImplementor session; 27 private Set uninitializedFields; 28 private final String entityName; 29 private transient boolean initializing; 30 private boolean dirty; 31 32 private FieldInterceptor(SessionImplementor session, String entityName, Set 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 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 intercept(Object target, String fieldName, Object 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 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; return result; 85 } 86 else { 87 return value; 88 } 89 } 90 91 public boolean readBoolean(Object target, String name, boolean oldValue) { 92 return ( ( Boolean ) intercept( target, name, oldValue ? Boolean.TRUE : Boolean.FALSE ) ) 93 .booleanValue(); 94 } 95 96 public byte readByte(Object target, String name, byte oldValue) { 97 return ( ( Byte ) intercept( target, name, new Byte ( oldValue ) ) ).byteValue(); 98 } 99 100 public char readChar(Object target, String name, char oldValue) { 101 return ( ( Character ) intercept( target, name, new Character ( oldValue ) ) ) 102 .charValue(); 103 } 104 105 public double readDouble(Object target, String name, double oldValue) { 106 return ( ( Double ) intercept( target, name, new Double ( oldValue ) ) ) 107 .doubleValue(); 108 } 109 110 public float readFloat(Object target, String name, float oldValue) { 111 return ( ( Float ) intercept( target, name, new Float ( oldValue ) ) ) 112 .floatValue(); 113 } 114 115 public int readInt(Object target, String name, int oldValue) { 116 return ( ( Integer ) intercept( target, name, new Integer ( oldValue ) ) ) 117 .intValue(); 118 } 119 120 public long readLong(Object target, String name, long oldValue) { 121 return ( ( Long ) intercept( target, name, new Long ( oldValue ) ) ).longValue(); 122 } 123 124 public short readShort(Object target, String name, short oldValue) { 125 return ( ( Short ) intercept( target, name, new Short ( oldValue ) ) ) 126 .shortValue(); 127 } 128 129 public Object readObject(Object target, String name, Object oldValue) { 130 Object 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 target, String 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 target, String name, byte oldValue, byte newValue) { 147 dirty(); 148 intercept( target, name, new Byte ( oldValue ) ); 149 return newValue; 150 } 151 152 public char writeChar(Object target, String name, char oldValue, char newValue) { 153 dirty(); 154 intercept( target, name, new Character ( oldValue ) ); 155 return newValue; 156 } 157 158 public double writeDouble(Object target, String name, double oldValue, double newValue) { 159 dirty(); 160 intercept( target, name, new Double ( oldValue ) ); 161 return newValue; 162 } 163 164 public float writeFloat(Object target, String name, float oldValue, float newValue) { 165 dirty(); 166 intercept( target, name, new Float ( oldValue ) ); 167 return newValue; 168 } 169 170 public int writeInt(Object target, String name, int oldValue, int newValue) { 171 dirty(); 172 intercept( target, name, new Integer ( oldValue ) ); 173 return newValue; 174 } 175 176 public long writeLong(Object target, String name, long oldValue, long newValue) { 177 dirty(); 178 intercept( target, name, new Long ( oldValue ) ); 179 return newValue; 180 } 181 182 public short writeShort(Object target, String name, short oldValue, short newValue) { 183 dirty(); 184 intercept( target, name, new Short ( oldValue ) ); 185 return newValue; 186 } 187 188 public Object writeObject(Object target, String name, Object oldValue, Object newValue) { 189 dirty(); 190 intercept( target, name, oldValue ); 191 return newValue; 192 } 193 194 public String toString() { 195 return "FieldInterceptor(" + 196 "entityName=" + entityName + 197 ",dirty=" + dirty + 198 ",uninitializedFields=" + uninitializedFields + 199 ')'; 200 } 201 202 public static void clearDirty(Object entity) { 203 if ( hasInterceptor( entity ) ) { 204 getFieldInterceptor(entity).clearDirty(); 205 } 206 } 207 208 public static boolean hasInterceptor(Object entity) { 209 return ( entity instanceof InterceptFieldEnabled ) && 210 ( (InterceptFieldEnabled) entity ).getInterceptFieldCallback() != null; 211 } 212 213 public static FieldInterceptor getFieldInterceptor(Object entity) { 214 return (FieldInterceptor) ( (InterceptFieldEnabled) entity ).getInterceptFieldCallback(); 215 } 216 217 public static FieldInterceptor initFieldInterceptor(Object entity, String entityName, SessionImplementor session, Set lazyProps) { 218 FieldInterceptor fieldInterceptor = new FieldInterceptor( session, entityName, lazyProps ); 219 ( ( InterceptFieldEnabled ) entity ).setInterceptFieldCallback(fieldInterceptor); 220 return fieldInterceptor; 221 } 222 } | Popular Tags |