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