You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@cayenne.apache.org by aa...@apache.org on 2006/09/17 19:14:52 UTC
svn commit: r447099 -
/incubator/cayenne/main/trunk/core/cayenne-jdk1.4/src/main/java/org/apache/cayenne/conf/WebApplicationContextFilter.java
Author: aadamchik
Date: Sun Sep 17 10:14:51 2006
New Revision: 447099
URL: http://svn.apache.org/viewvc?view=rev&rev=447099
Log:
CAY-654 - rewriting context filter
Modified:
incubator/cayenne/main/trunk/core/cayenne-jdk1.4/src/main/java/org/apache/cayenne/conf/WebApplicationContextFilter.java
Modified: incubator/cayenne/main/trunk/core/cayenne-jdk1.4/src/main/java/org/apache/cayenne/conf/WebApplicationContextFilter.java
URL: http://svn.apache.org/viewvc/incubator/cayenne/main/trunk/core/cayenne-jdk1.4/src/main/java/org/apache/cayenne/conf/WebApplicationContextFilter.java?view=diff&rev=447099&r1=447098&r2=447099
==============================================================================
--- incubator/cayenne/main/trunk/core/cayenne-jdk1.4/src/main/java/org/apache/cayenne/conf/WebApplicationContextFilter.java (original)
+++ incubator/cayenne/main/trunk/core/cayenne-jdk1.4/src/main/java/org/apache/cayenne/conf/WebApplicationContextFilter.java Sun Sep 17 10:14:51 2006
@@ -17,7 +17,6 @@
* under the License.
****************************************************************/
-
package org.apache.cayenne.conf;
import java.io.IOException;
@@ -34,106 +33,67 @@
import org.apache.cayenne.access.DataContext;
/**
- * <p>
- * <code>WebApplicationContextFilter</code> is another helper class to help integrate
- * Cayenne with web applications. The implementation is similar to
- * <code>org.apache.cayenne.conf.WebApplicationContextProvider</code> however it
- * allows for integration with containers that support version 2.3 of the servlet
- * specification for example Tomcat 4.x. via the usage of filers.
- * </p>
- * <p>
- * Whenever this filter is processed it attempts bind a <code>DataContext</code> to the
- * thread. It retrieves the <code>DataContext</code> via the
- * <code>BasicServletConfiguration.getDefaultDataContext()</code> and binds it to the
- * thread using <code>DataContext.bindThreadDataContext()</code> method. If the session
- * has not been created this filter will also create a new session.
- * </p>
- * <p>
- * During initialization (init() method) this filter initializes the
- * <code>BasicServletConfiguration</code> with the servlet context via the
- * initializeConfiguration() method
- * </p>
- * <p>
- * This filter can be installed in the web container as a "filter" as follows:
- *
- * <pre>
- * <filter>
- * <filter-name>WebApplicationContextFilter</filter-name>
- * <filter-class>org.apache.cayenne.conf.WebApplicationContextFilter</filter-class>
- * </filter>
- * </pre>
- *
- * Then the mapping needs to be created to direct all or some requests to be processed by
- * the filter as follows:
+ * A Servlet Filter that binds session DataContext to the current request thread. During
+ * the request application code without any knowledge of the servlet environment can
+ * access DataContext via {@link DataContext#getThreadDataContext()} method. <p/> To
+ * enable the filter add XML similar to this in the <code>web.xml</code> descriptor of a
+ * web application:
*
* <pre>
- * <filter-mapping>
- * <filter-name>WebApplicationContextFilter</filter-name>
- * <url-pattern>/*</url-pattern>
- * </filter-mapping>
+ * <filter>
+ * <filter-name>CayenneFilter</filter-name>
+ * <filter-class>org.apache.cayenne.conf.WebApplicationContextFilter</filter-class>
+ * </filter>
+ * <filter-mapping>
+ * <filter-name>CayenneFilter</filter-name>
+ * <url-pattern>/*</url-pattern>
+ * </filter-mapping>
* </pre>
*
- * The above example the filter would be applied to all the servlets and static content
- * pages in the Web application, because every request URI matches the '/*' URL pattern.
- * The problem with this mapping however is the fact that this filter will run for every
- * request made, whether for images and/or static content and dynamic content request.
- * This maybe detrimental to performance. Hence the mapping url patter should be set
- * accordingly.
- * </p>
- *
- * @author Gary Jarrel
+ * @author Andrus Adamchik
+ * @since 1.2
*/
public class WebApplicationContextFilter implements Filter {
- /**
- * Does nothing. As per the servlet specification, gets called by the container when
- * the filter is taken out of service.
- */
- public void destroy() {
- // empty
+ public void init(FilterConfig filterConfig) throws ServletException {
+ ServletUtil.initializeSharedConfiguration(filterConfig.getServletContext());
}
/**
- * Initializes the <code>BasicServletConfiguration</code> via the
- * initializeConfiguration() method. Also saves the FilterConfing to a private local
- * variable for possible later access. This method is part of the <code>Filter</code>
- * interface and is called by the container when the filter is placed into service.
+ * Cleanup callback method that does nothing, as the filter doesn't store any state.
*/
- public synchronized void init(FilterConfig config) throws ServletException {
- ServletUtil.initializeSharedConfiguration(config.getServletContext());
+ // TODO: andrus 9/17/2006 - should we shut down Cayenne stack? I.e. should it be
+ // complimentary to "init"?
+ public void destroy() {
+ // noop
}
/**
- * Retrieves the <code>DataContext</code> bound to the <code>HttpSession</code>
- * via <code>BasicServletConfiguration.
-
- * getDefaultContext()</code>, and binds it to
- * the current thread.
+ * The main worker method that binds a DataContext to the current thread on entry and
+ * unbinds it on exit (regardless of whether any exceptions occured in the request).
*/
public void doFilter(
ServletRequest request,
ServletResponse response,
FilterChain chain) throws IOException, ServletException {
-
- if (request instanceof HttpServletRequest) {
- HttpSession session = ((HttpServletRequest) request).getSession(true);
- DataContext dataContext = ServletUtil.getSessionContext(session);
+ boolean reset = false;
- if (dataContext == null) {
-
- throw new ServletException("DataContext was null and could "
- + "not be bound to thread.");
- }
+ if (request instanceof HttpServletRequest) {
+ reset = true;
- DataContext.bindThreadDataContext(dataContext);
+ HttpSession session = ((HttpServletRequest) request).getSession(true);
+ DataContext context = ServletUtil.getSessionContext(session);
+ DataContext.bindThreadDataContext(context);
}
-
+
try {
chain.doFilter(request, response);
}
finally {
- DataContext.bindThreadDataContext(null);
+ if (reset) {
+ DataContext.bindThreadDataContext(null);
+ }
}
}
}