KickJava   Java API By Example, From Geeks To Geeks.

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


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.util.Iterator JavaDoc;
25 import java.util.List JavaDoc;
26
27 import java.rmi.RemoteException JavaDoc;
28
29 import javax.ejb.ConcurrentAccessException JavaDoc;
30 import javax.ejb.EJBException JavaDoc;
31 import javax.ejb.ApplicationException JavaDoc;
32 import org.jboss.ejb3.EJBContainer;
33 import org.jboss.ejb3.metamodel.AssemblyDescriptor;
34 import org.jboss.annotation.ejb.SerializedConcurrentAccess;
35 import org.jboss.aop.advice.Interceptor;
36 import org.jboss.aop.joinpoint.Invocation;
37
38 /**
39  * Comment
40  *
41  * @author <a HREF="mailto:bill@jboss.org">Bill Burke</a>
42  * @version $Revision: 56573 $
43  */

44 public class StatefulInstanceInterceptor implements Interceptor
45 {
46
47    public StatefulInstanceInterceptor()
48    {
49    }
50
51    public String JavaDoc getName()
52    {
53       return null;
54    }
55
56    public Object JavaDoc invoke(Invocation invocation) throws Throwable JavaDoc
57    {
58       StatefulContainerInvocation ejb = (StatefulContainerInvocation) invocation;
59       Object JavaDoc id = ejb.getId();
60       StatefulContainer container = (StatefulContainer) ejb.getAdvisor();
61       StatefulBeanContext target = container.getCache().get(id);
62
63       boolean block = container.resolveAnnotation(SerializedConcurrentAccess.class) != null;
64
65       if (block)
66       {
67          target.getLock().lockInterruptibly();
68       }
69       else
70       {
71          synchronized (target)
72          {
73             if (target.isInInvocation()) throw new ConcurrentAccessException JavaDoc("no concurrent calls on stateful bean '" + container.getName() + "' (EJB3 4.3.13)");
74             target.setInInvocation(true);
75          }
76       }
77       ejb.setTargetObject(target.getInstance());
78       ejb.setBeanContext(target);
79       StatefulBeanContext.currentBean.push(target);
80       try
81       {
82          if (target.isDiscarded()) throw new EJBException JavaDoc("SFSB was discarded by another thread");
83          return ejb.invokeNext();
84       }
85       catch (Exception JavaDoc ex)
86       {
87          if (isApplicationException(ex.getClass(), container)) throw ex;
88          if (ex instanceof RuntimeException JavaDoc
89                  || ex instanceof RemoteException JavaDoc)
90          {
91             container.getCache().remove(id);
92             target.setDiscarded(true);
93          }
94          throw ex;
95       }
96       finally
97       {
98          StatefulBeanContext.currentBean.pop();
99          synchronized (target)
100          {
101             target.setInInvocation(false);
102             if (!target.isTxSynchronized() && !target.isDiscarded()) container.getCache().finished(target);
103             if (block) target.getLock().unlock();
104          }
105       }
106    }
107    
108    public static boolean isApplicationException(Class JavaDoc<?> exceptionClass, EJBContainer container)
109    {
110       if (exceptionClass.isAnnotationPresent(ApplicationException JavaDoc.class))
111          return true;
112       
113       AssemblyDescriptor assembly = container.getAssemblyDescriptor();
114       if (assembly != null)
115       {
116          List JavaDoc exceptions = assembly.getApplicationExceptions();
117          if (exceptions.size() > 0)
118          {
119             Iterator JavaDoc exceptionIterator = exceptions.iterator();
120             while (exceptionIterator.hasNext())
121             {
122                org.jboss.ejb3.metamodel.ApplicationException exception = (org.jboss.ejb3.metamodel.ApplicationException)exceptionIterator.next();
123                if (exception.getExceptionClass().equals(exceptionClass.getName()))
124                   return true;
125             }
126          }
127          
128       }
129       return false;
130    }
131 }
132
Popular Tags