KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectweb > jalisto > se > impl > lock > DeadLockToken


1 /*
2  * Jalisto - JAva LIght STOrage
3  * Copyright (C) 2000-2005 Xcalia http://www.xcalia.com
4  *
5  * This library is free software; you can redistribute it and/or
6  * modify it under the terms of the GNU Lesser General Public
7  * License as published by the Free Software Foundation; either
8  * version 2.1 of the License, or (at your option) any later version.
9  *
10  * This library is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13  * Lesser General Public License for more details.
14  *
15  * You should have received a copy of the GNU Lesser General Public
16  * License along with this library; if not, write to the Free Software
17  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307, USA.
18  *
19  * Xcalia
20  * 71, rue Desnouettes
21  * 75014 Paris - France
22  * http://www.xcalia.com
23  */

24 package org.objectweb.jalisto.se.impl.lock;
25
26 import org.objectweb.jalisto.se.exception.multi.LockException;
27
28 import java.io.Serializable JavaDoc;
29 import java.util.ArrayList JavaDoc;
30 import java.util.HashMap JavaDoc;
31 import java.util.Map JavaDoc;
32
33 public class DeadLockToken implements Serializable JavaDoc {
34
35     public DeadLockToken(Object JavaDoc tokenId) {
36         this.tokenId = tokenId;
37         nexts = new ArrayList JavaDoc();
38         tokens.put(tokenId, this);
39     }
40
41     public static DeadLockToken getToken(Object JavaDoc tokenId) {
42         return (DeadLockToken) tokens.get(tokenId);
43     }
44
45     /**
46      * ********** STATIC *****************************
47      */

48     public static Map JavaDoc 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 JavaDoc toString() {
75         if (nexts.isEmpty()) {
76             return "DLT " + tokenId + " waiting for nobody";
77         }
78         StringBuffer JavaDoc result = new StringBuffer JavaDoc();
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 JavaDoc tokens = new HashMap JavaDoc();
107     private ArrayList JavaDoc nexts;
108     private Object JavaDoc tokenId;
109
110     static final long serialVersionUID = -7589377087964761459L;
111 }
112
Popular Tags