KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > tc > objectserver > lockmanager > api > LockEventMonitor


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.api;
5
6 import EDU.oswego.cs.dl.util.concurrent.LinkedQueue;
7
8
9 public class LockEventMonitor implements LockEventListener {
10
11   public static final int DEFAULT_WAITER_COUNT = -1;
12   
13   private LinkedQueue notifyAddPendingCalls = new LinkedQueue();
14   private LinkedQueue notifyAwardCalls = new LinkedQueue();
15   private LinkedQueue notifyRevokeCalls = new LinkedQueue();
16   
17   public void notifyAddPending(int waiterCount, LockAwardContext ctxt) {
18     enqueue(notifyAddPendingCalls, new CallContext(waiterCount, ctxt));
19   }
20
21   public void notifyAward(int waiterCount, LockAwardContext ctxt) {
22     enqueue(notifyAwardCalls, new CallContext(waiterCount, ctxt));
23   }
24   
25   public void notifyRevoke(LockAwardContext ctxt) {
26     enqueue(notifyRevokeCalls, new CallContext(DEFAULT_WAITER_COUNT, ctxt));
27   }
28
29   public CallContext waitForNotifyAddPending(long timeout) throws Exception JavaDoc {
30     return dequeue(notifyAddPendingCalls, timeout);
31   }
32
33   public CallContext waitForNotifyAward(long timeout) throws Exception JavaDoc {
34     return dequeue(notifyAwardCalls, timeout);
35   }
36
37   public CallContext waitForNotifyRevoke(long timeout) throws Exception JavaDoc {
38     return dequeue(notifyRevokeCalls, timeout);
39   }
40
41   public void reset() throws Exception JavaDoc {
42     drainQueue(notifyAddPendingCalls);
43     drainQueue(notifyAwardCalls);
44     drainQueue(notifyRevokeCalls);
45   }
46   
47   private void enqueue(LinkedQueue queue, Object JavaDoc o) {
48     try {
49       queue.put(o);
50     } catch (InterruptedException JavaDoc e) {
51       e.printStackTrace();
52     }
53   }
54   
55   private CallContext dequeue(LinkedQueue queue, long timeout) throws Exception JavaDoc {
56     return (CallContext) queue.poll(timeout);
57   }
58   
59   private void drainQueue(LinkedQueue queue) throws Exception JavaDoc {
60     while (! queue.isEmpty()) {
61       queue.poll(0);
62     }
63   }
64   
65   public class CallContext {
66     public final int waiterCount;
67     public final LockAwardContext ctxt;
68     
69     public CallContext(int waiterCount, LockAwardContext ctxt) {
70       this.waiterCount = waiterCount;
71       this.ctxt = ctxt;
72     }
73   }
74
75 }
Popular Tags