Skip to content

Commit

Permalink
Finish 2.3.3 Plugin Management
Browse files Browse the repository at this point in the history
This exercise was a lot, but it was actually quite fun to implement. ^^
  • Loading branch information
flofriday committed May 1, 2023
1 parent 552a684 commit cf7431c
Showing 1 changed file with 54 additions and 1 deletion.
Original file line number Diff line number Diff line change
@@ -1,7 +1,60 @@
package dst.ass2.aop.management;

import dst.ass2.aop.IPluginExecutable;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.After;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.aspectj.lang.annotation.Pointcut;

import java.util.Map;
import java.util.Timer;
import java.util.TimerTask;
import java.util.concurrent.ConcurrentHashMap;

@Aspect
public class ManagementAspect {

// TODO
Map<IPluginExecutable, TimerTask> interuptTimers = new ConcurrentHashMap<>();

@Pointcut("execution(void dst.ass2.aop.IPluginExecutable.execute())")
public void executionMethodPointcut() {
}

@Before("executionMethodPointcut()")
public void executionStart(JoinPoint joinPoint) {
var plugin = (IPluginExecutable) joinPoint.getTarget();

long timeoutMs = 0;
try {
var method = plugin.getClass().getMethod("execute");
var annotation = method.getAnnotation(Timeout.class);
if (annotation == null)
return;

timeoutMs = annotation.value();
} catch (NoSuchMethodException e) {
throw new RuntimeException(e);
}

var interruptTask = new TimerTask() {
@Override
public void run() {
plugin.interrupted();
}
};

interuptTimers.put(plugin, interruptTask);
new Timer().schedule(interruptTask, timeoutMs);
}

@After("executionMethodPointcut()")
public void executionEnds(JoinPoint joinPoint) {
var plugin = (IPluginExecutable) joinPoint.getTarget();
var task = interuptTimers.get(plugin);
if (task == null)
return;
task.cancel();
}

}

0 comments on commit cf7431c

Please sign in to comment.