|
1
|
1 |
|
package io.mcarle.strix; |
|
2
|
|
|
|
|
3
|
|
|
import io.mcarle.strix.annotation.Transactional; |
|
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.annotation.Pointcut; |
|
8
|
|
|
|
|
9
|
|
|
@Aspect |
|
10
|
1 |
|
public class TransactionalAspect { |
|
11
|
|
|
|
|
12
|
|
|
/** |
|
13
|
|
|
* Checks, if strix is started |
|
14
|
|
|
* |
|
15
|
|
|
* @return The state of strix: {@code true} if it is started, {@code false} otherwise. |
|
16
|
|
|
*/ |
|
17
|
|
|
@Pointcut("if()") |
|
18
|
|
|
public static boolean isPersistenceStarted() { |
|
19
|
|
|
return StrixManager.STARTED; |
|
20
|
|
|
} |
|
21
|
|
|
|
|
22
|
|
|
/** |
|
23
|
|
|
* Matches any execution of a public method |
|
24
|
|
|
*/ |
|
25
|
|
|
@Pointcut("execution(public * *(..))") |
|
26
|
|
|
public void publicMethod() { |
|
27
|
|
|
} |
|
28
|
|
|
|
|
29
|
|
|
/** |
|
30
|
|
|
* Matches any execution of a method |
|
31
|
|
|
*/ |
|
32
|
|
|
@Pointcut("execution(* *(..))") |
|
33
|
|
|
public void anyMethod() { |
|
34
|
|
|
} |
|
35
|
|
|
|
|
36
|
|
|
/** |
|
37
|
|
|
* Matches any method, which is annotated with {@link io.mcarle.strix.annotation.NoTransaction} |
|
38
|
|
|
*/ |
|
39
|
|
|
@Pointcut("@annotation(io.mcarle.strix.annotation.NoTransaction)") |
|
40
|
|
|
public void noTransactionAnnotated() { |
|
41
|
|
|
} |
|
42
|
|
|
|
|
43
|
|
|
/** |
|
44
|
|
|
* Matches any method, which is annotated with {@link Transactional} |
|
45
|
|
|
*/ |
|
46
|
|
|
@Pointcut("@annotation(io.mcarle.strix.annotation.Transactional)") |
|
47
|
|
|
public void transactionalAnnotated() { |
|
48
|
|
|
} |
|
49
|
|
|
|
|
50
|
|
|
/** |
|
51
|
|
|
* If strix is started: Matches any method, which is not annotated with |
|
52
|
|
|
* {@link io.mcarle.strix.annotation.NoTransaction} |
|
53
|
|
|
*/ |
|
54
|
|
|
@Pointcut("isPersistenceStarted() && anyMethod() && ! noTransactionAnnotated()") |
|
55
|
|
|
public void startedAndMethodNotAnnotatedWithNoTransaction() { |
|
56
|
|
|
} |
|
57
|
|
|
|
|
58
|
|
|
/** |
|
59
|
|
|
* If strix is started: Matches any public method, which is not annotated with |
|
60
|
|
|
* {@link io.mcarle.strix.annotation.NoTransaction} |
|
61
|
|
|
*/ |
|
62
|
|
|
@Pointcut("startedAndMethodNotAnnotatedWithNoTransaction() && publicMethod()") |
|
63
|
|
|
public void startedAndPublicMethodNotAnnotatedWithNoTransaction() { |
|
64
|
|
|
} |
|
65
|
|
|
|
|
66
|
|
|
/** |
|
67
|
|
|
* If strix is started: Executes around any public method, which is annotated with {@link Transactional} and not |
|
68
|
|
|
* with {@link io.mcarle.strix.annotation.NoTransaction}. |
|
69
|
|
|
* |
|
70
|
|
|
* @param joinPoint The join point of AspectJ |
|
71
|
|
|
* @param transactional The {@link Transactional} annotation of the method |
|
72
|
|
|
* @return The result of the aspected method |
|
73
|
|
|
* @throws Throwable If the aspected method throws an exception |
|
74
|
|
|
*/ |
|
75
|
|
|
@Around("startedAndMethodNotAnnotatedWithNoTransaction() && @annotation(transactional)") |
|
76
|
|
|
public Object aroundMethodAnnotatedWithTransactional( |
|
77
|
|
|
ProceedingJoinPoint joinPoint, |
|
78
|
|
|
Transactional transactional |
|
79
|
|
|
) throws Throwable { |
|
80
|
1 |
|
return StrixManager.handleTransactionalMethodExecution(joinPoint, transactional); |
|
81
|
|
|
} |
|
82
|
|
|
|
|
83
|
|
|
/** |
|
84
|
|
|
* If strix is started: Executes around any public method, which is not annotated with {@link Transactional} or |
|
85
|
|
|
* {@link io.mcarle.strix.annotation.NoTransaction}, but is part of a class annotated with {@link Transactional}. |
|
86
|
|
|
* |
|
87
|
|
|
* @param joinPoint The join point of AspectJ |
|
88
|
|
|
* @param transactional The {@link Transactional} annotation of the class |
|
89
|
|
|
* @return The result of the aspected method |
|
90
|
|
|
* @throws Throwable If the aspected method throws an exception |
|
91
|
|
|
*/ |
|
92
|
|
|
@Around("startedAndPublicMethodNotAnnotatedWithNoTransaction() && @within(transactional) && ! transactionalAnnotated()") |
|
93
|
|
|
public Object aroundMethodNotAnnotatedWithTransactionalInClassAnnotatedWithTransactional( |
|
94
|
|
|
ProceedingJoinPoint joinPoint, |
|
95
|
|
|
Transactional transactional |
|
96
|
|
|
) throws Throwable { |
|
97
|
1 |
|
return StrixManager.handleTransactionalMethodExecution(joinPoint, transactional); |
|
98
|
|
|
} |
|
99
|
|
|
|
|
100
|
|
|
} |
|
101
|
|
|
|