KickJava   Java API By Example, From Geeks To Geeks.

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


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 javax.transaction.Synchronization JavaDoc;
25 import javax.transaction.Transaction JavaDoc;
26 import javax.transaction.SystemException JavaDoc;
27 import javax.transaction.RollbackException JavaDoc;
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 /**
36  * Handles @Remove on a Stateful bean.
37  *
38  * @author <a HREF="mailto:bill@jboss.org">Bill Burke</a>
39  */

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 JavaDoc getName()
51    {
52       return this.getClass().getName();
53    }
54
55    private static class RemoveSynchronization implements Synchronization JavaDoc
56    {
57       protected boolean retainIfException;
58       private StatefulContainer container;
59       private Object JavaDoc id;
60
61       public RemoveSynchronization(StatefulContainer container, Object JavaDoc 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 JavaDoc t)
81          {
82             if (!retainIfException)
83             {
84                container.getCache().remove(id);
85             }
86             throw new RuntimeException JavaDoc(t);
87          }
88          container.getCache().remove(id);
89       }
90    }
91
92    public Object JavaDoc invoke(Invocation invocation) throws Throwable JavaDoc
93    {
94       Object JavaDoc rtn = null;
95       try
96       {
97          rtn = invocation.invokeNext();
98       }
99       catch (Throwable JavaDoc t)
100       {
101          // don't remove if we're an applicationexception and retain is true
102
if (TxUtil.getApplicationException(t.getClass(), invocation) != null && retainIfException) throw t;
103
104          // otherwise, just remove it.
105
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 JavaDoc
113    {
114       StatefulContainerInvocation ejb = (StatefulContainerInvocation) invocation;
115       StatefulBeanContext ctx = (StatefulBeanContext)ejb.getBeanContext();
116       if (ctx.isDiscarded() || ctx.isRemoved()) return;
117       Object JavaDoc id = ejb.getId();
118
119
120       StatefulContainer container = (StatefulContainer) ejb.getAdvisor();
121       Transaction JavaDoc tx = null;
122       try
123       {
124          tx = TxUtil.getTransactionManager().getTransaction();
125       }
126       catch (SystemException JavaDoc e)
127       {
128          throw new RuntimeException JavaDoc(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 JavaDoc e)
138          {
139             throw new RuntimeException JavaDoc(e);
140          }
141          catch (SystemException JavaDoc e)
142          {
143             throw new RuntimeException JavaDoc(e);
144          }
145       }
146       else
147       {
148          container.getCache().remove(id);
149       }
150    }
151 }
152
Popular Tags