1 22 package org.jboss.ejb3.stateful; 23 24 import java.util.Iterator ; 25 import java.util.List ; 26 27 import java.rmi.RemoteException ; 28 29 import javax.ejb.ConcurrentAccessException ; 30 import javax.ejb.EJBException ; 31 import javax.ejb.ApplicationException ; 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 44 public class StatefulInstanceInterceptor implements Interceptor 45 { 46 47 public StatefulInstanceInterceptor() 48 { 49 } 50 51 public String getName() 52 { 53 return null; 54 } 55 56 public Object invoke(Invocation invocation) throws Throwable 57 { 58 StatefulContainerInvocation ejb = (StatefulContainerInvocation) invocation; 59 Object 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 ("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 ("SFSB was discarded by another thread"); 83 return ejb.invokeNext(); 84 } 85 catch (Exception ex) 86 { 87 if (isApplicationException(ex.getClass(), container)) throw ex; 88 if (ex instanceof RuntimeException 89 || ex instanceof RemoteException ) 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 <?> exceptionClass, EJBContainer container) 109 { 110 if (exceptionClass.isAnnotationPresent(ApplicationException .class)) 111 return true; 112 113 AssemblyDescriptor assembly = container.getAssemblyDescriptor(); 114 if (assembly != null) 115 { 116 List exceptions = assembly.getApplicationExceptions(); 117 if (exceptions.size() > 0) 118 { 119 Iterator 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 |