1 package org.apache.turbine.util.hibernate; 2 3 18 19 import java.io.IOException ; 20 21 import javax.servlet.Filter ; 22 import javax.servlet.FilterChain ; 23 import javax.servlet.FilterConfig ; 24 import javax.servlet.ServletException ; 25 import javax.servlet.ServletRequest ; 26 import javax.servlet.ServletResponse ; 27 import javax.servlet.http.HttpServletRequest ; 28 import javax.servlet.http.HttpServletResponse ; 29 import javax.servlet.http.HttpSession ; 30 31 import net.sf.hibernate.Session; 32 33 import org.apache.commons.logging.Log; 34 import org.apache.commons.logging.LogFactory; 35 36 41 public class HibernateFilter implements Filter 42 { 43 45 47 50 private Log log = LogFactory.getLog(HibernateFilter.class); 51 private FilterConfig filterConfig = null; 52 53 55 public void init(FilterConfig filterConfig) throws ServletException 56 { 57 this.filterConfig = filterConfig; 58 59 } 60 61 64 public void destroy() 65 { 66 filterConfig = null; 67 } 68 69 public void doFilter(ServletRequest req, ServletResponse resp, FilterChain chain) throws IOException , ServletException 70 { 71 HttpServletRequest request = (HttpServletRequest ) req; 73 HttpServletResponse response = (HttpServletResponse ) resp; 74 HttpSession session = request.getSession(true); 75 76 Session 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 exc) 90 { 91 log.error("Error closing hibernate session.", exc); 92 exc.printStackTrace(); 93 } 94 } 95 } 96 97 public static Session getSession() throws PersistenceException 98 { 99 try 100 { 101 102 return HibernateUtils.currentSession(); 103 } 104 catch (Exception e) 105 { 106 throw new PersistenceException("Could not find current Hibernate session.", e); 107 } 108 109 } 110 } 111 | Popular Tags |