KickJava   Java API By Example, From Geeks To Geeks.

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


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.api.internal.DataWrapper;
27 import org.objectweb.jalisto.se.api.internal.multi.LockTable;
28 import org.objectweb.jalisto.se.exception.multi.LockException;
29
30 import java.util.*;
31
32 public class LockTableImpl implements LockTable {
33
34     public LockTableImpl() {
35         locks = new Hashtable();
36     }
37
38     public synchronized Iterator getKeys() {
39         ArrayList keys = new ArrayList();
40         keys.addAll(locks.keySet());
41         return keys.iterator();
42     }
43
44     public synchronized Lock getLockOrCreate(Object JavaDoc floid) {
45         Lock lock;
46         if (!locks.containsKey(floid)) {
47             lock = Lock.newInstance();
48             locks.put(floid, lock);
49         } else {
50             lock = (Lock) locks.get(floid);
51         }
52         return lock;
53     }
54
55     public synchronized Lock getLock(Object JavaDoc floid) {
56         if (!locks.containsKey(floid)) {
57             throw new LockException();
58         }
59         return (Lock) locks.get(floid);
60     }
61
62     public synchronized DataWrapper getLockedValue(Object JavaDoc floid, Object JavaDoc sessionId) {
63         Lock lock = (Lock) locks.get(floid);
64         if (lock != null) {
65             return lock.getModifiedValue(sessionId);
66         }
67         throw new LockException("no lock find with this floid " + floid);
68     }
69
70     // TODO methode à élimminer surement
71
public synchronized void checkLocksForSession(Collection classeFloids, Object JavaDoc sessionId) {
72         Iterator floids = locks.keySet().iterator();
73         while (floids.hasNext()) {
74             Object JavaDoc floid = floids.next();
75             Lock lock = (Lock) locks.get(floid);
76             if (lock.hasToBeRemoveForExtentWith(sessionId)) {
77                 classeFloids.remove(floid);
78             }
79         }
80     }
81
82     public synchronized boolean containsDeletedValue(Object JavaDoc floid, Object JavaDoc sessionId) {
83         Lock lock = (Lock) locks.get(floid);
84         if (lock != null) {
85             return (lock.getCurrentAction(sessionId) == ObjectTransactionnalState.DELETE_ACTION);
86         }
87         return false;
88     }
89
90     public synchronized boolean containsLockedValue(Object JavaDoc floid, Object JavaDoc sessionId) {
91         Lock lock = (Lock) locks.get(floid);
92         if (lock != null) {
93             return (lock.getModifiedValue(sessionId) != null);
94         }
95         return false;
96     }
97
98     public boolean hasLock(Object JavaDoc floid) {
99         return locks.containsKey(floid);
100     }
101
102     public void publishLocks() {
103         System.out.println(locks.toString());
104     }
105
106     // lock is already synchronized by caller
107
public synchronized void removeLockByFloid(Object JavaDoc floid) {
108         Lock lock = (Lock) locks.get(floid);
109         if (lock != null) {
110             if (lock.canBeDeleted()) {
111                 locks.remove(floid);
112                 lock.release();
113             }
114         }
115     }
116
117     public boolean isEmpty() {
118         return locks.isEmpty();
119     }
120
121
122     private Map locks; // key is floid
123
}
124
Popular Tags