KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > ojb > otm > lock > LockManager


1 package org.apache.ojb.otm.lock;
2
3 /* Copyright 2003-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 import org.apache.ojb.broker.Identity;
19 import org.apache.ojb.broker.PersistenceBroker;
20 import org.apache.ojb.otm.core.Transaction;
21 import org.apache.ojb.otm.lock.isolation.TransactionIsolation;
22 import org.apache.ojb.otm.lock.map.LockMap;
23
24 /**
25  *
26  * Manages locks on objects across transactions.
27  *
28  * @author <a HREF="mailto:rraghuram@hotmail.com">Raghu Rajah</a>
29  *
30  */

31 public class LockManager
32 {
33     private static LockManager _Instance = new LockManager();
34
35     public static LockManager getInstance ()
36     {
37         return _Instance;
38     }
39
40     private LockManager()
41     {
42
43     }
44
45     public void ensureLock(Identity oid, Transaction tx, int lock,
46                            PersistenceBroker pb)
47         throws LockingException
48     {
49         LockMap lockMap = tx.getKit().getLockMap();
50         ObjectLock objectLock = lockMap.getLock(oid);
51         TransactionIsolation isolation;
52
53         isolation = IsolationFactory.getIsolationLevel(pb, objectLock);
54
55         if (lock == LockType.READ_LOCK)
56         {
57             isolation.readLock(tx, objectLock);
58         }
59         else if (lock == LockType.WRITE_LOCK)
60         {
61             isolation.writeLock(tx, objectLock);
62         }
63     }
64
65     public int getLockHeld(Identity oid, Transaction tx)
66     {
67         LockMap lockMap = tx.getKit().getLockMap();
68         ObjectLock lock = lockMap.getLock(oid);
69
70         int lockHeld = LockType.NO_LOCK;
71         if (tx.equals(lock.getWriter()))
72         {
73             lockHeld = LockType.WRITE_LOCK;
74         }
75         else if (lock.isReader(tx))
76         {
77             lockHeld = LockType.READ_LOCK;
78         }
79
80         return lockHeld;
81     }
82
83     public void releaseLock(Identity oid, Transaction tx)
84     {
85         LockMap lockMap = tx.getKit().getLockMap();
86
87         ObjectLock objectLock = lockMap.getLock(oid);
88         objectLock.releaseLock(tx);
89     }
90 }
91
Popular Tags