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.objectserver.context;
006:
007: import com.tc.async.api.EventContext;
008: import com.tc.net.groups.NodeID;
009: import com.tc.object.lockmanager.api.LockID;
010: import com.tc.object.lockmanager.api.LockLevel;
011: import com.tc.object.lockmanager.api.ThreadID;
012: import com.tc.object.lockmanager.impl.GlobalLockInfo;
013: import com.tc.object.lockmanager.impl.GlobalLockStateInfo;
014: import com.tc.objectserver.lockmanager.api.LockWaitContext;
015: import com.tc.objectserver.lockmanager.impl.Holder;
016: import com.tc.util.Assert;
017:
018: import java.util.ArrayList;
019: import java.util.Collection;
020: import java.util.Iterator;
021:
022: public class LockResponseContext implements EventContext {
023:
024: public static final int LOCK_AWARD = 1;
025: public static final int LOCK_RECALL = 2;
026: public static final int LOCK_WAIT_TIMEOUT = 3;
027: public static final int LOCK_INFO = 4;
028: public static final int LOCK_NOT_AWARDED = 5;
029: public static final int LOCK_STAT_ENABLED = 6;
030: public static final int LOCK_STAT_DISABLED = 7;
031:
032: private final LockID lockID;
033: private final ThreadID threadID;
034: private final int level;
035: private final NodeID nodeID;
036: private final int responseType;
037: private final int stackTraceDepth;
038: private final int statCollectFrequency;
039: private final GlobalLockInfo globalLockInfo;
040:
041: public LockResponseContext(LockID lockID, NodeID nodeID,
042: ThreadID threadID, int level, int lockRequestQueueLength,
043: Collection greedyHolders, Collection holders,
044: Collection waiters, int type) {
045: this (lockID, nodeID, threadID, level, new GlobalLockInfo(
046: lockID, level, lockRequestQueueLength,
047: getGlobalLockHolderInfo(greedyHolders),
048: getGlobalLockHolderInfo(holders),
049: getGlobalLockWaiterInfo(lockID, waiters)), type, -1, -1);
050: }
051:
052: public LockResponseContext(LockID lockID, NodeID nodeID,
053: ThreadID sourceID, int level, int type) {
054: this (lockID, nodeID, sourceID, level, null, type, -1, -1);
055: }
056:
057: public LockResponseContext(LockID lockID, NodeID nodeID,
058: ThreadID sourceID, int level, int type,
059: int stackTraceDepth, int statCollectFrequency) {
060: this (lockID, nodeID, sourceID, level, null, type,
061: stackTraceDepth, statCollectFrequency);
062: }
063:
064: private LockResponseContext(LockID lockID, NodeID nodeID,
065: ThreadID sourceID, int level,
066: GlobalLockInfo globalLockInfo, int type,
067: int stackTraceDepth, int statCollectFrequency) {
068: this .lockID = lockID;
069: this .nodeID = nodeID;
070: this .threadID = sourceID;
071: this .level = level;
072: this .responseType = type;
073: this .globalLockInfo = globalLockInfo;
074: this .stackTraceDepth = stackTraceDepth;
075: this .statCollectFrequency = statCollectFrequency;
076: Assert.assertTrue(responseType == LOCK_AWARD
077: || responseType == LOCK_RECALL
078: || responseType == LOCK_WAIT_TIMEOUT
079: || responseType == LOCK_INFO
080: || responseType == LOCK_NOT_AWARDED
081: || responseType == LOCK_STAT_ENABLED
082: || responseType == LOCK_STAT_DISABLED);
083: }
084:
085: public NodeID getNodeID() {
086: return nodeID;
087: }
088:
089: public int getType() {
090: return level;
091: }
092:
093: public LockID getLockID() {
094: return lockID;
095: }
096:
097: public ThreadID getThreadID() {
098: return threadID;
099: }
100:
101: public int getLockLevel() {
102: return level;
103: }
104:
105: public int getStackTraceDepth() {
106: return stackTraceDepth;
107: }
108:
109: public int getStatCollectFrequency() {
110: return statCollectFrequency;
111: }
112:
113: public GlobalLockInfo getGlobalLockInfo() {
114: return globalLockInfo;
115: }
116:
117: public boolean isLockAward() {
118: return (this .responseType == LOCK_AWARD);
119: }
120:
121: public boolean isLockRecall() {
122: return (this .responseType == LOCK_RECALL);
123: }
124:
125: public boolean isLockWaitTimeout() {
126: return (this .responseType == LOCK_WAIT_TIMEOUT);
127: }
128:
129: public boolean isLockInfo() {
130: return (this .responseType == LOCK_INFO);
131: }
132:
133: public boolean isLockNotAwarded() {
134: return (this .responseType == LOCK_NOT_AWARDED);
135: }
136:
137: public boolean isLockStatEnabled() {
138: return (this .responseType == LOCK_STAT_ENABLED);
139: }
140:
141: public boolean isLockStatDisabled() {
142: return (this .responseType == LOCK_STAT_DISABLED);
143: }
144:
145: public String toString() {
146: return "LockResponseContext(" + lockID + "," + nodeID + ","
147: + threadID + ", " + LockLevel.toString(level) + " , "
148: + toString(responseType) + ")";
149: }
150:
151: private static String toString(int responseType2) {
152: switch (responseType2) {
153: case LOCK_AWARD:
154: return "LOCK_AWARD";
155: case LOCK_RECALL:
156: return "LOCK_RECALL";
157: case LOCK_WAIT_TIMEOUT:
158: return "LOCK_WAIT_TIMEOUT";
159: case LOCK_INFO:
160: return "LOCK_INFO";
161: case LOCK_NOT_AWARDED:
162: return "LOCK_NOT_AWARDED";
163: default:
164: return "UNKNOWN";
165: }
166: }
167:
168: private static Collection getGlobalLockHolderInfo(
169: Collection holderInfo) {
170: Collection holdersInfo = new ArrayList();
171: for (Iterator i = holderInfo.iterator(); i.hasNext();) {
172: Holder holder = (Holder) i.next();
173: holdersInfo.add(new GlobalLockStateInfo(holder.getLockID(),
174: holder.getNodeID(), holder.getThreadID(), holder
175: .getTimestamp(), holder.getTimeout(),
176: holder.getLockLevel()));
177: }
178: return holdersInfo;
179: }
180:
181: private static Collection getGlobalLockWaiterInfo(LockID id,
182: Collection waiters) {
183: Collection waitersInfo = new ArrayList();
184: for (Iterator i = waiters.iterator(); i.hasNext();) {
185: LockWaitContext lockWaitContext = (LockWaitContext) i
186: .next();
187: waitersInfo.add(new GlobalLockStateInfo(id, lockWaitContext
188: .getNodeID(), lockWaitContext.getThreadID(),
189: lockWaitContext.getTimestamp(), -1, lockWaitContext
190: .lockLevel()));
191: }
192: return waitersInfo;
193: }
194: }
|