KickJava   Java API By Example, From Geeks To Geeks.

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


1 /* ====================================================================
2  *
3  * The ObjectStyle Group Software License, version 1.1
4  * ObjectStyle Group - http://objectstyle.org/
5  *
6  * Copyright (c) 2002-2005, Andrei (Andrus) Adamchik and individual authors
7  * of the software. All rights reserved.
8  *
9  * Redistribution and use in source and binary forms, with or without
10  * modification, are permitted provided that the following conditions
11  * are met:
12  *
13  * 1. Redistributions of source code must retain the above copyright
14  * notice, this list of conditions and the following disclaimer.
15  *
16  * 2. Redistributions in binary form must reproduce the above copyright
17  * notice, this list of conditions and the following disclaimer in
18  * the documentation and/or other materials provided with the
19  * distribution.
20  *
21  * 3. The end-user documentation included with the redistribution, if any,
22  * must include the following acknowlegement:
23  * "This product includes software developed by independent contributors
24  * and hosted on ObjectStyle Group web site (http://objectstyle.org/)."
25  * Alternately, this acknowlegement may appear in the software itself,
26  * if and wherever such third-party acknowlegements normally appear.
27  *
28  * 4. The names "ObjectStyle Group" and "Cayenne" must not be used to endorse
29  * or promote products derived from this software without prior written
30  * permission. For written permission, email
31  * "andrus at objectstyle dot org".
32  *
33  * 5. Products derived from this software may not be called "ObjectStyle"
34  * or "Cayenne", nor may "ObjectStyle" or "Cayenne" appear in their
35  * names without prior written permission.
36  *
37  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
38  * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
39  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
40  * DISCLAIMED. IN NO EVENT SHALL THE OBJECTSTYLE GROUP OR
41  * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
42  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
43  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
44  * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
45  * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
46  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
47  * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
48  * SUCH DAMAGE.
49  * ====================================================================
50  *
51  * This software consists of voluntary contributions made by many
52  * individuals and hosted on ObjectStyle Group web site. For more
53  * information on the ObjectStyle Group, please see
54  * <http://objectstyle.org/>.
55  */

56
57 package org.objectstyle.cayenne.conf;
58
59 import java.io.IOException JavaDoc;
60
61 import javax.servlet.Filter JavaDoc;
62 import javax.servlet.FilterChain JavaDoc;
63 import javax.servlet.FilterConfig JavaDoc;
64 import javax.servlet.ServletException JavaDoc;
65 import javax.servlet.ServletRequest JavaDoc;
66 import javax.servlet.ServletResponse JavaDoc;
67 import javax.servlet.http.HttpServletRequest JavaDoc;
68 import javax.servlet.http.HttpSession JavaDoc;
69
70 import org.apache.log4j.Logger;
71 import org.objectstyle.cayenne.access.DataContext;
72
73 /**
74  * <p>
75  * <code>WebApplicationContextFilter</code> is another helper class to help integrate
76  * Cayenne with web applications. The implementation is similar to
77  * <code>org.objectstyle.cayenne.conf.WebApplicationContextProvider</code> however it
78  * allows for integration with containers that support version 2.3 of the servlet
79  * specification for example Tomcat 4.x. via the usage of filers.
80  * </p>
81  * <p>
82  * Whenever this filter is processed it attempts bind a <code>DataContext</code> to the
83  * thread. It retrieves the <code>DataContext</code> via the
84  * <code>BasicServletConfiguration.getDefaultDataContext()</code> and binds it to the
85  * thread using <code>DataContext.bindThreadDataContext()</code> method. If the session
86  * has not been created this filter will also create a new session.
87  * </p>
88  * <p>
89  * During initialization (init() method) this filter initializes the
90  * <code>BasicServletConfiguration</code> with the servlet context via the
91  * initializeConfiguration() method
92  * </p>
93  * <p>
94  * This filter can be installed in the web container as a &quot;filter&quot; as follows:
95  *
96  * <pre>
97  *
98  * &lt;filter&gt;
99  * &lt;filter-name&gt;WebApplicationContextFilter&lt;/filter-name&gt;
100  * &lt;filter-class&gt;org.objectstyle.cayenne.conf.WebApplicationContextFilter&lt;/filter-class&gt;
101  * &lt;/filter&gt;
102  *
103  * </pre>
104  *
105  * Then the mapping needs to be created to direct all or some requests to be processed by
106  * the filter as follows:
107  *
108  * <pre>
109  *
110  * &lt;filter-mapping&gt;
111  * &lt;filter-name&gt;WebApplicationContextFilter&lt;/filter-name&gt;
112  * &lt;url-pattern&gt;/*&lt;/url-pattern&gt;
113  * &lt;/filter-mapping&gt;
114  *
115  * </pre>
116  *
117  * The above example the filter would be applied to all the servlets and static content
118  * pages in the Web application, because every request URI matches the '/*' URL pattern.
119  * The problem with this mapping however is the fact that this filter will run for every
120  * request made, whether for images and/or static content and dynamic content request.
121  * This maybe detrimental to performance. Hence the mapping url patter should be set
122  * accordingly.
123  * </p>
124  *
125  * @author Gary Jarrel
126  * @see org.objectstyle.cayenne.conf.WebApplicationContextProvider
127  */

128 public class WebApplicationContextFilter implements Filter JavaDoc {
129
130     private static Logger logger = Logger.getLogger(WebApplicationContextFilter.class);
131
132     protected FilterConfig JavaDoc config = null;
133
134     /**
135      * Does nothing. As per the servlet specification, gets called by the container when
136      * the filter is taken out of service.
137      */

138     public void destroy() {
139         // empty
140
}
141
142     /**
143      * Initializes the <code>BasicServletConfiguration</code> via the
144      * initializeConfiguration() method. Also saves the FilterConfing to a private local
145      * variable for possible later access. This method is part of the <code>Filter</code>
146      * interface and is called by the container when the filter is placed into service.
147      */

148
149     public synchronized void init(FilterConfig JavaDoc config) throws ServletException JavaDoc {
150         this.config = config;
151         ServletUtil.initializeSharedConfiguration(config.getServletContext());
152     }
153
154     /**
155      * Retrieves the <code>DataContext</code> bound to the <code>HttpSession</code>
156      * via <code>BasicServletConfiguration.
157
158      * getDefaultContext()</code>, and binds it to
159      * the current thread.
160      */

161     public void doFilter(
162             ServletRequest JavaDoc request,
163             ServletResponse JavaDoc response,
164             FilterChain JavaDoc chain) throws IOException JavaDoc, ServletException JavaDoc {
165
166         if (logger.isDebugEnabled()) {
167             logger.debug("start WebApplicationContextFilter.doFilter. URL - "
168                     + ((HttpServletRequest JavaDoc) request).getRequestURL());
169         }
170
171         if (request instanceof HttpServletRequest JavaDoc) {
172             HttpSession JavaDoc session = ((HttpServletRequest JavaDoc) 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 JavaDoc("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     /**
194      * This method returns the <code>FilterConfig</code> object with which this filter
195      * was initialized.
196      */

197     public FilterConfig JavaDoc getFilterConfig() {
198         return this.config;
199     }
200 }
201
202
Popular Tags