Example usage for org.aspectj.lang ProceedingJoinPoint getArgs

List of usage examples for org.aspectj.lang ProceedingJoinPoint getArgs

Introduction

In this page you can find the example usage for org.aspectj.lang ProceedingJoinPoint getArgs.

Prototype

Object[] getArgs();

Source Link

Usage

From source file:org.flite.cach3.aop.L2ReadThroughMultiCacheAdvice.java

License:Open Source License

@Around("getMulti()")
public Object cacheMulti(final ProceedingJoinPoint pjp) throws Throwable {
    // If we've disabled the caching programmatically (or via properties file) just flow through.
    if (isCacheDisabled()) {
        LOG.debug("Caching is disabled.");
        return pjp.proceed();
    }/*from  w w  w. j  av  a 2 s . c o  m*/

    AnnotationInfo info;
    final MultiCacheCoordinator coord = new MultiCacheCoordinator();
    Object[] args = pjp.getArgs();
    try {
        // Get the target method being invoked, and make sure it returns the correct info.
        coord.setMethod(getMethodToCache(pjp));
        verifyReturnTypeIsList(coord.getMethod(), ReadThroughMultiCache.class);

        // Get the annotation associated with this method, and make sure the values are valid.
        final L2ReadThroughMultiCache annotation = coord.getMethod()
                .getAnnotation(L2ReadThroughMultiCache.class);

        info = getAnnotationInfo(annotation, coord.getMethod().getName());

        // Get the list of objects that will provide the keys to all the cache values.
        coord.setKeyObjects(ReadThroughMultiCacheAdvice
                .getKeyObjectList(info.getAsInteger(AType.KEY_INDEX, null), pjp, coord.getMethod()));

        // Create key->object and object->key mappings.
        coord.setHolder(ReadThroughMultiCacheAdvice.convertIdObjectsToKeyMap(coord.getKeyObjects(),
                info.getAsString(AType.NAMESPACE), info.getAsString(AType.KEY_PREFIX),
                info.getAsString(AType.KEY_TEMPLATE), factory, methodStore, args));

        // Get the full list of cache keys and ask the cache for the corresponding values.
        coord.setInitialKey2Result(
                getCache().getBulk(coord.getKey2Obj().keySet(), info.<Duration>getAsType(AType.WINDOW, null)));

        // We've gotten all positive cache results back, so build up a results list and return it.
        if (coord.getMissObjects().size() < 1) {
            return coord.generateResultList();
        }

        // Create the new list of arguments with a subset of the key objects that aren't in the cache.
        args = coord.modifyArgumentList(args, info.getAsInteger(AType.KEY_INDEX, null));
    } catch (Throwable ex) {
        // If there's an exception somewhere in the caching code, then just bail out
        // and call through to the target method with the original parameters.
        if (LOG.isDebugEnabled()) {
            LOG.warn("Caching on " + pjp.toShortString() + " aborted due to an error.", ex);
        } else {
            LOG.warn("Caching on " + pjp.toShortString() + " aborted due to an error: " + ex.getMessage());
        }
        return pjp.proceed();
    }

    /*
    Call the target method with the new subset of arguments.
    We are calling this outside of the try/catch block in case there are some
    'not our fault' problems with the target method. (Connection issues, etc...)
    Though, this decision could go either way, really.
    */
    final List results = (List) pjp.proceed(args);

    try {

        if (results.size() != coord.getMissObjects().size()) {
            throw new RuntimeException("Did not receive a correlated amount of data from the target method.");
        }

        final String[] cacheBaseIds = new String[results.size()];
        final ImmutableMap.Builder<String, Object> builder = ImmutableMap.builder();
        for (int ix = 0; ix < results.size(); ix++) {
            final Object keyObject = coord.getMissObjects().get(ix);
            final Object resultObject = results.get(ix) == null ? new PertinentNegativeNull() : results.get(ix);
            final String cacheKey = coord.getObj2Key().get(keyObject);
            final String cacheBase = coord.getObj2Base().get(keyObject);
            boolean cacheable = true;
            if (resultObject instanceof CacheConditionally) {
                cacheable = ((CacheConditionally) resultObject).isCacheable();
            }
            if (cacheable) {
                builder.put(cacheKey, resultObject);
            } else {
                coord.getKey2Result().put(cacheKey, resultObject);
            }
            cacheBaseIds[ix] = cacheBase;
        }
        final ImmutableMap<String, Object> input = builder.build();
        getCache().setBulk(input, info.<Duration>getAsType(AType.WINDOW, null));
        coord.getKey2Result().putAll(input);

        return coord.generateResultList();
    } catch (Throwable ex) {
        if (LOG.isDebugEnabled()) {
            LOG.warn("Caching on " + pjp.toShortString()
                    + " aborted due to an error. (The underlying method will be called twice.)", ex);
        } else {
            LOG.warn("Caching on " + pjp.toShortString()
                    + " aborted due to an error. (The underlying method will be called twice.): "
                    + ex.getMessage());
        }
        return pjp.proceed();
    }
}

From source file:org.flite.cach3.aop.L2ReadThroughSingleCacheAdvice.java

License:Open Source License

@Around("getSingle()")
public Object cacheSingle(final ProceedingJoinPoint pjp) throws Throwable {
    // If we've disabled the caching programmatically (or via properties file) just flow through.
    if (isCacheDisabled()) {
        LOG.debug("Caching is disabled.");
        return pjp.proceed();
    }/* ww w.  ja va2  s  . c om*/

    // This is injected caching.  If anything goes wrong in the caching, LOG the crap outta it,
    // but do not let it surface up past the AOP injection itself.
    final String baseKey;
    final String cacheKey;
    final L2ReadThroughSingleCache annotation;
    final AnnotationInfo info;
    final Object[] args = pjp.getArgs();
    try {
        final Method methodToCache = getMethodToCache(pjp);
        annotation = methodToCache.getAnnotation(L2ReadThroughSingleCache.class);
        info = getAnnotationInfo(annotation, methodToCache.getName());
        baseKey = generateBaseKeySingle(args, info, methodToCache.toString());
        cacheKey = buildCacheKey(baseKey, info.getAsString(AType.NAMESPACE, null),
                info.getAsString(AType.KEY_PREFIX, null));

        final Map<String, Object> results = getCache().getBulk(Arrays.asList(cacheKey),
                info.<Duration>getAsType(AType.WINDOW, null));
        final Object result = results == null ? null : results.get(cacheKey);
        if (result != null) {
            //               LOG.debug("Cache hit for key " + cacheKey);
            return (result instanceof PertinentNegativeNull) ? null : result;
        }
    } catch (Throwable ex) {
        if (LOG.isDebugEnabled()) {
            LOG.warn("Caching on " + pjp.toShortString() + " aborted due to an error.", ex);
        } else {
            LOG.warn("Caching on " + pjp.toShortString() + " aborted due to an error: " + ex.getMessage());
        }
        return pjp.proceed();
    }

    final Object result = pjp.proceed();

    // This is injected caching.  If anything goes wrong in the caching, LOG the crap outta it,
    // but do not let it surface up past the AOP injection itself.
    try {
        final Object submission = (result == null) ? new PertinentNegativeNull() : result;
        boolean cacheable = true;
        if (submission instanceof CacheConditionally) {
            cacheable = ((CacheConditionally) submission).isCacheable();
        }
        if (cacheable) {
            getCache().setBulk(ImmutableMap.of(cacheKey, submission),
                    info.<Duration>getAsType(AType.WINDOW, null));
        }
    } catch (Throwable ex) {
        if (LOG.isDebugEnabled()) {
            LOG.warn("Caching on " + pjp.toShortString() + " aborted due to an error.", ex);
        } else {
            LOG.warn("Caching on " + pjp.toShortString() + " aborted due to an error: " + ex.getMessage());
        }
    }
    return result;
}

From source file:org.flite.cach3.aop.ReadThroughAssignCacheAdvice.java

License:Open Source License

@Around("getSingleAssign()")
public Object cacheAssign(final ProceedingJoinPoint pjp) throws Throwable {
    // If we've disabled the caching programmatically (or via properties file) just flow through.
    if (isCacheDisabled()) {
        LOG.debug("Caching is disabled.");
        return pjp.proceed();
    }/*  w ww  . j  av  a2  s .  c o m*/

    final MemcachedClientIF cache = getMemcachedClient();
    // This is injected caching.  If anything goes wrong in the caching, LOG the crap outta it,
    // but do not let it surface up past the AOP injection itself.
    final String cacheKey;
    final AnnotationInfo info;
    try {
        final Method methodToCache = getMethodToCache(pjp);
        final ReadThroughAssignCache annotation = methodToCache.getAnnotation(ReadThroughAssignCache.class);
        info = getAnnotationInfo(annotation, methodToCache.getName(), getJitterDefault());
        cacheKey = buildCacheKey(info.getAsString(AType.ASSIGN_KEY), info.getAsString(AType.NAMESPACE),
                info.getAsString(AType.KEY_PREFIX));
        final Object result = cache.get(cacheKey);
        if (result != null) {
            return (result instanceof PertinentNegativeNull) ? null : result;
        }
    } catch (Throwable ex) {
        if (LOG.isDebugEnabled()) {
            LOG.warn("Caching on " + pjp.toShortString() + " aborted due to an error.", ex);
        } else {
            LOG.warn("Caching on " + pjp.toShortString() + " aborted due to an error: " + ex.getMessage());
        }
        return pjp.proceed();
    }

    final Object result = pjp.proceed();

    // This is injected caching.  If anything goes wrong in the caching, LOG the crap outta it,
    // but do not let it surface up past the AOP injection itself.
    try {
        final Object submission = (result == null) ? new PertinentNegativeNull() : result;
        boolean cacheable = true;
        if (submission instanceof CacheConditionally) {
            cacheable = ((CacheConditionally) submission).isCacheable();
        }
        if (cacheable) {
            cache.set(cacheKey, calculateJitteredExpiration(info.getAsInteger(AType.EXPIRATION),
                    info.getAsInteger(AType.JITTER)), submission);
        }

        // Notify the observers that a cache interaction happened.
        final List<ReadThroughAssignCacheListener> listeners = getPertinentListeners(
                ReadThroughAssignCacheListener.class, info.getAsString(AType.NAMESPACE));
        if (listeners != null && !listeners.isEmpty()) {
            for (final ReadThroughAssignCacheListener listener : listeners) {
                try {
                    listener.triggeredReadThroughAssignCache(info.getAsString(AType.NAMESPACE),
                            info.getAsString(AType.ASSIGN_KEY, null), result, pjp.getArgs());
                } catch (Exception ex) {
                    LOG.warn("Problem when triggering a listener.", ex);
                }
            }
        }
    } catch (Throwable ex) {
        if (LOG.isDebugEnabled()) {
            LOG.warn("Caching on " + pjp.toShortString() + " aborted due to an error.", ex);
        } else {
            LOG.warn("Caching on " + pjp.toShortString() + " aborted due to an error: " + ex.getMessage());
        }
    }
    return result;
}

From source file:org.flite.cach3.aop.ReadThroughMultiCacheAdvice.java

License:Open Source License

@Around("getMulti()")
public Object cacheMulti(final ProceedingJoinPoint pjp) throws Throwable {
    // If we've disabled the caching programmatically (or via properties file) just flow through.
    if (isCacheDisabled()) {
        LOG.debug("Caching is disabled.");
        return pjp.proceed();
    }//from  w w w  .  j a va2 s.co  m

    final MemcachedClientIF cache = getMemcachedClient();
    // This is injected caching.  If anything goes wrong in the caching, LOG the crap outta it,
    // but do not let it surface up past the AOP injection itself.
    final MultiCacheCoordinator coord = new MultiCacheCoordinator();
    AnnotationInfo info;
    Object[] args = pjp.getArgs();
    try {
        // Get the target method being invoked, and make sure it returns the correct info.
        coord.setMethod(getMethodToCache(pjp));
        verifyReturnTypeIsList(coord.getMethod(), ReadThroughMultiCache.class);

        // Get the annotation associated with this method, and make sure the values are valid.
        final ReadThroughMultiCache annotation = coord.getMethod().getAnnotation(ReadThroughMultiCache.class);

        info = getAnnotationInfo(annotation, coord.getMethod().getName(), getJitterDefault());

        // Get the list of objects that will provide the keys to all the cache values.
        coord.setKeyObjects(getKeyObjectList(info.getAsInteger(AType.KEY_INDEX, null), pjp, coord.getMethod()));

        // Create key->object and object->key mappings.
        coord.setHolder(convertIdObjectsToKeyMap(coord.getKeyObjects(), info.getAsString(AType.NAMESPACE),
                info.getAsString(AType.KEY_PREFIX), info.getAsString(AType.KEY_TEMPLATE), factory, methodStore,
                args));

        // Get the full list of cache keys and ask the cache for the corresponding values.
        coord.setInitialKey2Result(cache.getBulk(coord.getKey2Obj().keySet()));

        // We've gotten all positive cache results back, so build up a results list and return it.
        if (coord.getMissObjects().size() < 1) {
            return coord.generateResultList();
        }

        // Create the new list of arguments with a subset of the key objects that aren't in the cache.
        args = coord.modifyArgumentList(args, info.getAsInteger(AType.KEY_INDEX, null));
    } catch (Throwable ex) {
        // If there's an exception somewhere in the caching code, then just bail out
        // and call through to the target method with the original parameters.
        if (LOG.isDebugEnabled()) {
            LOG.warn("Caching on " + pjp.toShortString() + " aborted due to an error.", ex);
        } else {
            LOG.warn("Caching on " + pjp.toShortString() + " aborted due to an error: " + ex.getMessage());
        }
        return pjp.proceed();
    }

    /*
    Call the target method with the new subset of arguments.
    We are calling this outside of the try/catch block in case there are some
    'not our fault' problems with the target method. (Connection issues, etc...)
    Though, this decision could go either way, really.
     */
    final List results = (List) pjp.proceed(args);

    try {

        if (results.size() != coord.getMissObjects().size()) {
            throw new RuntimeException("Did not receive a correlated amount of data from the target method.");
        }

        final String[] cacheBaseIds = new String[results.size()];
        for (int ix = 0; ix < results.size(); ix++) {
            final Object keyObject = coord.getMissObjects().get(ix);
            final Object resultObject = results.get(ix) == null ? new PertinentNegativeNull() : results.get(ix);
            final String cacheKey = coord.obj2Key.get(keyObject);
            final String cacheBase = coord.obj2Base.get(keyObject);
            boolean cacheable = true;
            if (resultObject instanceof CacheConditionally) {
                cacheable = ((CacheConditionally) resultObject).isCacheable();
            }
            if (cacheable) {
                cache.set(cacheKey, calculateJitteredExpiration(info.getAsInteger(AType.EXPIRATION),
                        info.getAsInteger(AType.JITTER)), resultObject);
            }
            coord.getKey2Result().put(cacheKey, resultObject);
            cacheBaseIds[ix] = cacheBase;
        }

        // Notify the observers that a cache interaction happened.
        final List<ReadThroughMultiCacheListener> listeners = getPertinentListeners(
                ReadThroughMultiCacheListener.class, info.getAsString(AType.NAMESPACE));
        if (listeners != null && !listeners.isEmpty()) {
            for (final ReadThroughMultiCacheListener listener : listeners) {
                try {
                    listener.triggeredReadThroughMultiCache(info.getAsString(AType.NAMESPACE),
                            info.getAsString(AType.KEY_PREFIX, null), Arrays.asList(cacheBaseIds), results,
                            args);
                } catch (Exception ex) {
                    LOG.warn("Problem when triggering a listener.", ex);
                }
            }
        }

        return coord.generateResultList();
    } catch (Throwable ex) {
        if (LOG.isDebugEnabled()) {
            LOG.warn("Caching on " + pjp.toShortString()
                    + " aborted due to an error. (The underlying method will be called twice.)", ex);
        } else {
            LOG.warn("Caching on " + pjp.toShortString()
                    + " aborted due to an error. (The underlying method will be called twice.): "
                    + ex.getMessage());
        }
        return pjp.proceed();
    }
}

From source file:org.flite.cach3.aop.ReadThroughSingleCacheAdvice.java

License:Open Source License

@Around("getSingle()")
public Object cacheSingle(final ProceedingJoinPoint pjp) throws Throwable {
    // If we've disabled the caching programmatically (or via properties file) just flow through.
    if (isCacheDisabled()) {
        LOG.debug("Caching is disabled.");
        return pjp.proceed();
    }/*from  w  w  w .  j a v a  2s.com*/

    final MemcachedClientIF cache = getMemcachedClient();
    // This is injected caching.  If anything goes wrong in the caching, LOG the crap outta it,
    // but do not let it surface up past the AOP injection itself.
    final String baseKey;
    final String cacheKey;
    final ReadThroughSingleCache annotation;
    final AnnotationInfo info;
    final Object[] args = pjp.getArgs();
    try {
        final Method methodToCache = getMethodToCache(pjp);
        annotation = methodToCache.getAnnotation(ReadThroughSingleCache.class);
        info = getAnnotationInfo(annotation, methodToCache.getName(), getJitterDefault());
        baseKey = generateBaseKeySingle(args, info, methodToCache.toString());
        cacheKey = buildCacheKey(baseKey, info.getAsString(AType.NAMESPACE, null),
                info.getAsString(AType.KEY_PREFIX, null));

        final Object result = cache.get(cacheKey);
        if (result != null) {
            return (result instanceof PertinentNegativeNull) ? null : result;
        }
    } catch (Throwable ex) {
        if (LOG.isDebugEnabled()) {
            LOG.warn("Caching on " + pjp.toShortString() + " aborted due to an error.", ex);
        } else {
            LOG.warn("Caching on " + pjp.toShortString() + " aborted due to an error: " + ex.getMessage());
        }
        return pjp.proceed();
    }

    final Object result = pjp.proceed();

    // This is injected caching.  If anything goes wrong in the caching, LOG the crap outta it,
    // but do not let it surface up past the AOP injection itself.
    try {
        final Object submission = (result == null) ? new PertinentNegativeNull() : result;
        boolean cacheable = true;
        if (submission instanceof CacheConditionally) {
            cacheable = ((CacheConditionally) submission).isCacheable();
        }
        if (cacheable) {
            cache.set(cacheKey, calculateJitteredExpiration(info.getAsInteger(AType.EXPIRATION),
                    info.getAsInteger(AType.JITTER)), submission);
        }

        // Notify the observers that a cache interaction happened.
        final List<ReadThroughSingleCacheListener> listeners = getPertinentListeners(
                ReadThroughSingleCacheListener.class, info.getAsString(AType.NAMESPACE));
        if (listeners != null && !listeners.isEmpty()) {
            for (final ReadThroughSingleCacheListener listener : listeners) {
                try {
                    listener.triggeredReadThroughSingleCache(info.getAsString(AType.NAMESPACE),
                            info.getAsString(AType.KEY_PREFIX, null), baseKey, result, args);
                } catch (Exception ex) {
                    LOG.warn("Problem when triggering a listener.", ex);
                }
            }
        }
    } catch (Throwable ex) {
        if (LOG.isDebugEnabled()) {
            LOG.warn("Caching on " + pjp.toShortString() + " aborted due to an error.", ex);
        } else {
            LOG.warn("Caching on " + pjp.toShortString() + " aborted due to an error: " + ex.getMessage());
        }
    }
    return result;
}

From source file:org.flockdata.spring.annotations.FlockDataAspect.java

License:Open Source License

@Around(value = "@annotation(annotation)")
public void createEntity(final ProceedingJoinPoint joinPoint, final FlockEntity annotation) throws Throwable {
    try {/*from  w  ww .j  a v  a 2 s . c  o  m*/
        logger.debug("createEntity() is running!");
        logger.debug("hijacked method : {}", joinPoint.getSignature().getName());
        logger.debug("hijacked arguments : {}", Arrays.toString(joinPoint.getArgs()));
        joinPoint.proceed();
        logger.debug("Around before is running!\r\n");
        joinPoint.proceed(); //continue on the intercepted method
    } finally {
        logger.info("Around after is running!");
    }
}

From source file:org.flockdata.spring.annotations.FlockDataAspect.java

License:Open Source License

@Around(value = "@annotation(annotation)")
public void createEntityLog(final ProceedingJoinPoint joinPoint, final FlockLog annotation) throws Throwable {
    try {/*  ww w . j a v a 2  s.c  om*/
        logger.debug("createAuditLog() is running!");
        logger.debug("hijacked method : {}", joinPoint.getSignature().getName());
        logger.debug("hijacked arguments : {}", Arrays.toString(joinPoint.getArgs()));
        joinPoint.proceed();
        logger.debug("Around before is running!\r\n");
        joinPoint.proceed(); //continue on the intercepted method
    } finally {
        logger.debug("Around after is running!");
    }
}

From source file:org.focusns.common.event.support.EventInterceptor.java

License:Open Source License

@Around("@within(org.springframework.stereotype.Service)")
public Object weave(ProceedingJoinPoint pjp) throws Throwable {
    // MethodInvocation invocation = (MethodInvocation) pjp;
    Method method = getMethod(pjp);
    Map<String, Object> args = getArguments(method, pjp.getArgs());
    ////from w  ww  .  j a  v  a2 s . co  m
    Object result = null;
    try {
        triggerEvent(Event.Point.BEFORE, method, args, null, null);
        //
        result = pjp.proceed();
        //
        triggerEvent(Event.Point.AFTER, method, args, result, null);
        //
    } catch (Throwable throwable) {
        //
        triggerEvent(Event.Point.AFTER_THROWING, method, args, result, throwable);
    }
    //
    return result;
}

From source file:org.fornax.cartridges.sculptor.framework.event.annotation.PublishAdvice.java

License:Apache License

@Around("@annotation(publish)")
public Object publish(ProceedingJoinPoint joinPoint, Publish publish) throws Throwable {

    Object retVal = joinPoint.proceed();

    String topic = publish.topic();
    Event event = null;//from   w  w w . j  av a  2 s. c om
    if (publish.eventType() == NullEventType.class) {
        if (retVal instanceof Event) {
            event = (Event) retVal;
        } else {
            for (Object each : joinPoint.getArgs()) {
                if (each instanceof Event) {
                    event = (Event) each;
                    break;
                }
            }
        }
    } else {
        event = createEvent(publish.eventType(), retVal, joinPoint.getArgs());
    }

    if (event == null) {
        throw new IllegalArgumentException(
                "Return value or some argument need to be of event type, or match constructor of specified eventType");
    }

    EventBus eventBus = getEventBus(publish.eventBus());
    eventBus.publish(topic, event);

    return retVal;
}

From source file:org.geosdi.geoplatform.cas.aop.SecurityCASAOP.java

License:Open Source License

@Around("execution(* it.geosolutions.geoserver.rest.GeoServerRESTReader.*(..))")
public Object adviceGeoServerRestReader(ProceedingJoinPoint proceedingJoinPoint) throws Throwable {
    logger.info("#########################AOP GeoServerRESTReader.*(..) is running...");
    Assertion receivedAssertion = this.retrieveAssertion();
    Object[] parameters = proceedingJoinPoint.getArgs();
    Class[] types = new Class[parameters.length];
    for (int i = 0; i < parameters.length; i++) {
        Object object = parameters[i];
        types[i] = object.getClass();/*w w w . jav  a  2s  .  co m*/
        logger.info("##########################Parameter type: {}\n", object.getClass());
    }
    casRestReader.setCasAssertion(receivedAssertion);
    try {
        Method method = GeoServerCASRESTReader.class.getMethod(proceedingJoinPoint.getSignature().getName(),
                types);
        method.setAccessible(Boolean.TRUE);
        return method.invoke(this.casRestReader, proceedingJoinPoint.getArgs());
    } catch (Exception e) {
        e.printStackTrace();
        throw new IllegalStateException(e.getMessage());
    }
}