Example usage for javax.interceptor InvocationContext getContextData

List of usage examples for javax.interceptor InvocationContext getContextData

Introduction

In this page you can find the example usage for javax.interceptor InvocationContext getContextData.

Prototype

public Map<String, Object> getContextData();

Source Link

Document

Enables an interceptor to retrieve or update the data associated with the invocation by another interceptor, business method, and/or webservices endpoint in the invocation chain.

Usage

From source file:org.commonjava.indy.metrics.jaxrs.interceptor.MetricsInterceptor.java

@AroundInvoke
public Object operation(InvocationContext context) throws Exception {
    if (!config.isMetricsEnabled()) {
        return context.proceed();
    }/*from w  w w.  jav a  2s. com*/

    Method method = context.getMethod();
    Measure measure = method.getAnnotation(Measure.class);
    if (measure == null) {
        measure = method.getDeclaringClass().getAnnotation(Measure.class);
    }

    logger.trace("Gathering metrics for: {}", context.getContextData());
    String nodePrefix = config.getNodePrefix();

    String defaultName = getDefaultName(context.getMethod().getDeclaringClass(), context.getMethod().getName());

    List<Timer.Context> timers = Stream.of(measure.timers()).map(named -> {
        String name = getName(nodePrefix, named.value(), defaultName, TIMER);
        Timer.Context tc = metricsManager.getTimer(name).time();
        logger.trace("START: {} ({})", name, tc);
        return tc;
    }).collect(Collectors.toList());

    try {
        return context.proceed();
    } catch (Exception e) {
        Stream.of(measure.exceptions()).forEach((named) -> {
            String name = getName(nodePrefix, named.value(), defaultName, EXCEPTION);
            Meter meter = metricsManager.getMeter(name);
            logger.trace("ERRORS++ {}", name);
            meter.mark();
        });

        throw e;
    } finally {
        if (timers != null) {
            timers.forEach(timer -> {
                logger.trace("STOP: {}", timer);
                timer.stop();
            });

        }
        Stream.of(measure.meters()).forEach((named) -> {
            String name = getName(nodePrefix, named.value(), defaultName, METER);
            Meter meter = metricsManager.getMeter(name);
            logger.trace("CALLS++ {}", name);
            meter.mark();
        });
    }
}

From source file:org.hibersap.ejb.interceptor.HibersapSessionInterceptor.java

@AroundInvoke
public Object injectSessionsIntoEjb(final InvocationContext ctx) throws Exception {
    Set<Field> sessionFields = getHibersapSessionFields(ctx.getTarget());

    Map<Session, String> sessionsCreated = new HashMap<Session, String>();
    try {//from   w  w w.ja v a 2s.  co  m
        for (Field sessionField : sessionFields) {
            String jndiName = getSessionManagerJndiName(sessionField);
            String key = HIBERSAP_SESSION_PREFIX + jndiName;
            Session session = (Session) ctx.getContextData().get(key);

            if (session == null) {
                LOGGER.debug("Erzeuge Hibersap-Session fr SessionManager " + jndiName);
                session = openSession(jndiName);
                sessionsCreated.put(session, jndiName);
                ctx.getContextData().put(key, session);
            }

            injectSessionIntoTarget(ctx.getTarget(), sessionField, session);
        }

        return ctx.proceed();
    } finally {
        closeSessions(sessionsCreated, ctx.getContextData());
    }
}

From source file:org.rhq.enterprise.server.authz.RequiredPermissionsInterceptor.java

/**
 * Returns a string representation of the given invocation context so it can be displayed in messages.
 *
 * @param  invocation/*from  www  . jav  a  2s  . c om*/
 *
 * @return string containing information about the invocation
 */
private String getInvocationString(InvocationContext invocation) {
    StringBuffer buf = new StringBuffer("invocation: ");
    buf.append("method=" + invocation.getMethod().toGenericString());
    buf.append(",context-data=" + invocation.getContextData());
    return buf.toString();
}