KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > directwebremoting > hibernate > H3SessionAjaxFilter


1 /*
2  * Copyright 2005 Joe Walker
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */

16 package org.directwebremoting.hibernate;
17
18 import java.lang.reflect.Method JavaDoc;
19
20 import javax.servlet.ServletContext JavaDoc;
21
22 import org.directwebremoting.AjaxFilter;
23 import org.directwebremoting.AjaxFilterChain;
24 import org.directwebremoting.WebContextFactory;
25 import org.directwebremoting.util.Logger;
26 import org.hibernate.Session;
27 import org.hibernate.SessionFactory;
28 import org.hibernate.Transaction;
29
30 /**
31  * An {@link AjaxFilter} that uses DWR Hibernate support classes to do a
32  * {@link Session#beginTransaction()} before passing the control on to the chain
33  * and a {@link Transaction#commit()} after.
34  * @author Joe Walker [joe at getahead dot ltd dot uk]
35  */

36 public class H3SessionAjaxFilter implements AjaxFilter
37 {
38     /* (non-Javadoc)
39      * @see org.directwebremoting.AjaxFilter#doFilter(java.lang.Object, java.lang.reflect.Method, java.lang.Object[], org.directwebremoting.AjaxFilterChain)
40      */

41     public Object JavaDoc doFilter(Object JavaDoc object, Method JavaDoc method, Object JavaDoc[] params, AjaxFilterChain chain) throws Exception JavaDoc
42     {
43         ServletContext JavaDoc context = WebContextFactory.get().getServletContext();
44         SessionFactory sessionFactory = (SessionFactory) context.getAttribute(ATTRIBUTE_SESSION);
45
46         Transaction transaction = null;
47         if (sessionFactory != null)
48         {
49             Session session = sessionFactory.getCurrentSession();
50             transaction = session.beginTransaction();
51         }
52         else
53         {
54             log.error("SessionFactory not initialized for this web application. Use: H3SessionAjaxFilter.setSessionFactory(servletContext, sessionFactory);");
55         }
56
57         Object JavaDoc reply = chain.doFilter(object, method, params);
58
59         if (transaction != null)
60         {
61             transaction.commit();
62         }
63
64         return reply;
65     }
66
67     /**
68      * Assigns a {@link SessionFactory} to a {@link ServletContext} so DWR knows
69      * how to get hold of a {@link org.hibernate.Session}.
70      * @param context The Servlet environment to store the ServletContext in
71      * @param sessionFactory The Hibernate session factory to register
72      */

73     public static void setSessionFactory(ServletContext JavaDoc context, SessionFactory sessionFactory)
74     {
75         context.setAttribute(ATTRIBUTE_SESSION, sessionFactory);
76     }
77
78     /**
79      * Get access to a Session, given the {@link SessionFactory} linked in
80      * {@link #setSessionFactory(ServletContext, SessionFactory)}
81      * @param context The webapp to link the calls together
82      * @return A Session from the {@link SessionFactory} or null if
83      * {@link #setSessionFactory(ServletContext, SessionFactory)} has not been
84      * called for this {@link ServletContext}
85      */

86     public static Session getCurrentSession(ServletContext JavaDoc context)
87     {
88         SessionFactory sessionFactory = (SessionFactory) context.getAttribute(ATTRIBUTE_SESSION);
89         if (sessionFactory == null)
90         {
91             return null;
92         }
93
94         return sessionFactory.getCurrentSession();
95     }
96
97     /**
98      * The log stream
99      */

100     private static final Logger log = Logger.getLogger(H3SessionAjaxFilter.class);
101
102     /**
103      * Under what name do we store the session factory?
104      */

105     protected static final String JavaDoc ATTRIBUTE_SESSION = "org.directwebremoting.hibernate.SessionFactory";
106 }
107
Popular Tags