001: /*
002: * All content copyright (c) 2003-2006 Terracotta, Inc., except as may otherwise be noted in a separate copyright
003: * notice. All rights reserved.
004: */
005: package com.tc.object.tx;
006:
007: import com.tc.object.ObjectID;
008: import com.tc.object.TCObject;
009: import com.tc.object.lockmanager.api.LockID;
010: import com.tc.util.SequenceID;
011:
012: /**
013: * Base class for client transaction implementations
014: */
015: abstract class AbstractClientTransaction implements ClientTransaction {
016:
017: private SequenceID seqID = SequenceID.NULL_ID;
018: private final TransactionID txID;
019: private TransactionContext transactionContext;
020: private boolean alreadyCommittedFlag;
021:
022: AbstractClientTransaction(TransactionID txID) {
023: this .txID = txID;
024: }
025:
026: public synchronized void setSequenceID(SequenceID sequenceID) {
027: if (!seqID.isNull())
028: throw new AssertionError(
029: "Attempt to set sequence id more than once.");
030: if (sequenceID == null || sequenceID.isNull())
031: throw new AssertionError(
032: "Attempt to set sequence id to null: " + sequenceID);
033: this .seqID = sequenceID;
034: }
035:
036: public synchronized SequenceID getSequenceID() {
037: return this .seqID;
038: }
039:
040: public void setTransactionContext(
041: TransactionContext transactionContext) {
042: this .transactionContext = transactionContext;
043: }
044:
045: public TxnType getTransactionType() {
046: return transactionContext.getType();
047: }
048:
049: public LockID[] getAllLockIDs() {
050: return transactionContext.getAllLockIDs();
051: }
052:
053: public TransactionID getTransactionID() {
054: return txID;
055: }
056:
057: public LockID getLockID() {
058: return transactionContext.getLockID();
059: }
060:
061: public final void createObject(TCObject source) {
062: if (transactionContext.getType() == TxnType.READ_ONLY) {
063: throw new AssertionError(source.getClass().getName()
064: + " was already checked to have write access!");
065: }
066: alreadyCommittedCheck();
067: basicCreate(source);
068: }
069:
070: public final void createRoot(String name, ObjectID rootID) {
071: if (transactionContext.getType() == TxnType.READ_ONLY) {
072: throw new AssertionError(name
073: + " was already checked to have write access!");
074: }
075: alreadyCommittedCheck();
076: basicCreateRoot(name, rootID);
077: }
078:
079: public final void fieldChanged(TCObject source, String classname,
080: String fieldname, Object newValue, int index) {
081: assertNotReadOnlyTxn();
082: if (source.getTCClass().isEnum()) {
083: throw new AssertionError("fieldChanged() on an enum type "
084: + source.getTCClass().getPeerClass().getName());
085: }
086:
087: alreadyCommittedCheck();
088: basicFieldChanged(source, classname, fieldname, newValue, index);
089: }
090:
091: public final void literalValueChanged(TCObject source,
092: Object newValue, Object oldValue) {
093: assertNotReadOnlyTxn();
094: alreadyCommittedCheck();
095: basicLiteralValueChanged(source, newValue, oldValue);
096: }
097:
098: public final void arrayChanged(TCObject source, int startPos,
099: Object array, int length) {
100: assertNotReadOnlyTxn();
101: alreadyCommittedCheck();
102: basicArrayChanged(source, startPos, array, length);
103: }
104:
105: public final void logicalInvoke(TCObject source, int method,
106: Object[] parameters, String methodName) {
107: assertNotReadOnlyTxn();
108: alreadyCommittedCheck();
109: basicLogicalInvoke(source, method, parameters);
110: }
111:
112: public boolean isNull() {
113: return false;
114: }
115:
116: protected void alreadyCommittedCheck() {
117: if (alreadyCommittedFlag) {
118: throw new AssertionError("Transaction " + txID
119: + " already commited.");
120: }
121: }
122:
123: private void assertNotReadOnlyTxn() {
124: if (transactionContext.getType() == TxnType.READ_ONLY) {
125: throw new AssertionError("fail to perform read only check");
126: }
127: }
128:
129: public void setAlreadyCommitted() {
130: alreadyCommittedCheck();
131: this .alreadyCommittedFlag = true;
132: }
133:
134: abstract protected void basicCreate(TCObject object);
135:
136: abstract protected void basicCreateRoot(String name, ObjectID rootID);
137:
138: abstract protected void basicFieldChanged(TCObject source,
139: String classname, String fieldname, Object newValue,
140: int index);
141:
142: abstract protected void basicLiteralValueChanged(TCObject source,
143: Object newValue, Object oldValue);
144:
145: abstract protected void basicArrayChanged(TCObject source,
146: int startPos, Object array, int length);
147:
148: abstract protected void basicLogicalInvoke(TCObject source,
149: int method, Object[] parameters);
150:
151: }
|