KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > catalina > session > SessionLock


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 2005 Sun Microsystems, Inc. All rights reserved.
22  */
/*
23  * SessionLock.java
24  *
25  * Created on January 21, 2003, 4:34 PM
26  * HERCULES:add
27  */

28
29 package org.apache.catalina.session;
30
31
32 public class SessionLock {
33     
34     private static final String JavaDoc BACKGROUND_LOCK = "background_lock";
35     private static final String JavaDoc FOREGROUND_LOCK = "foreground_lock";
36     
37     /** Creates a new instance of SessionLock */
38     public SessionLock() {
39     }
40
41     /**
42      * get the lock type
43      *
44      */

45     public String JavaDoc getLockType() {
46         return _lockType;
47     }
48
49     /**
50      * set the lock type - lockType must be BACKGROUND_LOCK or FOREGROUND_LOCK
51      *
52      * @param lockType the type of the lock
53      */

54     public void setLockType(String JavaDoc lockType) {
55         _lockType = lockType;
56     }
57     
58     /**
59      * get the foregroundRefCount
60      *
61      */

62     public int getForegroundRefCount() {
63         return _foregroundRefCount;
64     }
65     
66     /**
67      * set the foregroundRefCount
68      *
69      * @param foregroundRefCount
70      */

71     public void setForegroundRefCount(int foregroundRefCount) {
72         _foregroundRefCount = foregroundRefCount;
73     }
74     
75     /**
76      * increment the foregroundRefCount
77      *
78      */

79     public void incrementForegroundRefCount() {
80         _foregroundRefCount++;
81     }
82     
83     /**
84      * decrement the foregroundRefCount
85      *
86      */

87     public void decrementForegroundRefCount() {
88         _foregroundRefCount--;
89     }
90     
91     /**
92      * return whether lock is background locked
93      *
94      */

95     public boolean isBackgroundLocked() {
96         if(_lockType == null) {
97             return false;
98         }
99         return (_lockType.equals(BACKGROUND_LOCK));
100     }
101     
102     /**
103      * return whether lock is foreground locked
104      *
105      */

106     public boolean isForegroundLocked() {
107         if(_lockType == null) {
108             return false;
109         }
110         return (_lockType.equals(FOREGROUND_LOCK));
111     }
112     
113     /**
114      * return whether lock is locked (either foreground or background)
115      *
116      */

117     public boolean isLocked() {
118         return (_lockType != null);
119     }
120    
121     /**
122      * unlock the lock
123      * if background locked the lock will become fully unlocked
124      * if foreground locked the lock will become fully unlocked
125      * if foregroundRefCount was 1; otherwise it will
126      * decrement the foregroundRefCount and the lock will remain foreground locked
127      *
128      */

129     public void unlock() {
130         if(!isLocked())
131             return;
132         if(isBackgroundLocked()) {
133             this.setLockType(null);
134             this.setForegroundRefCount(0);
135             return;
136         }
137         if(isForegroundLocked()) {
138             decrementForegroundRefCount();
139             if(_foregroundRefCount == 0) {
140                 this.setLockType(null);
141             }
142         }
143     }
144     
145     /**
146      * unlock the lock for the foreground locked case
147      * the lock will be unlocked
148      * if foregroundRefCount was 1; otherwise it will
149      * decrement the foregroundRefCount and the lock will remain foreground locked
150      *
151      */

152     public void unlockForeground() {
153         //unlock if the lock is foreground locked
154
//else do nothing
155
if(!isLocked())
156             return;
157         if(isForegroundLocked()) {
158             decrementForegroundRefCount();
159             if(_foregroundRefCount == 0) {
160                 this.setLockType(null);
161             }
162         }
163     }
164     
165     /**
166      * unlock the lock
167      * this is a force unlock; foregroundRefCount is ignored
168      *
169      */

170     public void unlockForegroundCompletely() {
171         //unlock completely if the lock is foreground locked
172
//else do nothing
173
if(!isLocked())
174             return;
175         if(isForegroundLocked()) {
176             this.setForegroundRefCount(0);
177             this.setLockType(null);
178         }
179     }
180     
181     /**
182      * unlock the lock for the background locked case
183      * the lock will be unlocked
184      *
185      */

186     public void unlockBackground() {
187         //unlock if the lock is background locked
188
//else do nothing
189
if(!isLocked())
190             return;
191         if(isBackgroundLocked()) {
192             this.setLockType(null);
193             this.setForegroundRefCount(0);
194             return;
195         }
196     }
197     
198     /**
199      * if possible, the lock will be foreground locked
200      * if it was already foreground locked; it will
201      * remain so and the foregroundRefCount will be incremented
202      *
203      * if the lock is already background locked the method
204      * will return false and the lock remains background locked
205      * (i.e. lock failed) otherwise it will return true (lock succeeded)
206      */

207     public synchronized boolean lockForeground() {
208         if(isBackgroundLocked()) {
209             return false;
210         }
211         if(isForegroundLocked()) {
212             incrementForegroundRefCount();
213         } else {
214             setForegroundRefCount(1);
215         }
216         setLockType(FOREGROUND_LOCK);
217         return true;
218     }
219     
220     /**
221      * if possible, the lock will be background locked
222      *
223      * if the lock is already foreground locked the method
224      * will return false and the lock remains foreground locked
225      * (i.e. lock failed) otherwise it will return true (lock succeeded)
226      */

227     public synchronized boolean lockBackground() {
228         if(isForegroundLocked()) {
229             return false;
230         }
231         setLockType(BACKGROUND_LOCK);
232         setForegroundRefCount(0);
233         return true;
234     }
235     
236     /**
237      * returns String representation of the state of the lock
238      */

239     public String JavaDoc toString() {
240         StringBuffer JavaDoc sb = new StringBuffer JavaDoc(50);
241         sb.append("_lockType= " + _lockType);
242         sb.append("\n" + "foregroundRefCount= " + _foregroundRefCount);
243         return sb.toString();
244     }
245     
246     private String JavaDoc _lockType = null;
247     private int _foregroundRefCount = 0;
248     
249 }
250
Popular Tags