KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > commons > transaction > locking > ReadWriteLockManager


1 /*
2  * $Header: /home/jerenkrantz/tmp/commons/commons-convert/cvs/home/cvs/jakarta-commons//transaction/src/java/org/apache/commons/transaction/locking/ReadWriteLockManager.java,v 1.4 2005/01/09 15:12:11 ozeigermann Exp $
3  * $Revision$
4  * $Date: 2005-02-26 14:16:14 +0100 (Sa, 26 Feb 2005) $
5  *
6  * ====================================================================
7  *
8  * Copyright 1999-2004 The Apache Software Foundation
9  *
10  * Licensed under the Apache License, Version 2.0 (the "License");
11  * you may not use this file except in compliance with the License.
12  * You may obtain a copy of the License at
13  *
14  * http://www.apache.org/licenses/LICENSE-2.0
15  *
16  * Unless required by applicable law or agreed to in writing, software
17  * distributed under the License is distributed on an "AS IS" BASIS,
18  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
19  * See the License for the specific language governing permissions and
20  * limitations under the License.
21  *
22  */

23
24 package org.apache.commons.transaction.locking;
25
26 import org.apache.commons.transaction.util.LoggerFacade;
27
28 /**
29  * Manager for {@link org.apache.commons.transaction.locking.ReadWriteLock}s on resources.
30  *
31  * @version $Revision$
32  * @since 1.1
33  */

34 public class ReadWriteLockManager extends GenericLockManager {
35
36     /**
37      * Creates a new read/write lock manager.
38      *
39      * @param logger generic logger used for all kind of debug logging
40      * @param timeoutMSecs specifies the maximum time to wait for a lock in milliseconds
41      */

42     public ReadWriteLockManager(LoggerFacade logger, long timeoutMSecs) {
43         super(ReadWriteLock.WRITE_LOCK, logger, timeoutMSecs);
44     }
45
46     protected ReadWriteLockManager(int maxLockLevel, LoggerFacade logger, long timeoutMSecs)
47             throws IllegalArgumentException JavaDoc {
48         super(maxLockLevel, logger, timeoutMSecs);
49     }
50
51     /**
52      * Tries to acquire a shared, reentrant read lock on a resource. <br>
53      * <br>
54      * This method does not block, but immediatly returns. If a lock is not
55      * available <code>false</code> will be returned.
56      *
57      * @param ownerId
58      * a unique id identifying the entity that wants to acquire this
59      * lock
60      * @param resourceId
61      * the resource to get the lock for
62      * @return <code>true</code> if the lock has been acquired, <code>false</code> otherwise
63      */

64     public boolean tryReadLock(Object JavaDoc ownerId, Object JavaDoc resourceId) {
65         return tryLock(ownerId, resourceId, ReadWriteLock.READ_LOCK, true);
66     }
67
68     /**
69      * Tries to acquire an exclusive, reentrant write lock on a resource. <br>
70      * <br>
71      * This method does not block, but immediatly returns. If a lock is not
72      * available <code>false</code> will be returned.
73      *
74      * @param ownerId
75      * a unique id identifying the entity that wants to acquire this
76      * lock
77      * @param resourceId
78      * the resource to get the lock for
79      * @return <code>true</code> if the lock has been acquired, <code>false</code> otherwise
80      */

81     public boolean tryWriteLock(Object JavaDoc ownerId, Object JavaDoc resourceId) {
82         return tryLock(ownerId, resourceId, ReadWriteLock.WRITE_LOCK, true);
83     }
84
85     /**
86      * Determines if a shared, reentrant read lock on a resource
87      * <em>could</em> be acquired without actually acquiring it. <br>
88      * <br>
89      * This method does not block, but immediatly returns. If a lock is not
90      * available <code>false</code> will be returned.
91      *
92      * @param ownerId
93      * a unique id identifying the entity that wants to acquire this
94      * lock
95      * @param resourceId
96      * the resource to get the lock for
97      * @return <code>true</code> if the lock could be acquired, <code>false</code> otherwise
98      */

99     public boolean checkReadLock(Object JavaDoc ownerId, Object JavaDoc resourceId) {
100         return checkLock(ownerId, resourceId, ReadWriteLock.READ_LOCK, true);
101     }
102
103     /**
104      * Determines if an exclusive, reentrant write lock on a resource
105      * is held by an owner. <br>
106      *
107      * @param ownerId
108      * a unique id identifying the entity that wants to check this
109      * lock
110      * @param resourceId
111      * the resource to get the lock for
112      * @return <code>true</code> if the lock is held by the owner, <code>false</code> otherwise
113      */

114     public boolean hasWriteLock(Object JavaDoc ownerId, Object JavaDoc resourceId) {
115         return hasLock(ownerId, resourceId, ReadWriteLock.WRITE_LOCK);
116     }
117
118     /**
119      * Determines if a shared, reentrant read lock on a resource
120      * is held by an owner. <br>
121      *
122      * @param ownerId
123      * a unique id identifying the entity that wants to check this
124      * lock
125      * @param resourceId
126      * the resource to get the lock for
127      * @return <code>true</code> if the lock is held by the owner, <code>false</code> otherwise
128      */

129     public boolean hasReadLock(Object JavaDoc ownerId, Object JavaDoc resourceId) {
130         return hasLock(ownerId, resourceId, ReadWriteLock.READ_LOCK);
131     }
132
133     /**
134      * Determines if an exclusive, reentrant write lock on a resource
135      * <em>could</em> be acquired without actually acquiring it. <br>
136      * <br>
137      * This method does not block, but immediatly returns. If a lock is not
138      * available <code>false</code> will be returned.
139      *
140      * @param ownerId
141      * a unique id identifying the entity that wants to acquire this
142      * lock
143      * @param resourceId
144      * the resource to get the lock for
145      * @return <code>true</code> if the lock could be acquired, <code>false</code> otherwise
146      */

147     public boolean checkWriteLock(Object JavaDoc ownerId, Object JavaDoc resourceId) {
148         return checkLock(ownerId, resourceId, ReadWriteLock.WRITE_LOCK, true);
149     }
150
151     /**
152      * Tries to acquire a shared, reentrant read lock on a resource. <br>
153      * <br>
154      * This method blocks and waits for the lock in case it is not avaiable. If
155      * there is a timeout or a deadlock or the thread is interrupted a
156      * LockException is thrown.
157      *
158      * @param ownerId
159      * a unique id identifying the entity that wants to acquire this
160      * lock
161      * @param resourceId
162      * the resource to get the lock for
163      * @throws LockException
164      * will be thrown when the lock can not be acquired
165      */

166     public void readLock(Object JavaDoc ownerId, Object JavaDoc resourceId) throws LockException {
167         lock(ownerId, resourceId, ReadWriteLock.READ_LOCK, GenericLock.COMPATIBILITY_REENTRANT,
168                 false, globalTimeoutMSecs);
169     }
170
171     /**
172      * Tries to acquire an exclusive, reentrant write lock on a resource. <br>
173      * <br>
174      * This method blocks and waits for the lock in case it is not avaiable. If
175      * there is a timeout or a deadlock or the thread is interrupted a
176      * LockException is thrown.
177      *
178      * @param ownerId
179      * a unique id identifying the entity that wants to acquire this
180      * lock
181      * @param resourceId
182      * the resource to get the lock for
183      * @throws LockException
184      * will be thrown when the lock can not be acquired
185      */

186     public void writeLock(Object JavaDoc ownerId, Object JavaDoc resourceId) throws LockException {
187         lock(ownerId, resourceId, ReadWriteLock.WRITE_LOCK, GenericLock.COMPATIBILITY_REENTRANT,
188                 true, globalTimeoutMSecs);
189     }
190
191     protected GenericLock createLock(Object JavaDoc resourceId) {
192         synchronized (globalLocks) {
193             GenericLock lock = new ReadWriteLock(resourceId, logger);
194             globalLocks.put(resourceId, lock);
195             return lock;
196         }
197     }
198
199 }
Popular Tags