Example usage for org.aspectj.lang JoinPoint getThis

List of usage examples for org.aspectj.lang JoinPoint getThis

Introduction

In this page you can find the example usage for org.aspectj.lang JoinPoint getThis.

Prototype

Object getThis();

Source Link

Document

Returns the currently executing object.

Usage

From source file:at.ac.tuwien.infosys.jcloudscale.datastore.test.aspect.JUnitTestSeparatorAspect.java

License:Apache License

@Before("execution(@org.junit.Test void *.*(..))")
public void runTest(JoinPoint jp) {

    MethodSignature method = (MethodSignature) jp.getSignature();
    Object that = jp.getThis();
    System.err.println("############################################################");
    System.err.println("############################################################");
    System.err.println("Launching test " + that.getClass().getCanonicalName() + "." + method.getName() + "()");
    System.err.println("############################################################");
    System.err.println("############################################################");

}

From source file:at.ac.tuwien.infosys.jcloudscale.server.aspects.eventing.ExecutionEventAspect.java

License:Apache License

@Before("within(at.ac.tuwien.infosys.jcloudscale.server.CloudObjectInvocation) && execution(public void java.lang.Thread+.run())")
public void matchStartInvocation(JoinPoint jp) throws JCloudScaleException {

    try {/* www .  j  av a2 s .c  om*/
        Field this$0 = jp.getThis().getClass().getDeclaredField("this$0");
        this$0.setAccessible(true);
        CloudObjectInvocation inv = (CloudObjectInvocation) this$0.get(jp.getThis());
        // XXX AbstractJCloudScaleServerRunner
        // UUID serverId = JCloudScaleServerRunner.getInstance().getId();
        UUID serverId = AbstractJCloudScaleServerRunner.getInstance().getId();
        ExecutionStartedEvent event = new ExecutionStartedEvent();
        initializeBaseEventProperties(event);
        event.setMethod(inv.getMethod().getName());
        event.setObjectId(inv.getObjectId());
        event.setRequestId(inv.getRequestId());
        event.setObjectType(inv.getCloudObject().getClass());
        event.setHostId(serverId);
        getMqHelper().sendEvent(event);
        log.finer("Sent start invocation event for object " + inv.getObjectId());
    } catch (Exception e) {
        e.printStackTrace();
        log.severe("Error while triggering ExecutionStartedEvent: " + e.getMessage());
    }

}

From source file:at.ac.tuwien.infosys.jcloudscale.server.aspects.eventing.ExecutionEventAspect.java

License:Apache License

@After("within(at.ac.tuwien.infosys.jcloudscale.server.CloudObjectInvocation) && execution(public void java.lang.Thread+.run())")
public void matchFinishInvocation(JoinPoint jp) throws JCloudScaleException {

    try {/*from   w  w w .  j a  v  a  2s.  c om*/
        Field this$0 = jp.getThis().getClass().getDeclaredField("this$0");
        this$0.setAccessible(true);
        CloudObjectInvocation inv = (CloudObjectInvocation) this$0.get(jp.getThis());
        // XXX AbstractJCloudScaleServerRunner
        // UUID serverId = JCloudScaleServerRunner.getInstance().getId();
        UUID serverId = AbstractJCloudScaleServerRunner.getInstance().getId();
        if (inv.getStatus().equals(InvocationStatus.FINISHED)) {
            ExecutionFinishedEvent event = new ExecutionFinishedEvent();
            initializeBaseEventProperties(event);
            event.setMethod(inv.getMethod().getName());
            event.setObjectId(inv.getObjectId());
            event.setRequestId(inv.getRequestId());
            event.setObjectType(inv.getCloudObject().getClass());
            event.setHostId(serverId);
            getMqHelper().sendEvent(event);
            log.finer("Sent finished invocation event for object " + inv.getObjectId());
        } else if (inv.getStatus().equals(InvocationStatus.FAULTED)) {
            ExecutionFailedEvent event = new ExecutionFailedEvent();
            initializeBaseEventProperties(event);
            event.setMethod(inv.getMethod().getName());
            event.setObjectId(inv.getObjectId());
            event.setRequestId(inv.getRequestId());
            event.setException(inv.getError());
            event.setObjectType(inv.getCloudObject().getClass());
            event.setHostId(serverId);
            getMqHelper().sendEvent(event);
            log.finer("Sent failed invocation event for object " + inv.getObjectId());
        } else {
            log.severe("Finished invocation with unsupported outcome: " + inv.getStatus());
        }
    } catch (JMSException | NoSuchFieldException | IllegalAccessException e) {
        e.printStackTrace();
        log.severe("Error while triggering ExecutionFinishedEvent: " + e.getMessage());
    }

}

From source file:at.ac.tuwien.infosys.jcloudscale.server.aspects.eventing.ObjectLifecycleEventAspect.java

License:Apache License

@AfterReturning(pointcut = "execution(public String at.ac.tuwien.infosys.jcloudscale.server.JCloudScaleServer.createNewCloudObject(..))", returning = "ret")
public void matchObjectCreated(JoinPoint jp, String ret) throws JCloudScaleException {

    try {/*ww  w. j  a  v a2 s  .  c  om*/
        ObjectCreatedEvent event = new ObjectCreatedEvent();
        initializeBaseEventProperties(event);
        UUID objectId = UUID.fromString(ret);
        event.setObjectId(objectId);
        JCloudScaleServer server = (JCloudScaleServer) jp.getThis();
        // XXX AbstractJCloudScaleServerRunner
        // UUID serverId = JCloudScaleServerRunner.getInstance().getId();
        UUID serverId = AbstractJCloudScaleServerRunner.getInstance().getId();
        event.setHostId(serverId);
        ClassLoader cl = server.getCloudObjectClassLoader(objectId);
        Class<?> theClazz = Class.forName((String) (jp.getArgs()[0]), true, cl);
        event.setObjectType(theClazz);
        getMqHelper().sendEvent(event);
        log.finer("Sent object created for object " + objectId.toString());
    } catch (Exception e) {
        e.printStackTrace();
        log.severe("Error while triggering ObjectCreatedEvent: " + e.getMessage());
    }

}

From source file:com.esofthead.mycollab.common.interceptor.aspect.AuditLogAspect.java

License:Open Source License

@Before("(execution(public * com.esofthead.mycollab..service..*.updateWithSession(..)) || (execution(public * com.esofthead.mycollab..service..*.updateSelectiveWithSession(..)))) && args(bean, username)")
public void traceBeforeUpdateActivity(JoinPoint joinPoint, Object bean, String username) {

    Advised advised = (Advised) joinPoint.getThis();
    Class<?> cls = advised.getTargetSource().getTargetClass();

    Auditable auditAnnotation = cls.getAnnotation(Auditable.class);
    if (auditAnnotation != null) {
        try {//w  w w  . ja  v  a  2  s  .c  o m
            int typeid = (Integer) PropertyUtils.getProperty(bean, "id");
            int sAccountId = (Integer) PropertyUtils.getProperty(bean, "saccountid");
            // store old value to map, wait until the update process
            // successfully then add to log item

            // get old value
            Object service = advised.getTargetSource().getTarget();
            Method findMethod = null;
            Object oldValue = null;
            try {
                findMethod = cls.getMethod("findById", int.class, int.class);
            } catch (Exception e) {
                findMethod = cls.getMethod("findByPrimaryKey", Serializable.class, int.class, int.class);
            }
            oldValue = findMethod.invoke(service, typeid, sAccountId);
            String key = bean.toString() + auditAnnotation.type() + typeid;

            caches.put(key, oldValue);
        } catch (Exception e) {
            LOG.error("Error when save audit for save action of service " + cls.getName(), e);
        }
    }
}

From source file:com.esofthead.mycollab.common.interceptor.aspect.AuditLogAspect.java

License:Open Source License

@AfterReturning("(execution(public * com.esofthead.mycollab..service..*.updateWithSession(..)) || (execution(public * com.esofthead.mycollab..service..*.updateSelectiveWithSession(..))))  && args(bean, username)")
public void traceAfterUpdateActivity(JoinPoint joinPoint, Object bean, String username) {

    Advised advised = (Advised) joinPoint.getThis();
    Class<?> cls = advised.getTargetSource().getTargetClass();

    Traceable traceableAnnotation = cls.getAnnotation(Traceable.class);
    Integer activityStreamId = null;
    if (traceableAnnotation != null) {
        try {//from  w  w w .  jav  a 2  s .  co  m
            ActivityStream activity = TraceableAspect.constructActivity(traceableAnnotation, bean, username,
                    ActivityStreamConstants.ACTION_UPDATE);
            activityStreamId = activityStreamService.save(activity);
        } catch (Exception e) {
            LOG.error("Error when save activity for save action of service " + cls.getName(), e);
        }
    }

    int sAccountId;
    try {
        sAccountId = (Integer) PropertyUtils.getProperty(bean, "saccountid");
    } catch (IllegalAccessException | InvocationTargetException | NoSuchMethodException e) {
        return;
    }

    Integer auditLogId = saveAuditLog(cls, bean, username, sAccountId, activityStreamId);

    Watchable watchableAnnotation = cls.getAnnotation(Watchable.class);
    if (watchableAnnotation != null) {
        try {
            int monitorTypeId = (Integer) PropertyUtils.getProperty(bean, "id");
            String monitorType = watchableAnnotation.type();

            Integer extraTypeId = null;
            if (!"".equals(watchableAnnotation.extraTypeId())) {
                extraTypeId = (Integer) PropertyUtils.getProperty(bean, watchableAnnotation.extraTypeId());
            }

            MonitorItem monitorItem = new MonitorItem();
            monitorItem.setMonitorDate(new GregorianCalendar().getTime());
            monitorItem.setType(monitorType);
            monitorItem.setTypeid(monitorTypeId);
            monitorItem.setExtratypeid(extraTypeId);
            monitorItem.setUser(username);
            monitorItem.setSaccountid(sAccountId);

            if (!monitorItemService.isUserWatchingItem(username, monitorType, monitorTypeId)) {
                monitorItemService.saveWithSession(monitorItem, username);
            }

            // check whether the current user is in monitor list, if
            // not add him in
            if (!watchableAnnotation.userFieldName().equals("")) {
                String moreUser = (String) PropertyUtils.getProperty(bean, watchableAnnotation.userFieldName());
                if (moreUser != null && !moreUser.equals(username)) {
                    monitorItem.setId(null);
                    monitorItem.setUser(moreUser);
                    if (!monitorItemService.isUserWatchingItem(moreUser, monitorType, monitorTypeId)) {
                        monitorItemService.saveWithSession(monitorItem, moreUser);
                    }
                }
            }

            // Save notification email
            RelayEmailNotification relayNotification = new RelayEmailNotification();
            relayNotification.setChangeby(username);
            relayNotification.setChangecomment("");
            relayNotification.setSaccountid(sAccountId);
            relayNotification.setType(monitorType);
            relayNotification.setTypeid("" + monitorTypeId);
            relayNotification.setEmailhandlerbean(watchableAnnotation.emailHandlerBean().getName());
            if (auditLogId != null) {
                relayNotification.setExtratypeid(auditLogId);
            }

            relayNotification.setAction(MonitorTypeConstants.UPDATE_ACTION);

            relayEmailNotificationService.saveWithSession(relayNotification, username);
        } catch (Exception e) {
            LOG.error("Error when save audit for save action of service " + cls.getName() + "and bean: "
                    + BeanUtility.printBeanObj(bean), e);
        }
    }
}

From source file:com.esofthead.mycollab.common.interceptor.aspect.MonitorItemAspect.java

License:Open Source License

@AfterReturning("execution(public * com.esofthead.mycollab..service..*.saveWithSession(..)) && args(bean, username)")
public void traceSaveActivity(JoinPoint joinPoint, Object bean, String username) {
    Advised advised = (Advised) joinPoint.getThis();
    Class<?> cls = advised.getTargetSource().getTargetClass();

    Watchable watchableAnnotation = cls.getAnnotation(Watchable.class);
    if (watchableAnnotation != null) {
        try {//from w  w  w  .  ja  v a 2s . c  o m
            int sAccountId = (Integer) PropertyUtils.getProperty(bean, "saccountid");

            Integer extraTypeId = null;
            if (!"".equals(watchableAnnotation.extraTypeId())) {
                extraTypeId = (Integer) PropertyUtils.getProperty(bean, watchableAnnotation.extraTypeId());
            }

            MonitorItem monitorItem = new MonitorItem();
            monitorItem.setMonitorDate(new GregorianCalendar().getTime());
            monitorItem.setType(watchableAnnotation.type());
            monitorItem.setTypeid((Integer) PropertyUtils.getProperty(bean, "id"));
            monitorItem.setExtratypeid(extraTypeId);
            monitorItem.setUser(username);
            monitorItem.setSaccountid(sAccountId);

            monitorItemService.saveWithSession(monitorItem, username);
            LOG.debug("Save monitor item: " + BeanUtility.printBeanObj(monitorItem));

            if (!watchableAnnotation.userFieldName().equals("")) {
                String moreUser = (String) PropertyUtils.getProperty(bean, watchableAnnotation.userFieldName());
                if (moreUser != null && !moreUser.equals(username)) {
                    monitorItem.setId(null);
                    monitorItem.setUser(moreUser);
                    monitorItemService.saveWithSession(monitorItem, moreUser);
                }
            }

            RelayEmailNotification relayNotification = new RelayEmailNotification();
            relayNotification.setChangeby(username);
            relayNotification.setChangecomment("");
            relayNotification.setSaccountid(sAccountId);
            relayNotification.setType(watchableAnnotation.type());
            relayNotification.setAction(MonitorTypeConstants.CREATE_ACTION);
            int typeid = (Integer) PropertyUtils.getProperty(bean, "id");
            relayNotification.setTypeid("" + typeid);
            relayNotification.setEmailhandlerbean(watchableAnnotation.emailHandlerBean().getName());
            relayEmailNotificationService.saveWithSession(relayNotification, username);
            // Save notification item

        } catch (Exception e) {
            LOG.error("Error when save relay email notification for save action of service " + cls.getName(),
                    e);
        }
    }

}

From source file:com.esofthead.mycollab.common.interceptor.aspect.TraceableAspect.java

License:Open Source License

@AfterReturning("execution(public * com.esofthead.mycollab..service..*.saveWithSession(..)) && args(bean, username)")
public void traceSaveActivity(JoinPoint joinPoint, Object bean, String username) {

    Advised advised = (Advised) joinPoint.getThis();
    Class<?> cls = advised.getTargetSource().getTargetClass();

    Traceable traceableAnnotation = cls.getAnnotation(Traceable.class);
    if (traceableAnnotation != null) {
        try {/*from  w  w  w .j  a v a 2  s.co m*/
            ActivityStream activity = constructActivity(traceableAnnotation, bean, username,
                    ActivityStreamConstants.ACTION_CREATE);
            activityStreamService.save(activity);
        } catch (Exception e) {
            LOG.error("Error when save activity for save action of service " + cls.getName(), e);
        }
    }

}

From source file:com.esofthead.mycollab.common.interceptor.aspect.TraceableCreateAspect.java

License:Open Source License

@AfterReturning("execution(public * com.esofthead.mycollab..service..*.saveWithSession(..)) && args(bean, username)")
public void traceSaveActivity(JoinPoint joinPoint, Object bean, String username) {
    Advised advised = (Advised) joinPoint.getThis();
    Class<?> cls = advised.getTargetSource().getTargetClass();

    Traceable traceableAnnotation = cls.getAnnotation(Traceable.class);
    if (traceableAnnotation != null) {
        try {//from w  w w  .  j  a  va2 s  . c o  m
            ActivityStreamWithBLOBs activity = constructActivity(cls, traceableAnnotation, bean, username,
                    ActivityStreamConstants.ACTION_CREATE);
            activityStreamService.save(activity);
        } catch (Exception e) {
            LOG.error("Error when save activity for save action of service " + cls.getName(), e);
        }
    }
}

From source file:com.esofthead.mycollab.common.interceptor.aspect.TraceableCreateAspect.java

License:Open Source License

@AfterReturning("execution(public * com.esofthead.mycollab..service..*.removeWithSession(..)) && args(bean, username, sAccountId)")
public void traceDeleteActivity(JoinPoint joinPoint, Object bean, String username, Integer sAccountId) {
    Advised advised = (Advised) joinPoint.getThis();
    Class<?> cls = advised.getTargetSource().getTargetClass();

    Traceable traceableAnnotation = cls.getAnnotation(Traceable.class);
    if (traceableAnnotation != null) {
        try {//from w ww.  j  a  va  2 s.c om
            ActivityStreamWithBLOBs activity = constructActivity(cls, traceableAnnotation, bean, username,
                    ActivityStreamConstants.ACTION_DELETE);
            activityStreamService.save(activity);
        } catch (Exception e) {
            LOG.error("Error when save activity for save action of service " + cls.getName(), e);
        }
    }
}