1 6 7 package com.sun.enterprise.web; 8 9 import javax.servlet.ServletException ; 10 import javax.servlet.ServletRequest ; 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 23 public class PESessionLocker extends BaseSessionLocker implements SessionLocker { 24 25 26 public PESessionLocker() { 27 } 28 29 30 public PESessionLocker(Context ctx) { 31 this(); 32 _context = ctx; 33 } 34 35 44 public boolean lockSession(ServletRequest request) throws ServletException { 45 boolean result = false; 46 Session sess = this.getSession(request); 47 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 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 stdSess.unlockBackground(); 73 } 74 } 75 } 76 return result; 77 } 78 79 private Session getSession(ServletRequest request) { 80 javax.servlet.http.HttpServletRequest httpReq = 81 (javax.servlet.http.HttpServletRequest ) request; 82 javax.servlet.http.HttpSession httpSess = httpReq.getSession(false); 83 if(httpSess == null) { 84 return null; 85 } 86 String id = httpSess.getId(); 87 Manager mgr = _context.getManager(); 88 Session sess = null; 89 try { 90 sess = mgr.findSession(id); 91 } catch (java.io.IOException ex) {} 92 93 return sess; 94 } 95 96 protected void threadSleep(long sleepTime) { 97 98 try { 99 Thread.sleep(sleepTime); 100 } catch (InterruptedException e) { 101 ; 102 } 103 104 } 105 106 110 public void unlockSession(ServletRequest request) { 111 Session sess = this.getSession(request); 112 if(sess != null) { 114 StandardSession stdSess = (StandardSession) sess; 115 stdSess.unlockForeground(); 116 } 117 } 118 119 } 120 | Popular Tags |