KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > jofti > locking > LockManager


1 /*
2  * Created on 19-Apr-2004
3  *
4  */

5 package com.jofti.locking;
6
7 import com.jofti.btree.Node;
8 import com.jofti.exception.JoftiException;
9
10 import com.jofti.oswego.concurrent.ReadWriteLock;;
11
12 /**
13  * Provides the locking and unlocking functions for the BTree nodes.<p>
14  *
15  * @author Steve Woodcock
16  *
17  */

18 public class LockManager {
19     
20     public static final int WRITE_LOCK =1;
21     public static final int READ_LOCK =2;
22     
23
24     
25     private LockManager(){
26         // not implemented as is a static helper
27
}
28     
29     public static void acquireLock(Node node, int lockType) throws JoftiException{
30         if (node != null){
31             try {
32                 
33                 if (lockType == WRITE_LOCK){
34                     node.nodeLock.writeLock().acquire();
35                 }else{
36                     node.nodeLock.readLock().acquire();
37                 }
38             } catch (InterruptedException JavaDoc ie){
39                 throw new JoftiException("Unable to acquire node lock on node "+node);
40             }
41         }
42     }
43     
44     public static boolean attemptLock(Node node, int lockType, long time) throws JoftiException{
45         if (node != null){
46             try {
47                 if (lockType == WRITE_LOCK){
48                     return node.nodeLock.writeLock().attempt(time);
49                 }else{
50                     return node.nodeLock.readLock().attempt(time);
51                 }
52             } catch (InterruptedException JavaDoc ie){
53                 throw new JoftiException("Unable to acquire node lock on node "+node);
54             }
55         }
56         return false;
57     }
58     
59     public static void acquireRWLock(ReadWriteLock lock, int lockType) throws JoftiException{
60         if (lock != null){
61             try {
62                 if (lockType == WRITE_LOCK){
63                     lock.writeLock().acquire();
64                 }else{
65                     lock.readLock().acquire();
66                 }
67             } catch (InterruptedException JavaDoc ie){
68                 throw new JoftiException("Unable to acquire RW lock on lock " + lock);
69             }
70         }
71     }
72     
73     public static void releaseRWLock(ReadWriteLock lock, int lockType){
74         if (lockType == WRITE_LOCK){
75             lock.writeLock().release();
76         }else{
77             lock.readLock().release();
78         }
79     }
80     
81     public static void releaseLock(Node node, int lockType){
82         if (lockType == WRITE_LOCK){
83             node.nodeLock.writeLock().release();
84         }else{
85             node.nodeLock.readLock().release();
86         }
87     }
88     
89     
90     
91    
92     
93     
94 }
95
Popular Tags