KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > tc > object > lockmanager > api > TestRemoteLockManager


1 /*
2  * All content copyright (c) 2003-2006 Terracotta, Inc., except as may otherwise be noted in a separate copyright
3  * notice. All rights reserved.
4  */

5 package com.tc.object.lockmanager.api;
6
7 import com.tc.exception.ImplementMe;
8 import com.tc.object.session.SessionProvider;
9 import com.tc.object.tx.TransactionID;
10 import com.tc.object.tx.WaitInvocation;
11 import com.tc.util.concurrent.NoExceptionLinkedQueue;
12
13 import java.util.Arrays JavaDoc;
14 import java.util.Collection JavaDoc;
15 import java.util.HashMap JavaDoc;
16 import java.util.Iterator JavaDoc;
17 import java.util.LinkedList JavaDoc;
18 import java.util.Map JavaDoc;
19
20 /**
21  * @author steve
22  */

23 public class TestRemoteLockManager implements RemoteLockManager {
24   public final LockResponder LOOPBACK_LOCK_RESPONDER = new LoopbackLockResponder();
25   public final LockResponder NULL_LOCK_RESPONDER = new LockResponder() {
26                                                                 public void respondToLockRequest(LockRequest request) {
27                                                                   return;
28                                                                 }
29                                                               };
30   private ClientLockManager lockManager;
31   private Map JavaDoc locks = new HashMap JavaDoc();
32   private int lockRequests = 0;
33   private int unlockRequests = 0;
34   private int flushCount = 0;
35   private boolean isGreedy = false;
36   public LockResponder lockResponder = LOOPBACK_LOCK_RESPONDER;
37
38   public final NoExceptionLinkedQueue lockRequestCalls = new NoExceptionLinkedQueue();
39   private final SessionProvider sessionProvider;
40
41   public TestRemoteLockManager(SessionProvider sessionProvider) {
42     this.sessionProvider = sessionProvider;
43   }
44
45   public void setClientLockManager(ClientLockManager lockManager) {
46     this.lockManager = lockManager;
47   }
48
49   public synchronized int getLockRequestCount() {
50     return this.lockRequests;
51   }
52
53   public synchronized int getUnlockRequestCount() {
54     return this.unlockRequests;
55   }
56
57   // *************************
58
// This manager doesn't implement lock upgrades correctly. Lock
59
// upgrades/downgrades are always awarded. Locks held
60
// by other transactions are not taken into consideration
61
// *************************
62

63   public synchronized void requestLock(LockID lockID, ThreadID threadID, int lockType) {
64     lockRequests++;
65     lockRequestCalls.put(new Object JavaDoc[] { lockID, threadID, new Integer JavaDoc(lockType) });
66
67     LockRequest request;
68     if (isGreedy) {
69       request = new LockRequest(lockID, ThreadID.VM_ID, LockLevel.makeGreedy(lockType));
70     } else {
71       request = new LockRequest(lockID, threadID, lockType);
72     }
73
74     if (!locks.containsKey(lockID)) {
75       locks.put(lockID, new LinkedList JavaDoc(Arrays.asList(new Object JavaDoc[] { new Lock(threadID, lockType) })));
76       lockResponder.respondToLockRequest(request);
77       return;
78     }
79
80     LinkedList JavaDoc myLocks = (LinkedList JavaDoc) locks.get(lockID);
81     for (Iterator JavaDoc iter = myLocks.iterator(); iter.hasNext();) {
82       // allow lock upgrades/downgrades
83
Lock lock = (Lock) iter.next();
84       if (lock.threadID.equals(threadID)) {
85         lock.upCount();
86         lockResponder.respondToLockRequest(request);
87         return;
88       }
89     }
90
91     myLocks.addLast(new Lock(request.threadID(), request.lockLevel()));
92   }
93
94   public synchronized void makeLocksGreedy() {
95     isGreedy = true;
96   }
97
98   public synchronized void makeLocksNotGreedy() {
99     isGreedy = false;
100   }
101
102   public synchronized void releaseLock(LockID lockID, ThreadID threadID) {
103     unlockRequests++;
104
105     LinkedList JavaDoc myLocks = (LinkedList JavaDoc) locks.get(lockID);
106
107     Lock current = (Lock) myLocks.getFirst();
108     if (current.threadID.equals(threadID)) {
109       int count = current.downCount();
110       if (count == 0) {
111         myLocks.removeFirst();
112       } else {
113         return;
114       }
115     }
116
117     if (myLocks.isEmpty()) {
118       locks.remove(lockID);
119       return;
120     }
121
122     Lock lock = (Lock) myLocks.remove(0);
123     lockResponder.respondToLockRequest(new LockRequest(lockID, lock.threadID, lock.type));
124   }
125
126   public void releaseLockWait(LockID lockID, ThreadID threadID, WaitInvocation call) {
127     return;
128   }
129
130   public void recallCommit(LockID lockID, Collection JavaDoc lockContext, Collection JavaDoc waitContext, Collection JavaDoc pendingRequests) {
131     return;
132   }
133
134   public synchronized void flush(LockID lockID) {
135     flushCount++;
136   }
137
138   public synchronized void resetFlushCount() {
139     flushCount = 0;
140   }
141
142   public synchronized int getFlushCount() {
143     return flushCount;
144   }
145
146   public boolean isTransactionsForLockFlushed(LockID lockID, LockFlushCallback callback) {
147     return true;
148   }
149
150   public void notify(LockID lockID, TransactionID transactionID, boolean all) {
151     // TODO: this really should get called by a test at some point. At such
152
// time, you probably want to record the
153
// request and return
154
throw new ImplementMe();
155   }
156
157   public interface LockResponder {
158     void respondToLockRequest(LockRequest request);
159   }
160
161   private class LoopbackLockResponder implements LockResponder {
162     public void respondToLockRequest(LockRequest request) {
163       lockManager.awardLock(sessionProvider.getSessionID(), request.lockID(), request.threadID(), request.lockLevel());
164     }
165   }
166
167   private static class Lock {
168     final ThreadID threadID;
169     final int type;
170     int count = 1;
171
172     Lock(ThreadID threadID, int type) {
173       this.threadID = threadID;
174       this.type = type;
175     }
176
177     int upCount() {
178       return ++count;
179     }
180
181     int downCount() {
182       return --count;
183     }
184   }
185
186   public void queryLock(LockID lockID, ThreadID threadID) {
187     throw new ImplementMe();
188   }
189
190   public void tryRequestLock(LockID lockID, ThreadID threadID, int lockType) {
191     //
192
}
193
194   public void interrruptWait(LockID lockID, ThreadID threadID) {
195     throw new ImplementMe();
196   }
197 }
Popular Tags