Search in sources :

Example 1 with FaultToleranceDefinitionException

use of org.eclipse.microprofile.faulttolerance.exceptions.FaultToleranceDefinitionException in project Payara by payara.

the class FallbackValidator method validateAnnotation.

/**
 * Validate that the Fallback annotation is correct.
 * @param fallback The fallback annotation to validate
 * @param annotatedMethod The method annotated with @Fallback
 * @param config The config of the application
 * @throws ClassNotFoundException If the fallbackClass could not be found
 * @throws NoSuchMethodException If the fallbackMethod could not be found
 */
public static void validateAnnotation(Fallback fallback, AnnotatedMethod<?> annotatedMethod, Config config) throws ClassNotFoundException, NoSuchMethodException {
    // Get the fallbackMethod
    String fallbackMethod = (String) FaultToleranceCdiUtils.getOverrideValue(config, Fallback.class, "fallbackMethod", annotatedMethod.getJavaMember().getName(), annotatedMethod.getJavaMember().getDeclaringClass().getCanonicalName(), String.class).orElse(fallback.fallbackMethod());
    // Get the fallbackClass, and check that it can be found
    Class<? extends FallbackHandler> fallbackClass = (Class<? extends FallbackHandler>) Thread.currentThread().getContextClassLoader().loadClass((String) FaultToleranceCdiUtils.getOverrideValue(config, Fallback.class, "value", annotatedMethod.getJavaMember().getName(), annotatedMethod.getJavaMember().getDeclaringClass().getCanonicalName(), String.class).orElse(fallback.value().getName()));
    // Validate the annotated method
    if (fallbackMethod != null && !fallbackMethod.isEmpty()) {
        if (fallbackClass != null && fallbackClass != Fallback.DEFAULT.class) {
            throw new FaultToleranceDefinitionException("Both a fallback class and method have been set.");
        } else {
            try {
                if (annotatedMethod.getJavaMember().getDeclaringClass().getDeclaredMethod(fallbackMethod, annotatedMethod.getJavaMember().getParameterTypes()).getReturnType() != annotatedMethod.getJavaMember().getReturnType()) {
                    throw new FaultToleranceDefinitionException("Return type of fallback method does not match.");
                }
            } catch (NoSuchMethodException ex) {
                throw new FaultToleranceDefinitionException("Could not find fallback method: " + fallbackMethod, ex);
            }
        }
    } else if (fallbackClass != null && fallbackClass != Fallback.DEFAULT.class) {
        if (fallbackClass.getDeclaredMethod(FALLBACK_HANDLER_METHOD_NAME, ExecutionContext.class).getReturnType() != annotatedMethod.getJavaMember().getReturnType()) {
            throw new FaultToleranceDefinitionException("Return type of fallback class handle method does not match.");
        }
    }
}
Also used : FallbackHandler(org.eclipse.microprofile.faulttolerance.FallbackHandler) ExecutionContext(org.eclipse.microprofile.faulttolerance.ExecutionContext) Fallback(org.eclipse.microprofile.faulttolerance.Fallback) FaultToleranceDefinitionException(org.eclipse.microprofile.faulttolerance.exceptions.FaultToleranceDefinitionException)

Aggregations

ExecutionContext (org.eclipse.microprofile.faulttolerance.ExecutionContext)1 Fallback (org.eclipse.microprofile.faulttolerance.Fallback)1 FallbackHandler (org.eclipse.microprofile.faulttolerance.FallbackHandler)1 FaultToleranceDefinitionException (org.eclipse.microprofile.faulttolerance.exceptions.FaultToleranceDefinitionException)1