]> git.somenet.org - pub/jan/dst18.git/blob - ass2-aop/src/main/java/dst/ass2/aop/management/ManagementAspect.java
[2.3.3] Interruption timers. We have them.
[pub/jan/dst18.git] / ass2-aop / src / main / java / dst / ass2 / aop / management / ManagementAspect.java
1 package dst.ass2.aop.management;
2
3 import dst.ass2.aop.IPluginExecutable;
4 import org.aspectj.lang.ProceedingJoinPoint;
5 import org.aspectj.lang.annotation.Around;
6 import org.aspectj.lang.annotation.Aspect;
7 import org.aspectj.lang.reflect.MethodSignature;
8
9 import java.util.Timer;
10 import java.util.TimerTask;
11
12 @Aspect
13 public class ManagementAspect {
14     @Around("execution(* dst.ass2.aop.IPluginExecutable.execute(..)) && @annotation(Timeout)")
15     public Object around(ProceedingJoinPoint point) {
16         // setup interruptionTimer.
17         Timeout timeoutAnn = ((MethodSignature) point.getSignature()).getMethod().getAnnotation(Timeout.class);
18         System.out.println("ManagementAspect - sched");
19
20         Timer interruptionTimer = new Timer(true);
21         interruptionTimer.schedule(new TimerTask() {
22             @Override
23             public void run() {
24                 System.out.println("ManagementAspect - interrupt");
25                 ((IPluginExecutable) point.getTarget()).interrupted();
26             }
27         }, timeoutAnn.value());
28
29         //execute actual function
30         Object result = null;
31         try {
32             result = point.proceed();
33         } catch (Throwable throwable) {
34             throwable.printStackTrace();
35         }
36
37         // once the function completed, cancel interruptionTimer and return results.
38         interruptionTimer.cancel();
39         return result;
40     }
41 }