1 9 package org.jboss.portal.core.invocation; 10 11 import org.jboss.portal.server.invocation.Interceptor; 12 import org.jboss.portal.server.invocation.Invocation; 13 import org.jboss.portal.server.Window; 14 import org.jboss.portal.server.Instance; 15 import org.jboss.portal.server.Component; 16 import org.jboss.portal.server.WindowContext; 17 import org.jboss.portal.server.output.ErrorResult; 18 import org.jboss.portal.portlet.invocation.PortletKey; 19 import org.jboss.portal.common.transaction.Transactions; 20 import org.jboss.portal.common.transaction.TransactionException; 21 import org.jboss.portal.core.plugins.transaction.TransactionPlugin; 22 23 import javax.transaction.TransactionManager ; 24 import javax.transaction.Transaction ; 25 import javax.naming.Context ; 26 import javax.naming.InitialContext ; 27 import javax.naming.NamingException ; 28 29 33 public class TransactionInterceptor implements Interceptor 34 { 35 36 37 private TransactionManager tm; 38 39 public Object invoke(final Invocation invocation) 40 { 41 WindowContext windowContext = (WindowContext)invocation.getAttachment(PortletKey.WINDOW_CONTEXT); 42 43 try 44 { 45 TransactionManager tm = getTransactionManager(); 47 Transactions.Type txType = Transactions.TYPE_NOT_SUPPORTED; 48 49 Window window = (Window)invocation.getAttachment(PortletKey.WINDOW); 51 Instance instance = window.getInstance(); 52 Component component = instance.getComponent(); 53 TransactionPlugin txPlugin = (TransactionPlugin)component.getPlugin("Transaction"); 54 if (txPlugin != null) 55 { 56 txType = txPlugin.getTxType(); 57 } 58 59 Transaction oldTx = null; 61 try 62 { 63 oldTx = Transactions.applyBefore(txType, tm); 64 return invocation.invokeNext(); 65 } 66 catch (TransactionException e) 67 { 68 return new ErrorResult(windowContext, e); 69 } 70 finally 71 { 72 try 73 { 74 Transactions.applyAfter(txType, tm, oldTx); 75 } 76 catch (TransactionException e) 77 { 78 e.printStackTrace(); 79 } 80 } 81 } 82 catch (NamingException e) 83 { 84 return new ErrorResult(windowContext, e); 85 } 86 } 87 88 private TransactionManager getTransactionManager() throws NamingException 89 { 90 if (tm == null) 91 { 92 Context ctx = new InitialContext (); 93 tm = (TransactionManager )ctx.lookup("java:/TransactionManager"); 94 } 95 return tm; 96 } 97 } 98 | Popular Tags |