Search in sources :

Example 6 with Fallback

use of org.eclipse.microprofile.faulttolerance.Fallback 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

Fallback (org.eclipse.microprofile.faulttolerance.Fallback)6 FaultToleranceService (fish.payara.microprofile.faulttolerance.FaultToleranceService)5 FallbackPolicy (fish.payara.microprofile.faulttolerance.interceptors.fallback.FallbackPolicy)5 AroundInvoke (javax.interceptor.AroundInvoke)5 Config (org.eclipse.microprofile.config.Config)5 InvocationManager (org.glassfish.api.invocation.InvocationManager)5 Retry (org.eclipse.microprofile.faulttolerance.Retry)3 NoSuchElementException (java.util.NoSuchElementException)2 NamingException (javax.naming.NamingException)2 Callable (java.util.concurrent.Callable)1 ExecutionException (java.util.concurrent.ExecutionException)1 TimeoutException (java.util.concurrent.TimeoutException)1 ManagedExecutorService (javax.enterprise.concurrent.ManagedExecutorService)1 ExecutionContext (org.eclipse.microprofile.faulttolerance.ExecutionContext)1 FallbackHandler (org.eclipse.microprofile.faulttolerance.FallbackHandler)1 BulkheadException (org.eclipse.microprofile.faulttolerance.exceptions.BulkheadException)1 CircuitBreakerOpenException (org.eclipse.microprofile.faulttolerance.exceptions.CircuitBreakerOpenException)1 FaultToleranceDefinitionException (org.eclipse.microprofile.faulttolerance.exceptions.FaultToleranceDefinitionException)1 FaultToleranceException (org.eclipse.microprofile.faulttolerance.exceptions.FaultToleranceException)1 TimeoutException (org.eclipse.microprofile.faulttolerance.exceptions.TimeoutException)1