1 /* 2 * Created on 04/04/2005 3 * 4 */ 5 package comum.database; 6 7 import java.io.IOException; 8 9 import javax.servlet.Filter; 10 import javax.servlet.FilterChain; 11 import javax.servlet.FilterConfig; 12 import javax.servlet.ServletException; 13 import javax.servlet.ServletRequest; 14 import javax.servlet.ServletResponse; 15 16 import org.apache.log4j.Logger; 17 18 /** 19 * Filtro que fecha a sessao corrente do hibernate cada vez que o Web Container processa 20 * uma pagina. O filtro e configurado no web.xml.<br> 21 * 22 * @author garten 23 * @since N/C 24 * @version N/C 25 */ 26 public class FechaSessaoFilter implements Filter { 27 28 /** 29 * Construtor.<br> 30 * 31 * @author N/C 32 * @since N/C 33 * @version N/C 34 */ 35 public FechaSessaoFilter() { 36 super(); 37 } 38 39 /* (non-Javadoc) 40 * @see javax.servlet.Filter#init(javax.servlet.FilterConfig) 41 */ 42 /** 43 * Metodo de inicializacao.<br> 44 * 45 * @author N/C 46 * @since N/C 47 * @version N/C 48 * @param FilterConfig arg0 49 * @throws ServletException 50 */ 51 public void init(FilterConfig arg0) throws ServletException { 52 } 53 54 /* (non-Javadoc) 55 * @see javax.servlet.Filter#doFilter(javax.servlet.ServletRequest, javax.servlet.ServletResponse, javax.servlet.FilterChain) 56 */ 57 /** 58 * Esse filtro faz pos-processamento das requisições, fechando a sessão do Hibernate.<br> 59 * 60 * @author N/C 61 * @since N/C 62 * @version N/C 63 * @Param ServletRequest request 64 * @Param ServletResponse response 65 * @Param FilterChain chain 66 * @Throws IOException 67 * @Throws ServletException 68 */ 69 public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException { 70 71 //String pagina = ((HttpServletRequest) request).getServletPath(); 72 // executa a pagina e outros filtros, se existirem 73 chain.doFilter(request, response); 74 75 // por ultimo, fecha a sessao aberta. 76 try { 77 HibernateUtil.closeSession(); 78 } catch (Exception e) { 79 Logger logger = Logger.getLogger(this.getClass()); 80 logger.error(e); 81 HibernateUtil.clearSession(); 82 } 83 } 84 85 /* (non-Javadoc) 86 * @see javax.servlet.Filter#destroy() 87 */ 88 /** 89 * Metodo destrutor.<br> 90 * 91 * @author N/C 92 * @since N/C 93 * @version N/C 94 */ 95 public void destroy() { 96 } 97 98 }