1 9 package org.jboss.portal.core.servlet; 10 11 import java.io.IOException ; 12 13 import javax.naming.InitialContext ; 14 import javax.naming.NamingException ; 15 import javax.naming.Context ; 16 import javax.servlet.Filter ; 17 import javax.servlet.FilterChain ; 18 import javax.servlet.FilterConfig ; 19 import javax.servlet.ServletException ; 20 import javax.servlet.ServletRequest ; 21 import javax.servlet.ServletResponse ; 22 import javax.transaction.TransactionManager ; 23 import javax.transaction.Transaction ; 24 25 import org.apache.log4j.Logger; 26 import org.jboss.portal.common.transaction.Transactions; 27 import org.jboss.portal.common.transaction.NestedException; 28 import org.jboss.portal.common.transaction.TransactionException; 29 30 37 public class TransactionFilter implements Filter 38 { 39 40 42 43 private TransactionManager tm; 44 45 47 private static Logger log = Logger.getLogger(TransactionFilter.class); 48 49 51 public void init(FilterConfig filterConfig) throws ServletException 52 { 53 try 54 { 55 Context ctx = new InitialContext (); 56 tm = (TransactionManager )ctx.lookup("java:/TransactionManager"); 57 } 58 catch (NamingException e) 59 { 60 throw new ServletException (e); 61 } 62 } 63 64 public void destroy() 65 { 66 this.tm = null; 67 } 68 69 public void doFilter(final ServletRequest request, 70 final ServletResponse response, 71 final FilterChain chain) 72 throws ServletException , IOException 73 { 74 75 Transaction oldTx = null; 76 try 77 { 78 oldTx = Transactions.applyBefore(Transactions.TYPE_REQUIRED, tm); 79 chain.doFilter(request, response); 80 } 81 catch (TransactionException e) 82 { 83 throw new ServletException (e); 84 } 85 finally 86 { 87 try 88 { 89 Transactions.applyAfter(Transactions.TYPE_REQUIRED, tm, oldTx); 90 } 91 catch (TransactionException e) 92 { 93 log.error("", e); 94 } 95 } 96 } 97 } 98 | Popular Tags |