KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > turbine > util > hibernate > HibernateFilter


1 package org.apache.turbine.util.hibernate;
2  
3 /*
4  * Copyright 2001-2004 The Apache Software Foundation.
5  *
6  * Licensed under the Apache License, Version 2.0 (the "License")
7  * you may not use this file except in compliance with the License.
8  * 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, software
13  * distributed under the License is distributed on an "AS IS" BASIS,
14  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15  * See the License for the specific language governing permissions and
16  * limitations under the License.
17  */

18
19 import java.io.IOException JavaDoc;
20
21 import javax.servlet.Filter JavaDoc;
22 import javax.servlet.FilterChain JavaDoc;
23 import javax.servlet.FilterConfig JavaDoc;
24 import javax.servlet.ServletException JavaDoc;
25 import javax.servlet.ServletRequest JavaDoc;
26 import javax.servlet.ServletResponse JavaDoc;
27 import javax.servlet.http.HttpServletRequest JavaDoc;
28 import javax.servlet.http.HttpServletResponse JavaDoc;
29 import javax.servlet.http.HttpSession JavaDoc;
30
31 import net.sf.hibernate.Session;
32
33 import org.apache.commons.logging.Log;
34 import org.apache.commons.logging.LogFactory;
35
36 /**
37  * HibernateFilter
38  *
39  * @version $Id: HibernateFilter.java,v 1.1.2.1 2004/02/27 05:12:43 seade Exp $
40  */

41 public class HibernateFilter implements Filter JavaDoc
42 {
43     //~ Static fields/initializers =============================================
44

45     //~ Instance fields ========================================================
46

47     /**
48      * The <code>Log</code> instance for this class
49      */

50     private Log log = LogFactory.getLog(HibernateFilter.class);
51     private FilterConfig JavaDoc filterConfig = null;
52
53     //~ Methods ================================================================
54

55     public void init(FilterConfig JavaDoc filterConfig) throws ServletException JavaDoc
56     {
57         this.filterConfig = filterConfig;
58
59     }
60
61     /**
62      * Destroys the filter.
63      */

64     public void destroy()
65     {
66         filterConfig = null;
67     }
68
69     public void doFilter(ServletRequest JavaDoc req, ServletResponse JavaDoc resp, FilterChain JavaDoc chain) throws IOException JavaDoc, ServletException JavaDoc
70     {
71         // cast to the types I want to use
72
HttpServletRequest JavaDoc request = (HttpServletRequest JavaDoc) req;
73         HttpServletResponse JavaDoc response = (HttpServletResponse JavaDoc) resp;
74         HttpSession JavaDoc session = request.getSession(true);
75
76         Session JavaDoc ses = null;
77         boolean sessionCreated = false;
78
79         try
80         {
81             chain.doFilter(request, response);
82         }
83         finally
84         {
85             try
86             {
87                 HibernateUtils.closeSession();
88             }
89             catch (Exception JavaDoc exc)
90             {
91                 log.error("Error closing hibernate session.", exc);
92                 exc.printStackTrace();
93             }
94         }
95     }
96
97     public static Session JavaDoc getSession() throws PersistenceException
98     {
99         try
100         {
101
102             return HibernateUtils.currentSession();
103         }
104         catch (Exception JavaDoc e)
105         {
106             throw new PersistenceException("Could not find current Hibernate session.", e);
107         }
108
109     }
110 }
111
Popular Tags