001    package org.apache.ojb.otm.lock;
002    
003    /* Copyright 2003-2005 The Apache Software Foundation
004     *
005     * Licensed under the Apache License, Version 2.0 (the "License");
006     * you may not use this file except in compliance with the License.
007     * You may obtain a copy of the License at
008     *
009     *     http://www.apache.org/licenses/LICENSE-2.0
010     *
011     * Unless required by applicable law or agreed to in writing, software
012     * distributed under the License is distributed on an "AS IS" BASIS,
013     * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014     * See the License for the specific language governing permissions and
015     * limitations under the License.
016     */
017    
018    import org.apache.ojb.broker.Identity;
019    import org.apache.ojb.broker.PersistenceBroker;
020    import org.apache.ojb.otm.core.Transaction;
021    import org.apache.ojb.otm.lock.isolation.TransactionIsolation;
022    import org.apache.ojb.otm.lock.map.LockMap;
023    
024    /**
025     *
026     * Manages locks on objects across transactions.
027     *
028     * @author <a href="mailto:rraghuram@hotmail.com">Raghu Rajah</a>
029     *
030     */
031    public class LockManager
032    {
033        private static LockManager _Instance = new LockManager();
034    
035        public static LockManager getInstance ()
036        {
037            return _Instance;
038        }
039    
040        private LockManager()
041        {
042    
043        }
044    
045        public void ensureLock(Identity oid, Transaction tx, int lock,
046                               PersistenceBroker pb)
047            throws LockingException
048        {
049            LockMap lockMap = tx.getKit().getLockMap();
050            ObjectLock objectLock = lockMap.getLock(oid);
051            TransactionIsolation isolation;
052    
053            isolation = IsolationFactory.getIsolationLevel(pb, objectLock);
054    
055            if (lock == LockType.READ_LOCK)
056            {
057                isolation.readLock(tx, objectLock);
058            }
059            else if (lock == LockType.WRITE_LOCK)
060            {
061                isolation.writeLock(tx, objectLock);
062            }
063        }
064    
065        public int getLockHeld(Identity oid, Transaction tx)
066        {
067            LockMap lockMap = tx.getKit().getLockMap();
068            ObjectLock lock = lockMap.getLock(oid);
069    
070            int lockHeld = LockType.NO_LOCK;
071            if (tx.equals(lock.getWriter()))
072            {
073                lockHeld = LockType.WRITE_LOCK;
074            }
075            else if (lock.isReader(tx))
076            {
077                lockHeld = LockType.READ_LOCK;
078            }
079    
080            return lockHeld;
081        }
082    
083        public void releaseLock(Identity oid, Transaction tx)
084        {
085            LockMap lockMap = tx.getKit().getLockMap();
086    
087            ObjectLock objectLock = lockMap.getLock(oid);
088            objectLock.releaseLock(tx);
089        }
090    }