KickJava   Java API By Example, From Geeks To Geeks.

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


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.impl.LogicalOid;
27 import org.objectweb.jalisto.se.api.internal.DataWrapper;
28 import org.objectweb.jalisto.se.api.internal.multi.SessionMulti;
29 import org.objectweb.jalisto.se.api.internal.multi.LockManager;
30 import org.objectweb.jalisto.se.api.internal.multi.LockTable;
31 import org.objectweb.jalisto.se.exception.multi.SleepingException;
32
33 import java.util.Collection JavaDoc;
34
35 public abstract class LockManagerDefault implements LockManager {
36
37     public void setLockTable(LockTable lockTable) {
38         this.lockTable = lockTable;
39     }
40
41     public DataWrapper getLockedValue(LogicalOid floid, Object JavaDoc sessionId) {
42         return lockTable.getLockedValue(floid, sessionId);
43     }
44
45     public void setLockedValue(LogicalOid floid, Object JavaDoc sessionId, DataWrapper value, short update) {
46         lockTable.getLock(floid).updateTransactionnalValue(sessionId, value, update);
47     }
48
49     public short getUpdate(LogicalOid floid, Object JavaDoc sessionId) {
50         return lockTable.getLock(floid).getUpdate(sessionId);
51     }
52
53     public void setSession(SessionMulti session) {
54         this.session = session;
55         this.sessionId = session.getSessionId();
56         this.accessController = session.getAccessController();
57     }
58
59     protected Lock takeLock(Object JavaDoc floid, int action) {
60         boolean isLockTaken = false;
61         boolean isAsleeping = false;
62         Lock lock = null;
63         while (!isLockTaken) {
64             synchronized (lockTable) { // avoid someone remove lock after we get it and before we put a lock
65
lock = lockTable.getLockOrCreate(floid);
66                 try {
67                     if (action == READ) {
68                         lock.takeReadLock(sessionId);
69                     } else {
70                         lock.takeWriteLock(sessionId);
71                     }
72                     isLockTaken = true;
73                 } catch (SleepingException fse) {
74                     isAsleeping = lock.addSleepingSession(sessionId);
75                 }
76             }
77             if (isAsleeping) {
78                 session.goToBed();
79                 session.sleep(floid, "during takeLock with floid = " + String.valueOf(floid) +
80                                      " with action code = " + action);
81                 isAsleeping = false;
82             }
83         }
84         return lock;
85     }
86
87     /**
88      * ***********************************************************************************************
89      */

90
91     public void begin() {
92         session.getOidTable().begin(sessionId);
93     }
94
95     public void checkLocksForSession(Collection JavaDoc floids, Object JavaDoc sessionId) {
96         lockTable.checkLocksForSession(floids, sessionId);
97     }
98
99     public boolean containsDeletedValue(LogicalOid floid, Object JavaDoc sessionId) {
100         return lockTable.containsDeletedValue(floid, sessionId);
101     }
102
103     public boolean containsLockedValue(LogicalOid floid, Object JavaDoc sessionId) {
104         return lockTable.containsLockedValue(floid, sessionId);
105     }
106
107     public void finishCreateObject(LogicalOid floid, DataWrapper objectToCreate) {
108     }
109
110     public void finishDeleteObjectByOid(LogicalOid floid) {
111     }
112
113     public void finishMakeNewFileOid(LogicalOid floid) {
114     }
115
116     public void finishReadObjectByOid(LogicalOid floid, DataWrapper objectToRead, short update) {
117     }
118
119     public void finishUpdateObjectByOid(LogicalOid floid, DataWrapper objectToUpdate, short update) {
120     }
121
122     public void prepareCreateObject(LogicalOid floid, DataWrapper objectToCreate) {
123     }
124
125     public void prepareDeleteObjectByOid(LogicalOid floid, short update) {
126     }
127
128     public void prepareReadObjectByOid(LogicalOid floid) {
129     }
130
131
132     protected SessionMulti session;
133     protected LockTable lockTable;
134     protected AccessController accessController;
135     protected Object JavaDoc sessionId;
136
137
138     protected static final int READ = 0;
139     protected static final int WRITE = 1;
140 }
141
Popular Tags