1 19 20 package org.apache.cayenne.conf; 21 22 import java.io.IOException ; 23 24 import javax.servlet.Filter ; 25 import javax.servlet.FilterChain ; 26 import javax.servlet.FilterConfig ; 27 import javax.servlet.ServletException ; 28 import javax.servlet.ServletRequest ; 29 import javax.servlet.ServletResponse ; 30 import javax.servlet.http.HttpServletRequest ; 31 import javax.servlet.http.HttpSession ; 32 33 import org.apache.cayenne.access.DataContext; 34 35 56 public class WebApplicationContextFilter implements Filter { 57 58 public void init(FilterConfig filterConfig) throws ServletException { 59 ServletUtil.initializeSharedConfiguration(filterConfig.getServletContext()); 60 } 61 62 65 public void destroy() { 68 } 70 71 75 public void doFilter( 76 ServletRequest request, 77 ServletResponse response, 78 FilterChain chain) throws IOException , ServletException { 79 80 boolean reset = false; 81 82 if (request instanceof HttpServletRequest ) { 83 reset = true; 84 85 HttpSession session = ((HttpServletRequest ) request).getSession(true); 86 DataContext context = ServletUtil.getSessionContext(session); 87 DataContext.bindThreadDataContext(context); 88 } 89 90 try { 91 chain.doFilter(request, response); 92 } 93 finally { 94 if (reset) { 95 DataContext.bindThreadDataContext(null); 96 } 97 } 98 } 99 } 100 | Popular Tags |