From 97d26d9d0f03cae46e79f49682308d5e8f3c5628 Mon Sep 17 00:00:00 2001 From: Jan Vales Date: Fri, 4 May 2018 06:25:24 +0200 Subject: [PATCH] [2.3.2] Logging aspect done. --- .../dst/ass2/aop/logging/LoggingAspect.java | 46 ++++++++++++++++++- 1 file changed, 45 insertions(+), 1 deletion(-) diff --git a/ass2-aop/src/main/java/dst/ass2/aop/logging/LoggingAspect.java b/ass2-aop/src/main/java/dst/ass2/aop/logging/LoggingAspect.java index c0336c6..9bc2913 100644 --- a/ass2-aop/src/main/java/dst/ass2/aop/logging/LoggingAspect.java +++ b/ass2-aop/src/main/java/dst/ass2/aop/logging/LoggingAspect.java @@ -1,7 +1,51 @@ package dst.ass2.aop.logging; +import org.aspectj.lang.ProceedingJoinPoint; +import org.aspectj.lang.annotation.Around; +import org.aspectj.lang.annotation.Aspect; + +import java.lang.reflect.Field; +import java.util.logging.Logger; + +@Aspect public class LoggingAspect { + @Around("execution(* dst.ass2.aop.IPluginExecutable.execute(..)) && !@annotation(Invisible)") + public Object around(ProceedingJoinPoint point) { + //try to get logger, if one exists. + Logger logger = null; + for (Field f : point.getTarget().getClass().getDeclaredFields()) { + if (f.getType().isAssignableFrom(Logger.class)) { + try { + boolean accessible = f.isAccessible(); + f.setAccessible(true); + logger = (Logger) f.get(point.getTarget()); + f.setAccessible(accessible); + } catch (IllegalAccessException ignored) { + } + } + } + + //log before + if (logger != null) { + logger.info("Plugin " + point.getTarget().getClass() + " started to execute"); + } else { + System.out.println("Plugin " + point.getTarget().getClass() + " started to execute"); + } - // TODO + //execute actual function + Object result = null; + try { + result = point.proceed(); + } catch (Throwable throwable) { + throwable.printStackTrace(); + } + // log after. + if (logger != null) { + logger.info("Plugin " + point.getTarget().getClass() + " is finished"); + } else { + System.out.println("Plugin " + point.getTarget().getClass() + " is finished"); + } + return result; + } } -- 2.43.0