KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*
2  * PESessionLocker.java
3  *
4  * Created on January 18, 2006, 4:46 PM
5  */

6
7 package com.sun.enterprise.web;
8
9 import javax.servlet.ServletException JavaDoc;
10 import javax.servlet.ServletRequest JavaDoc;
11 import org.apache.catalina.Context;
12 import org.apache.catalina.Manager;
13 import org.apache.catalina.Session;
14 import org.apache.catalina.SessionLocker;
15 import org.apache.catalina.session.BaseSessionLocker;
16 import org.apache.catalina.session.StandardSession;
17
18
19 /**
20  *
21  * @author lwhite
22  */

23 public class PESessionLocker extends BaseSessionLocker implements SessionLocker {
24     
25     /** Creates a new instance of PESessionLocker */
26     public PESessionLocker() {
27     }
28     
29     /** Creates a new instance of PESessionLocker */
30     public PESessionLocker(Context ctx) {
31         this();
32         _context = ctx;
33     }
34     
35     /**
36      * lock the session associated with this request
37      * this will be a foreground lock
38      * checks for background lock to clear
39      * and does a decay poll loop to wait until
40      * it is clear; after 5 times it takes control for
41      * the foreground
42      * @param request
43      */

44     public boolean lockSession(ServletRequest JavaDoc request) throws ServletException JavaDoc {
45         boolean result = false;
46         Session sess = this.getSession(request);
47         //now lock the session
48
if(sess != null) {
49             long pollTime = 200L;
50             int maxNumberOfRetries = 7;
51             int tryNumber = 0;
52             boolean keepTrying = true;
53             boolean lockResult = false;
54             StandardSession stdSess = (StandardSession) sess;
55             //try to lock up to maxNumberOfRetries times
56
//poll and wait starting with 200 ms
57
while(keepTrying) {
58                 lockResult = stdSess.lockForeground();
59                 if(lockResult) {
60                     keepTrying = false;
61                     result = true;
62                     break;
63                 }
64                 tryNumber++;
65                 if(tryNumber < maxNumberOfRetries) {
66                     pollTime = pollTime * 2L;
67                     threadSleep(pollTime);
68                 } else {
69                     //tried to wait and lock maxNumberOfRetries times; throw an exception
70
//throw new ServletException("unable to acquire session lock");
71
//instead of above; unlock the background so we can take over
72
stdSess.unlockBackground();
73                 }
74             }
75         }
76         return result;
77     }
78     
79     private Session getSession(ServletRequest JavaDoc request) {
80         javax.servlet.http.HttpServletRequest JavaDoc httpReq =
81             (javax.servlet.http.HttpServletRequest JavaDoc) request;
82         javax.servlet.http.HttpSession JavaDoc httpSess = httpReq.getSession(false);
83         if(httpSess == null) {
84             return null;
85         }
86         String JavaDoc id = httpSess.getId();
87         Manager mgr = _context.getManager();
88         Session sess = null;
89         try {
90             sess = mgr.findSession(id);
91         } catch (java.io.IOException JavaDoc ex) {}
92
93         return sess;
94     }
95     
96     protected void threadSleep(long sleepTime) {
97
98         try {
99             Thread.sleep(sleepTime);
100         } catch (InterruptedException JavaDoc e) {
101             ;
102         }
103
104     }
105     
106     /**
107      * unlock the session associated with this request
108      * @param request
109      */

110     public void unlockSession(ServletRequest JavaDoc request) {
111         Session sess = this.getSession(request);
112         //now unlock the session
113
if(sess != null) {
114             StandardSession stdSess = (StandardSession) sess;
115             stdSess.unlockForeground();
116         }
117     }
118     
119 }
120
Popular Tags