KickJava   Java API By Example, From Geeks To Geeks.

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


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
29 import java.util.Iterator JavaDoc;
30
31 public class LockManagerPessimistic extends LockManagerDefault {
32
33     public void prepareCreateObject(LogicalOid floid, DataWrapper objectToCreate) {
34         takeLock(floid, WRITE);
35     }
36
37     public void prepareDeleteObjectByOid(LogicalOid floid, short update) {
38         takeLock(floid, WRITE).addTransactionnalValue(sessionId, null,
39                                                       ObjectTransactionnalState.DELETE_ACTION,
40                                                       (short) 0);
41     }
42
43     public void prepareReadObjectByOid(LogicalOid floid) {
44         takeLock(floid, READ);
45     }
46
47     public void prepareUpdateObjectByOid(LogicalOid floid, DataWrapper objectToUpdate) {
48         takeLock(floid, WRITE);
49     }
50
51
52     public void finishCreateObject(LogicalOid floid, DataWrapper objectToCreate) {
53         lockTable.getLock(floid).addTransactionnalValue(sessionId, objectToCreate,
54                                                         ObjectTransactionnalState.CREATE_ACTION, (short) 0);
55     }
56
57     public void finishReadObjectByOid(LogicalOid floid, DataWrapper objectToRead, short update) {
58         lockTable.getLock(floid).addTransactionnalValue(sessionId, objectToRead,
59                                                         ObjectTransactionnalState.READ_ACTION, update);
60     }
61
62     public void finishUpdateObjectByOid(LogicalOid floid, DataWrapper objectToUpdate, short update) {
63         takeLock(floid, WRITE).addTransactionnalValue(sessionId, objectToUpdate,
64                                                       ObjectTransactionnalState.UPDATE_ACTION, update);
65     }
66
67
68     public synchronized void commit() {
69         try {
70             accessController.getAccessControl(AccessController.COMMIT_ROLLBACK_LOCKMANAGER);
71             Iterator JavaDoc floids = lockTable.getKeys();
72             while (floids.hasNext()) {
73                 LogicalOid floid = (LogicalOid) floids.next();
74                 Lock lock = lockTable.getLockOrCreate(floid);
75                 if (lock.hasModifiedValueWith(sessionId)) {
76                     if (lock.hasWriteLockWith(sessionId)) {
77                         if (lock.getCurrentAction(sessionId) == ObjectTransactionnalState.CREATE_ACTION) {
78                             session.createObjectCommit(floid);
79                         } else if (lock.getCurrentAction(sessionId) == ObjectTransactionnalState.UPDATE_ACTION) {
80                             lock.update(sessionId);
81                             session.updateObjectCommit(floid);
82                         } else if (lock.getCurrentAction(sessionId) == ObjectTransactionnalState.DELETE_ACTION) {
83                             session.deleteObjectCommit(floid);
84                         } else if (lock.getCurrentAction(sessionId) == ObjectTransactionnalState.NEW_OID_ACTION) {
85                             session.makeNewFileOidCommit(floid);
86                         }
87                     }
88                     lock.releaseAllLock(sessionId);
89                     lockTable.removeLockByFloid(floid);
90                 }
91             }
92             session.getFileAccess().getPhysicalAccess().commit();
93         } finally {
94             accessController.releaseAccessControl(AccessController.COMMIT_ROLLBACK_LOCKMANAGER);
95         }
96     }
97
98     public synchronized void rollback() {
99         try {
100             accessController.getAccessControl(AccessController.COMMIT_ROLLBACK_LOCKMANAGER);
101             Iterator JavaDoc floids = lockTable.getKeys();
102             while (floids.hasNext()) {
103                 LogicalOid floid = (LogicalOid) floids.next();
104                 Lock lock = lockTable.getLockOrCreate(floid);
105                 if (lock.hasModifiedValueWith(sessionId)) {
106                     if (lock.hasWriteLockWith(sessionId)) {
107                         if (lock.getCurrentAction(sessionId) == ObjectTransactionnalState.CREATE_ACTION) {
108                             session.createObjectRollback(floid);
109                         } else if (lock.getCurrentAction(sessionId) == ObjectTransactionnalState.UPDATE_ACTION) {
110                             session.updateObjectRollback(floid);
111                         } else if (lock.getCurrentAction(sessionId) == ObjectTransactionnalState.DELETE_ACTION) {
112                             session.deleteObjectRollback(floid);
113                         } else if (lock.getCurrentAction(sessionId) == ObjectTransactionnalState.NEW_OID_ACTION) {
114                             session.makeNewFileOidRollback(floid);
115                         }
116                     }
117                     lock.releaseAllLock(sessionId);
118                     lockTable.removeLockByFloid(floid);
119                 }
120             }
121             session.getFileAccess().rollback(sessionId);
122         } finally {
123             accessController.releaseAccessControl(AccessController.COMMIT_ROLLBACK_LOCKMANAGER);
124         }
125     }
126
127 }
128
Popular Tags