KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > tc > object > lockmanager > impl > GlobalLockInfo


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.object.lockmanager.impl;
5
6 import com.tc.io.TCByteBufferInputStream;
7 import com.tc.io.TCByteBufferOutput;
8 import com.tc.io.TCSerializable;
9 import com.tc.object.lockmanager.api.LockID;
10
11 import java.io.IOException JavaDoc;
12 import java.util.ArrayList JavaDoc;
13 import java.util.Collection JavaDoc;
14 import java.util.Iterator JavaDoc;
15
16 /**
17  * This class is used to hold the gloabl information of an lock object and passed back to the client
18  * when a client queries about the information of a lock.
19  */

20 public class GlobalLockInfo implements TCSerializable {
21   private LockID lockID;
22   private int level;
23   private int lockRequestQueueLength;
24   private int lockUpgradeQueueLength;
25   private Collection JavaDoc greedyHoldersInfo;
26   private Collection JavaDoc holdersInfo;
27   private Collection JavaDoc waitersInfo;
28
29   public GlobalLockInfo() {
30     super();
31   }
32
33   public GlobalLockInfo(LockID lockID, int level, int lockRequestQueueLength, int lockUpgradeQueueLength, Collection JavaDoc greedyHolders, Collection JavaDoc holders, Collection JavaDoc waiters) {
34     this.lockID = lockID;
35     this.level = level;
36     this.lockRequestQueueLength = lockRequestQueueLength;
37     this.lockUpgradeQueueLength = lockUpgradeQueueLength;
38     this.greedyHoldersInfo = greedyHolders;
39     this.holdersInfo = holders;
40     this.waitersInfo = waiters;
41   }
42   
43   public int getLockRequestQueueLength() {
44     return lockRequestQueueLength;
45   }
46
47   public int getLockUpgradeQueueLength() {
48     return lockUpgradeQueueLength;
49   }
50
51   public boolean isLocked() {
52     return (holdersInfo != null && holdersInfo.size() > 0) ||
53            (greedyHoldersInfo != null && greedyHoldersInfo.size() > 0);
54   }
55
56   public Collection JavaDoc getGreedyHoldersInfo() {
57     return greedyHoldersInfo;
58   }
59
60   public Collection JavaDoc getHoldersInfo() {
61     return holdersInfo;
62   }
63
64   public Collection JavaDoc getWaitersInfo() {
65     return waitersInfo;
66   }
67
68   public void serializeTo(TCByteBufferOutput serialOutput) {
69     serialOutput.writeString(lockID.asString());
70     serialOutput.writeInt(level);
71     serialOutput.writeInt(lockRequestQueueLength);
72     serialOutput.writeInt(lockUpgradeQueueLength);
73     serialOutput.writeInt(holdersInfo.size());
74     for (Iterator JavaDoc i = holdersInfo.iterator(); i.hasNext();) {
75       GlobalLockStateInfo holderInfo = (GlobalLockStateInfo) i.next();
76       holderInfo.serializeTo(serialOutput);
77     }
78     serialOutput.writeInt(greedyHoldersInfo.size());
79     for (Iterator JavaDoc i=greedyHoldersInfo.iterator(); i.hasNext(); ) {
80       GlobalLockStateInfo holderInfo = (GlobalLockStateInfo) i.next();
81       holderInfo.serializeTo(serialOutput);
82     }
83     serialOutput.writeInt(waitersInfo.size());
84     for (Iterator JavaDoc i=waitersInfo.iterator(); i.hasNext(); ) {
85       GlobalLockStateInfo holderInfo = (GlobalLockStateInfo) i.next();
86       holderInfo.serializeTo(serialOutput);
87     }
88   }
89
90   public Object JavaDoc deserializeFrom(TCByteBufferInputStream serialInput) throws IOException JavaDoc {
91     this.lockID = new LockID(serialInput.readString());
92     this.level = serialInput.readInt();
93     this.lockRequestQueueLength = serialInput.readInt();
94     this.lockUpgradeQueueLength = serialInput.readInt();
95     int size = serialInput.readInt();
96     holdersInfo = new ArrayList JavaDoc(size);
97     for (int i = 0; i < size; i++) {
98       GlobalLockStateInfo holderInfo = new GlobalLockStateInfo();
99       holderInfo.deserializeFrom(serialInput);
100       holdersInfo.add(holderInfo);
101     }
102     size = serialInput.readInt();
103     greedyHoldersInfo = new ArrayList JavaDoc(size);
104     for (int i = 0; i < size; i++) {
105       GlobalLockStateInfo holderInfo = new GlobalLockStateInfo();
106       holderInfo.deserializeFrom(serialInput);
107       greedyHoldersInfo.add(holderInfo);
108     }
109     size = serialInput.readInt();
110     waitersInfo = new ArrayList JavaDoc(size);
111     for (int i = 0; i < size; i++) {
112       GlobalLockStateInfo holderInfo = new GlobalLockStateInfo();
113       holderInfo.deserializeFrom(serialInput);
114       waitersInfo.add(holderInfo);
115     }
116     return this;
117   }
118
119 }
Popular Tags