1 56 57 package org.objectstyle.cayenne.conf; 58 59 import java.io.IOException ; 60 61 import javax.servlet.Filter ; 62 import javax.servlet.FilterChain ; 63 import javax.servlet.FilterConfig ; 64 import javax.servlet.ServletException ; 65 import javax.servlet.ServletRequest ; 66 import javax.servlet.ServletResponse ; 67 import javax.servlet.http.HttpServletRequest ; 68 import javax.servlet.http.HttpSession ; 69 70 import org.apache.log4j.Logger; 71 import org.objectstyle.cayenne.access.DataContext; 72 73 128 public class WebApplicationContextFilter implements Filter { 129 130 private static Logger logger = Logger.getLogger(WebApplicationContextFilter.class); 131 132 protected FilterConfig config = null; 133 134 138 public void destroy() { 139 } 141 142 148 149 public synchronized void init(FilterConfig config) throws ServletException { 150 this.config = config; 151 ServletUtil.initializeSharedConfiguration(config.getServletContext()); 152 } 153 154 161 public void doFilter( 162 ServletRequest request, 163 ServletResponse response, 164 FilterChain chain) throws IOException , ServletException { 165 166 if (logger.isDebugEnabled()) { 167 logger.debug("start WebApplicationContextFilter.doFilter. URL - " 168 + ((HttpServletRequest ) request).getRequestURL()); 169 } 170 171 if (request instanceof HttpServletRequest ) { 172 HttpSession session = ((HttpServletRequest ) request).getSession(true); 173 DataContext dataContext = ServletUtil.getSessionContext(session); 174 175 if (dataContext == null) { 176 logger.debug("DataContext was null. Throwing Exception"); 177 178 throw new ServletException ("DataContext was null and could " 179 + "not be bound to thred"); 180 } 181 182 DataContext.bindThreadDataContext(dataContext); 183 logger.debug("DataContext bound, continuing in chain"); 184 } 185 else { 186 logger.debug("requests that are not HttpServletRequest are not supported.."); 187 } 188 189 chain.doFilter(request, response); 190 DataContext.bindThreadDataContext(null); 191 } 192 193 197 public FilterConfig getFilterConfig() { 198 return this.config; 199 } 200 } 201 202 | Popular Tags |