-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
This exercise was a lot, but it was actually quite fun to implement. ^^
- Loading branch information
Showing
1 changed file
with
54 additions
and
1 deletion.
There are no files selected for viewing
55 changes: 54 additions & 1 deletion
55
ass2-aop/src/main/java/dst/ass2/aop/management/ManagementAspect.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
} | ||
|
||
} |