moves the commons code to the AbstractEndPointHandler (cherry picked from commit 5c66e03dda516a86057d5605c40eca1ee658c3c8)
Project: http://git-wip-us.apache.org/repos/asf/tomee/repo Commit: http://git-wip-us.apache.org/repos/asf/tomee/commit/75dc2160 Tree: http://git-wip-us.apache.org/repos/asf/tomee/tree/75dc2160 Diff: http://git-wip-us.apache.org/repos/asf/tomee/diff/75dc2160 Branch: refs/heads/tomee-1.7.x Commit: 75dc2160caade2da0f823fad3ce161c44a472587 Parents: a771ef6 Author: Otavio Santana <[email protected]> Authored: Fri Dec 15 08:02:29 2017 -0300 Committer: Thiago Veronezi <[email protected]> Committed: Wed Jan 17 12:55:34 2018 -0500 ---------------------------------------------------------------------- .../openejb/core/mdb/EndpointHandler.java | 16 +- .../openejb/core/mdb/PoolEndpointHandler.java | 149 ++----------------- 2 files changed, 12 insertions(+), 153 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/tomee/blob/75dc2160/container/openejb-core/src/main/java/org/apache/openejb/core/mdb/EndpointHandler.java ---------------------------------------------------------------------- diff --git a/container/openejb-core/src/main/java/org/apache/openejb/core/mdb/EndpointHandler.java b/container/openejb-core/src/main/java/org/apache/openejb/core/mdb/EndpointHandler.java index 0ab2938..e87e8fc 100644 --- a/container/openejb-core/src/main/java/org/apache/openejb/core/mdb/EndpointHandler.java +++ b/container/openejb-core/src/main/java/org/apache/openejb/core/mdb/EndpointHandler.java @@ -66,6 +66,7 @@ public class EndpointHandler extends AbstractEndpointHandler { // before completed successfully we are now ready to invoke bean state = State.BEFORE_CALLED; } + @Override protected void recreateInstance(final boolean exceptionAlreadyThrown) throws UnavailableException { try { @@ -82,6 +83,7 @@ public class EndpointHandler extends AbstractEndpointHandler { } } + @Override public void release() { if (state == State.RELEASED) { return; @@ -96,18 +98,4 @@ public class EndpointHandler extends AbstractEndpointHandler { instance = null; } } - - private boolean isValidException(final Method method, final Throwable throwable) { - if (throwable instanceof RuntimeException || throwable instanceof Error) { - return true; - } - - final Class<?>[] exceptionTypes = method.getExceptionTypes(); - for (final Class<?> exceptionType : exceptionTypes) { - if (exceptionType.isInstance(throwable)) { - return true; - } - } - return false; - } } http://git-wip-us.apache.org/repos/asf/tomee/blob/75dc2160/container/openejb-core/src/main/java/org/apache/openejb/core/mdb/PoolEndpointHandler.java ---------------------------------------------------------------------- diff --git a/container/openejb-core/src/main/java/org/apache/openejb/core/mdb/PoolEndpointHandler.java b/container/openejb-core/src/main/java/org/apache/openejb/core/mdb/PoolEndpointHandler.java index f507a35..8fe722a 100644 --- a/container/openejb-core/src/main/java/org/apache/openejb/core/mdb/PoolEndpointHandler.java +++ b/container/openejb-core/src/main/java/org/apache/openejb/core/mdb/PoolEndpointHandler.java @@ -17,77 +17,34 @@ package org.apache.openejb.core.mdb; -import org.apache.openejb.ApplicationException; import org.apache.openejb.BeanContext; import org.apache.openejb.OpenEJBException; import org.apache.openejb.SystemException; import org.apache.openejb.core.ThreadContext; -import org.apache.openejb.resource.activemq.jms2.DelegateMessage; -import org.apache.openejb.resource.activemq.jms2.JMS2; -import javax.ejb.EJBException; -import javax.jms.Message; import javax.resource.spi.ApplicationServerInternalException; import javax.resource.spi.UnavailableException; -import javax.resource.spi.endpoint.MessageEndpoint; import javax.transaction.xa.XAResource; -import java.lang.reflect.InvocationHandler; import java.lang.reflect.Method; -import java.util.Arrays; -public class PoolEndpointHandler implements InvocationHandler, MessageEndpoint { - private volatile Boolean isAmq; +public class PoolEndpointHandler extends AbstractEndpointHandler { - - private final BaseMdbContainer container; private final BeanContext deployment; private final MdbInstanceManager instanceManager; private final XAResource xaResource; - private State state = State.NONE; - private Object instance; private ThreadContext callContext; + public PoolEndpointHandler(final BaseMdbContainer container, final BeanContext deployment, final MdbInstanceManager instanceManager, final XAResource xaResource) throws UnavailableException { - this.container = container; + super(container); this.deployment = deployment; this.instanceManager = instanceManager; this.xaResource = xaResource; this.callContext = ThreadContext.getThreadContext(); } - public Object invoke(final Object proxy, final Method method, final Object[] args) throws Throwable { - - final String methodName = method.getName(); - final Class<?>[] parameterTypes = method.getParameterTypes(); - - if (method.getDeclaringClass() == Object.class) { - if ("toString".equals(methodName) && parameterTypes.length == 0) { - return toString(); - } else if ("equals".equals(methodName) && parameterTypes.length == 1) { - return equals(args[0]); - } else if ("hashCode".equals(methodName) && parameterTypes.length == 0) { - return hashCode(); - } else { - throw new UnsupportedOperationException("Unkown method: " + method); - } - } - - if ("beforeDelivery".equals(methodName) && Arrays.deepEquals(new Class[]{Method.class}, parameterTypes)) { - beforeDelivery((Method) args[0]); - return null; - } else if ("afterDelivery".equals(methodName) && parameterTypes.length == 0) { - afterDelivery(); - return null; - } else if ("release".equals(methodName) && parameterTypes.length == 0) { - release(); - return null; - } else { - final Object value = deliverMessage(method, args); - return value; - } - } - + @Override public void beforeDelivery(final Method method) throws ApplicationServerInternalException { // verify current state switch (state) { @@ -107,7 +64,7 @@ public class PoolEndpointHandler implements InvocationHandler, MessageEndpoint { } catch (final SystemException se) { final Throwable throwable = se.getRootCause() != null ? se.getRootCause() : se; throw new ApplicationServerInternalException(throwable); - } catch (OpenEJBException oe){ + } catch (OpenEJBException oe) { throw new ApplicationServerInternalException(oe); } @@ -115,83 +72,11 @@ public class PoolEndpointHandler implements InvocationHandler, MessageEndpoint { state = State.BEFORE_CALLED; } - public Object deliverMessage(final Method method, final Object[] args) throws Throwable { - - boolean callBeforeAfter = false; - - // verify current state - switch (state) { - case NONE: - try { - beforeDelivery(method); - } catch (final ApplicationServerInternalException e) { - throw (EJBException) new EJBException().initCause(e.getCause()); - } - callBeforeAfter = true; - state = State.METHOD_CALLED; - break; - case BEFORE_CALLED: - state = State.METHOD_CALLED; - break; - case RELEASED: - throw new IllegalStateException("Message endpoint factory has been released"); - case METHOD_CALLED: - case SYSTEM_EXCEPTION: - throw new IllegalStateException("The last message delivery must be completed with an afterDeliver before another message can be delivered"); - } - - Throwable throwable = null; - Object value = null; - try { - // deliver the message - value = container.invoke(instance, method, null, wrapMessageForAmq5(args)); - } catch (final SystemException se) { - throwable = se.getRootCause() != null ? se.getRootCause() : se; - state = State.SYSTEM_EXCEPTION; - } catch (final ApplicationException ae) { - throwable = ae.getRootCause() != null ? ae.getRootCause() : ae; - } finally { - // if the adapter is not using before/after, we must call afterDelivery to clean up - if (callBeforeAfter) { - try { - afterDelivery(); - } catch (final ApplicationServerInternalException e) { - throwable = throwable == null ? e.getCause() : throwable; - } catch (final UnavailableException e) { - throwable = throwable == null ? e : throwable; - } - } - } + @Override + protected void recreateInstance(boolean exceptionAlreadyThrown) throws UnavailableException { - if (throwable != null) { - throwable.printStackTrace(); - if (isValidException(method, throwable)) { - throw throwable; - } else { - throw new EJBException().initCause(throwable); - } - } - return value; } - // workaround for AMQ 5/JMS 2 support - private Object[] wrapMessageForAmq5(final Object[] args) { - if (args == null || args.length != 1 || DelegateMessage.class.isInstance(args[0])) { - return args; - } - - if (isAmq == null) { - synchronized (this) { - if (isAmq == null) { - isAmq = args[0].getClass().getName().startsWith("org.apache.activemq."); - } - } - } - if (isAmq) { - args[0] = JMS2.wrap(Message.class.cast(args[0])); - } - return args; - } public void afterDelivery() throws ApplicationServerInternalException, UnavailableException { // verify current state @@ -223,7 +108,7 @@ public class PoolEndpointHandler implements InvocationHandler, MessageEndpoint { } - + @Override public void release() { if (state == State.RELEASED) { return; @@ -234,26 +119,12 @@ public class PoolEndpointHandler implements InvocationHandler, MessageEndpoint { try { container.release(deployment, instance); } finally { - try{ + try { instanceManager.poolInstance(callContext, instance); - } catch (OpenEJBException e){ + } catch (OpenEJBException e) { e.printStackTrace(); } instance = null; } } - - private boolean isValidException(final Method method, final Throwable throwable) { - if (throwable instanceof RuntimeException || throwable instanceof Error) { - return true; - } - - final Class<?>[] exceptionTypes = method.getExceptionTypes(); - for (final Class<?> exceptionType : exceptionTypes) { - if (exceptionType.isInstance(throwable)) { - return true; - } - } - return false; - } }
