KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectweb > jalisto > se > impl > multi > SessionMultiImpl


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.multi;
25
26 import org.objectweb.jalisto.se.api.*;
27 import org.objectweb.jalisto.se.api.internal.multi.LockManager;
28 import org.objectweb.jalisto.se.api.internal.multi.LockTable;
29 import org.objectweb.jalisto.se.api.internal.multi.SessionMulti;
30 import org.objectweb.jalisto.se.api.internal.DataWrapper;
31 import org.objectweb.jalisto.se.api.internal.Constants;
32 import org.objectweb.jalisto.se.exception.JalistoException;
33 import org.objectweb.jalisto.se.exception.IdentityException;
34 import org.objectweb.jalisto.se.exception.TransactionException;
35 import org.objectweb.jalisto.se.impl.*;
36 import org.objectweb.jalisto.se.impl.server.PhysicalOid;
37 import org.objectweb.jalisto.se.impl.server.SessionImpl;
38 import org.objectweb.jalisto.se.impl.lock.*;
39 import org.objectweb.jalisto.se.impl.trace.Trace;
40 import org.objectweb.jalisto.se.JalistoFactory;
41
42 public class SessionMultiImpl extends SessionImpl implements SessionMulti {
43
44     public SessionMultiImpl(JalistoProperties properties) {
45         super(properties);
46         trace.println(Trace.DEBUG, "CREATE A MULTI SESSION, id = {0}", sessionId);
47         lockTable = JalistoFactory.getInternalFactory().getLockTable(properties);
48         accessController = JalistoFactory.getInternalFactory().getAccessController(properties);
49         String JavaDoc txMode = properties.getProperty(JalistoProperties.CONCURRENCY_MODE_KEY);
50         if (txMode.equalsIgnoreCase(Constants.OPTIMISTIC_MODE)) {
51             setOptimistic();
52         } else if (txMode.equalsIgnoreCase(Constants.PESSIMISTIC_MODE)) {
53             setPessimistic();
54         } else {
55             throw new JalistoException(
56                     JalistoProperties.CONCURRENCY_MODE_KEY + " not valide in database properties");
57         }
58         try {
59             timeOut = (new Integer JavaDoc(properties.getProperty(JalistoProperties.TIME_OUT_KEY))).intValue();
60         } catch (Exception JavaDoc e) {
61             throw new JalistoException(JalistoProperties.TIME_OUT_KEY + " not valide in database properties");
62         }
63         new DeadLockToken(sessionId);
64     }
65
66
67     public Object JavaDoc makeNewFileOid(Class JavaDoc objectClass) {
68         return makeNewFileOid(objectClass.getName());
69     }
70
71     public Object JavaDoc makeNewFileOid(String JavaDoc objectClassName) {
72         trace.println(Trace.SESSION, "{0} makeNewFileOid({1})", sessionId, objectClassName);
73         checkValidity("makeNewFileOid", true);
74         try {
75             accessController.getAccessControl(AccessController.NEW_OID);
76             Object JavaDoc theClid = repository.getClidFromClassName(objectClassName);
77             LogicalOid floid = oidTable.allocateNewFloid(((Short JavaDoc) theClid).shortValue());
78             PhysicalOid fpoid = fileAccess.allocateNewFpoid(sessionId, theClid);
79             oidTable.insertFpoid(sessionId, floid, fpoid);
80             lockManager.finishMakeNewFileOid(floid);
81             return floid.getClone();
82         } finally {
83             accessController.releaseAccessControl(AccessController.NEW_OID);
84         }
85     }
86
87     public Object JavaDoc makeNewFileOid(LogicalOid floid) {
88         trace.println(Trace.SESSION, "{0} makeNewFileOid({1})", sessionId, floid);
89         checkValidity("makeNewFileOid", true);
90         try {
91             LogicalOid clone = floid.getClone();
92             accessController.getAccessControl(AccessController.NEW_OID);
93             Object JavaDoc theClid = new Short JavaDoc(clone.getClid());
94             PhysicalOid fpoid = fileAccess.allocateNewFpoid(sessionId, theClid);
95             oidTable.insertFpoid(sessionId, clone, fpoid);
96             lockManager.finishMakeNewFileOid(clone);
97             return floid;
98         } finally {
99             accessController.releaseAccessControl(AccessController.NEW_OID);
100         }
101     }
102
103     public void makeNewFileOidCommit(LogicalOid floid) {
104         // nothing to do
105
}
106
107     public void makeNewFileOidRollback(LogicalOid floid) {
108         PhysicalOid fpoid = oidTable.getFpoid(sessionId, floid);
109         oidTable.removeFloid(sessionId, floid);
110         if (fpoid != null) {
111             fileAccess.desallocateFpoid(sessionId, fpoid);
112         } else {
113             throw new IdentityException("the given oid " + floid + " doesn't exist in this Jalisto datastore");
114         }
115     }
116
117     public Object JavaDoc createObject(Object JavaDoc oid, Object JavaDoc[] objectToCreate) {
118         checkValidity("createObject", true);
119         DataWrapper datas = JalistoFactory.getInternalFactory().getNewDataWrapper(props, objectToCreate);
120         LogicalOid floid = getFloidFromOid(oid);
121         oidTable.markAsCreated(sessionId, floid);
122         lockManager.prepareCreateObject(floid, datas);
123         cache.put(floid, datas);
124         lockManager.finishCreateObject(floid, datas);
125         return floid;
126     }
127
128     public void createObjectCommit(LogicalOid floid) {
129         trace.println(Trace.SESSION, "{0} createObjectCommit({1})", sessionId, floid);
130         PhysicalOid fpoid = oidTable.getFpoid(sessionId, floid);
131         DataWrapper datas = (DataWrapper) cache.get(floid);
132         if (datas == null) {
133             datas = lockManager.getLockedValue(floid, sessionId);
134         }
135         fileAccess.insertDatas(sessionId, fpoid, datas);
136     }
137
138     public void createObjectRollback(LogicalOid floid) {
139         trace.println(Trace.SESSION, "{0} createObjectRollback({1})", sessionId, floid);
140         if (cache.containsKey(floid)) {
141             cache.remove(floid);
142         }
143         PhysicalOid fpoid = oidTable.getFpoid(sessionId, floid);
144         oidTable.removeFloid(sessionId, floid);
145         if (fpoid != null) {
146             fileAccess.desallocateFpoid(sessionId, fpoid);
147         } else {
148             throw new IdentityException("the given oid " + floid + " doesn't exist in this Jalisto datastore");
149         }
150     }
151
152     public Object JavaDoc[] readObjectByOid(Object JavaDoc oid, boolean withCache) {
153         return readObjectByOidInDataObject(oid, withCache).getDatas();
154     }
155
156     public DataWrapper readObjectByOidInDataObject(Object JavaDoc oid, boolean withCache) {
157         trace.println(Trace.SESSION, "{0} readObjectByOidInDataObject({1})", sessionId, oid);
158         checkValidity("readObjectByOidInDataObject", true);
159         LogicalOid floid = getFloidFromOid(oid);
160
161         // delete not commited for this session
162
if (lockManager.containsDeletedValue(floid, sessionId)) {
163             lockManager.finishReadObjectByOid(floid, null, (short) -1);
164             throw new IdentityException("the given oid " + oid + " doesn't exist in this Jalisto datastore");
165         }
166
167         lockManager.prepareReadObjectByOid(floid);
168
169         DataWrapper result = null;
170         short update;
171         boolean readedFromCache = false;
172
173         if (withCache && cache.containsKey(floid)) {
174             result = (DataWrapper) cache.get(floid);
175             if (result != null) {
176                 readedFromCache = true;
177             }
178         }
179         // create or update not commited for this session
180
if (lockManager.containsLockedValue(floid, sessionId)) {
181             if (!readedFromCache) {
182                 result = lockManager.getLockedValue(floid, sessionId);
183             }
184             update = lockManager.getUpdate(floid, sessionId);
185         // object not modified by current session, read it in base
186
} else {
187             try {
188                 if (!readedFromCache) {
189                     PhysicalOid fpoid = oidTable.getFpoid(sessionId, floid);
190                     if (fpoid != null) {
191                         result = fileAccess.readDatas(sessionId, fpoid);
192                     }
193                 }
194                 update = oidTable.getUpdate(sessionId, floid, true);
195             } catch (IdentityException jalistoIe) {
196                 lockManager.finishReadObjectByOid(floid, null, (short) -1);
197                 throw new IdentityException("the given oid " + oid + " doesn't exist in this Jalisto datastore", jalistoIe);
198             }
199         }
200
201         if (result == null) {
202             lockManager.finishReadObjectByOid(floid, null, (short) -1);
203             throw new IdentityException("the given oid " + oid + " doesn't exist in this Jalisto datastore");
204         }
205
206         if (withCache && readedFromCache) {
207             cache.put(floid, result);
208         }
209
210         lockManager.finishReadObjectByOid(floid, result, update);
211         return result;
212     }
213
214     public Object JavaDoc[] refreshObjectByOid(Object JavaDoc oid) {
215         trace.println(Trace.SESSION, "{0} refreshObjectByOid({1})", sessionId, oid);
216         checkValidity("refreshObjectByOid", true);
217         LogicalOid floid = getFloidFromOid(oid);
218
219         if (!lockManager.containsLockedValue(floid, sessionId)) {
220             return readObjectByOidInDataObject(oid, true).getDatas();
221         }
222
223         // delete not commited for this session
224
if (lockManager.containsDeletedValue(floid, sessionId)) {
225             throw new IdentityException("the given oid " + oid + " doesn't exist in this Jalisto datastore");
226         }
227
228         DataWrapper result = null;
229         short update = lockManager.getUpdate(floid, sessionId);
230         try {
231             PhysicalOid fpoid = oidTable.getFpoid(sessionId, floid);
232             if (fpoid != null) {
233                 result = fileAccess.readDatas(sessionId, fpoid);
234                 update = oidTable.getUpdate(sessionId, floid, false);
235                 oidTable.setUpdate(sessionId, floid, update);
236             }
237         } catch (IdentityException jalistoIe) {
238             throw new IdentityException("the given oid " + oid + " doesn't exist in this Jalisto datastore", jalistoIe);
239         }
240         if (result == null) {
241             throw new IdentityException("the given oid " + oid + " doesn't exist in this Jalisto datastore");
242         }
243
244         lockManager.setLockedValue(floid, sessionId, result, update);
245         cache.put(floid, result);
246
247         return result.getDatas();
248     }
249
250     public Object JavaDoc updateObjectByOid(Object JavaDoc oid, Object JavaDoc[] objectToUpdate) {
251         trace.println(Trace.SESSION, "{0} updateObjectByOid({1})", sessionId, oid);
252         checkValidity("updateObjectByOid", true);
253         checkSerialisable(objectToUpdate);
254         LogicalOid floid = getFloidFromOid(oid);
255         short update;
256         DataWrapper datas;
257         if (lockManager.containsLockedValue(floid, sessionId)) {
258             datas = lockManager.getLockedValue(floid, sessionId);
259             update = lockManager.getUpdate(floid, sessionId);
260         } else {
261             datas = JalistoFactory.getInternalFactory().getNewDataWrapper(props, objectToUpdate);
262             update = oidTable.getUpdate(sessionId, floid, true);
263         }
264         datas.setDatas(objectToUpdate);
265         cache.put(floid, datas);
266         lockManager.finishUpdateObjectByOid(floid, datas, update);
267         return floid.getClone();
268     }
269
270     public void updateObjectCommit(LogicalOid floid) {
271         trace.println(Trace.SESSION, "{0} updateObjectCommit({1})", sessionId, floid);
272         boolean findInCache = true;
273         DataWrapper datas = (DataWrapper) cache.get(floid);
274         if (datas == null) {
275             findInCache = false;
276             datas = lockManager.getLockedValue(floid, sessionId);
277         }
278         PhysicalOid fpoid = oidTable.getFpoid(sessionId, floid);
279         if (fpoid != null) {
280             PhysicalOid updatedFpoid = fileAccess.updateDatas(sessionId, fpoid, datas);
281             if (!updatedFpoid.equals(fpoid)) {
282                 oidTable.updatePoid(sessionId, floid, updatedFpoid);
283             }
284         } else {
285             throw new IdentityException("the given oid " + floid + " doesn't exist in this Jalisto datastore");
286         }
287         if (!findInCache) {
288             cache.put(floid, datas);
289         }
290     }
291
292     public void updateObjectRollback(LogicalOid floid) {
293         trace.println(Trace.SESSION, "{0} updateObjectRollback({1})", sessionId, floid);
294         if (cache.containsKey(floid)) {
295             cache.remove(floid);
296         }
297     }
298
299     public void deleteObjectByOid(Object JavaDoc oid) {
300         trace.println(Trace.SESSION, "{0} deleteObjectByOid({1})", sessionId, oid);
301         checkValidity("deleteObjectByOid", true);
302         LogicalOid floid = getFloidFromOid(oid);
303
304         short update;
305         if (lockManager.containsLockedValue(floid, sessionId)) {
306             update = lockManager.getUpdate(floid, sessionId);
307         } else {
308             update = oidTable.getUpdate(sessionId, floid, true);
309         }
310         if (update == -1) {
311             throw new IdentityException("the given oid " + floid + " doesn't exist in this Jalisto datastore");
312         }
313
314         lockManager.prepareDeleteObjectByOid(floid, update);
315
316         if (cache.containsKey(floid)) {
317             cache.remove(floid);
318         }
319         oidTable.removeFloid(sessionId, floid);
320         lockManager.finishDeleteObjectByOid(floid);
321     }
322
323     public void deleteObjectCommit(LogicalOid floid) {
324         trace.println(Trace.SESSION, "{0} deleteObjectCommit({1})", sessionId, floid);
325         PhysicalOid fpoid = oidTable.getDeletedFpoid(sessionId, floid);
326         if (fpoid != null) {
327             fileAccess.deleteDatas(sessionId, fpoid);
328         }
329     }
330
331     public void deleteObjectRollback(LogicalOid floid) {
332         // nothing to do here
333
}
334
335
336     public void begin() {
337         trace.println(Trace.SESSION, "{0} begin()", sessionId);
338         checkIsOpen("begin");
339         cache.clear();
340         lockManager.begin();
341         fileAccess.begin(sessionId);
342     }
343
344     public void commit() {
345         trace.println(Trace.SESSION, "{0} commit()", sessionId);
346         checkIsOpen("commit");
347         lockManager.commit();
348     }
349
350     public void rollback() {
351         trace.println(Trace.SESSION, "{0} rollback()", sessionId);
352         checkIsOpen("rollback");
353         lockManager.rollback();
354     }
355
356     public LockManager getLockManager() {
357         return lockManager;
358     }
359
360     public AccessController getAccessController() {
361         return accessController;
362     }
363
364     public void setOptimistic() {
365         trace.println(Trace.SESSION, "{0} setOptimistic()", sessionId);
366         if (isOpen) {
367             throw new TransactionException("Session must not be open during setOptimistic");
368         }
369         if (transaction.isActive()) {
370             throw new TransactionException("Transaction must not be active during setOptimistic");
371         }
372         lockManager = new LockManagerOptimistic();
373         lockManager.setLockTable(lockTable);
374         lockManager.setSession(this);
375     }
376
377     public void setPessimistic() {
378         trace.println(Trace.SESSION, "{0} setPessimistic()", sessionId);
379         if (isOpen) {
380             throw new TransactionException("Session must not be open during setPessimistic");
381         }
382         if (transaction.isActive()) {
383             throw new TransactionException("Transaction must not be active during setPessimistic");
384         }
385         lockManager = new LockManagerPessimistic();
386         lockManager.setLockTable(lockTable);
387         lockManager.setSession(this);
388     }
389
390
391     public void goToBed() {
392         trace.println(Trace.SESSION, "{0} goToBed()", sessionId);
393         sessionIsSleeping = true;
394     }
395
396     public void inspectLocks() {
397         lockTable.publishLocks();
398     }
399
400
401     public void sleep(LogicalOid floid) {
402         sleep(floid, "");
403     }
404
405     public void sleep(Object JavaDoc floid, String JavaDoc message) {
406         trace.println(Trace.SESSION, "{0} sleeps()", sessionId);
407         trace.println(Trace.DEBUG, "{0} sleeps()", sessionId);
408         if (sessionIsSleeping) {
409             int i = 0;
410             while (sessionIsSleeping && (i < timeOut)) {
411                 Thread.yield();
412                 i++;
413             }
414             if (i == timeOut) {
415                 sessionIsSleeping = false;
416                 lockTable.getLock(floid).resolveTimeOut(sessionId);
417             }
418         }
419         trace.println(Trace.DEBUG, "{0} awakes", sessionId);
420     }
421
422     public void wakeUp() {
423         trace.println(Trace.SESSION, "{0} wakeUp()", sessionId);
424         sessionIsSleeping = false;
425     }
426
427
428     private LockManager lockManager;
429     private LockTable lockTable;
430     private AccessController accessController;
431     private volatile boolean sessionIsSleeping = false;
432     private int timeOut;
433 }
434
Popular Tags