All Downloads are FREE. Search and download functionalities are using the official Maven repository.

net.anotheria.moskito.integration.cdi.count.CountInterceptor Maven / Gradle / Ivy

There is a newer version: 4.0.3
Show newest version
package net.anotheria.moskito.integration.cdi.count;

import net.anotheria.moskito.core.counter.CounterStats;
import net.anotheria.moskito.core.counter.CounterStatsFactory;
import net.anotheria.moskito.core.dynamic.IOnDemandStatsFactory;
import net.anotheria.moskito.core.dynamic.OnDemandStatsProducer;
import net.anotheria.moskito.integration.cdi.AbstractInterceptor;

import javax.interceptor.AroundInvoke;
import javax.interceptor.AroundTimeout;
import javax.interceptor.Interceptor;
import javax.interceptor.InvocationContext;
import java.io.Serializable;
import java.lang.reflect.Method;

/**
 * {@link Count} interceptor.
 *
 * @author Alex Osadchy
 * @author Leon Rosenberg
 */
@Count
@Interceptor
public class CountInterceptor extends AbstractInterceptor implements Serializable {
    /**
     * SerialVersionUID.
     */
    private static final long serialVersionUID = -3519810777335621747L;

    @Override
    protected final Class getStatsClass() {
        return CounterStats.class;
    }

    @Override
    protected final IOnDemandStatsFactory getStatsFactory() {
        return CounterStatsFactory.DEFAULT_INSTANCE;
    }

    /**
     * Around method invoke.
     *
     * @param ctx context
     * @return method result
     * @throws Throwable any exception
     */
    @AroundInvoke
    @AroundTimeout
    public Object aroundInvoke(InvocationContext ctx) throws Throwable {
        final Method method = ctx.getMethod();

        final Count annotation = getAnnotationFromContext(ctx, Count.class);
        final CountParameter parameter = method.getAnnotation(CountParameter.class);

        final String producerId = annotation.producerId().isEmpty() ? method.getDeclaringClass().getSimpleName() : annotation.producerId();
        final String caseName = (parameter != null && !parameter.value().isEmpty()) ? parameter.value() : method.getName();
        final OnDemandStatsProducer onDemandProducer = getProducer(method.getDeclaringClass(), producerId, annotation.category(), annotation.subsystem(), false);

        if (onDemandProducer != null) {
            final CounterStats defaultStats = getDefaultStats(onDemandProducer);
            final CounterStats stats = getStats(onDemandProducer, caseName);

            if (defaultStats != null)
                defaultStats.inc();
            if (stats != null)
                stats.inc();
        }

        return proceed(ctx);
    }

}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy