1 package org.objectweb.perseus.concurrency.distributed.globallock; 2 3 import org.objectweb.perseus.concurrency.distributed.globallock.api.DeadLockException; 4 import org.objectweb.perseus.concurrency.distributed.globallock.api.GlobalLock; 5 import org.objectweb.perseus.concurrency.distributed.globallock.api.GlobalLockWaiter; 6 import org.objectweb.perseus.concurrency.distributed.globallock.lib.GlobalLockUser; 7 import org.objectweb.perseus.distribution.util.MessageEventListener; 8 import org.objectweb.perseus.distribution.util.MessageEventListenerRegistry; 9 10 import java.io.Serializable ; 11 12 36 public class GlobalLockInterceptor implements GlobalLock { 37 38 39 static final public boolean trace = false; 40 41 MessageEventListenerRegistry reg; 42 43 GlobalLockUser glu; 44 String name; 47 49 public interface FailureListener { 50 void handle(Serializable resId, Serializable nodeId); 51 } 52 53 public GlobalLockInterceptor(GlobalLockUser glu, 54 MessageEventListenerRegistry reg) { 58 this.glu = glu; 59 this.reg = reg; 63 } 64 65 66 public GlobalLockWaiter upgrade(byte lck, boolean sync, long timeout) 67 throws DeadLockException, InterruptedException { 68 GlobalLockWaiter w = glu.upgrade(lck, false, timeout); 69 MessageEventListener l = reg.getListener(); 70 if (l != null) l.handledByUser(); 71 if (sync && (w != null)) { 72 boolean ok = w.waitLock(timeout); 73 if (!ok) { 74 if (trace) trace("WAKE UP TIMED OUT (Waiting: " + lck + ")"); 75 throw new DeadLockException(); 76 } 77 78 if (trace) trace("WAKE UP (Waiting: " + lck + ")"); 79 w.signalHandled(); 80 return null; 81 } else { 82 return w; 83 } 84 85 } 86 87 public void downgrade(byte lck) { 88 glu.downgrade(lck); 89 } 90 91 public byte getGrantable() { 92 return glu.getGrantable(); 93 } 94 95 public void uncache() { 96 glu.uncache(); 97 } 98 99 100 101 102 public MessageEventListenerRegistry getReg() { 103 return reg; 104 } 105 106 public void setReg(MessageEventListenerRegistry reg) { 107 this.reg = reg; 108 } 109 public synchronized String toString() { 110 return glu.toString(); 111 } 112 113 protected void trace(String s) { 114 System.out.println(this + " " + s); 115 } 116 117 void setName(String s) { 118 name = s; 119 } 120 121 String getName() { 122 return name; 123 } 124 125 126 } 127 | Popular Tags |