KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jboss > ejb3 > stateful > SessionSynchronizationInterceptor


1 /*
2   * JBoss, Home of Professional Open Source
3   * Copyright 2005, JBoss Inc., and individual contributors as indicated
4   * by the @authors tag. See the copyright.txt in the distribution for a
5   * full listing of individual contributors.
6   *
7   * This is free software; you can redistribute it and/or modify it
8   * under the terms of the GNU Lesser General Public License as
9   * published by the Free Software Foundation; either version 2.1 of
10   * 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 of
14   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15   * Lesser General Public License for more details.
16   *
17   * You should have received a copy of the GNU Lesser General Public
18   * License along with this software; if not, write to the Free
19   * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
20   * 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 JavaDoc;
25 import javax.ejb.EJBException JavaDoc;
26 import javax.ejb.SessionSynchronization JavaDoc;
27 import javax.transaction.RollbackException JavaDoc;
28 import javax.transaction.Status JavaDoc;
29 import javax.transaction.Synchronization JavaDoc;
30 import javax.transaction.SystemException JavaDoc;
31 import javax.transaction.Transaction JavaDoc;
32 import javax.transaction.TransactionManager JavaDoc;
33 import org.jboss.aop.advice.Interceptor;
34 import org.jboss.aop.joinpoint.Invocation;
35 import org.jboss.ejb3.tx.TxUtil;
36
37 /**
38  * Comment
39  *
40  * @author <a HREF="mailto:bill@jboss.org">Bill Burke</a>
41  * @version $Revision: 44568 $
42  */

43 public class SessionSynchronizationInterceptor implements Interceptor
44 {
45    private TransactionManager JavaDoc tm;
46
47    public SessionSynchronizationInterceptor()
48    {
49       this.tm = TxUtil.getTransactionManager();
50    }
51
52    public String JavaDoc getName()
53    {
54       return null;
55    }
56
57    protected static class SFSBSessionSynchronization implements Synchronization JavaDoc
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 JavaDoc bean = (SessionSynchronization JavaDoc) ctx.getInstance();
69          try
70          {
71             bean.beforeCompletion();
72          }
73          catch (RemoteException JavaDoc e)
74          {
75             throw new RuntimeException JavaDoc(e);
76          }
77       }
78
79       public void afterCompletion(int status)
80       {
81          ctx.setTxSynchronized(false);
82          SessionSynchronization JavaDoc bean = (SessionSynchronization JavaDoc) ctx.getInstance();
83          try
84          {
85             if (status == Status.STATUS_COMMITTED)
86             {
87                bean.afterCompletion(true);
88             }
89             else
90             {
91                bean.afterCompletion(false);
92             }
93          }
94          catch (RemoteException JavaDoc ignore)
95          {
96          }
97          finally
98          {
99             StatefulContainer container = (StatefulContainer) ctx.getContainer();
100             container.getCache().finished(ctx);
101          }
102       }
103    }
104
105    protected void registerSessionSynchronization(StatefulBeanContext ctx) throws SystemException JavaDoc
106    {
107       if (ctx.isTxSynchronized()) return;
108       Transaction JavaDoc 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 JavaDoc bean = (SessionSynchronization JavaDoc) ctx.getInstance();
113       try
114       {
115          bean.afterBegin();
116       }
117       catch (EJBException JavaDoc e)
118       {
119          throw e;
120       }
121       catch (RemoteException JavaDoc e)
122       {
123          throw new EJBException JavaDoc(e);
124       }
125       try
126       {
127          tx.registerSynchronization(synch);
128       }
129       catch (RollbackException JavaDoc ignore)
130       {
131       }
132    }
133
134    public Object JavaDoc invoke(Invocation invocation) throws Throwable JavaDoc
135    {
136       StatefulContainerInvocation ejb = (StatefulContainerInvocation) invocation;
137       StatefulBeanContext target = (StatefulBeanContext) ejb.getBeanContext();
138       if (target.getInstance() instanceof SessionSynchronization JavaDoc)
139       {
140          registerSessionSynchronization(target);
141       }
142       return ejb.invokeNext();
143    }
144 }
145
Popular Tags