KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > tc > objectserver > lockmanager > impl > Holder


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.lockmanager.impl;
5
6 import com.tc.async.api.Sink;
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.objectserver.lockmanager.api.LockAwardContext;
12 import com.tc.util.Assert;
13
14 public class Holder implements LockAwardContext {
15   private final ServerThreadContext threadContext;
16   private final LockID lockID;
17   private final ChannelID channelID;
18   private final ThreadID threadID;
19   private final long timeout;
20   private final long timestamp;
21   private int lockLevel;
22   private Sink sink;
23
24   public Holder(LockID lockID, ServerThreadContext txn, long timeout) {
25     this.timestamp = System.currentTimeMillis();
26     this.lockID = lockID;
27     this.threadContext = txn;
28     this.channelID = txn.getId().getChannelID();
29     this.threadID = txn.getId().getClientThreadID();
30     this.timeout = timeout;
31     this.lockLevel = LockLevel.NIL_LOCK_LEVEL;
32   }
33
34   public synchronized boolean isUpgrade() {
35     return LockLevel.isRead(lockLevel) && LockLevel.isWrite(lockLevel);
36   }
37
38   synchronized int addLockLevel(int level) {
39     if (LockLevel.isGreedy(level)) {
40       Assert.assertEquals(getThreadID(), ThreadID.VM_ID);
41       level = LockLevel.makeNotGreedy(level);
42     }
43     return this.lockLevel |= level;
44   }
45
46   synchronized int removeLockLevel(int level) {
47     return this.lockLevel ^= level;
48   }
49
50   public synchronized int getLockLevel() {
51     return this.lockLevel;
52   }
53
54   public ChannelID getChannelID() {
55     return channelID;
56   }
57
58   public LockID getLockID() {
59     return lockID;
60   }
61
62   public long getTimeout() {
63     return this.timeout;
64   }
65
66   public ThreadID getThreadID() {
67     return threadID;
68   }
69
70   public boolean isGreedy() {
71     return (getThreadID().equals(ThreadID.VM_ID));
72   }
73
74   public long getTimestamp() {
75     return timestamp;
76   }
77
78   public String JavaDoc toString() {
79     return "Holder" + "@" + System.identityHashCode(this) + "[" + channelID + "," + threadID + ",level="
80            + LockLevel.toString(getLockLevel()) + ",timeout=" + timeout + "]";
81   }
82
83   ServerThreadContext getThreadContext() {
84     return threadContext;
85   }
86
87   public void setSink(Sink lockResponseSink) {
88     Assert.assertTrue(isGreedy());
89     sink = lockResponseSink;
90   }
91
92   public Sink getSink() {
93     Assert.assertTrue(isGreedy());
94     return sink;
95   }
96 }
Popular Tags