1 24 package org.objectweb.jalisto.se.impl.lock; 25 26 import org.objectweb.jalisto.se.exception.multi.LockException; 27 28 import java.io.Serializable ; 29 import java.util.ArrayList ; 30 import java.util.HashMap ; 31 import java.util.Map ; 32 33 public class DeadLockToken implements Serializable { 34 35 public DeadLockToken(Object tokenId) { 36 this.tokenId = tokenId; 37 nexts = new ArrayList (); 38 tokens.put(tokenId, this); 39 } 40 41 public static DeadLockToken getToken(Object tokenId) { 42 return (DeadLockToken) tokens.get(tokenId); 43 } 44 45 48 public static Map getTokens() { 49 return tokens; 50 } 51 52 public boolean isWaitingFor(DeadLockToken token) { 53 return nexts.contains(token); 54 } 55 56 public boolean couldWakeUp() { 57 return nexts.isEmpty(); 58 } 59 60 public synchronized void noMoreWaitFor(DeadLockToken releasingSessionToken) { 61 nexts.remove(releasingSessionToken); 62 } 63 64 public synchronized void timeOutWakeUp(DeadLockToken tokenWhoWaitForThisOne) { 65 if (tokenWhoWaitForThisOne != null) { 66 tokenWhoWaitForThisOne.noMoreWaitFor(this); 67 for (int i = 0; i < nexts.size(); i++) { 68 tokenWhoWaitForThisOne.waitFor((DeadLockToken) nexts.get(i)); 69 } 70 } 71 nexts.clear(); 72 } 73 74 public String toString() { 75 if (nexts.isEmpty()) { 76 return "DLT " + tokenId + " waiting for nobody"; 77 } 78 StringBuffer result = new StringBuffer (); 79 result.append("DLT ").append(tokenId).append(" waiting for {"); 80 for (int i = 0; i < nexts.size(); i++) { 81 result.append(((DeadLockToken) nexts.get(i)).tokenId); 82 if (i != (nexts.size() - 1)) { 83 result.append(", "); 84 } 85 } 86 result.append('}'); 87 return result.toString(); 88 } 89 90 public synchronized void waitFor(DeadLockToken tokenToWaitFor) { 91 tokenToWaitFor.detectDeadLock(this); 92 nexts.add(tokenToWaitFor); 93 } 94 95 private void detectDeadLock(DeadLockToken candidate) { 96 if (this.equals(candidate)) { 97 throw new LockException("dead lock : candidate " + candidate + " with " + tokens.toString()); 98 } 99 if (!nexts.isEmpty()) { 100 for (int i = 0; i < nexts.size(); i++) { 101 ((DeadLockToken) nexts.get(i)).detectDeadLock(candidate); 102 } 103 } 104 } 105 106 private static Map tokens = new HashMap (); 107 private ArrayList nexts; 108 private Object tokenId; 109 110 static final long serialVersionUID = -7589377087964761459L; 111 } 112 | Popular Tags |