001: /*
002: * JBoss, Home of Professional Open Source.
003: * Copyright 2006, Red Hat Middleware LLC, and individual contributors
004: * as indicated by the @author tags. See the copyright.txt file in the
005: * distribution for a full listing of individual contributors.
006: *
007: * This is free software; you can redistribute it and/or modify it
008: * under the terms of the GNU Lesser General Public License as
009: * published by the Free Software Foundation; either version 2.1 of
010: * the License, or (at your option) any later version.
011: *
012: * This software is distributed in the hope that it will be useful,
013: * but WITHOUT ANY WARRANTY; without even the implied warranty of
014: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
015: * Lesser General Public License for more details.
016: *
017: * You should have received a copy of the GNU Lesser General Public
018: * License along with this software; if not, write to the Free
019: * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
020: * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
021: */
022: package org.jboss.ejb3.stateful;
023:
024: import java.util.Iterator;
025: import java.util.List;
026:
027: import java.rmi.RemoteException;
028:
029: import javax.ejb.ConcurrentAccessException;
030: import javax.ejb.EJBException;
031: import javax.ejb.ApplicationException;
032: import org.jboss.ejb3.EJBContainer;
033: import org.jboss.ejb3.metamodel.AssemblyDescriptor;
034: import org.jboss.annotation.ejb.SerializedConcurrentAccess;
035: import org.jboss.aop.advice.Interceptor;
036: import org.jboss.aop.joinpoint.Invocation;
037:
038: /**
039: * Comment
040: *
041: * @author <a href="mailto:bill@jboss.org">Bill Burke</a>
042: * @version $Revision: 57207 $
043: */
044: public class StatefulInstanceInterceptor implements Interceptor {
045:
046: public StatefulInstanceInterceptor() {
047: }
048:
049: public String getName() {
050: return null;
051: }
052:
053: public Object invoke(Invocation invocation) throws Throwable {
054: StatefulContainerInvocation ejb = (StatefulContainerInvocation) invocation;
055: Object id = ejb.getId();
056: StatefulContainer container = (StatefulContainer) ejb
057: .getAdvisor();
058: StatefulBeanContext target = container.getCache().get(id);
059:
060: boolean block = container
061: .resolveAnnotation(SerializedConcurrentAccess.class) != null;
062:
063: if (block) {
064: target.getLock().lockInterruptibly();
065: } else {
066: synchronized (target) {
067: if (target.isInInvocation())
068: throw new ConcurrentAccessException(
069: "no concurrent calls on stateful bean '"
070: + container.getName()
071: + "' (EJB3 4.3.13)");
072: target.setInInvocation(true);
073: }
074: }
075: ejb.setTargetObject(target.getInstance());
076: ejb.setBeanContext(target);
077: StatefulBeanContext.currentBean.push(target);
078: try {
079: if (target.isDiscarded())
080: throw new EJBException(
081: "SFSB was discarded by another thread");
082: return ejb.invokeNext();
083: } catch (Exception ex) {
084: if (isApplicationException(ex.getClass(), container))
085: throw ex;
086: if (ex instanceof RuntimeException
087: || ex instanceof RemoteException) {
088: container.getCache().remove(id);
089: target.setDiscarded(true);
090: }
091: throw ex;
092: } finally {
093: StatefulBeanContext.currentBean.pop();
094: synchronized (target) {
095: target.setInInvocation(false);
096: if (!target.isTxSynchronized() && !target.isDiscarded())
097: container.getCache().finished(target);
098: if (block)
099: target.getLock().unlock();
100: }
101: }
102: }
103:
104: public static boolean isApplicationException(
105: Class<?> exceptionClass, EJBContainer container) {
106: if (exceptionClass
107: .isAnnotationPresent(ApplicationException.class))
108: return true;
109:
110: AssemblyDescriptor assembly = container.getAssemblyDescriptor();
111: if (assembly != null) {
112: List exceptions = assembly.getApplicationExceptions();
113: if (exceptions.size() > 0) {
114: Iterator exceptionIterator = exceptions.iterator();
115: while (exceptionIterator.hasNext()) {
116: org.jboss.ejb3.metamodel.ApplicationException exception = (org.jboss.ejb3.metamodel.ApplicationException) exceptionIterator
117: .next();
118: if (exception.getExceptionClass().equals(
119: exceptionClass.getName()))
120: return true;
121: }
122: }
123:
124: }
125: return false;
126: }
127: }
|