01: /*
02: * All content copyright (c) 2003-2006 Terracotta, Inc., except as may otherwise be noted in a separate copyright notice. All rights reserved.
03: */
04: package com.tc.objectserver.lockmanager.api;
05:
06: import EDU.oswego.cs.dl.util.concurrent.LinkedQueue;
07:
08: public class LockEventMonitor implements LockEventListener {
09:
10: public static final int DEFAULT_WAITER_COUNT = -1;
11:
12: private LinkedQueue notifyAddPendingCalls = new LinkedQueue();
13: private LinkedQueue notifyAwardCalls = new LinkedQueue();
14: private LinkedQueue notifyRevokeCalls = new LinkedQueue();
15:
16: public void notifyAddPending(int waiterCount, LockAwardContext ctxt) {
17: enqueue(notifyAddPendingCalls, new CallContext(waiterCount,
18: 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(
27: DEFAULT_WAITER_COUNT, ctxt));
28: }
29:
30: public CallContext waitForNotifyAddPending(long timeout)
31: throws Exception {
32: return dequeue(notifyAddPendingCalls, timeout);
33: }
34:
35: public CallContext waitForNotifyAward(long timeout)
36: throws Exception {
37: return dequeue(notifyAwardCalls, timeout);
38: }
39:
40: public CallContext waitForNotifyRevoke(long timeout)
41: throws Exception {
42: return dequeue(notifyRevokeCalls, timeout);
43: }
44:
45: public void reset() throws Exception {
46: drainQueue(notifyAddPendingCalls);
47: drainQueue(notifyAwardCalls);
48: drainQueue(notifyRevokeCalls);
49: }
50:
51: private void enqueue(LinkedQueue queue, Object o) {
52: try {
53: queue.put(o);
54: } catch (InterruptedException e) {
55: e.printStackTrace();
56: }
57: }
58:
59: private CallContext dequeue(LinkedQueue queue, long timeout)
60: throws Exception {
61: return (CallContext) queue.poll(timeout);
62: }
63:
64: private void drainQueue(LinkedQueue queue) throws Exception {
65: while (!queue.isEmpty()) {
66: queue.poll(0);
67: }
68: }
69:
70: public class CallContext {
71: public final int waiterCount;
72: public final LockAwardContext ctxt;
73:
74: public CallContext(int waiterCount, LockAwardContext ctxt) {
75: this.waiterCount = waiterCount;
76: this.ctxt = ctxt;
77: }
78: }
79:
80: }
|