Search in sources :

Example 1 with InvocationContext

use of javax.interceptor.InvocationContext in project wildfly by wildfly.

the class PassivationAnnotationParsingProcessor method processPassivation.

private void processPassivation(final EEModuleDescription eeModuleDescription, final AnnotationTarget target, final DotName annotationType, final EEApplicationClasses applicationClasses) throws DeploymentUnitProcessingException {
    if (!(target instanceof MethodInfo)) {
        throw EeLogger.ROOT_LOGGER.methodOnlyAnnotation(annotationType);
    }
    final MethodInfo methodInfo = MethodInfo.class.cast(target);
    final ClassInfo classInfo = methodInfo.declaringClass();
    final EEModuleClassDescription classDescription = eeModuleDescription.addOrGetLocalClassDescription(classInfo.name().toString());
    final Type[] args = methodInfo.args();
    if (args.length > 1) {
        ROOT_LOGGER.warn(EeLogger.ROOT_LOGGER.invalidNumberOfArguments(methodInfo.name(), annotationType, classInfo.name()));
        return;
    } else if (args.length == 1 && !args[0].name().toString().equals(InvocationContext.class.getName())) {
        ROOT_LOGGER.warn(EeLogger.ROOT_LOGGER.invalidSignature(methodInfo.name(), annotationType, classInfo.name(), "void methodName(InvocationContext ctx)"));
        return;
    }
    final MethodIdentifier methodIdentifier;
    if (args.length == 0) {
        methodIdentifier = MethodIdentifier.getIdentifier(Void.TYPE, methodInfo.name());
    } else {
        methodIdentifier = MethodIdentifier.getIdentifier(Void.TYPE, methodInfo.name(), InvocationContext.class);
    }
    final InterceptorClassDescription.Builder builder = InterceptorClassDescription.builder(classDescription.getInterceptorClassDescription());
    if (annotationType == POST_ACTIVATE) {
        builder.setPostActivate(methodIdentifier);
    } else {
        builder.setPrePassivate(methodIdentifier);
    }
    classDescription.setInterceptorClassDescription(builder.build());
}
Also used : Type(org.jboss.jandex.Type) InterceptorClassDescription(org.jboss.as.ee.component.interceptors.InterceptorClassDescription) MethodInfo(org.jboss.jandex.MethodInfo) EEModuleClassDescription(org.jboss.as.ee.component.EEModuleClassDescription) MethodIdentifier(org.jboss.invocation.proxy.MethodIdentifier) InvocationContext(javax.interceptor.InvocationContext) ClassInfo(org.jboss.jandex.ClassInfo)

Example 2 with InvocationContext

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

the class MetricsInterceptor method operation.

@AroundInvoke
public Object operation(InvocationContext context) throws Exception {
    if (!config.isMetricsEnabled())
        return context.proceed();
    IndyMetrics metrics = context.getMethod().getAnnotation(IndyMetrics.class);
    if (metrics == null) {
        return context.proceed();
    }
    logger.debug("Gathering metrics for: {}", context.getContextData());
    Measure measures = metrics.measure();
    List<Timer.Context> timers = Stream.of(measures.timers()).map(named -> util.getTimer(named).time()).collect(Collectors.toList());
    try {
        return context.proceed();
    } catch (Exception e) {
        Measure me = metrics.exceptions();
        Stream.of(me.meters()).forEach((named) -> {
            Meter requests = util.getMeter(named);
            requests.mark();
        });
        throw e;
    } finally {
        if (timers != null) {
            timers.forEach(Timer.Context::stop);
        }
        Stream.of(measures.meters()).forEach((named) -> {
            Meter requests = util.getMeter(named);
            requests.mark();
        });
    }
}
Also used : Logger(org.slf4j.Logger) InvocationContext(javax.interceptor.InvocationContext) IndyMetrics(org.commonjava.indy.measure.annotation.IndyMetrics) IndyMetricsConfig(org.commonjava.indy.metrics.conf.IndyMetricsConfig) LoggerFactory(org.slf4j.LoggerFactory) Collectors(java.util.stream.Collectors) IndyMetricsManager(org.commonjava.indy.IndyMetricsManager) Inject(javax.inject.Inject) Interceptor(javax.interceptor.Interceptor) Meter(com.codahale.metrics.Meter) List(java.util.List) Measure(org.commonjava.indy.measure.annotation.Measure) Stream(java.util.stream.Stream) IndyMetricsNamed(org.commonjava.indy.metrics.conf.annotation.IndyMetricsNamed) Timer(com.codahale.metrics.Timer) AroundInvoke(javax.interceptor.AroundInvoke) InvocationContext(javax.interceptor.InvocationContext) Meter(com.codahale.metrics.Meter) Measure(org.commonjava.indy.measure.annotation.Measure) IndyMetrics(org.commonjava.indy.measure.annotation.IndyMetrics) AroundInvoke(javax.interceptor.AroundInvoke)

Example 3 with InvocationContext

use of javax.interceptor.InvocationContext in project tomee by apache.

the class InterceptorStack method invoke.

public Object invoke(final javax.xml.rpc.handler.MessageContext messageContext, final Object... parameters) throws Exception {
    try {
        final InvocationContext invocationContext = new JaxRpcInvocationContext(operation, interceptors, beanInstance, targetMethod, messageContext, parameters);
        ThreadContext.getThreadContext().set(InvocationContext.class, invocationContext);
        return invocationContext.proceed();
    } finally {
        ThreadContext.getThreadContext().remove(InvocationContext.class);
    }
}
Also used : InvocationContext(javax.interceptor.InvocationContext)

Example 4 with InvocationContext

use of javax.interceptor.InvocationContext in project tomee by apache.

the class InterceptorBindingBuilder method toMethods.

/**
     * Used for getting the java.lang.reflect.Method objects for the following callbacks:
     * <p/>
     * - @PostConstruct <any-scope> void <method-name>(InvocationContext)
     * - @PreDestroy <any-scope> void <method-name>(InvocationContext)
     * - @PrePassivate <any-scope> void <method-name>(InvocationContext)
     * - @PostActivate <any-scope> void <method-name>(InvocationContext)
     * - @AroundInvoke <any-scope> Object <method-name>(InvocationContext) throws Exception
     * - @AroundTimeout <any-scope> Object <method-name>(InvocationContext) throws Exception
     *
     * @param clazz
     * @param callbackInfos the raw CallbackInfo objects
     * @param callbacks     the collection where the created methods will be placed
     */
private void toMethods(final Class<?> clazz, final List<CallbackInfo> callbackInfos, final Set<Method> callbacks) {
    final List<Method> methods = new ArrayList<Method>();
    for (final CallbackInfo callbackInfo : callbackInfos) {
        try {
            Method method = getMethod(clazz, callbackInfo.method, InvocationContext.class);
            if (callbackInfo.className == null && method.getDeclaringClass().equals(clazz) && !methods.contains(method)) {
                methods.add(method);
            }
            if (method.getDeclaringClass().getName().equals(callbackInfo.className) && !methods.contains(method)) {
                methods.add(method);
            } else {
                // check for a private method on the declared class
                // find declared class
                Class<?> c = clazz;
                while (c != null && !c.getName().equals(callbackInfo.className)) {
                    c = c.getSuperclass();
                }
                // get callback method
                if (c != null) {
                    try {
                        method = getMethod(c, callbackInfo.method, InvocationContext.class);
                        // make sure it is private
                        if (Modifier.isPrivate(method.getModifiers()) && !methods.contains(method)) {
                            SetAccessible.on(method);
                            methods.add(method);
                        }
                    } catch (final NoSuchMethodException e) {
                    // no-op
                    }
                }
            }
        } catch (final NoSuchMethodException e) {
            logger.warning("Interceptor method not found (skipping): public Object " + callbackInfo.method + "(InvocationContext); in class " + clazz.getName());
        }
    }
    Collections.sort(methods, new MethodCallbackComparator());
    callbacks.addAll(methods);
}
Also used : ArrayList(java.util.ArrayList) Method(java.lang.reflect.Method) InvocationContext(javax.interceptor.InvocationContext)

Example 5 with InvocationContext

use of javax.interceptor.InvocationContext in project tomee by apache.

the class EjbMethodInvoker method performInvocation.

@Override
protected Object performInvocation(final Exchange exchange, final Object serviceObject, final Method m, final Object[] paramArray) throws Exception {
    InvocationContext invContext = exchange.get(InvocationContext.class);
    invContext.setParameters(paramArray);
    Object res = invContext.proceed();
    EjbMessageContext ctx = (EjbMessageContext) invContext.getContextData();
    Map<String, Object> handlerProperties = (Map<String, Object>) exchange.get(HANDLER_PROPERTIES);
    addHandlerProperties(ctx, handlerProperties);
    updateWebServiceContext(exchange, ctx);
    return res;
}
Also used : InvocationContext(javax.interceptor.InvocationContext) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) ConcurrentMap(java.util.concurrent.ConcurrentMap) Map(java.util.Map)

Aggregations

InvocationContext (javax.interceptor.InvocationContext)7 Method (java.lang.reflect.Method)2 Meter (com.codahale.metrics.Meter)1 Timer (com.codahale.metrics.Timer)1 ArrayList (java.util.ArrayList)1 List (java.util.List)1 Map (java.util.Map)1 ConcurrentHashMap (java.util.concurrent.ConcurrentHashMap)1 ConcurrentMap (java.util.concurrent.ConcurrentMap)1 TimeUnit (java.util.concurrent.TimeUnit)1 Lock (java.util.concurrent.locks.Lock)1 ReadWriteLock (java.util.concurrent.locks.ReadWriteLock)1 Collectors (java.util.stream.Collectors)1 Stream (java.util.stream.Stream)1 Inject (javax.inject.Inject)1 AroundInvoke (javax.interceptor.AroundInvoke)1 Interceptor (javax.interceptor.Interceptor)1 IndyMetricsManager (org.commonjava.indy.IndyMetricsManager)1 IndyMetrics (org.commonjava.indy.measure.annotation.IndyMetrics)1 Measure (org.commonjava.indy.measure.annotation.Measure)1