Coverage Report - org.apache.ojb.odmg.locking.SerializableStrategy
 
Classes in this File Line Coverage Branch Coverage Complexity
SerializableStrategy
N/A
N/A
8.5
 
 1  
 package org.apache.ojb.odmg.locking;
 2  
 
 3  
 /* Copyright 2002-2005 The Apache Software Foundation
 4  
  *
 5  
  * Licensed under the Apache License, Version 2.0 (the "License");
 6  
  * you may not use this file except in compliance with the License.
 7  
  * You may obtain a copy of the License at
 8  
  *
 9  
  *     http://www.apache.org/licenses/LICENSE-2.0
 10  
  *
 11  
  * Unless required by applicable law or agreed to in writing, software
 12  
  * distributed under the License is distributed on an "AS IS" BASIS,
 13  
  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 14  
  * See the License for the specific language governing permissions and
 15  
  * limitations under the License.
 16  
  */
 17  
 
 18  
 
 19  
 import org.apache.ojb.odmg.TransactionImpl;
 20  
 
 21  
 import java.util.Collection;
 22  
 
 23  
 /**
 24  
  * The implementation of the Serializable Locking strategy.
 25  
  *
 26  
  * @author Thomas Mahler & David Dixon-Peugh
 27  
  */
 28  
 public class SerializableStrategy extends AbstractLockStrategy
 29  
 {
 30  
     /**
 31  
      * acquire a read lock on Object obj for Transaction tx.
 32  
      * @param tx the transaction requesting the lock
 33  
      * @param obj the Object to be locked
 34  
      * @return true if successful, else false
 35  
      *
 36  
      */
 37  
     public boolean readLock(TransactionImpl tx, Object obj)
 38  
     {
 39  
         LockEntry writer = getWriter(obj);
 40  
         Collection readers = getReaders(obj);
 41  
         if (writer == null)
 42  
         {
 43  
             // only one reader at a time
 44  
             if (readers.size() == 0)
 45  
             {
 46  
                 if (addReader(tx, obj))
 47  
                 {
 48  
                     readers = getReaders(obj);
 49  
                     if (readers.size() == 1)
 50  
                     {
 51  
                         return true;
 52  
                     }
 53  
                     else
 54  
                     {
 55  
                         removeReader(tx, obj);
 56  
                         return readLock(tx, obj);
 57  
                     }
 58  
                 }
 59  
                 else
 60  
                     return readLock(tx, obj);
 61  
             }
 62  
             else if ((readers.size() == 1) && (((LockEntry) readers.iterator().next()).isOwnedBy(tx)))
 63  
             {
 64  
                 // I'm the reader, thus I am allowed to read even more !
 65  
                 return true;
 66  
             }
 67  
         }
 68  
         else if (writer.isOwnedBy(tx))
 69  
         {
 70  
             return true;    // If I'm the writer, I can read.
 71  
         }
 72  
 
 73  
         return false;
 74  
     }
 75  
 
 76  
     /**
 77  
      * acquire a write lock on Object obj for Transaction tx.
 78  
      * @param tx the transaction requesting the lock
 79  
      * @param obj the Object to be locked
 80  
      * @return true if successful, else false
 81  
      *
 82  
      */
 83  
     public boolean writeLock(TransactionImpl tx, Object obj)
 84  
     {
 85  
         LockEntry writer = getWriter(obj);
 86  
         Collection readers = getReaders(obj);
 87  
         if (writer == null)
 88  
         {
 89  
             if (readers.size() == 0)
 90  
             {
 91  
                 if (setWriter(tx, obj))
 92  
                     return true;
 93  
                 else
 94  
                     return writeLock(tx, obj);
 95  
             }
 96  
 
 97  
             else if (readers.size() == 1)
 98  
             {
 99  
                 if (((LockEntry) readers.iterator().next()).isOwnedBy(tx))
 100  
                     return upgradeLock(tx, obj);
 101  
             }
 102  
         }
 103  
         else if (writer.isOwnedBy(tx))
 104  
         {
 105  
             return true;    // If I'm the writer, then I can write.
 106  
         }
 107  
         return false;
 108  
     }
 109  
 
 110  
     /**
 111  
      * acquire a lock upgrade (from read to write) lock on Object obj for Transaction tx.
 112  
      * @param tx the transaction requesting the lock
 113  
      * @param obj the Object to be locked
 114  
      * @return true if successful, else false
 115  
      *
 116  
      */
 117  
     public boolean upgradeLock(TransactionImpl tx, Object obj)
 118  
     {
 119  
         LockEntry writer = getWriter(obj);
 120  
         if (writer == null)
 121  
         {
 122  
             Collection readers = getReaders(obj);
 123  
             if (readers.size() == 1)
 124  
             {
 125  
                 LockEntry reader = (LockEntry) readers.iterator().next();
 126  
                 if (reader.isOwnedBy(tx))
 127  
                 {
 128  
                     if (upgradeLock(reader))
 129  
                         return true;
 130  
                     else
 131  
                         return upgradeLock(tx, obj);
 132  
                 }
 133  
             }
 134  
             else
 135  
             {
 136  
                 if (readers.size() == 0)
 137  
                 {
 138  
                     if (setWriter(tx, obj))
 139  
                         return true;
 140  
                     else
 141  
                         return upgradeLock(tx, obj);
 142  
                 }
 143  
             }
 144  
         }
 145  
         else if (writer.isOwnedBy(tx))
 146  
         {
 147  
             return true;    // If I already have Write, then I've upgraded.
 148  
         }
 149  
 
 150  
         return false;
 151  
     }
 152  
 
 153  
     /**
 154  
      * release a lock on Object obj for Transaction tx.
 155  
      * @param tx the transaction releasing the lock
 156  
      * @param obj the Object to be unlocked
 157  
      * @return true if successful, else false
 158  
      *
 159  
      */
 160  
     public boolean releaseLock(TransactionImpl tx, Object obj)
 161  
     {
 162  
         LockEntry writer = getWriter(obj);
 163  
         if (writer != null && writer.isOwnedBy(tx))
 164  
         {
 165  
             removeWriter(writer);
 166  
             return true;
 167  
         }
 168  
 
 169  
         if (hasReadLock(tx, obj))
 170  
         {
 171  
             removeReader(tx, obj);
 172  
             return true;
 173  
         }
 174  
         else
 175  
             return false;
 176  
 
 177  
     }
 178  
 
 179  
     /**
 180  
      * checks whether the specified Object obj is read-locked by Transaction tx.
 181  
      * @param tx the transaction
 182  
      * @param obj the Object to be checked
 183  
      * @return true if lock exists, else false
 184  
      */
 185  
     public boolean checkRead(TransactionImpl tx, Object obj)
 186  
     {
 187  
         if (hasReadLock(tx, obj))
 188  
         {
 189  
             return true;
 190  
         }
 191  
         LockEntry writer = getWriter(obj);
 192  
         if (writer != null && writer.isOwnedBy(tx))
 193  
         {
 194  
             return true;
 195  
         }
 196  
         else
 197  
             return false;
 198  
     }
 199  
 
 200  
     /**
 201  
      * checks whether the specified Object obj is write-locked by Transaction tx.
 202  
      * @param tx the transaction
 203  
      * @param obj the Object to be checked
 204  
      * @return true if lock exists, else false
 205  
      */
 206  
     public boolean checkWrite(TransactionImpl tx, Object obj)
 207  
     {
 208  
         LockEntry writer = getWriter(obj);
 209  
         return (writer != null && writer.isOwnedBy(tx));
 210  
     }
 211  
 }