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.api;
006:
007: import org.apache.commons.lang.builder.HashCodeBuilder;
008:
009: import com.tc.io.TCByteBufferInput;
010: import com.tc.io.TCByteBufferOutput;
011: import com.tc.io.TCSerializable;
012: import com.tc.net.groups.ClientID;
013: import com.tc.net.groups.NodeID;
014: import com.tc.net.groups.NodeIDSerializer;
015: import com.tc.util.Assert;
016:
017: import java.io.IOException;
018:
019: /**
020: * Client/Server intermediate fromat for holding the context of a lock request/award. This class bridges the types used
021: * internally by the ClientLockManager and the server LockManager so they can be sent in messages back and forth to each
022: * other.
023: */
024: public class LockContext implements TCSerializable {
025:
026: private LockID lockID;
027: private int lockLevel;
028: private NodeID nodeID;
029: private ThreadID threadID;
030: private int hashCode;
031:
032: public LockContext() {
033: return;
034: }
035:
036: public LockContext(LockID lockID, NodeID nodeID, ThreadID threadID,
037: int lockLevel) {
038: this .lockID = lockID;
039: this .nodeID = nodeID;
040: this .threadID = threadID;
041: Assert.assertFalse(LockLevel.isSynchronous(lockLevel));
042: this .lockLevel = lockLevel;
043: this .hashCode = new HashCodeBuilder(5503, 6737).append(lockID)
044: .append(nodeID).append(threadID).append(lockLevel)
045: .toHashCode();
046: }
047:
048: public String toString() {
049: return "LockContext [ " + lockID + ", "
050: + LockLevel.toString(lockLevel) + ", " + nodeID + ", "
051: + threadID + ", " + hashCode + "] ";
052: }
053:
054: public NodeID getNodeID() {
055: return nodeID;
056: }
057:
058: public LockID getLockID() {
059: return lockID;
060: }
061:
062: public int getLockLevel() {
063: return this .lockLevel;
064: }
065:
066: public ThreadID getThreadID() {
067: return threadID;
068: }
069:
070: public boolean equals(Object o) {
071: if (o == this )
072: return true;
073: if (!(o instanceof LockContext))
074: return false;
075: LockContext cmp = (LockContext) o;
076: return lockID.equals(cmp.lockID)
077: && threadID.equals(cmp.threadID)
078: && lockLevel == cmp.lockLevel
079: && nodeID.equals(cmp.nodeID);
080: }
081:
082: public int hashCode() {
083: return hashCode;
084: }
085:
086: public void serializeTo(TCByteBufferOutput output) {
087: output.writeString(lockID.asString());
088: NodeIDSerializer ns = new NodeIDSerializer(this .nodeID);
089: ns.serializeTo(output);
090: output.writeLong(threadID.toLong());
091: output.writeInt(lockLevel);
092: }
093:
094: public Object deserializeFrom(TCByteBufferInput input)
095: throws IOException {
096: lockID = new LockID(input.readString());
097: NodeIDSerializer ns = new NodeIDSerializer();
098: ns.deserializeFrom(input);
099: nodeID = (ClientID) ns.getNodeID();
100: threadID = new ThreadID(input.readLong());
101: lockLevel = input.readInt();
102: return this;
103: }
104:
105: }
|