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.msg;
006:
007: import com.tc.bytes.TCByteBuffer;
008: import com.tc.io.TCByteBufferOutput;
009: import com.tc.net.protocol.tcm.MessageChannel;
010: import com.tc.net.protocol.tcm.MessageMonitor;
011: import com.tc.net.protocol.tcm.TCMessageHeader;
012: import com.tc.net.protocol.tcm.TCMessageType;
013: import com.tc.object.lockmanager.api.LockID;
014: import com.tc.object.lockmanager.api.LockLevel;
015: import com.tc.object.lockmanager.api.ThreadID;
016: import com.tc.object.lockmanager.impl.GlobalLockInfo;
017: import com.tc.object.session.SessionID;
018:
019: import java.io.IOException;
020:
021: public class LockResponseMessage extends DSOMessageBase {
022: private static final byte TYPE = 1;
023: private static final byte THREAD_ID = 2;
024: private static final byte LOCK_ID = 3;
025: private static final byte LOCK_LEVEL = 7;
026: private static final byte GLOBAL_LOCK_INFO = 8;
027: private static final byte LOCK_STACK_TRACE_DEPTH = 9;
028: private static final byte LOCK_STAT_COLLECT_FREQUENCY = 10;
029:
030: public static final int LOCK_AWARD = 1;
031: public static final int LOCK_RECALL = 2;
032: public static final int LOCK_WAIT_TIMEOUT = 3;
033: public static final int LOCK_INFO = 4;
034: public static final int LOCK_NOT_AWARDED = 5;
035: public static final int LOCK_STAT_ENABLED = 6;
036: public static final int LOCK_STAT_DISABLED = 7;
037:
038: private int type;
039: private ThreadID threadID;
040: private LockID lockID;
041: private int lockLevel;
042: private GlobalLockInfo globalLockInfo;
043: private int stackTraceDepth;
044: private int statCollectFrequency;
045:
046: public LockResponseMessage(SessionID sessionID,
047: MessageMonitor monitor, TCByteBufferOutput out,
048: MessageChannel channel, TCMessageType type) {
049: super (sessionID, monitor, out, channel, type);
050: }
051:
052: public LockResponseMessage(SessionID sessionID,
053: MessageMonitor monitor, MessageChannel channel,
054: TCMessageHeader header, TCByteBuffer[] data) {
055: super (sessionID, monitor, channel, header, data);
056: }
057:
058: protected void dehydrateValues() {
059: putNVPair(TYPE, this .type);
060: putNVPair(LOCK_ID, this .lockID.asString());
061: putNVPair(THREAD_ID, this .threadID.toLong());
062: putNVPair(LOCK_LEVEL, this .lockLevel);
063: if (globalLockInfo != null) {
064: putNVPair(GLOBAL_LOCK_INFO, globalLockInfo);
065: }
066: if (isLockStatEnabled()) {
067: putNVPair(LOCK_STACK_TRACE_DEPTH, this .stackTraceDepth);
068: putNVPair(LOCK_STAT_COLLECT_FREQUENCY,
069: this .statCollectFrequency);
070: }
071: }
072:
073: protected String describePayload() {
074: StringBuffer rv = new StringBuffer();
075: rv.append("Type : ");
076:
077: if (isLockAward()) {
078: rv.append("LOCK AWARD \n");
079: } else if (isLockRecall()) {
080: rv.append("LOCK RECALL \n");
081: } else if (isLockWaitTimeout()) {
082: rv.append("LOCK WAIT TIMEOUT \n");
083: } else if (isLockInfo()) {
084: rv.append("LOCK INFO");
085: } else if (isLockNotAwarded()) {
086: rv.append("LOCK NOT AWARDED");
087: } else if (isLockStatEnabled()) {
088: rv.append("ENABLE LOCK STATISTICS");
089: } else if (isLockStatDisabled()) {
090: rv.append("DISABLE LOCK STATISTICS");
091: } else {
092: rv.append("UNKNOWN \n");
093: }
094:
095: rv.append(lockID).append(' ').append(threadID).append(' ')
096: .append("Lock Type: ").append(
097: LockLevel.toString(lockLevel)).append('\n');
098:
099: return rv.toString();
100: }
101:
102: protected boolean hydrateValue(byte name) throws IOException {
103: switch (name) {
104: case TYPE:
105: this .type = getIntValue();
106: return true;
107: case THREAD_ID:
108: // TODO: Make this use a transactionID factory so that we can avoid dups
109: this .threadID = new ThreadID(getLongValue());
110: return true;
111: case LOCK_ID:
112: // TODO: Make this use a lockID factory so that we can avoid dups
113: lockID = new LockID(getStringValue());
114: return true;
115: case LOCK_LEVEL:
116: this .lockLevel = getIntValue();
117: return true;
118: case GLOBAL_LOCK_INFO:
119: globalLockInfo = new GlobalLockInfo();
120: getObject(globalLockInfo);
121: return true;
122: case LOCK_STACK_TRACE_DEPTH:
123: this .stackTraceDepth = getIntValue();
124: return true;
125: case LOCK_STAT_COLLECT_FREQUENCY:
126: this .statCollectFrequency = getIntValue();
127: return true;
128: default:
129: return false;
130: }
131: }
132:
133: public boolean isLockAward() {
134: return (this .type == LOCK_AWARD);
135: }
136:
137: public boolean isLockRecall() {
138: return (this .type == LOCK_RECALL);
139: }
140:
141: public boolean isLockWaitTimeout() {
142: return (this .type == LOCK_WAIT_TIMEOUT);
143: }
144:
145: public boolean isLockInfo() {
146: return (this .type == LOCK_INFO);
147: }
148:
149: public boolean isLockNotAwarded() {
150: return (this .type == LOCK_NOT_AWARDED);
151: }
152:
153: public boolean isLockStatEnabled() {
154: return (this .type == LOCK_STAT_ENABLED);
155: }
156:
157: public boolean isLockStatDisabled() {
158: return (this .type == LOCK_STAT_DISABLED);
159: }
160:
161: public LockID getLockID() {
162: return this .lockID;
163: }
164:
165: public ThreadID getThreadID() {
166: return this .threadID;
167: }
168:
169: public int getLockLevel() {
170: return this .lockLevel;
171: }
172:
173: public int getStackTraceDepth() {
174: return stackTraceDepth;
175: }
176:
177: public int getStatCollectFrequency() {
178: return statCollectFrequency;
179: }
180:
181: public GlobalLockInfo getGlobalLockInfo() {
182: return globalLockInfo;
183: }
184:
185: public void initializeLockAward(LockID lid, ThreadID sid, int level) {
186: this .type = LOCK_AWARD;
187: initialize(lid, sid, level, null);
188: }
189:
190: public void initializeLockNotAwarded(LockID lid, ThreadID sid,
191: int level) {
192: this .type = LOCK_NOT_AWARDED;
193: initialize(lid, sid, level);
194: }
195:
196: public void initializeLockRecall(LockID lid, ThreadID sid, int level) {
197: this .type = LOCK_RECALL;
198: initialize(lid, sid, level);
199: }
200:
201: public void initializeLockWaitTimeout(LockID lid, ThreadID sid,
202: int level) {
203: this .type = LOCK_WAIT_TIMEOUT;
204: initialize(lid, sid, level);
205: }
206:
207: public void initializeLockInfo(LockID lid, ThreadID sid, int level,
208: GlobalLockInfo info) {
209: this .type = LOCK_INFO;
210: initialize(lid, sid, level, info);
211: }
212:
213: public void initializeLockStatEnable(LockID lid, ThreadID sid,
214: int level, int stackTraceDepth, int statCollectFrequency) {
215: this .type = LOCK_STAT_ENABLED;
216: initialize(lid, sid, level);
217: this .stackTraceDepth = stackTraceDepth;
218: this .statCollectFrequency = statCollectFrequency;
219: }
220:
221: public void initializeLockStatDisable(LockID lid, ThreadID sid,
222: int level) {
223: this .type = LOCK_STAT_DISABLED;
224: initialize(lid, sid, level);
225: }
226:
227: private void initialize(LockID lid, ThreadID sid, int level) {
228: initialize(lid, sid, level, null);
229: }
230:
231: private void initialize(LockID lid, ThreadID sid, int level,
232: GlobalLockInfo info) {
233: this.threadID = sid;
234: this.lockID = lid;
235: this.lockLevel = level;
236: this.globalLockInfo = info;
237: }
238:
239: }
|