PageRenderTime 54ms CodeModel.GetById 31ms app.highlight 19ms RepoModel.GetById 1ms app.codeStats 0ms

/jboss-as-7.1.1.Final/ejb3/src/main/java/org/jboss/as/ejb3/component/interceptors/EjbExceptionTransformingInterceptorFactories.java

#
Java | 120 lines | 75 code | 10 blank | 35 comment | 4 complexity | 423f04c397ad8e38434a3ea70ffbbf0f MD5 | raw file
Possible License(s): LGPL-2.1, Apache-2.0
  1/*
  2 * JBoss, Home of Professional Open Source
  3 * Copyright 2010, Red Hat 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 */
 22package org.jboss.as.ejb3.component.interceptors;
 23
 24import java.rmi.NoSuchObjectException;
 25import java.rmi.RemoteException;
 26
 27import javax.ejb.CreateException;
 28import javax.ejb.EJBException;
 29import javax.ejb.EJBTransactionRequiredException;
 30import javax.ejb.EJBTransactionRolledbackException;
 31import javax.ejb.NoSuchEJBException;
 32import javax.ejb.NoSuchEntityException;
 33import javax.ejb.NoSuchObjectLocalException;
 34import javax.ejb.TransactionRequiredLocalException;
 35import javax.ejb.TransactionRolledbackLocalException;
 36import javax.transaction.TransactionRequiredException;
 37import javax.transaction.TransactionRolledbackException;
 38
 39import org.jboss.invocation.ImmediateInterceptorFactory;
 40import org.jboss.invocation.Interceptor;
 41import org.jboss.invocation.InterceptorContext;
 42import org.jboss.invocation.InterceptorFactory;
 43
 44/**
 45 * An interceptor that transforms EJB 3.0 business interface exceptions to EJB 2.x exceptions when required.
 46 * <p/>
 47 * This allows us to keep the actual
 48 *
 49 * @author Stuart Douglas
 50 */
 51public class EjbExceptionTransformingInterceptorFactories {
 52
 53    /**
 54     * We need to return a CreateException to the client.
 55     * <p/>
 56     * Rather than forcing all create exceptions everywhere to propagate, and generally making a mess, we stash
 57     * the exception here, and then re-throw it from the exception transforming interceptor.
 58     */
 59    private static final ThreadLocal<CreateException> CREATE_EXCEPTION = new ThreadLocal<CreateException>();
 60
 61    public static final InterceptorFactory REMOTE_INSTANCE = new ImmediateInterceptorFactory(new Interceptor() {
 62        @Override
 63        public Object processInvocation(final InterceptorContext context) throws Exception {
 64            try {
 65                return context.proceed();
 66            } catch (EJBTransactionRequiredException e) {
 67                throw new TransactionRequiredException(e.getMessage());
 68            } catch (EJBTransactionRolledbackException e) {
 69                throw new TransactionRolledbackException(e.getMessage());
 70            } catch (NoSuchEJBException e) {
 71                throw new NoSuchObjectException(e.getMessage());
 72            } catch (NoSuchEntityException e) {
 73                throw new NoSuchObjectException(e.getMessage());
 74            } catch (EJBException e) {
 75                //as the create exception is not propagated the init method interceptor just stashes it in a ThreadLocal
 76                CreateException createException = popCreateException();
 77                if (createException != null) {
 78                    throw createException;
 79                }
 80                throw new RemoteException("Invocation failed", e);
 81            }
 82        }
 83    });
 84
 85    public static final InterceptorFactory LOCAL_INSTANCE = new ImmediateInterceptorFactory(new Interceptor() {
 86        @Override
 87        public Object processInvocation(final InterceptorContext context) throws Exception {
 88            try {
 89                return context.proceed();
 90            } catch (EJBTransactionRequiredException e) {
 91                throw new TransactionRequiredLocalException(e.getMessage());
 92            } catch (EJBTransactionRolledbackException e) {
 93                throw new TransactionRolledbackLocalException(e.getMessage());
 94            } catch (NoSuchEJBException e) {
 95                throw new NoSuchObjectLocalException(e.getMessage());
 96            } catch (NoSuchEntityException e) {
 97                throw new NoSuchObjectLocalException(e.getMessage());
 98            } catch (EJBException e) {
 99                CreateException createException = popCreateException();
100                if (createException != null) {
101                    throw createException;
102                }
103                throw e;
104            }
105        }
106    });
107
108    public static void setCreateException(CreateException exception) {
109        CREATE_EXCEPTION.set(exception);
110    }
111
112    public static CreateException popCreateException() {
113        try {
114            return CREATE_EXCEPTION.get();
115        } finally {
116            CREATE_EXCEPTION.remove();
117        }
118    }
119
120}