Example usage for org.aspectj.lang ProceedingJoinPoint proceed

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

Introduction

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

Prototype

public Object proceed() throws Throwable;

Source Link

Document

Proceed with the next advice or target method invocation

Usage

From source file:com.solace.ExceptionHandlingAspect.java

License:Open Source License

/**
 * Will fire around methods decorated with {@link ExceptionHandled} in
 * following fashion:/*from  ww  w  .  j av  a 2 s . c om*/
 * <p>
 * <ol>
 * <li>if {@link ExceptionHandled#logExceptionStack()} is true then a
 * uniform exception will be printed to {@link Logger#error(String)}</li>
 * <li>if {@link ExceptionHandled#handlers()} is not empty the exception
 * will be:</li>
 * <ol>
 * <li>evaluated to see if it is an instance of
 * {@link ExceptionHandledBy#handleFor()} <b>OR</b> if the exception is
 * derived from a class in {@link ExceptionHandledBy#handleFor()} if
 * {@link ExceptionHandledBy#checkForDerived()} is <b>true</b></li>
 * <li>if one or both of the previous conditions are met the exception will
 * be applied to each {@link IExceptionHandler} in
 * {@link ExceptionHandledBy#handlers()}</li>
 * <li>finally, if {@link ExceptionHandledBy#rethrowAs()} is set to a class
 * derived off of {@link Exception} the new exception will be created
 * through {@link Exception#Exception(String, Throwable)} and rethrown</li>
 * </ol>
 * </ol>
 * <p>
 * It should be noted that the rethrow could be held in
 * {@link ExceptionHandled} however, at this point, we don't quite know how
 * people will want to use the advice. Until Because of this we will set one
 * at the global level to be handled after all evaluations have occurred.
 * Now if, however, a nested {@link ExceptionHandledBy} has a rethrowAs set
 * the global will be missed as the rethrow will occur inside of the loop
 * evaluation.
 * 
 * @param pjp
 *            The ProceedingJoinPoint with metadata around the method that
 *            will be called
 * @param exceptionHandled
 *            A collection of exception types to be handled. If defined we
 *            will proceed calls down to the ExceptionHandler. If none will
 *            proceed then we automatically move forward.
 * @return the object returned by the joinpoint
 * @throws Throwable
 */
@SuppressWarnings("unchecked")
@Around(value = "call(* *(..)) && @annotation(exceptionHandled)", argNames = "pjp,exceptionHandled")
public Object handle(final ProceedingJoinPoint pjp, final ExceptionHandled exceptionHandled) throws Throwable {

    Object retVal = null;

    try {

        retVal = pjp.proceed();

    } catch (Exception e) {

        if (exceptionHandled.logExceptionStack()) {
            Logger logger = null;

            if (pjp.getThis() == null)
                logger = Logger.getLogger("main");
            else
                logger = Logger.getLogger(pjp.getThis().getClass());

            logger.error("Exception caught:\nInput arguments: {}", e, buildArgsString(pjp.getArgs()));
        }

        for (ExceptionHandledBy by : exceptionHandled.handlers()) {

            for (Class<? extends Exception> eClass : by.handleFor()) {
                if (eClass.equals(e.getClass())
                        || (by.checkForDerived() && eClass.isAssignableFrom(e.getClass()))) {
                    for (Class<? extends IExceptionHandler> handlerClass : by.handlers()) {
                        IExceptionHandler handler = null;

                        if ((handler = instances.get(handlerClass)) == null) {
                            handler = handlerClass.newInstance();
                            instances.put(handlerClass, handler);
                        }

                        // invoke the IExceptionHandler by leveraging
                        // the code from the JoinPoint
                        // passed in the ProceedingJoinPoint instance
                        handler.handle(pjp.getThis(), e.getMessage(), e, pjp.getArgs());
                    }

                    if (Exception.class.isAssignableFrom(by.rethrowAs())) {
                        Class<? extends Exception> rethrowClass = (Class<? extends Exception>) by.rethrowAs();

                        LOGGER.debug("Rethrowing as {}", rethrowClass.toString());

                        Constructor<? extends Exception> ctor = rethrowClass.getConstructor(String.class,
                                Throwable.class);

                        Exception rethrow = ctor.newInstance(e.getMessage(), e);

                        throw rethrow;
                    }
                }
            }
        }

        if (Exception.class.isAssignableFrom(exceptionHandled.rethrowAs())) {
            Class<? extends Exception> rethrowClass = (Class<? extends Exception>) exceptionHandled.rethrowAs();

            LOGGER.debug("Global rethrow as {}", rethrowClass.toString());

            Constructor<? extends Exception> ctor = rethrowClass.getConstructor(String.class, Throwable.class);

            Exception rethrow = ctor.newInstance(e.getMessage(), e);

            throw rethrow;
        }
    }

    return retVal;
}

From source file:com.spring.aop.EmployeeAroundAspect.java

@Around("execution(* com.spring.aop.Employee.getName())")
public Object employeeAroundAdvice(ProceedingJoinPoint proceedingJoinPoint) {
    System.out.println("Before invoking getName() method");
    Object value = null;/*ww w . ja v a2s  . co m*/
    try {
        value = proceedingJoinPoint.proceed();
    } catch (Throwable e) {
        e.printStackTrace();
    }
    System.out.println("After invoking getName() method. Return value=" + value);
    return value;
}

From source file:com.spstudio.common.log.ServiceExceptionLogAspect.java

@Around("serviceAspect()")
public Object doAroundTask(ProceedingJoinPoint pjp) throws Throwable {
    logger.warn("Entering.." + pjp.getSignature().getDeclaringTypeName() + "." + pjp.getSignature().getName());
    try {//from w  ww.j a  va2  s . c  om
        return pjp.proceed();
    } catch (Throwable ex) {

        System.out.println("error in around");
        throw ex;
    } finally {
        logger.warn(
                "Exiting.." + pjp.getSignature().getDeclaringTypeName() + "." + pjp.getSignature().getName());
    }
}

From source file:com.spstudio.session.filter.SessionAOP.java

@Around(value = "@annotation(com.spstudio.session.UserSession)")
public Object aroundManager(ProceedingJoinPoint pj) throws Exception {
    HttpServletRequest request = SysContent.getRequest();
    HttpServletResponse response = SysContent.getResponse();
    HttpSession session = SysContent.getSession();

    String path = request.getContextPath();
    String basePath = request.getScheme() + "://" + request.getServerName() + ":" + request.getServerPort()
            + path + "/";

    UserSessionType type = this.getSessionType(pj);
    if (type == null) {
        throw new Exception("The value of NeedSession is must.");
    }//from ww w. ja va  2  s.  c om

    Object uobj = session.getAttribute("user");
    Object mobj = session.getAttribute("manager");

    boolean isUser = type == UserSessionType.USER && uobj != null;
    boolean isManager = type == UserSessionType.MANAGER && mobj != null;
    boolean isUserOrManager = type == UserSessionType.OR && (mobj != null || uobj != null);
    try {
        if (isUser || isManager || isUserOrManager) {
            return pj.proceed();
        } else { // ?session  
            if (request.getHeader("x-requested-with") != null
                    && request.getHeader("x-requested-with").equalsIgnoreCase( //ajax?       
                            "XMLHttpRequest")) {
                response.addHeader("sessionstatus", "timeout");
                // EasyUi  
                //response.getWriter().print("{\"rows\":[],\"success\":false,\"total\":0}");       
            } else {//http?       
                response.sendRedirect(basePath + "error/nosession");
            }
        }
    } catch (Throwable e) {
        // TODO Auto-generated catch block  
        e.printStackTrace();
    }
    return null;
}

From source file:com.square.logs.aop.advice.GestionLogAdvice.java

License:Open Source License

/**
 * ajoute le log de l'appel service.//from  www .  j a va  2  s.c o m
 * @param point l'appel intercept
 * @return Object retour de l'appel
 * @throws Throwable exception
 */
public Object log(ProceedingJoinPoint point) throws Throwable {
    Object retVal = null;
    final String methodName = point.getSignature().toLongString();
    final Object[] args = point.getArgs();

    final LogDto logDto = new LogDto();
    logDto.setApplication(application);
    logDto.setProfondeur(profondeur);
    logDto.setDate(Calendar.getInstance());
    logDto.setSignature(methodName);

    try {
        logDto.setParametres(Arrays.asList(args));
        retVal = point.proceed();
        logDto.setResultat(retVal);
        if (logLevel == LogLevel.INFO) {
            logService.ajouterLog(logDto);
        }
        return retVal;
    } catch (Throwable e) {
        if (logLevel != LogLevel.NONE) {
            logDto.setTraceException(e.getMessage());
            logService.ajouterLog(logDto);
        }
        throw e;
    }
}

From source file:com.square.synchro.aop.CreerEspaceClientAdvice.java

License:Open Source License

/**
 * {@inheritDoc}//from  ww  w . j  a  v  a  2 s .co  m
 */
public Object transformationVivier(ProceedingJoinPoint pjp) throws Throwable {
    // Rcupration du paramtre & analyse des synchros  lancer :
    final Long idPersonne = (Long) pjp.getArgs()[0];
    final Object retVal = pjp.proceed();
    try {
        creerEspaceClient(idPersonne);
    } catch (Exception e) {
        logger.fatal(messageSourceUtil.get(MessageKeyUtil.LOGGER_FATAL_CREATION_ESPACE_CLIENT_PP,
                new String[] { e.getMessage() }), e);
    }
    return retVal;
}

From source file:com.square.synchro.aop.ModifierPasswordAdvice.java

License:Open Source License

/**
 * {@inheritDoc}// w w  w  .  j a  v a  2s .co  m
 */
public Object modifierMotDePasse(ProceedingJoinPoint pjp) throws Throwable {
    logger.debug("AOP modifyPassword");
    // FIXME OpenSource
    //        final ModifyPasswordDto modifyPasswordDto = (ModifyPasswordDto) pjp.getArgs()[0];
    //
    //        // on recupere l'email
    //        final UtilisateurDto utilisateur = utilisateurService.getUtilisateurById(modifyPasswordDto.getIdUser());
    //
    final Object retVal = pjp.proceed();
    //        try {
    //            modifierMotDePasseZimbra(utilisateur.getEmail(), modifyPasswordDto.getActualPassword(), modifyPasswordDto.getNewPassword());
    //        } catch (Exception e) {
    //            logger.fatal("Erreur Synchro MDP Zimbra,  poursuite du traitement : " + e.getMessage(), e);
    //        }
    return retVal;
}

From source file:com.square.synchro.aop.ModifierPasswordAdvice.java

License:Open Source License

/**
 * {@inheritDoc}//from w ww .  j  ava 2 s . c om
 */
public Object reinitialiserMotDePasseUtilisateur(ProceedingJoinPoint pjp) throws Throwable {
    logger.debug("AOP reinitialiserMotDePasseUtilisateur");
    // FIXME OpenSource
    //        final Long idUtilisateur = (Long) pjp.getArgs()[0];
    //
    //        // on recupere l'email et le mot de passe
    //        final UtilisateurDto utilisateur = utilisateurService.getUtilisateurById(idUtilisateur);
    //
    final Object retVal = pjp.proceed();
    //        try {
    //            final UtilisateurDto utilisateurRetour = (UtilisateurDto) retVal;
    //            modifierMotDePasseZimbra(utilisateur.getEmail(), utilisateur.getPassword(), utilisateurRetour.getPassword());
    //        } catch (Exception e) {
    //            logger.fatal("Erreur Synchro MDP Zimbra,  poursuite du traitement : " + e.getMessage(), e);
    //        }
    return retVal;
}

From source file:com.stackify.metric.aop.aspectj.CounterAndTimerAspect.java

License:Apache License

/**
 * Around advice for a CounterAndTimer metric
 * @param cata CounterAndTimer annotation
 * @return The underlying method's return
 * @throws Throwable /*  ww  w  .java 2 s.  co m*/
 */
@Around("execution(* *(..)) && @annotation(cata)")
public Object around(final ProceedingJoinPoint joinPoint, final com.stackify.metric.aop.CounterAndTimer cata)
        throws Throwable {

    long start = System.currentTimeMillis();

    try {
        return joinPoint.proceed();
    } finally {
        MetricFactory.getCounterAndTimer(cata.category(), cata.name()).startMs(start);
    }
}

From source file:com.stackify.metric.aop.aspectj.TimerAspect.java

License:Apache License

/**
 * Around advice for a Timer metric//from  ww  w.ja  v a  2s .  com
 * @param ta Timer annotation
 * @return The underlying method's return
 * @throws Throwable 
 */
@Around("execution(* *(..)) && @annotation(ta)")
public Object around(final ProceedingJoinPoint joinPoint, final com.stackify.metric.aop.Timer ta)
        throws Throwable {

    long start = System.currentTimeMillis();

    try {
        return joinPoint.proceed();
    } finally {
        MetricFactory.getTimer(ta.category(), ta.name()).startMs(start);
    }
}