KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > tc > objectserver > context > LockResponseContext


1 /*
2  * All content copyright (c) 2003-2006 Terracotta, Inc., except as may otherwise be noted in a separate copyright notice. All rights reserved.
3  */

4 package com.tc.objectserver.context;
5
6 import com.tc.async.api.EventContext;
7 import com.tc.net.protocol.tcm.ChannelID;
8 import com.tc.object.lockmanager.api.LockID;
9 import com.tc.object.lockmanager.api.LockLevel;
10 import com.tc.object.lockmanager.api.ThreadID;
11 import com.tc.object.lockmanager.impl.GlobalLockInfo;
12 import com.tc.object.lockmanager.impl.GlobalLockStateInfo;
13 import com.tc.objectserver.lockmanager.api.LockWaitContext;
14 import com.tc.objectserver.lockmanager.impl.Holder;
15 import com.tc.util.Assert;
16
17 import java.util.ArrayList JavaDoc;
18 import java.util.Collection JavaDoc;
19 import java.util.Iterator JavaDoc;
20
21 public class LockResponseContext implements EventContext {
22
23   public static final int LOCK_AWARD = 1;
24   public static final int LOCK_RECALL = 2;
25   public static final int LOCK_WAIT_TIMEOUT = 3;
26   public static final int LOCK_INFO = 4;
27   public static final int LOCK_NOT_AWARDED = 5;
28
29   private final LockID lockID;
30   private final ThreadID threadID;
31   private final int level;
32   private final ChannelID channelID;
33   private final int responseType;
34   private final GlobalLockInfo globalLockInfo;
35
36   public LockResponseContext(LockID lockID, ChannelID channelID, ThreadID threadID, int level,
37                              int lockRequestQueueLength, int lockUpgradeQueueLength, Collection JavaDoc greedyHolders,
38                              Collection JavaDoc holders, Collection JavaDoc waiters, int type) {
39     this.lockID = lockID;
40     this.channelID = channelID;
41     this.threadID = threadID;
42     this.level = level;
43     this.responseType = type;
44     this.globalLockInfo = new GlobalLockInfo(lockID, level, lockRequestQueueLength, lockUpgradeQueueLength,
45                                              getGlobalLockHolderInfo(greedyHolders), getGlobalLockHolderInfo(holders),
46                                              getGlobalLockWaiterInfo(lockID, waiters));
47     Assert.assertTrue(responseType == LOCK_AWARD || responseType == LOCK_RECALL || responseType == LOCK_WAIT_TIMEOUT
48                       || responseType == LOCK_INFO || responseType == LOCK_NOT_AWARDED);
49   }
50
51   public LockResponseContext(LockID lockID, ChannelID channelID, ThreadID sourceID, int level, int type) {
52     this.lockID = lockID;
53     this.channelID = channelID;
54     this.threadID = sourceID;
55     this.level = level;
56     this.responseType = type;
57     this.globalLockInfo = null;
58     Assert.assertTrue(responseType == LOCK_AWARD || responseType == LOCK_RECALL || responseType == LOCK_WAIT_TIMEOUT
59                       || responseType == LOCK_INFO || responseType == LOCK_NOT_AWARDED);
60   }
61
62   public ChannelID getChannelID() {
63     return channelID;
64   }
65
66   public int getType() {
67     return level;
68   }
69
70   public LockID getLockID() {
71     return lockID;
72   }
73
74   public ThreadID getThreadID() {
75     return threadID;
76   }
77
78   public int getLockLevel() {
79     return level;
80   }
81
82   public GlobalLockInfo getGlobalLockInfo() {
83     return globalLockInfo;
84   }
85
86   public boolean isLockAward() {
87     return (this.responseType == LOCK_AWARD);
88   }
89
90   public boolean isLockRecall() {
91     return (this.responseType == LOCK_RECALL);
92   }
93
94   public boolean isLockWaitTimeout() {
95     return (this.responseType == LOCK_WAIT_TIMEOUT);
96   }
97
98   public boolean isLockInfo() {
99     return (this.responseType == LOCK_INFO);
100   }
101
102   public boolean isLockNotAwarded() {
103     return (this.responseType == LOCK_NOT_AWARDED);
104   }
105
106   public String JavaDoc toString() {
107     return "LockResponseContext(" + lockID + "," + channelID + "," + threadID + ", " + LockLevel.toString(level)
108            + " , " + toString(responseType) + ")";
109   }
110
111   private static String JavaDoc toString(int responseType2) {
112     switch (responseType2) {
113       case LOCK_AWARD:
114         return "LOCK_AWARD";
115       case LOCK_RECALL:
116         return "LOCK_RECALL";
117       case LOCK_WAIT_TIMEOUT:
118         return "LOCK_WAIT_TIMEOUT";
119       case LOCK_INFO:
120         return "LOCK_INFO";
121       case LOCK_NOT_AWARDED:
122         return "LOCK_NOT_AWARDED";
123       default:
124         return "UNKNOWN";
125     }
126   }
127
128   private Collection JavaDoc getGlobalLockHolderInfo(Collection JavaDoc holderInfo) {
129     Collection JavaDoc holdersInfo = new ArrayList JavaDoc();
130     for (Iterator JavaDoc i = holderInfo.iterator(); i.hasNext();) {
131       Holder holder = (Holder) i.next();
132       holdersInfo.add(new GlobalLockStateInfo(holder.getLockID(), holder.getChannelID(), holder.getThreadID(), holder
133           .getTimestamp(), holder.getTimeout(), holder.getLockLevel()));
134     }
135     return holdersInfo;
136   }
137
138   private Collection JavaDoc getGlobalLockWaiterInfo(LockID id, Collection JavaDoc waiters) {
139     Collection JavaDoc waitersInfo = new ArrayList JavaDoc();
140     for (Iterator JavaDoc i = waiters.iterator(); i.hasNext();) {
141       LockWaitContext lockWaitContext = (LockWaitContext) i.next();
142       waitersInfo.add(new GlobalLockStateInfo(id, lockWaitContext.getChannelID(), lockWaitContext.getThreadID(),
143                                               lockWaitContext.getTimestamp(), -1, lockWaitContext.lockLevel()));
144     }
145     return waitersInfo;
146   }
147 }
Popular Tags