This repository has been archived by the owner on Oct 14, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 39
/
GuardListInterceptor.java
137 lines (112 loc) · 4.03 KB
/
GuardListInterceptor.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
/*
* Copyright (c) 2010 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.zkybase.kite.interceptor;
import java.lang.reflect.Method;
import java.util.Collections;
import java.util.LinkedList;
import java.util.List;
import org.aopalliance.intercept.MethodInterceptor;
import org.aopalliance.intercept.MethodInvocation;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.zkybase.kite.Guard;
import org.zkybase.kite.GuardCallback;
/**
* @author Willie Wheeler (willie.wheeler@gmail.com)
* @since 1.0
*/
public class GuardListInterceptor implements MethodInterceptor {
private static final Logger log = LoggerFactory.getLogger(GuardListInterceptor.class);
private GuardListSource source;
public GuardListSource getSource() { return source; }
public void setSource(GuardListSource source) { this.source = source; }
/* (non-Javadoc)
* @see org.aopalliance.intercept.MethodInterceptor#invoke(org.aopalliance.intercept.MethodInvocation)
*/
@Override
public Object invoke(final MethodInvocation invocation) throws Throwable {
List<Guard> guards = getGuards(invocation);
if (guards == null || guards.isEmpty()) {
log.debug("Executing method {} without guards", invocation.getMethod().getName());
return invocation.proceed();
}
// Build a chain of custom interceptors (one per guard), terminating with the invocation.
LinkedList<Guard> guardStack = new LinkedList<Guard>(guards);
Collections.reverse(guardStack);
Guard lastGuard = guardStack.pop();
Interceptor interceptor = new LastInterceptor(lastGuard, invocation);
while (!guardStack.isEmpty()) {
Guard guard = guardStack.pop();
interceptor = new NotLastInterceptor(guard, interceptor);
}
return interceptor.invoke();
}
private List<Guard> getGuards(MethodInvocation invocation) {
Method method = invocation.getMethod();
// thisObj can be null if the invocation's static part (i.e. static joinpoint) is static (i.e. class-scoped)
Object thisObj = invocation.getThis();
Class<?> clazz = (thisObj != null ? thisObj.getClass() : null);
return source.getGuards(method, clazz);
}
private static interface Interceptor {
Object invoke() throws Throwable;
}
private static class NotLastInterceptor implements Interceptor {
private Guard guard;
private Interceptor interceptor;
public NotLastInterceptor(Guard guard, Interceptor interceptor) {
this.guard = guard;
this.interceptor = interceptor;
}
@Override
public Object invoke() throws Throwable {
return guard.execute(new GuardCallback<Object>() {
@Override
public Object doInGuard() throws Throwable {
try {
log.debug("Entered guard: {}", guard.getName());
return interceptor.invoke();
} finally {
log.debug("Exiting guard: {}", guard.getName());
}
}
});
}
}
private static class LastInterceptor implements Interceptor {
private Guard guard;
private MethodInvocation invocation;
public LastInterceptor(Guard guard, MethodInvocation invocation) {
this.guard = guard;
this.invocation = invocation;
}
@Override
public Object invoke() throws Throwable {
return guard.execute(new GuardCallback<Object>() {
@Override
public Object doInGuard() throws Throwable {
try {
log.debug("Entered guard: {}", guard.getName());
log.debug("Executing target method: {}", invocation.getMethod().getName());
return invocation.proceed();
} finally {
log.debug("Exiting guard: {}", guard.getName());
}
}
});
}
}
}