1 /*2 * JBoss, Home of Professional Open Source3 * Copyright 2005, JBoss Inc., and individual contributors as indicated4 * by the @authors tag. See the copyright.txt in the distribution for a5 * full listing of individual contributors.6 *7 * This is free software; you can redistribute it and/or modify it8 * under the terms of the GNU Lesser General Public License as9 * published by the Free Software Foundation; either version 2.1 of10 * the License, or (at your option) any later version.11 *12 * This software is distributed in the hope that it will be useful,13 * but WITHOUT ANY WARRANTY; without even the implied warranty of14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU15 * Lesser General Public License for more details.16 *17 * You should have received a copy of the GNU Lesser General Public18 * License along with this software; if not, write to the Free19 * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA20 * 02110-1301 USA, or see the FSF site: http://www.fsf.org.21 */22 package org.jboss.ejb3.stateful;23 24 import java.rmi.RemoteException ;25 import javax.ejb.EJBException ;26 import javax.ejb.SessionSynchronization ;27 import javax.transaction.RollbackException ;28 import javax.transaction.Status ;29 import javax.transaction.Synchronization ;30 import javax.transaction.SystemException ;31 import javax.transaction.Transaction ;32 import javax.transaction.TransactionManager ;33 import org.jboss.aop.advice.Interceptor;34 import org.jboss.aop.joinpoint.Invocation;35 import org.jboss.ejb3.tx.TxUtil;36 37 /**38 * Comment39 *40 * @author <a HREF="mailto:bill@jboss.org">Bill Burke</a>41 * @version $Revision: 44568 $42 */43 public class SessionSynchronizationInterceptor implements Interceptor44 {45 private TransactionManager tm;46 47 public SessionSynchronizationInterceptor()48 {49 this.tm = TxUtil.getTransactionManager();50 }51 52 public String getName()53 {54 return null;55 }56 57 protected static class SFSBSessionSynchronization implements Synchronization 58 {59 private StatefulBeanContext ctx;60 61 public SFSBSessionSynchronization(StatefulBeanContext ctx)62 {63 this.ctx = ctx;64 }65 66 public void beforeCompletion()67 {68 SessionSynchronization bean = (SessionSynchronization ) ctx.getInstance();69 try70 {71 bean.beforeCompletion();72 }73 catch (RemoteException e)74 {75 throw new RuntimeException (e);76 }77 }78 79 public void afterCompletion(int status)80 {81 ctx.setTxSynchronized(false);82 SessionSynchronization bean = (SessionSynchronization ) ctx.getInstance();83 try84 {85 if (status == Status.STATUS_COMMITTED)86 {87 bean.afterCompletion(true);88 }89 else90 {91 bean.afterCompletion(false);92 }93 }94 catch (RemoteException ignore)95 {96 }97 finally98 {99 StatefulContainer container = (StatefulContainer) ctx.getContainer();100 container.getCache().finished(ctx);101 }102 }103 }104 105 protected void registerSessionSynchronization(StatefulBeanContext ctx) throws SystemException 106 {107 if (ctx.isTxSynchronized()) return;108 Transaction tx = tm.getTransaction();109 if (tx == null) return;110 if (tx.getStatus() == Status.STATUS_MARKED_ROLLBACK) return;111 SFSBSessionSynchronization synch = new SFSBSessionSynchronization(ctx);112 SessionSynchronization bean = (SessionSynchronization ) ctx.getInstance();113 try114 {115 bean.afterBegin();116 }117 catch (EJBException e)118 {119 throw e;120 }121 catch (RemoteException e)122 {123 throw new EJBException (e);124 }125 try126 {127 tx.registerSynchronization(synch);128 }129 catch (RollbackException ignore)130 {131 }132 }133 134 public Object invoke(Invocation invocation) throws Throwable 135 {136 StatefulContainerInvocation ejb = (StatefulContainerInvocation) invocation;137 StatefulBeanContext target = (StatefulBeanContext) ejb.getBeanContext();138 if (target.getInstance() instanceof SessionSynchronization )139 {140 registerSessionSynchronization(target);141 }142 return ejb.invokeNext();143 }144 }145