1 23 24 package com.sun.enterprise.web; 25 26 import java.io.IOException ; 27 import javax.servlet.ServletException ; 28 import javax.servlet.ServletRequest ; 29 import javax.servlet.http.HttpServletRequest ; 30 import javax.servlet.http.HttpSession ; 31 32 import java.util.logging.Logger ; 33 import java.util.logging.Level ; 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 48 public class PESessionLockingStandardPipeline extends WebPipeline { 49 50 54 public PESessionLockingStandardPipeline(Container container) { 55 super(container); 56 if (_logger == null) { 57 _logger = LogDomains.getLogger(LogDomains.WEB_LOGGER); 58 } 59 } 60 61 75 public void invoke(Request request, Response response) 76 throws IOException , ServletException { 77 78 this.lockSession(request); 79 try { 80 super.invoke(request, response); 81 } finally { 82 this.unlockSession(request); 83 } 84 } 85 86 90 protected Session getSession(Request request) { 91 ServletRequest servletReq = request.getRequest(); 92 HttpServletRequest httpReq = 93 (HttpServletRequest ) servletReq; 94 HttpSession httpSess = httpReq.getSession(false); 95 if(httpSess == null) 96 return null; 99 String 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 sess = null; 106 try { 107 sess = pmb.findSession(id); 108 } catch (java.io.IOException ex) {} 109 if(_logger.isLoggable(Level.FINEST)) { 110 _logger.finest("RETRIEVED_SESSION=" + sess); 111 } 112 return sess; 113 } 114 115 124 protected boolean lockSession(Request request) throws ServletException { 125 boolean result = false; 126 Session sess = this.getSession(request); 127 if(_logger.isLoggable(Level.FINEST)) { 128 _logger.finest("IN LOCK_SESSION: sess =" + sess); 129 } 130 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 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 _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 e) { 175 ; 176 } 177 178 } 179 180 184 protected void unlockSession(Request request) { 185 Session sess = this.getSession(request); 186 if(_logger.isLoggable(Level.FINEST)) { 187 _logger.finest("IN UNLOCK_SESSION: sess = " + sess); 188 } 189 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 206 protected static Logger _logger = null; 207 208 } 209 | Popular Tags |