1 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 ; 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 sessionId) { 42 return lockTable.getLockedValue(floid, sessionId); 43 } 44 45 public void setLockedValue(LogicalOid floid, Object sessionId, DataWrapper value, short update) { 46 lockTable.getLock(floid).updateTransactionnalValue(sessionId, value, update); 47 } 48 49 public short getUpdate(LogicalOid floid, Object 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 floid, int action) { 60 boolean isLockTaken = false; 61 boolean isAsleeping = false; 62 Lock lock = null; 63 while (!isLockTaken) { 64 synchronized (lockTable) { 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 90 91 public void begin() { 92 session.getOidTable().begin(sessionId); 93 } 94 95 public void checkLocksForSession(Collection floids, Object sessionId) { 96 lockTable.checkLocksForSession(floids, sessionId); 97 } 98 99 public boolean containsDeletedValue(LogicalOid floid, Object sessionId) { 100 return lockTable.containsDeletedValue(floid, sessionId); 101 } 102 103 public boolean containsLockedValue(LogicalOid floid, Object 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 sessionId; 136 137 138 protected static final int READ = 0; 139 protected static final int WRITE = 1; 140 } 141 | Popular Tags |