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();
}
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);
}
}
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;
}
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;
}
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;
}
Aggregations