001 package edu.nrao.sss.webapp; 002 003 import java.io.IOException; 004 005 import org.apache.log4j.Logger; 006 007 import org.hibernate.HibernateException; 008 009 import javax.servlet.Filter; 010 import javax.servlet.FilterChain; 011 import javax.servlet.FilterConfig; 012 import javax.servlet.ServletException; 013 import javax.servlet.ServletRequest; 014 import javax.servlet.ServletResponse; 015 import javax.servlet.ServletContext; 016 import javax.servlet.http.HttpSession; 017 import javax.servlet.http.HttpServletRequest; 018 019 import java.util.List; 020 import java.util.ArrayList; 021 022 import edu.nrao.sss.dao.HibernateDao; 023 024 /** 025 * This Filter is responsible for retrieving HibernateDaos from the 026 * HttpSession and commiting any open transactions for those Daos after the 027 * response has been rendered. It finds the Dao's in the HttpSession using the 028 * attribute names configured for the filter with the 029 * <code>edu.nrao.sss.dao.DaoSessionAttrNames</code> init-parameter. The 030 * parameter value should be a comma separated list of attribute names that 031 * can be looked up with {@link HttpSession#getAttribute(String)}. 032 */ 033 public class HibernateFilter implements Filter 034 { 035 private static final Logger log = Logger.getLogger(HibernateFilter.class); 036 037 protected final static String INIT_PARAM_NAME = "edu.nrao.sss.dao.DaoSessionAttrNames"; 038 039 private List<String> sessionAttrNames = new ArrayList<String>(); 040 041 /** 042 * See class comments. 043 */ 044 public void init(FilterConfig conf) 045 { 046 ServletContext ctx = conf.getServletContext(); 047 String attrNames = conf.getInitParameter(INIT_PARAM_NAME); 048 if (attrNames == null) 049 { 050 //Try to find it in the ServletContext. 051 attrNames = ctx.getInitParameter(INIT_PARAM_NAME); 052 053 //Assign a default if still not found. 054 if (attrNames == null) 055 attrNames = ""; 056 } 057 058 for (String attr : attrNames.split(",")) 059 { 060 if (attr.length() > 0) 061 { 062 this.sessionAttrNames.add(attr); 063 } 064 } 065 } 066 067 /** 068 * See class comments. 069 */ 070 @SuppressWarnings("unchecked") 071 public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) 072 throws IOException, ServletException 073 { 074 log.debug("STARTING REQUEST..."); 075 HttpServletRequest req = (HttpServletRequest)request; 076 HttpSession userSession = req.getSession(true); 077 078 //get list of dao attr names from the httpsession 079 for (String daoName : this.sessionAttrNames) 080 { 081 Object dao = userSession.getAttribute(daoName); 082 if (dao != null && dao instanceof HibernateDao) 083 { 084 try 085 { 086 log.debug("beginning transaction..."); 087 ((HibernateDao)dao).beginTransaction(); 088 } 089 catch(HibernateException he) 090 { 091 log.error("Couldn't begin transaction from Filter.", he); 092 } 093 } 094 095 else 096 { 097 log.warn("No dao's found in the http session for name: " + daoName); 098 } 099 } 100 101 try 102 { 103 chain.doFilter(request, response); 104 } 105 106 finally 107 { 108 //get list of dao attr names from the httpsession 109 for (String daoName : this.sessionAttrNames) 110 { 111 Object dao = userSession.getAttribute(daoName); 112 if (dao != null && dao instanceof HibernateDao) 113 { 114 try 115 { 116 log.debug("About to send response, committing any open transactions..."); 117 ((HibernateDao)dao).commitTransaction(); 118 } 119 catch(HibernateException he) 120 { 121 log.error("Couldn't commit transactions from Filter.", he); 122 } 123 } 124 125 else 126 { 127 log.warn("No dao's found in the http session for name: " + daoName); 128 } 129 } 130 } 131 132 log.debug("FINISHED REQUEST."); 133 } 134 135 /** empty implementation */ 136 public void destroy() {} 137 }