KickJava   Java API By Example, From Geeks To Geeks.

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


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

28
29 package org.apache.commons.transaction.locking;
30
31 import org.apache.commons.transaction.util.LoggerFacade;
32
33 /**
34  * Manager for
35  * {@link org.apache.commons.transaction.locking.ReadWriteUpgradeLock}s on
36  * resources. <br>
37  * <br>
38  * <p>
39  * The idea (as explained by Jim LoVerde) is that only one owner can hold an
40  * upgrade lock, but while that is held, it is possible for read locks to exist
41  * and/or be obtained, and when the request is made to upgrade to a write lock
42  * by the same owner, the lock manager prevents additional read locks until the
43  * write lock can be aquired.
44  * </p>
45  * <p>
46  * In this sense the write lock becomes preferred over all other locks when it gets upgraded from
47  * a upgrate lock. Preferred means that if it has to wait and others wait as well it will be
48  * served before all other none preferred locking requests.
49  * </p>
50  *
51  * @version $Revision$
52  *
53  * @see ReadWriteUpgradeLock
54  * @since 1.1
55  */

56 public class ReadWriteUpgradeLockManager extends ReadWriteLockManager {
57
58     /**
59      * Creates a new read/write/upgrade lock manager.
60      *
61      * @param logger generic logger used for all kind of debug logging
62      * @param timeoutMSecs specifies the maximum time to wait for a lock in milliseconds
63      */

64     public ReadWriteUpgradeLockManager(LoggerFacade logger, long timeoutMSecs) {
65         super(ReadWriteUpgradeLock.WRITE_LOCK, logger, timeoutMSecs);
66     }
67
68     /**
69      * Tries to acquire a reentrant upgrade 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 tryUpgradeLock(Object JavaDoc ownerId, Object JavaDoc resourceId) {
82         return tryLock(ownerId, resourceId, ReadWriteUpgradeLock.UPGRADE_LOCK, true);
83     }
84
85     /**
86      * Tries to acquire an exclusive, reentrant write lock on a resource. <br>
87      * <br>
88      * This method does not block, but immediatly returns. If a lock is not
89      * available <code>false</code> will be returned.
90      *
91      * @param ownerId
92      * a unique id identifying the entity that wants to acquire this
93      * lock
94      * @param resourceId
95      * the resource to get the lock for
96      * @return <code>true</code> if the lock has been acquired, <code>false</code> otherwise
97      */

98     public boolean tryWriteLock(Object JavaDoc ownerId, Object JavaDoc resourceId) {
99         return tryLock(ownerId, resourceId, ReadWriteUpgradeLock.WRITE_LOCK, true);
100     }
101
102     /**
103      * Tries to acquire a reentrant upgrade lock on a resource. <br>
104      * <br>
105      * This method blocks and waits for the lock in case it is not avaiable. If
106      * there is a timeout or a deadlock or the thread is interrupted a
107      * LockException is thrown.
108      *
109      * @param ownerId
110      * a unique id identifying the entity that wants to acquire this
111      * lock
112      * @param resourceId
113      * the resource to get the level for
114      * @throws LockException
115      * will be thrown when the lock can not be acquired
116      */

117     public void upgradeLock(Object JavaDoc ownerId, Object JavaDoc resourceId) throws LockException {
118         super.lock(ownerId, resourceId, ReadWriteUpgradeLock.UPGRADE_LOCK, true);
119     }
120
121     /**
122      * Tries to acquire an exclusive, reentrant write lock on a resource. <br>
123      * <br>
124      * This method blocks and waits for the lock in case it is not avaiable. If
125      * there is a timeout or a deadlock or the thread is interrupted a
126      * LockException is thrown.
127      *
128      * @param ownerId
129      * a unique id identifying the entity that wants to acquire this
130      * lock
131      * @param resourceId
132      * the resource to get the level for
133      * @throws LockException
134      * will be thrown when the lock can not be acquired
135      */

136     public void writeLock(Object JavaDoc ownerId, Object JavaDoc resourceId) throws LockException {
137         super.lock(ownerId, resourceId, ReadWriteUpgradeLock.WRITE_LOCK, true);
138     }
139
140     protected GenericLock createLock(Object JavaDoc resourceId) {
141         synchronized (globalLocks) {
142             GenericLock lock = new ReadWriteUpgradeLock(resourceId, logger);
143             globalLocks.put(resourceId, lock);
144             return lock;
145         }
146     }
147
148 }
Popular Tags