KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > sun > enterprise > web > PESessionLockingStandardPipeline


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 in
5  * compliance with the License.
6  *
7  * You can obtain a copy of the license at
8  * https://glassfish.dev.java.net/public/CDDLv1.0.html or
9  * glassfish/bootstrap/legal/CDDLv1.0.txt.
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 Notice in each file and include the License file
15  * at glassfish/bootstrap/legal/CDDLv1.0.txt.
16  * If applicable, add the following below the CDDL Header,
17  * with the fields enclosed by brackets [] replaced by
18  * you own identifying information:
19  * "Portions Copyrighted [year] [name of copyright owner]"
20  *
21  * Copyright 2006 Sun Microsystems, Inc. All rights reserved.
22  */

23
24 package com.sun.enterprise.web;
25
26 import java.io.IOException JavaDoc;
27 import javax.servlet.ServletException JavaDoc;
28 import javax.servlet.ServletRequest JavaDoc;
29 import javax.servlet.http.HttpServletRequest JavaDoc;
30 import javax.servlet.http.HttpSession JavaDoc;
31
32 import java.util.logging.Logger JavaDoc;
33 import java.util.logging.Level JavaDoc;
34 import com.sun.logging.LogDomains;
35
36 import org.apache.catalina.Container;
37 import org.apache.catalina.Manager;
38 import org.apache.catalina.Request;
39 import org.apache.catalina.Response;
40 import org.apache.catalina.Session;
41 import org.apache.catalina.session.PersistentManagerBase;
42 import org.apache.catalina.session.StandardSession;
43
44 /**
45  *
46  * @author Larry White
47  */

48 public class PESessionLockingStandardPipeline extends WebPipeline {
49     
50     /**
51      * creates an instance of PESessionLockingStandardPipeline
52      * @param container
53      */

54     public PESessionLockingStandardPipeline(Container container) {
55         super(container);
56         if (_logger == null) {
57             _logger = LogDomains.getLogger(LogDomains.WEB_LOGGER);
58         }
59     }
60     
61     /**
62      * Cause the specified request and response to be processed by the Valves
63      * associated with this pipeline, until one of these valves causes the
64      * response to be created and returned. The implementation must ensure
65      * that multiple simultaneous requests (on different threads) can be
66      * processed through the same Pipeline without interfering with each
67      * other's control flow.
68      *
69      * @param request The servlet request we are processing
70      * @param response The servlet response we are creating
71      *
72      * @exception IOException if an input/output error occurs
73      * @exception ServletException if a servlet exception is thrown
74      */

75     public void invoke(Request JavaDoc request, Response response)
76         throws IOException JavaDoc, ServletException JavaDoc {
77         
78         this.lockSession(request);
79         try {
80             super.invoke(request, response);
81         } finally {
82             this.unlockSession(request);
83         }
84     }
85     
86     /**
87      * get the session associated with this request
88      * @param request
89      */

90     protected Session JavaDoc getSession(Request JavaDoc request) {
91         ServletRequest JavaDoc servletReq = request.getRequest();
92         HttpServletRequest JavaDoc httpReq =
93             (HttpServletRequest JavaDoc) servletReq;
94         HttpSession JavaDoc httpSess = httpReq.getSession(false);
95         if(httpSess == null)
96             //need to null out session
97
//httpReq.setSession(null);
98
return null;
99         String JavaDoc id = httpSess.getId();
100         if(_logger.isLoggable(Level.FINEST)) {
101             _logger.finest("SESSION_ID=" + id);
102         }
103         Manager mgr = this.getContainer().getManager();
104         PersistentManagerBase pmb = (PersistentManagerBase)mgr;
105         Session JavaDoc sess = null;
106         try {
107             sess = pmb.findSession(id);
108         } catch (java.io.IOException JavaDoc ex) {}
109         if(_logger.isLoggable(Level.FINEST)) {
110             _logger.finest("RETRIEVED_SESSION=" + sess);
111         }
112          return sess;
113     }
114     
115     /**
116      * lock the session associated with this request
117      * this will be a foreground lock
118      * checks for background lock to clear
119      * and does a decay poll loop to wait until
120      * it is clear; after 5 times it takes control for
121      * the foreground
122      * @param request
123      */

124     protected boolean lockSession(Request JavaDoc request) throws ServletException JavaDoc {
125         boolean result = false;
126         Session JavaDoc sess = this.getSession(request);
127         if(_logger.isLoggable(Level.FINEST)) {
128             _logger.finest("IN LOCK_SESSION: sess =" + sess);
129         }
130         //now lock the session
131
if(sess != null) {
132             long pollTime = 200L;
133             int maxNumberOfRetries = 7;
134             int tryNumber = 0;
135             boolean keepTrying = true;
136             boolean lockResult = false;
137             if(_logger.isLoggable(Level.FINEST)) {
138                 _logger.finest("locking session: sess =" + sess);
139             }
140             StandardSession haSess = (StandardSession) sess;
141             //try to lock up to maxNumberOfRetries times
142
//poll and wait starting with 200 ms
143
while(keepTrying) {
144                 lockResult = haSess.lockForeground();
145                 if(lockResult) {
146                     keepTrying = false;
147                     result = true;
148                     break;
149                 }
150                 tryNumber++;
151                 if(tryNumber < maxNumberOfRetries) {
152                     pollTime = pollTime * 2L;
153                     threadSleep(pollTime);
154                 } else {
155                     //tried to wait and lock maxNumberOfRetries times; throw an exception
156
//throw new ServletException("unable to acquire session lock");
157
//instead of above; unlock the background so we can take over
158
_logger.warning("this should not happen-breaking background lock: sess =" + sess);
159                     haSess.unlockBackground();
160                 }
161             }
162             if(_logger.isLoggable(Level.FINEST)) {
163                 _logger.finest("finished locking session: sess =" + sess);
164                 _logger.finest("LOCK = " + haSess.getSessionLock());
165             }
166         }
167         return result;
168     }
169     
170     protected void threadSleep(long sleepTime) {
171
172         try {
173             Thread.sleep(sleepTime);
174         } catch (InterruptedException JavaDoc e) {
175             ;
176         }
177
178     }
179     
180     /**
181      * unlock the session associated with this request
182      * @param request
183      */

184     protected void unlockSession(Request JavaDoc request) {
185         Session JavaDoc sess = this.getSession(request);
186         if(_logger.isLoggable(Level.FINEST)) {
187             _logger.finest("IN UNLOCK_SESSION: sess = " + sess);
188         }
189         //now unlock the session
190
if(sess != null) {
191             if(_logger.isLoggable(Level.FINEST)) {
192                 _logger.finest("unlocking session: sess =" + sess);
193             }
194             StandardSession haSess = (StandardSession) sess;
195             haSess.unlockForeground();
196             if(_logger.isLoggable(Level.FINEST)) {
197                 _logger.finest("finished unlocking session: sess =" + sess);
198                 _logger.finest("LOCK = " + haSess.getSessionLock());
199             }
200         }
201     }
202         
203     /**
204      * The logger to use for logging ALL web container related messages.
205      */

206     protected static Logger JavaDoc _logger = null;
207     
208 }
209
Popular Tags