1 25 26 package org.objectweb.easybeans.transaction.interceptors; 27 28 import java.rmi.RemoteException ; 29 import java.util.Map ; 30 import java.util.WeakHashMap ; 31 32 import javax.ejb.EJBException ; 33 import javax.ejb.SessionSynchronization ; 34 import javax.ejb.TransactionRolledbackLocalException ; 35 import javax.transaction.RollbackException ; 36 import javax.transaction.SystemException ; 37 38 import org.objectweb.easybeans.api.EasyBeansInvocationContext; 39 import org.objectweb.easybeans.log.JLog; 40 import org.objectweb.easybeans.log.JLogFactory; 41 import org.objectweb.easybeans.transaction.SessionSynchronizationListener; 42 43 48 public class ListenerSessionSynchronizationInterceptor extends AbsTransactionInterceptor { 49 50 53 private JLog logger = JLogFactory.getLog(ListenerSessionSynchronizationInterceptor.class); 54 55 58 private Map <Object , SessionSynchronizationListener> listeners = new WeakHashMap <Object , SessionSynchronizationListener>(); 59 60 69 @Override 70 public Object intercept(final EasyBeansInvocationContext invocationContext) throws Exception { 71 logger.debug("Calling ListenerSessionSynchronizationInterceptor interceptor"); 72 if (getTransactionManager().getTransaction() != null) { 73 addSynchronization(invocationContext); 74 } else { 75 logger.warn("No transaction but the bean is implementing session synchonization interface."); 76 } 77 return invocationContext.proceed(); 78 } 79 80 86 private void addSynchronization(final EasyBeansInvocationContext invocationContext) { 87 Object o = invocationContext.getTarget(); 88 if (!(o instanceof SessionSynchronization )) { 89 throw new IllegalArgumentException ("This interceptor should not have been added on this " 90 + "bean which doesn't implement SessionSynchronization interface."); 91 } 92 SessionSynchronization bean = (SessionSynchronization ) o; 93 94 105 106 SessionSynchronizationListener sessionSynchronizationListener = listeners.get(bean); 109 if (sessionSynchronizationListener == null) { 110 sessionSynchronizationListener = new SessionSynchronizationListener(bean); 111 listeners.put(bean, sessionSynchronizationListener); 112 } 113 114 if (sessionSynchronizationListener.isReady()) { 116 117 try { 118 getTransactionManager().getTransaction().registerSynchronization(sessionSynchronizationListener); 119 } catch (IllegalStateException e) { 120 throw new EJBException ("Cannot register the synchronization", e); 121 } catch (RollbackException e) { 122 throw new TransactionRolledbackLocalException ("Session rolled back"); 123 } catch (SystemException e) { 124 throw new EJBException ("Cannot register the synchronization", e); 125 } 126 127 try { 129 bean.afterBegin(); 130 } catch (EJBException e) { 131 throw e; 132 } catch (RemoteException e) { 133 throw new EJBException ("Cannot call afterBefin method", e); 134 } 135 sessionSynchronizationListener.inTX(); 136 } 137 138 } 139 } 140 | Popular Tags |