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.lockmanager.impl;
006:
007: import com.tc.io.TCByteBufferInput;
008: import com.tc.io.TCByteBufferOutput;
009: import com.tc.io.TCSerializable;
010: import com.tc.object.lockmanager.api.LockID;
011:
012: import java.io.IOException;
013: import java.util.ArrayList;
014: import java.util.Collection;
015: import java.util.Iterator;
016:
017: /**
018: * This class is used to hold the gloabl information of an lock object and passed back to the client when a client
019: * queries about the information of a lock.
020: */
021: public class GlobalLockInfo implements TCSerializable {
022: private LockID lockID;
023: private int level;
024: private int lockRequestQueueLength;
025: private Collection greedyHoldersInfo;
026: private Collection holdersInfo;
027: private Collection waitersInfo;
028:
029: public GlobalLockInfo() {
030: super ();
031: }
032:
033: public GlobalLockInfo(LockID lockID, int level,
034: int lockRequestQueueLength, Collection greedyHolders,
035: Collection holders, Collection waiters) {
036: this .lockID = lockID;
037: this .level = level;
038: this .lockRequestQueueLength = lockRequestQueueLength;
039: this .greedyHoldersInfo = greedyHolders;
040: this .holdersInfo = holders;
041: this .waitersInfo = waiters;
042: }
043:
044: public int getLockRequestQueueLength() {
045: return lockRequestQueueLength;
046: }
047:
048: public boolean isLocked(int level) {
049: return (this .level == level)
050: && ((holdersInfo != null && holdersInfo.size() > 0) || (greedyHoldersInfo != null && greedyHoldersInfo
051: .size() > 0));
052: }
053:
054: public Collection getGreedyHoldersInfo() {
055: return greedyHoldersInfo;
056: }
057:
058: public Collection getHoldersInfo() {
059: return holdersInfo;
060: }
061:
062: public Collection getWaitersInfo() {
063: return waitersInfo;
064: }
065:
066: public void serializeTo(TCByteBufferOutput serialOutput) {
067: serialOutput.writeString(lockID.asString());
068: serialOutput.writeInt(level);
069: serialOutput.writeInt(lockRequestQueueLength);
070: serialOutput.writeInt(holdersInfo.size());
071: for (Iterator i = holdersInfo.iterator(); i.hasNext();) {
072: GlobalLockStateInfo holderInfo = (GlobalLockStateInfo) i
073: .next();
074: holderInfo.serializeTo(serialOutput);
075: }
076: serialOutput.writeInt(greedyHoldersInfo.size());
077: for (Iterator i = greedyHoldersInfo.iterator(); i.hasNext();) {
078: GlobalLockStateInfo holderInfo = (GlobalLockStateInfo) i
079: .next();
080: holderInfo.serializeTo(serialOutput);
081: }
082: serialOutput.writeInt(waitersInfo.size());
083: for (Iterator i = waitersInfo.iterator(); i.hasNext();) {
084: GlobalLockStateInfo holderInfo = (GlobalLockStateInfo) i
085: .next();
086: holderInfo.serializeTo(serialOutput);
087: }
088: }
089:
090: public Object deserializeFrom(TCByteBufferInput serialInput)
091: throws IOException {
092: this .lockID = new LockID(serialInput.readString());
093: this .level = serialInput.readInt();
094: this .lockRequestQueueLength = serialInput.readInt();
095: int size = serialInput.readInt();
096: holdersInfo = new ArrayList(size);
097: for (int i = 0; i < size; i++) {
098: GlobalLockStateInfo holderInfo = new GlobalLockStateInfo();
099: holderInfo.deserializeFrom(serialInput);
100: holdersInfo.add(holderInfo);
101: }
102: size = serialInput.readInt();
103: greedyHoldersInfo = new ArrayList(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(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: }
|