KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > oracle > toplink > essentials > internal > helper > DeferredLockManager


1 /*
2  * The contents of this file are subject to the terms
3  * of the Common Development and Distribution License
4  * (the "License"). You may not use this file except
5  * in compliance with the License.
6  *
7  * You can obtain a copy of the license at
8  * glassfish/bootstrap/legal/CDDLv1.0.txt or
9  * https://glassfish.dev.java.net/public/CDDLv1.0.html.
10  * See the License for the specific language governing
11  * permissions and limitations under the License.
12  *
13  * When distributing Covered Code, include this CDDL
14  * HEADER in each file and include the License file at
15  * glassfish/bootstrap/legal/CDDLv1.0.txt. If applicable,
16  * add the following below this CDDL HEADER, with the
17  * fields enclosed by brackets "[]" replaced with your
18  * own identifying information: Portions Copyright [yyyy]
19  * [name of copyright owner]
20  */

21 // Copyright (c) 1998, 2005, Oracle. All rights reserved.
22
package oracle.toplink.essentials.internal.helper;
23
24 import java.util.*;
25
26 /**
27  * INTERNAL:
28  * <p>
29  * <b>Purpose</b>: Be used for deadlock avoidance through allowing detection and resolution.
30  *
31  * <p>
32  * <b>Responsibilities</b>:
33  * <ul>
34  * <li> Keep track of all deferred locks of each thread.
35  * <li> Keep track of all active locks of each thread..
36  * <li> Maintain the depth of the each thread.
37  * </ul>
38  */

39 public class DeferredLockManager {
40     protected Vector deferredLocks;
41     protected Vector activeLocks;
42     protected int threadDepth;
43     protected boolean isThreadComplete;
44     
45     public static boolean SHOULD_USE_DEFERRED_LOCKS = true;
46
47     /**
48      * DeferredLockManager constructor comment.
49      */

50     public DeferredLockManager() {
51         super();
52         this.deferredLocks = new Vector(1);
53         this.activeLocks = new Vector(1);
54         this.threadDepth = 0;
55         this.isThreadComplete = false;
56     }
57
58     /**
59      * add a concurrency manager as active locks to the DLM
60      */

61     public void addActiveLock(Object JavaDoc manager) {
62         getActiveLocks().addElement(manager);
63     }
64
65     /**
66      * add a concurrency manager as deferred locks to the DLM
67      */

68     public void addDeferredLock(Object JavaDoc manager) {
69         getDeferredLocks().addElement(manager);
70     }
71
72     /**
73      * decrement the depth of the thread
74      */

75     public void decrementDepth() {
76         threadDepth--;
77     }
78
79     /**
80      * Return a set of the active locks from the DLM
81      */

82     public Vector getActiveLocks() {
83         return activeLocks;
84     }
85
86     /**
87      * Return a set of the deferred locks
88      */

89     public Vector getDeferredLocks() {
90         return deferredLocks;
91     }
92
93     /**
94      * Return the depth of the thread associated with the DLM, being used to release the lock
95      */

96     public int getThreadDepth() {
97         return threadDepth;
98     }
99
100     /**
101      * Return if there are any deferred locks.
102      */

103     public boolean hasDeferredLock() {
104         return !getDeferredLocks().isEmpty();
105     }
106
107     /**
108      * increment the depth of the thread
109      */

110     public void incrementDepth() {
111         threadDepth++;
112     }
113
114     /**
115      * Return if the thread is complete
116      */

117     public boolean isThreadComplete() {
118         return isThreadComplete;
119     }
120
121     /**
122      * Release the active lock on the DLM
123      */

124     public void releaseActiveLocksOnThread() {
125         Vector activeLocks = getActiveLocks();
126         if (!activeLocks.isEmpty()) {
127             for (Enumeration activeLocksEnum = activeLocks.elements();
128                      activeLocksEnum.hasMoreElements();) {
129                 ConcurrencyManager manager = (ConcurrencyManager)activeLocksEnum.nextElement();
130                 manager.release();
131             }
132         }
133         setIsThreadComplete(true);
134     }
135
136     /**
137      * set a set of the active locks to the DLM
138      */

139     public void setActiveLocks(Vector activeLocks) {
140         this.activeLocks = activeLocks;
141     }
142
143     /**
144      * set a set of the deferred locks to the DLM
145      */

146     public void setDeferredLocks(Vector deferredLocks) {
147         this.deferredLocks = deferredLocks;
148     }
149
150     /**
151      * set if the thread is complete in the given DLM
152      */

153     public void setIsThreadComplete(boolean isThreadComplete) {
154         this.isThreadComplete = isThreadComplete;
155     }
156 }
157
Popular Tags