12
12
import net .bytebuddy .asm .Advice ;
13
13
import net .bytebuddy .description .type .TypeDescription ;
14
14
import net .bytebuddy .matcher .ElementMatcher ;
15
+ import org .apache .pekko .http .javadsl .server .RouteResult ;
16
+ import org .apache .pekko .http .scaladsl .server .RequestContext ;
17
+ import scala .PartialFunction ;
18
+ import scala .Unit ;
19
+ import scala .concurrent .Future ;
20
+ import scala .util .Try ;
15
21
16
22
public class RouteConcatenationInstrumentation implements TypeInstrumentation {
17
23
@ Override
@@ -27,28 +33,48 @@ public void transform(TypeTransformer transformer) {
27
33
named ("$anonfun$$tilde$2" ), this .getClass ().getName () + "$Apply2Advice" );
28
34
}
29
35
36
+ public static class OnExitFinalizer implements PartialFunction <Try <RouteResult >, Unit > {
37
+ @ Override
38
+ public boolean isDefinedAt (Try <RouteResult > x ) {
39
+ return true ;
40
+ }
41
+
42
+ @ Override
43
+ public Unit apply (Try <RouteResult > v1 ) {
44
+ System .err .println ("===tilde$1 onExit===" );
45
+ PekkoRouteHolder .restore ();
46
+ return null ;
47
+ }
48
+ }
49
+
30
50
@ SuppressWarnings ("unused" )
31
51
public static class ApplyAdvice {
32
52
33
53
@ Advice .OnMethodEnter (suppress = Throwable .class )
34
54
public static void onEnter () {
35
- // when routing dsl uses concat(path(...) {...}, path(...) {...}) we'll restore the currently
36
- // matched route after each matcher so that match attempts that failed wouldn't get recorded
55
+ // when routing dsl uses concat(path(...) {...}, path(...) {...}) we'll restore
56
+ // the currently
57
+ // matched route after each matcher so that match attempts that failed wouldn't
58
+ // get recorded
37
59
// in the route
60
+ System .err .println ("===tilde$1 onEnter===" );
38
61
PekkoRouteHolder .save ();
39
62
}
40
63
41
64
@ Advice .OnMethodExit (onThrowable = Throwable .class , suppress = Throwable .class )
42
- public static void onExit () {
43
- PekkoRouteHolder .restore ();
65
+ public static void onExit (@ Advice .Argument (value = 2 ) RequestContext ctx , @ Advice .Return (readOnly = false ) Future <RouteResult > fut ) {
66
+ fut = fut
67
+ .andThen (new OnExitFinalizer (), ctx .executionContext ());
44
68
}
69
+
45
70
}
46
71
47
72
@ SuppressWarnings ("unused" )
48
73
public static class Apply2Advice {
49
74
50
75
@ Advice .OnMethodEnter (suppress = Throwable .class )
51
76
public static void onEnter () {
77
+ System .err .println ("===tilde$2 onEnter===" );
52
78
PekkoRouteHolder .reset ();
53
79
}
54
80
}
0 commit comments