]> git.somenet.org - pub/jan/dst18.git/blob - ass2-aop/src/main/java/dst/ass2/aop/logging/LoggingAspect.java
[2.3.2] Logging aspect done.
[pub/jan/dst18.git] / ass2-aop / src / main / java / dst / ass2 / aop / logging / LoggingAspect.java
1 package dst.ass2.aop.logging;
2
3 import org.aspectj.lang.ProceedingJoinPoint;
4 import org.aspectj.lang.annotation.Around;
5 import org.aspectj.lang.annotation.Aspect;
6
7 import java.lang.reflect.Field;
8 import java.util.logging.Logger;
9
10 @Aspect
11 public class LoggingAspect {
12     @Around("execution(* dst.ass2.aop.IPluginExecutable.execute(..)) && !@annotation(Invisible)")
13     public Object around(ProceedingJoinPoint point) {
14         //try to get logger, if one exists.
15         Logger logger = null;
16         for (Field f : point.getTarget().getClass().getDeclaredFields()) {
17             if (f.getType().isAssignableFrom(Logger.class)) {
18                 try {
19                     boolean accessible = f.isAccessible();
20                     f.setAccessible(true);
21                     logger = (Logger) f.get(point.getTarget());
22                     f.setAccessible(accessible);
23                 } catch (IllegalAccessException ignored) {
24                 }
25             }
26         }
27
28         //log before
29         if (logger != null) {
30             logger.info("Plugin " + point.getTarget().getClass() + " started to execute");
31         } else {
32             System.out.println("Plugin " + point.getTarget().getClass() + " started to execute");
33         }
34
35         //execute actual function
36         Object result = null;
37         try {
38             result = point.proceed();
39         } catch (Throwable throwable) {
40             throwable.printStackTrace();
41         }
42
43         // log after.
44         if (logger != null) {
45             logger.info("Plugin " + point.getTarget().getClass() + " is finished");
46         } else {
47             System.out.println("Plugin " + point.getTarget().getClass() + " is finished");
48         }
49         return result;
50     }
51 }