1 21 package oracle.toplink.essentials.internal.indirection; 23 24 import java.util.*; 25 import java.rmi.server.ObjID ; 26 import oracle.toplink.essentials.exceptions.ValidationException; 27 import oracle.toplink.essentials.indirection.*; 28 import oracle.toplink.essentials.mappings.*; 29 import oracle.toplink.essentials.logging.SessionLog; 30 import oracle.toplink.essentials.internal.sessions.UnitOfWorkImpl; 31 import oracle.toplink.essentials.descriptors.ClassDescriptor; 32 33 42 public abstract class UnitOfWorkValueHolder extends DatabaseValueHolder { 43 44 45 protected transient ValueHolderInterface wrappedValueHolder; 46 47 48 protected transient DatabaseMapping mapping; 49 50 51 protected ValueHolder backupValueHolder; 52 53 57 protected UnitOfWorkImpl remoteUnitOfWork; 58 protected Object sourceObject; 59 60 63 protected transient Object relationshipSourceObject; 64 protected String sourceAttributeName; 65 protected ObjID wrappedValueHolderRemoteID; 66 67 protected UnitOfWorkValueHolder(ValueHolderInterface attributeValue, Object clone, DatabaseMapping mapping, UnitOfWorkImpl unitOfWork) { 68 this.wrappedValueHolder = attributeValue; 69 this.mapping = mapping; 70 this.session = unitOfWork; 71 this.sourceAttributeName = mapping.getAttributeName(); 72 this.relationshipSourceObject = clone; 73 } 74 75 78 protected abstract Object buildBackupCloneFor(Object cloneAttributeValue); 79 80 83 public abstract Object buildCloneFor(Object originalAttributeValue); 84 85 protected ValueHolder getBackupValueHolder() { 86 return backupValueHolder; 87 } 88 89 protected DatabaseMapping getMapping() { 90 return mapping; 91 } 92 93 protected UnitOfWorkImpl getRemoteUnitOfWork() { 94 return remoteUnitOfWork; 95 } 96 97 protected String getSourceAttributeName() { 98 return sourceAttributeName; 99 } 100 101 protected Object getSourceObject() { 102 return sourceObject; 103 } 104 105 protected Object getRelationshipSourceObject() { 106 return this.relationshipSourceObject; 107 } 108 109 protected UnitOfWorkImpl getUnitOfWork() { 110 return (UnitOfWorkImpl)getSession(); 111 } 112 113 119 protected Object getValueFromServerObject() { 120 setSession(getRemoteUnitOfWork()); 121 Vector primaryKey = getSession().keyFromObject(getSourceObject()); 122 Object originalObject = getUnitOfWork().getParent().getIdentityMapAccessor().getFromIdentityMap(primaryKey, getSourceObject().getClass()); 123 if (originalObject == null) { 124 originalObject = getUnitOfWork().getParent().readObject(getSourceObject()); 125 } 126 ClassDescriptor descriptor = getSession().getDescriptor(originalObject); 127 DatabaseMapping mapping = descriptor.getObjectBuilder().getMappingForAttributeName(getSourceAttributeName()); 128 setMapping(mapping); 129 return getMapping().getRealAttributeValueFromObject(originalObject, getSession()); 130 } 131 132 137 protected Object instantiateImpl() { 138 if (getWrappedValueHolder() instanceof DatabaseValueHolder) { 139 synchronized(getWrappedValueHolder()){ 142 DatabaseValueHolder wrapped = (DatabaseValueHolder)getWrappedValueHolder(); 143 UnitOfWorkImpl unitOfWork = getUnitOfWork(); 144 if (!wrapped.isEasilyInstantiated()) { 145 if (wrapped.isPessimisticLockingValueHolder()) { 146 if (!unitOfWork.getCommitManager().isActive() && !unitOfWork.wasTransactionBegunPrematurely()) { 147 unitOfWork.beginEarlyTransaction(); 148 } 149 unitOfWork.log(SessionLog.FINEST, SessionLog.TRANSACTION, "instantiate_pl_relationship"); 150 } 151 if (unitOfWork.getCommitManager().isActive() || unitOfWork.wasTransactionBegunPrematurely()) { 152 return wrapped.instantiateForUnitOfWorkValueHolder(this); 156 } 157 } 158 } 159 } 160 Object originalAttributeValue = getWrappedValueHolder().getValue(); 161 return buildCloneFor(originalAttributeValue); 162 } 163 164 169 public boolean isEasilyInstantiated() { 170 return isInstantiated() || ((getWrappedValueHolder() != null) && (getWrappedValueHolder() instanceof DatabaseValueHolder) && ((DatabaseValueHolder)getWrappedValueHolder()).isEasilyInstantiated()); 171 } 172 173 181 public boolean isPessimisticLockingValueHolder() { 182 return ((getWrappedValueHolder() != null) && (getWrappedValueHolder() instanceof DatabaseValueHolder) && ((DatabaseValueHolder)getWrappedValueHolder()).isPessimisticLockingValueHolder()); 185 } 186 187 public ValueHolderInterface getWrappedValueHolder() { 188 return wrappedValueHolder; 189 } 190 191 194 public ObjID getWrappedValueHolderRemoteID() { 195 return this.wrappedValueHolderRemoteID; 196 } 197 198 202 public boolean isSerializedRemoteUnitOfWorkValueHolder() { 203 return (getRemoteUnitOfWork() != null) && (getRemoteUnitOfWork().getParent() != null) && (getWrappedValueHolder() == null); 204 } 205 206 210 protected Object instantiate() { 211 UnitOfWorkImpl unitOfWork; 212 if (isSerializedRemoteUnitOfWorkValueHolder()) { 213 unitOfWork = getRemoteUnitOfWork(); 214 } else { 215 unitOfWork = getUnitOfWork(); 216 } 217 if (unitOfWork == null){ 218 throw ValidationException.instantiatingValueholderWithNullSession(); 219 } 220 if (unitOfWork.isAfterWriteChangesButBeforeCommit()) { 221 throw ValidationException.illegalOperationForUnitOfWorkLifecycle(unitOfWork.getLifecycle(), "ValueHolder.instantiate()"); 222 } 223 224 Object originalAttributeValue; 225 Object cloneAttributeValue; 226 227 if (isSerializedRemoteUnitOfWorkValueHolder()) { 229 originalAttributeValue = getValueFromServerObject(); 230 cloneAttributeValue = buildCloneFor(originalAttributeValue); 231 } else { 232 cloneAttributeValue = instantiateImpl(); 233 } 234 235 if (getBackupValueHolder() != null) { 238 getBackupValueHolder().setValue(buildBackupCloneFor(cloneAttributeValue)); 239 } 240 return cloneAttributeValue; 241 } 242 243 253 public Object instantiateForUnitOfWorkValueHolder(UnitOfWorkValueHolder unitOfWorkValueHolder) { 254 return instantiate(); 257 } 258 259 268 public void releaseWrappedValueHolder() { 269 return; 271 } 272 273 276 protected void resetFields() { 277 } 279 280 public void setBackupValueHolder(ValueHolder backupValueHolder) { 281 this.backupValueHolder = backupValueHolder; 282 } 283 284 protected void setMapping(DatabaseMapping mapping) { 285 this.mapping = mapping; 286 } 287 288 protected void setRemoteUnitOfWork(UnitOfWorkImpl remoteUnitOfWork) { 289 this.remoteUnitOfWork = remoteUnitOfWork; 290 } 291 292 protected void setSourceAttributeName(String name) { 293 sourceAttributeName = name; 294 } 295 296 protected void setSourceObject(Object sourceObject) { 297 this.sourceObject = sourceObject; 298 } 299 300 protected void setRelationshipSourceObject(Object relationshipSourceObject) { 301 this.relationshipSourceObject = relationshipSourceObject; 302 } 303 304 protected void setWrappedValueHolder(DatabaseValueHolder valueHolder) { 305 wrappedValueHolder = valueHolder; 306 } 307 } 308 | Popular Tags |