KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > cayenne > conf > WebApplicationContextFilter


1 /*****************************************************************
2  * Licensed to the Apache Software Foundation (ASF) under one
3  * or more contributor license agreements. See the NOTICE file
4  * distributed with this work for additional information
5  * regarding copyright ownership. The ASF licenses this file
6  * to you under the Apache License, Version 2.0 (the
7  * "License"); you may not use this file except in compliance
8  * with the License. You may obtain a copy of the License at
9  *
10  * http://www.apache.org/licenses/LICENSE-2.0
11  *
12  * Unless required by applicable law or agreed to in writing,
13  * software distributed under the License is distributed on an
14  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15  * KIND, either express or implied. See the License for the
16  * specific language governing permissions and limitations
17  * under the License.
18  ****************************************************************/

19
20 package org.apache.cayenne.conf;
21
22 import java.io.IOException JavaDoc;
23
24 import javax.servlet.Filter JavaDoc;
25 import javax.servlet.FilterChain JavaDoc;
26 import javax.servlet.FilterConfig JavaDoc;
27 import javax.servlet.ServletException JavaDoc;
28 import javax.servlet.ServletRequest JavaDoc;
29 import javax.servlet.ServletResponse JavaDoc;
30 import javax.servlet.http.HttpServletRequest JavaDoc;
31 import javax.servlet.http.HttpSession JavaDoc;
32
33 import org.apache.cayenne.access.DataContext;
34
35 /**
36  * A Servlet Filter that binds session DataContext to the current request thread. During
37  * the request application code without any knowledge of the servlet environment can
38  * access DataContext via {@link DataContext#getThreadDataContext()} method. <p/> To
39  * enable the filter add XML similar to this in the <code>web.xml</code> descriptor of a
40  * web application:
41  *
42  * <pre>
43  * &lt;filter&gt;
44  * &lt;filter-name&gt;CayenneFilter&lt;/filter-name&gt;
45  * &lt;filter-class&gt;org.apache.cayenne.conf.WebApplicationContextFilter&lt;/filter-class&gt;
46  * &lt;/filter&gt;
47  * &lt;filter-mapping&gt;
48  * &lt;filter-name&gt;CayenneFilter&lt;/filter-name&gt;
49  * &lt;url-pattern&gt;/*&lt;/url-pattern&gt;
50  * &lt;/filter-mapping&gt;
51  * </pre>
52  *
53  * @author Andrus Adamchik
54  * @since 1.2
55  */

56 public class WebApplicationContextFilter implements Filter JavaDoc {
57
58     public void init(FilterConfig JavaDoc filterConfig) throws ServletException JavaDoc {
59         ServletUtil.initializeSharedConfiguration(filterConfig.getServletContext());
60     }
61
62     /**
63      * Cleanup callback method that does nothing, as the filter doesn't store any state.
64      */

65     // TODO: andrus 9/17/2006 - should we shut down Cayenne stack? I.e. should it be
66
// complimentary to "init"?
67
public void destroy() {
68         // noop
69
}
70
71     /**
72      * The main worker method that binds a DataContext to the current thread on entry and
73      * unbinds it on exit (regardless of whether any exceptions occured in the request).
74      */

75     public void doFilter(
76             ServletRequest JavaDoc request,
77             ServletResponse JavaDoc response,
78             FilterChain JavaDoc chain) throws IOException JavaDoc, ServletException JavaDoc {
79
80         boolean reset = false;
81
82         if (request instanceof HttpServletRequest JavaDoc) {
83             reset = true;
84
85             HttpSession JavaDoc session = ((HttpServletRequest JavaDoc) 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