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.lockmanager.api;
006:
007: import com.tc.net.groups.NodeID;
008: import com.tc.object.lockmanager.api.LockID;
009: import com.tc.object.lockmanager.api.LockLevel;
010: import com.tc.object.lockmanager.api.ThreadID;
011:
012: import java.io.Serializable;
013:
014: public class LockHolder implements Serializable {
015: private final static long NON_SET_TIME_MILLIS = -1;
016:
017: private final LockID lockID;
018: private final NodeID nodeID;
019: private final ThreadID threadID;
020: private final String lockLevel;
021: private final String channelAddr;
022: private long timeAcquired;
023: private long timeReleased;
024: private long timeRequested;
025: private long waitTimeInMillis;
026: private long heldTimeInMillis;
027:
028: public LockHolder(LockID lockID, NodeID cid, String channelAddr,
029: ThreadID threadID, int level, long timeRequested) {
030: this .lockID = lockID;
031: this .nodeID = cid;
032: this .channelAddr = channelAddr;
033: this .threadID = threadID;
034: this .timeRequested = timeRequested;
035: this .waitTimeInMillis = NON_SET_TIME_MILLIS;
036: this .heldTimeInMillis = NON_SET_TIME_MILLIS;
037: this .lockLevel = LockLevel.toString(level);
038: }
039:
040: public LockHolder(LockID lockID, NodeID cid, String channelAddr,
041: ThreadID threadID, int level) {
042: this (lockID, cid, channelAddr, threadID, level,
043: NON_SET_TIME_MILLIS);
044: }
045:
046: public LockID getLockID() {
047: return this .lockID;
048: }
049:
050: public String getLockLevel() {
051: return this .lockLevel;
052: }
053:
054: public NodeID getNodeID() {
055: return nodeID;
056: }
057:
058: public String getChannelAddr() {
059: return this .channelAddr;
060: }
061:
062: public long getTimeAcquired() {
063: return timeAcquired;
064: }
065:
066: public long getTimeReleased() {
067: return timeReleased;
068: }
069:
070: public ThreadID getThreadID() {
071: return threadID;
072: }
073:
074: public void lockAcquired(long timeAcquired) {
075: if (timeRequested <= 0) {
076: this .timeRequested = timeAcquired;
077: }
078: this .timeAcquired = timeAcquired;
079: }
080:
081: public void lockReleased() {
082: this .timeReleased = System.currentTimeMillis();
083: if (timeAcquired <= 0) {
084: timeAcquired = timeReleased;
085: }
086: }
087:
088: public long getWaitTimeInMillis() {
089: if (waitTimeInMillis == NON_SET_TIME_MILLIS) {
090: getAndSetWaitTimeInMillis();
091: }
092: return waitTimeInMillis;
093: }
094:
095: public long getHeldTimeInMillis() {
096: if (heldTimeInMillis == NON_SET_TIME_MILLIS) {
097: getAndSetHeldTimeInMillis();
098: }
099: return heldTimeInMillis;
100: }
101:
102: public long getAndSetWaitTimeInMillis() {
103: if (timeAcquired <= 0 && timeRequested <= 0) {
104: waitTimeInMillis = 0;
105: } else if (timeAcquired <= 0) {
106: waitTimeInMillis = System.currentTimeMillis()
107: - timeRequested;
108: } else {
109: waitTimeInMillis = timeAcquired - timeRequested;
110: }
111: return waitTimeInMillis;
112: }
113:
114: public long getAndSetHeldTimeInMillis() {
115: if (timeReleased <= 0 && timeAcquired <= 0) {
116: heldTimeInMillis = 0;
117: } else if (timeReleased <= 0) {
118: heldTimeInMillis = System.currentTimeMillis()
119: - timeAcquired;
120: } else {
121: heldTimeInMillis = timeReleased - timeAcquired;
122: }
123:
124: return heldTimeInMillis;
125: }
126:
127: public String toString() {
128: StringBuffer sb = new StringBuffer("[Lock ID: ");
129: sb.append(lockID);
130: sb.append(", Node ID: ");
131: sb.append(nodeID);
132: sb.append(", Thread ID: ");
133: sb.append(threadID);
134: sb.append(", lock level: ");
135: sb.append(lockLevel);
136: sb.append(", held time in millis: ");
137: sb.append(getAndSetHeldTimeInMillis());
138: sb.append(", wait time in millis: ");
139: sb.append(getAndSetWaitTimeInMillis());
140: sb.append(", time acquired: ");
141: sb.append(timeAcquired);
142: sb.append(", time released: ");
143: sb.append(timeReleased);
144: sb.append("]");
145: return sb.toString();
146: }
147: }
|