Search in sources :

Example 46 with AroundInvoke

use of javax.interceptor.AroundInvoke in project indy by Commonjava.

the class RestInterceptor method operation.

@AroundInvoke
public Object operation(InvocationContext context) throws Exception {
    Class<?> targetClass = context.getTarget().getClass();
    Path classAnno = null;
    do {
        classAnno = targetClass.getAnnotation(Path.class);
        targetClass = targetClass.getSuperclass();
    } while (classAnno == null && targetClass != null);
    if (getContext(REST_CLASS) == null) {
        String targetName = context.getMethod().getDeclaringClass().getSimpleName();
        setContext(REST_CLASS, targetName);
        String classPath = "";
        if (classAnno != null && getContext(REST_CLASS_PATH) == null) {
            classPath = classAnno.value();
            setContext(REST_CLASS_PATH, classPath);
        }
        Path methAnno = context.getMethod().getAnnotation(Path.class);
        if (methAnno != null && getContext(REST_METHOD_PATH) == null) {
            String methodPath = methAnno.value();
            setContext(REST_METHOD_PATH, methodPath);
            String endpointPath = Paths.get(classPath, methodPath).toString();
            setContext(REST_ENDPOINT_PATH, endpointPath);
        }
    }
    LoggerFactory.getLogger(context.getTarget().getClass()).trace("Interceptor decorating MDC.");
    return context.proceed();
}
Also used : Path(javax.ws.rs.Path) AroundInvoke(javax.interceptor.AroundInvoke)

Example 47 with AroundInvoke

use of javax.interceptor.AroundInvoke in project Payara by payara.

the class ClusterScopedInterceptor method lockAndRefresh.

@AroundInvoke
public Object lockAndRefresh(InvocationContext invocationContext) throws Exception {
    Class<?> beanClass = invocationContext.getMethod().getDeclaringClass();
    Clustered clusteredAnnotation = getAnnotation(beanManager, beanClass);
    try {
        lock(beanClass, clusteredAnnotation);
        return invocationContext.proceed();
    } finally {
        refresh(beanClass, invocationContext.getTarget());
        unlock(beanClass, clusteredAnnotation);
    }
}
Also used : Clustered(fish.payara.cluster.Clustered) AroundInvoke(javax.interceptor.AroundInvoke)

Example 48 with AroundInvoke

use of javax.interceptor.AroundInvoke in project Payara by payara.

the class CacheRemoveAllInterceptor method cacheRemoveAll.

@AroundInvoke
public Object cacheRemoveAll(InvocationContext ctx) throws Throwable {
    if (!isEnabled()) {
        return ctx.proceed();
    }
    CacheRemoveAll annotation = ctx.getMethod().getAnnotation(CacheRemoveAll.class);
    PayaraCacheKeyInvocationContext<CacheRemoveAll> pctx = new PayaraCacheKeyInvocationContext<>(ctx, annotation);
    if (!annotation.afterInvocation()) {
        doRemoveAll(pctx);
    }
    Object result = null;
    try {
        result = ctx.proceed();
    } catch (Throwable e) {
        if (annotation.afterInvocation()) {
            if (shouldIEvict(annotation.evictFor(), annotation.noEvictFor(), e)) {
                doRemoveAll(pctx);
            }
        }
        throw e;
    }
    if (annotation.afterInvocation()) {
        doRemoveAll(pctx);
    }
    return result;
}
Also used : CacheRemoveAll(javax.cache.annotation.CacheRemoveAll) PayaraCacheKeyInvocationContext(fish.payara.cdi.jsr107.implementation.PayaraCacheKeyInvocationContext) AroundInvoke(javax.interceptor.AroundInvoke)

Example 49 with AroundInvoke

use of javax.interceptor.AroundInvoke in project wildfly-swarm by wildfly-swarm.

the class SwaggerRestApplicationInterceptor method aroundInvoke.

/**
 * As per the JAX-RS specification, if a deployment sub-classes JAX-RS Application and returns a non-empty collection for
 * either {@link Application#getClasses()} or {@link Application#getSingletons()}, then, only the references mentioned in
 * those collections should be used as REST resources. This poses a slight problem when the developers <i>expect</i> to see
 * their Swagger resources, but don't see it (due to specification conformance). This method takes care of adding the
 * relevant resources (if required).
 */
@SuppressWarnings("unchecked")
@AroundInvoke
public Object aroundInvoke(InvocationContext context) throws Exception {
    Object response = context.proceed();
    // Verify if we need to do anything at all or not. This is to avoid the potential misconfiguration where this
    // interceptor gets added to beans that should not be included.
    Method method = context.getMethod();
    if (Application.class.isAssignableFrom(method.getDeclaringClass())) {
        if ("getClasses".equals(method.getName())) {
            Set<Class<?>> classes = new HashSet<>((Set<Class<?>>) response);
            // Check the response for singletons as well.
            Method getSingletons = Application.class.getDeclaredMethod("getSingletons");
            Set singletons = (Set) getSingletons.invoke(context.getTarget());
            if (!classes.isEmpty() || !singletons.isEmpty()) {
                classes.add(ApiListingResource.class);
                classes.add(SwaggerSerializers.class);
                response = classes;
                SwaggerMessages.MESSAGES.addingSwaggerResourcesToCustomApplicationSubClass();
            }
        }
    } else {
        SwaggerMessages.MESSAGES.warnInvalidBeanTarget(method.getDeclaringClass());
    }
    return response;
}
Also used : HashSet(java.util.HashSet) Set(java.util.Set) Method(java.lang.reflect.Method) HashSet(java.util.HashSet) AroundInvoke(javax.interceptor.AroundInvoke)

Example 50 with AroundInvoke

use of javax.interceptor.AroundInvoke in project wildfly-swarm by wildfly-swarm.

the class HystrixCommandInterceptor method interceptCommand.

@AroundInvoke
public Object interceptCommand(InvocationContext ic) throws Exception {
    Method method = ic.getMethod();
    ExecutionContextWithInvocationContext ctx = new ExecutionContextWithInvocationContext(ic);
    boolean shouldRunCommand = true;
    Object res = null;
    LOGGER.tracef("FT operation intercepted: %s", method);
    CommandMetadata metadata = commandMetadataMap.computeIfAbsent(method, CommandMetadata::new);
    RetryContext retryContext = nonFallBackEnable && metadata.operation.hasRetry() ? new RetryContext(metadata.operation.getRetry()) : null;
    SynchronousCircuitBreaker syncCircuitBreaker = null;
    while (shouldRunCommand) {
        shouldRunCommand = false;
        if (nonFallBackEnable && syncCircuitBreakerEnabled && metadata.hasCircuitBreaker()) {
            syncCircuitBreaker = getSynchronousCircuitBreaker(metadata.commandKey, metadata.operation.getCircuitBreaker());
        }
        Supplier<Object> fallback = null;
        if (retryContext == null || retryContext.isLastAttempt()) {
            fallback = metadata.getFallback(ctx);
        }
        DefaultCommand command = new DefaultCommand(metadata.setter, ctx, fallback);
        try {
            if (metadata.operation.isAsync()) {
                LOGGER.debugf("Queue up command for async execution: %s", metadata.operation);
                res = new AsyncFuture(command.queue());
            } else {
                LOGGER.debugf("Sync execution: %s]", metadata.operation);
                res = command.execute();
            }
            if (syncCircuitBreaker != null) {
                syncCircuitBreaker.executionSucceeded();
            }
        } catch (HystrixRuntimeException e) {
            if (syncCircuitBreaker != null) {
                syncCircuitBreaker.executionFailed();
            }
            HystrixRuntimeException.FailureType failureType = e.getFailureType();
            LOGGER.tracef("Hystrix runtime failure [%s] when invoking %s", failureType, method);
            switch(failureType) {
                case TIMEOUT:
                    TimeoutException timeoutException = new TimeoutException(e);
                    if (retryContext != null && retryContext.shouldRetry()) {
                        shouldRunCommand = shouldRetry(retryContext, timeoutException);
                        if (shouldRunCommand) {
                            continue;
                        }
                    }
                    throw timeoutException;
                case SHORTCIRCUIT:
                    throw new CircuitBreakerOpenException(method.getName());
                case REJECTED_THREAD_EXECUTION:
                case REJECTED_SEMAPHORE_EXECUTION:
                case REJECTED_SEMAPHORE_FALLBACK:
                    BulkheadException bulkheadException = new BulkheadException(e);
                    if (retryContext != null && retryContext.shouldRetry()) {
                        shouldRunCommand = shouldRetry(retryContext, bulkheadException);
                        if (shouldRunCommand) {
                            continue;
                        }
                    }
                    throw bulkheadException;
                case COMMAND_EXCEPTION:
                    if (retryContext != null && retryContext.shouldRetry()) {
                        shouldRunCommand = shouldRetry(retryContext, getCause(e));
                        continue;
                    }
                default:
                    throw getCause(e);
            }
        }
    }
    return res;
}
Also used : Method(java.lang.reflect.Method) HystrixRuntimeException(com.netflix.hystrix.exception.HystrixRuntimeException) CircuitBreakerOpenException(org.eclipse.microprofile.faulttolerance.exceptions.CircuitBreakerOpenException) BulkheadException(org.eclipse.microprofile.faulttolerance.exceptions.BulkheadException) TimeoutException(org.eclipse.microprofile.faulttolerance.exceptions.TimeoutException) AroundInvoke(javax.interceptor.AroundInvoke)

Aggregations

AroundInvoke (javax.interceptor.AroundInvoke)52 Method (java.lang.reflect.Method)10 InvocationManager (org.glassfish.api.invocation.InvocationManager)6 FaultToleranceService (fish.payara.microprofile.faulttolerance.FaultToleranceService)5 FallbackPolicy (fish.payara.microprofile.faulttolerance.interceptors.fallback.FallbackPolicy)5 InvocationTargetException (java.lang.reflect.InvocationTargetException)5 TransactionalException (javax.transaction.TransactionalException)5 Config (org.eclipse.microprofile.config.Config)5 Fallback (org.eclipse.microprofile.faulttolerance.Fallback)5 PayaraCacheKeyInvocationContext (fish.payara.cdi.jsr107.implementation.PayaraCacheKeyInvocationContext)4 Meter (com.codahale.metrics.Meter)2 Timer (com.codahale.metrics.Timer)2 TransactionManagerHelper (com.sun.enterprise.transaction.TransactionManagerHelper)2 CallerAccessException (fish.payara.cdi.auth.roles.CallerAccessException)2 RolesPermitted (fish.payara.cdi.auth.roles.RolesPermitted)2 Parameter (java.lang.reflect.Parameter)2 Principal (java.security.Principal)2 ArrayList (java.util.ArrayList)2 NoSuchElementException (java.util.NoSuchElementException)2 Message (javax.jms.Message)2