-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDynamicProxyExample.java
More file actions
140 lines (115 loc) · 3.41 KB
/
DynamicProxyExample.java
File metadata and controls
140 lines (115 loc) · 3.41 KB
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
138
139
140
import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;
import java.lang.reflect.Proxy;
enum ActionStatus {
READY,
RUNNING,
FINISHED,
FAILED
}
interface Action {
ActionStatus getStatus();
void execute();
}
abstract class AbstractAction implements Action {
protected ActionStatus status;
AbstractAction() {
this.status = ActionStatus.READY;
}
@Override
public ActionStatus getStatus() {
return status;
}
@Override
public void execute() {
status = ActionStatus.RUNNING;
try {
doExecute();
status = ActionStatus.FINISHED;
}
catch(Throwable t) {
status = ActionStatus.FAILED;
}
}
protected abstract void doExecute();
}
class SleepAction extends AbstractAction {
long millis;
SleepAction(long millis) {
this.millis = millis;
}
@Override
protected void doExecute() {
try {
Thread.sleep(millis);
} catch (InterruptedException e) {
// ignore
}
}
}
class LoggingInvocationHandler implements InvocationHandler {
private final Object target;
public LoggingInvocationHandler(Object target) {
this.target = target;
}
@Override
public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
System.out.println("Invoking " + method + " on object " + target);
Object returnValue = method.invoke(target, args);
System.out.println("Result is " + returnValue);
return returnValue;
}
}
class MagicActionHandler implements InvocationHandler {
private ActionStatus status;
private final Runnable runnable;
public MagicActionHandler(Runnable runnable) {
this.status = ActionStatus.READY;
this.runnable = runnable;
}
@Override
public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
String methodName = method.getName();
if ("getStatus".equals(methodName)) {
return status;
}
if ("execute".equals(methodName)) {
status = ActionStatus.RUNNING;
try {
runnable.run();
status = ActionStatus.FINISHED;
return null;
}
catch(Throwable t) {
status = ActionStatus.FAILED;
throw t;
}
}
if ("toString".equals(methodName)) {
return "MagicAction";
}
throw new NoSuchMethodException(methodName);
}
}
public class DynamicProxyExample {
public static void main(String[] args) {
Action action1 = (Action) Proxy.newProxyInstance(
DynamicProxyExample.class.getClassLoader(),
new Class[] { Action.class },
new LoggingInvocationHandler(new SleepAction(1000))
);
action1.execute();
Action action2 = (Action) Proxy.newProxyInstance(
DynamicProxyExample.class.getClassLoader(),
new Class[] { Action.class },
new MagicActionHandler( () -> System.out.println("RUN"))
);
action2.execute();
Action action3 = (Action) Proxy.newProxyInstance(
DynamicProxyExample.class.getClassLoader(),
new Class[] { Action.class },
new LoggingInvocationHandler(action2)
);
action3.execute();
}
}