1 22 package org.jboss.ejb3.stateful; 23 24 import javax.transaction.Synchronization ; 25 import javax.transaction.Transaction ; 26 import javax.transaction.SystemException ; 27 import javax.transaction.RollbackException ; 28 import org.jboss.aop.advice.Interceptor; 29 import org.jboss.aop.joinpoint.Invocation; 30 import org.jboss.ejb3.tx.TxUtil; 31 import org.jboss.ejb3.BeanContext; 32 import org.jboss.logging.Logger; 33 import org.jboss.tm.TxUtils; 34 35 40 public class StatefulRemoveInterceptor implements Interceptor 41 { 42 private static final Logger log = Logger.getLogger(StatefulRemoveInterceptor.class); 43 protected boolean retainIfException; 44 45 public StatefulRemoveInterceptor(boolean removeOnException) 46 { 47 this.retainIfException = removeOnException; 48 } 49 50 public String getName() 51 { 52 return this.getClass().getName(); 53 } 54 55 private static class RemoveSynchronization implements Synchronization 56 { 57 protected boolean retainIfException; 58 private StatefulContainer container; 59 private Object id; 60 61 public RemoveSynchronization(StatefulContainer container, Object id, boolean removeOnException) 62 { 63 this.container = container; 64 this.id = id; 65 this.retainIfException = removeOnException; 66 } 67 68 69 public void beforeCompletion() 70 { 71 } 72 73 public void afterCompletion(int status) 74 { 75 try 76 { 77 StatefulBeanContext ctx = container.getCache().get(id); 78 container.invokePreDestroy(ctx); 79 } 80 catch (Throwable t) 81 { 82 if (!retainIfException) 83 { 84 container.getCache().remove(id); 85 } 86 throw new RuntimeException (t); 87 } 88 container.getCache().remove(id); 89 } 90 } 91 92 public Object invoke(Invocation invocation) throws Throwable 93 { 94 Object rtn = null; 95 try 96 { 97 rtn = invocation.invokeNext(); 98 } 99 catch (Throwable t) 100 { 101 if (TxUtil.getApplicationException(t.getClass(), invocation) != null && retainIfException) throw t; 103 104 removeSession(invocation, true); 106 throw t; 107 } 108 removeSession(invocation, false); 109 return rtn; 110 } 111 112 protected void removeSession(Invocation invocation, boolean exceptionThrown) throws Throwable 113 { 114 StatefulContainerInvocation ejb = (StatefulContainerInvocation) invocation; 115 StatefulBeanContext ctx = (StatefulBeanContext)ejb.getBeanContext(); 116 if (ctx.isDiscarded() || ctx.isRemoved()) return; 117 Object id = ejb.getId(); 118 119 120 StatefulContainer container = (StatefulContainer) ejb.getAdvisor(); 121 Transaction tx = null; 122 try 123 { 124 tx = TxUtil.getTransactionManager().getTransaction(); 125 } 126 catch (SystemException e) 127 { 128 throw new RuntimeException (e); 129 } 130 131 if (tx != null && TxUtils.isActive(tx)) 132 { 133 try 134 { 135 tx.registerSynchronization(new RemoveSynchronization(container, id, exceptionThrown != true && retainIfException)); 136 } 137 catch (RollbackException e) 138 { 139 throw new RuntimeException (e); 140 } 141 catch (SystemException e) 142 { 143 throw new RuntimeException (e); 144 } 145 } 146 else 147 { 148 container.getCache().remove(id); 149 } 150 } 151 } 152 | Popular Tags |