Permalink
Browse files

Introducing two new interfaces for Exception Handling

This helps split up the user API from the SPI / internal calls.
  • Loading branch information...
LightGuard committed Apr 12, 2012
1 parent b2e7b90 commit f21bf9121a00aad5cea75957cca7c59cbbc3035b
Showing with 225 additions and 119 deletions.
  1. +78 −0 ...spike/core/api/src/main/java/org/apache/deltaspike/core/api/exception/control/ExceptionEvent.java
  2. +1 −1 ...aspike/core/api/src/main/java/org/apache/deltaspike/core/api/exception/control/HandlerMethod.java
  3. +46 −0 ...i/src/main/java/org/apache/deltaspike/core/spi/exception/control/IntrospectiveExceptionEvent.java
  4. +21 −40 ...a → impl/src/main/java/org/apache/deltaspike/core/impl/exception/control/ExceptionEventImpl.java}
  5. +9 −9 ...mpl/src/main/java/org/apache/deltaspike/core/impl/exception/control/ExceptionHandlerDispatch.java
  6. +3 −3 .../core/impl/src/main/java/org/apache/deltaspike/core/impl/exception/control/HandlerMethodImpl.java
  7. +6 −6 .../main/java/org/apache/deltaspike/core/impl/exception/control/OutboundParameterValueRedefiner.java
  8. +5 −4 ...re/impl/src/test/java/org/apache/deltaspike/test/core/impl/exception/control/event/EventTest.java
  9. +3 −3 ...java/org/apache/deltaspike/test/core/impl/exception/control/flow/AbortingBreadthFirstHandler.java
  10. +3 −3 ...c/test/java/org/apache/deltaspike/test/core/impl/exception/control/flow/AbortingDepthHandler.java
  11. +4 −4 ...est/java/org/apache/deltaspike/test/core/impl/exception/control/flow/ExceptionHandledHandler.java
  12. +5 −5 ...rc/test/java/org/apache/deltaspike/test/core/impl/exception/control/flow/ProceedCauseHandler.java
  13. +3 −3 ...mpl/src/test/java/org/apache/deltaspike/test/core/impl/exception/control/flow/RethrowHandler.java
  14. +3 −3 ...src/test/java/org/apache/deltaspike/test/core/impl/exception/control/flow/ThrowingNewHandler.java
  15. +2 −2 ...java/org/apache/deltaspike/test/core/impl/exception/control/handler/BadInjectionPointHandler.java
  16. +6 −6 ...t/java/org/apache/deltaspike/test/core/impl/exception/control/handler/CalledExceptionHandler.java
  17. +13 −13 ...ava/org/apache/deltaspike/test/core/impl/exception/control/handler/ExtensionExceptionHandler.java
  18. +2 −2 .../org/apache/deltaspike/test/core/impl/exception/control/handler/HandlerWhichThrowsExceptions.java
  19. +3 −3 ...l/src/test/java/org/apache/deltaspike/test/core/impl/exception/control/handler/UnMuteHandler.java
  20. +9 −9 ...ava/org/apache/deltaspike/test/core/impl/exception/control/traversal/ExceptionHandlerMethods.java
@@ -0,0 +1,78 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied. See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+
+package org.apache.deltaspike.core.api.exception.control;
+
+/**
+ * Payload for an exception to be handled. Implementations of this interface should not expose internals and remain
+ * immutable for this contract.
+ *
+ * @param <T> Exception type this event represents
+ */
+public interface ExceptionEvent<T extends Throwable>
+{
+ /**
+ * The exception causing this event.
+ */
+ T getException();
+
+ /**
+ * Instructs the dispatcher to abort further processing of handlers.
+ */
+ void abort();
+
+ /**
+ * Instructs the dispatcher to throw the original exception after handler processing.
+ */
+ void throwOriginal();
+
+ /**
+ * Instructs the dispatcher to terminate additional handler processing and mark the event as handled.
+ */
+ void handled();
+
+ /**
+ * Default instruction to dispatcher, continues handler processing.
+ */
+ void handledAndContinue();
+
+ /**
+ * Similar to {@link ExceptionEvent#handledAndContinue()},
+ * but instructs the dispatcher to markHandled to the next element
+ * in the cause chain without processing additional handlers for this cause chain element.
+ */
+ void skipCause();
+
+ /**
+ * Instructs the dispatcher to allow this handler to be invoked again.
+ */
+ void unmute();
+
+ /**
+ * Rethrow the exception, but use the given exception instead of the original.
+ *
+ * @param t Exception to be thrown in place of the original.
+ */
+ void rethrow(Throwable t);
+
+ /**
+ * Check to see if this exception has been handled.
+ */
+ boolean isMarkedHandled();
+}
@@ -52,7 +52,7 @@
*
* @param event event to pass to the handler.
*/
- void notify(CaughtException<T> event);
+ void notify(ExceptionEvent<T> event);
/**
* Obtains the precedence of the handler.
@@ -0,0 +1,46 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied. See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+
+package org.apache.deltaspike.core.spi.exception.control;
+
+import org.apache.deltaspike.core.api.exception.control.ExceptionEvent;
+import org.apache.deltaspike.core.api.exception.control.ExceptionHandlingFlow;
+
+/**
+ * Internal view into the ExceptionEvent. Methods on this interface are used by the ExceptionHandlerDispatcher.
+ */
+public interface IntrospectiveExceptionEvent<T extends Throwable> extends ExceptionEvent<T>
+{
+ /**
+ * Check to see if this event has been unmutted and therefore called again.
+ */
+ boolean isUnmute();
+
+ /**
+ * The next expected step in the exception handling flow (i.e. abort, rethrow, etc)
+ */
+ ExceptionHandlingFlow getCurrentExceptionHandlingFlow();
+
+ boolean isBeforeTraversal();
+
+ /**
+ * Returns the exception that should be thrown if the next step in the flow is THROW.
+ */
+ Throwable getThrowNewException();
+}
@@ -17,7 +17,11 @@
* under the License.
*/
-package org.apache.deltaspike.core.api.exception.control;
+package org.apache.deltaspike.core.impl.exception.control;
+
+import org.apache.deltaspike.core.api.exception.control.ExceptionHandlingFlow;
+import org.apache.deltaspike.core.api.exception.control.ExceptionStack;
+import org.apache.deltaspike.core.spi.exception.control.IntrospectiveExceptionEvent;
import javax.enterprise.inject.Typed;
@@ -27,10 +31,10 @@
*
* @param <T> Exception type this event represents
*/
-@SuppressWarnings({ "unchecked", "CdiManagedBeanInconsistencyInspection" })
+@SuppressWarnings({"unchecked", "CdiManagedBeanInconsistencyInspection"})
@Typed()
//X TODO discuss an interface to avoid internal methods in the api
-public class CaughtException<T extends Throwable>
+public class ExceptionEventImpl<T extends Throwable> implements IntrospectiveExceptionEvent<T>
{
private final T exception;
private boolean unmute;
@@ -48,7 +52,7 @@
* @param handled flag indicating the exception has already been handled by a previous handler
* @throws IllegalArgumentException if stack is null
*/
- public CaughtException(final ExceptionStack stack, final boolean beforeTraversal, final boolean handled)
+ public ExceptionEventImpl(final ExceptionStack stack, final boolean beforeTraversal, final boolean handled)
{
if (stack == null)
{
@@ -61,64 +65,49 @@ public CaughtException(final ExceptionStack stack, final boolean beforeTraversal
this.flow = ExceptionHandlingFlow.HANDLED_AND_CONTINUE;
}
+ @Override
public T getException()
{
return this.exception;
}
- /**
- * Instructs the dispatcher to abort further processing of handlers.
- */
+ @Override
public void abort()
{
this.flow = ExceptionHandlingFlow.ABORT;
}
- /**
- * Instructs the dispatcher to throw the original exception after handler processing.
- */
+ @Override
public void throwOriginal()
{
this.flow = ExceptionHandlingFlow.THROW_ORIGINAL;
}
- /**
- * Instructs the dispatcher to terminate additional handler processing and mark the event as handled.
- */
+ @Override
public void handled()
{
this.flow = ExceptionHandlingFlow.HANDLED;
}
- /**
- * Default instruction to dispatcher, continues handler processing.
- */
+ @Override
public void handledAndContinue()
{
this.flow = ExceptionHandlingFlow.HANDLED_AND_CONTINUE;
}
- /**
- * Similar to {@link org.apache.deltaspike.core.api.exception.control.CaughtException#handledAndContinue()},
- * but instructs the dispatcher to markHandled to the next element
- * in the cause chain without processing additional handlers for this cause chain element.
- */
+ @Override
public void skipCause()
{
this.flow = ExceptionHandlingFlow.SKIP_CAUSE;
}
- /**
- * Instructs the dispatcher to allow this handler to be invoked again.
- */
+ @Override
public void unmute()
{
this.unmute = true;
}
- /**
- * Internal only
- */
+ @Override
public boolean isUnmute()
{
return this.unmute;
@@ -129,40 +118,32 @@ public ExceptionStack getExceptionStack() {
}
*/
- /**
- * Internal only
- */
+ @Override
public ExceptionHandlingFlow getCurrentExceptionHandlingFlow()
{
return this.flow;
}
+ @Override
public boolean isMarkedHandled()
{
return this.markedHandled;
}
+ @Override
public boolean isBeforeTraversal()
{
return beforeTraversal;
}
- /**
- * Rethrow the exception, but use the given exception instead of the original.
- *
- * @param t Exception to be thrown in place of the original.
- */
+ @Override
public void rethrow(Throwable t)
{
this.throwNewException = t;
this.flow = ExceptionHandlingFlow.THROW;
}
- /**
- * Internal only.
- *
- * @return
- */
+ @Override
public Throwable getThrowNewException()
{
return this.throwNewException;
@@ -19,7 +19,6 @@
package org.apache.deltaspike.core.impl.exception.control;
-import org.apache.deltaspike.core.api.exception.control.CaughtException;
import org.apache.deltaspike.core.api.exception.control.ExceptionStack;
import org.apache.deltaspike.core.api.exception.control.ExceptionToCatch;
import org.apache.deltaspike.core.api.exception.control.HandlerMethod;
@@ -52,10 +51,10 @@
* Observes the event, finds the correct exception handler(s) and invokes them.
*
* @param exceptionEvent exception to be invoked
- * @param beanManager active bean manager
+ * @param beanManager active bean manager
* @throws Throwable If a handler requests the exception to be re-thrown.
*/
- @SuppressWarnings({ "unchecked", "MethodWithMultipleLoops", "ThrowableResultOfMethodCallIgnored" })
+ @SuppressWarnings({"unchecked", "MethodWithMultipleLoops", "ThrowableResultOfMethodCallIgnored"})
public void executeHandlers(@Observes @Any ExceptionToCatch exceptionEvent,
final BeanManager beanManager) throws Throwable
{
@@ -66,7 +65,7 @@ public void executeHandlers(@Observes @Any ExceptionToCatch exceptionEvent,
Throwable throwException = null;
final HandlerMethodStorage handlerMethodStorage =
- BeanProvider.getContextualReference(HandlerMethodStorage.class);
+ BeanProvider.getContextualReference(HandlerMethodStorage.class);
try
{
@@ -78,7 +77,8 @@ public void executeHandlers(@Observes @Any ExceptionToCatch exceptionEvent,
beanManager.fireEvent(stack); // Allow for modifying the exception stack
- inbound_cause: //indentation needed by the current checkstyle rules
+ inbound_cause:
+ //indentation needed by the current checkstyle rules
while (stack.getCurrent() != null)
{
final List<HandlerMethod<?>> breadthFirstHandlerMethods = new ArrayList<HandlerMethod<?>>(
@@ -92,7 +92,7 @@ public void executeHandlers(@Observes @Any ExceptionToCatch exceptionEvent,
LOG.fine(String.format("Notifying handler %s", handler));
@SuppressWarnings("rawtypes")
- final CaughtException breadthFirstEvent = new CaughtException(stack, true,
+ final ExceptionEventImpl breadthFirstEvent = new ExceptionEventImpl(stack, true,
exceptionEvent.isHandled());
handler.notify(breadthFirstEvent);
@@ -127,7 +127,7 @@ public void executeHandlers(@Observes @Any ExceptionToCatch exceptionEvent,
break;
default:
throw new IllegalStateException(
- "Unexpected enum type " + breadthFirstEvent.getCurrentExceptionHandlingFlow());
+ "Unexpected enum type " + breadthFirstEvent.getCurrentExceptionHandlingFlow());
}
}
}
@@ -149,7 +149,7 @@ public void executeHandlers(@Observes @Any ExceptionToCatch exceptionEvent,
LOG.fine(String.format("Notifying handler %s", handler));
@SuppressWarnings("rawtypes")
- final CaughtException depthFirstEvent = new CaughtException(stack, false,
+ final ExceptionEventImpl depthFirstEvent = new ExceptionEventImpl(stack, false,
exceptionEvent.isHandled());
handler.notify(depthFirstEvent);
@@ -183,7 +183,7 @@ public void executeHandlers(@Observes @Any ExceptionToCatch exceptionEvent,
break;
default:
throw new IllegalStateException(
- "Unexpected enum type " + depthFirstEvent.getCurrentExceptionHandlingFlow());
+ "Unexpected enum type " + depthFirstEvent.getCurrentExceptionHandlingFlow());
}
}
}
@@ -20,7 +20,7 @@
package org.apache.deltaspike.core.impl.exception.control;
import org.apache.deltaspike.core.api.exception.control.BeforeHandles;
-import org.apache.deltaspike.core.api.exception.control.CaughtException;
+import org.apache.deltaspike.core.api.exception.control.ExceptionEvent;
import org.apache.deltaspike.core.api.exception.control.HandlerMethod;
import org.apache.deltaspike.core.api.exception.control.Handles;
import org.apache.deltaspike.core.api.literal.AnyLiteral;
@@ -207,7 +207,7 @@ public Type getExceptionType()
* {@inheritDoc}
*/
@Override
- public void notify(final CaughtException<T> event)
+ public void notify(final ExceptionEvent<T> event)
{
CreationalContext<?> ctx = null;
try
@@ -266,7 +266,7 @@ public int getOrdinal()
if (!param.equals(this.handlerParameter))
{
this.injectionPoints.add(
- new ImmutableInjectionPoint(param, getBeanManager(), getBean(), false, false));
+ new ImmutableInjectionPoint(param, getBeanManager(), getBean(), false, false));
}
}
Oops, something went wrong.

0 comments on commit f21bf91

Please sign in to comment.