1 28 29 package com.caucho.server.webapp; 30 31 import com.caucho.log.Log; 32 import com.caucho.server.connection.AbstractHttpRequest; 33 import com.caucho.server.connection.AbstractHttpResponse; 34 import com.caucho.transaction.TransactionImpl; 35 import com.caucho.transaction.TransactionManagerImpl; 36 37 import javax.servlet.FilterChain ; 38 import javax.servlet.ServletException ; 39 import javax.servlet.ServletRequest ; 40 import javax.servlet.ServletResponse ; 41 import javax.servlet.http.HttpServletRequest ; 42 import javax.transaction.Status ; 43 import java.io.IOException ; 44 import java.util.logging.Level ; 45 import java.util.logging.Logger ; 46 47 51 public class ContextFilterChain implements FilterChain { 52 private static final Logger log = Log.open(ContextFilterChain.class); 53 54 private FilterChain _next; 56 57 private ClassLoader _classLoader; 59 private TransactionManagerImpl _tm; 61 private ErrorPageManager _errorPageManager; 63 64 70 public ContextFilterChain(FilterChain next) 71 { 72 _next = next; 73 74 _classLoader = Thread.currentThread().getContextClassLoader(); 75 76 try { 77 _tm = TransactionManagerImpl.getInstance(); 78 } catch (Throwable e) { 79 log.log(Level.WARNING, e.toString(), e); 80 } 81 } 82 83 86 public void setErrorPageManager(ErrorPageManager errorPageManager) 87 { 88 _errorPageManager = errorPageManager; 89 } 90 91 99 public void doFilter(ServletRequest request, 100 ServletResponse response) 101 throws ServletException , IOException 102 { 103 Thread thread = Thread.currentThread(); 104 ClassLoader oldLoader = thread.getContextClassLoader(); 105 106 try { 107 thread.setContextClassLoader(_classLoader); 108 _next.doFilter(request, response); 109 } catch (ServletException e) { 110 if (_errorPageManager != null) 111 _errorPageManager.sendServletError(e, request, response); 112 else 113 throw e; 114 } catch (IOException e) { 115 if (_errorPageManager != null) 116 _errorPageManager.sendServletError(e, request, response); 117 else 118 throw e; 119 } catch (RuntimeException e) { 120 if (_errorPageManager != null) 121 _errorPageManager.sendServletError(e, request, response); 122 else 123 throw e; 124 } finally { 125 if (request instanceof AbstractHttpRequest) 127 ((AbstractHttpRequest) request).finish(); 128 129 if (_tm != null) { 130 try { 131 TransactionImpl transaction = _tm.getCurrent(); 132 if (transaction.getStatus() != Status.STATUS_NO_TRANSACTION) { 133 log.warning("Transaction not properly closed for " + ((HttpServletRequest ) request).getRequestURL()); 134 } 135 transaction.close(); 136 } catch (Throwable e) { 137 log.log(Level.WARNING, e.getMessage(), e); 138 } 139 } 140 141 if (response instanceof AbstractHttpResponse) 142 ((AbstractHttpResponse) response).finish(); 143 144 thread.setContextClassLoader(oldLoader); 145 } 146 } 147 } 148 | Popular Tags |