1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20 package org.apache.mina.statemachine;
21
22 import java.lang.reflect.InvocationHandler;
23 import java.lang.reflect.Method;
24 import java.lang.reflect.Proxy;
25
26 import org.apache.mina.statemachine.context.SingletonStateContextLookup;
27 import org.apache.mina.statemachine.context.StateContext;
28 import org.apache.mina.statemachine.context.StateContextLookup;
29 import org.apache.mina.statemachine.event.DefaultEventFactory;
30 import org.apache.mina.statemachine.event.Event;
31 import org.apache.mina.statemachine.event.EventArgumentsInterceptor;
32 import org.apache.mina.statemachine.event.EventFactory;
33 import org.apache.mina.statemachine.event.UnhandledEventException;
34 import org.slf4j.Logger;
35 import org.slf4j.LoggerFactory;
36
37
38
39
40
41
42
43 public class StateMachineProxyBuilder {
44 private static final Logger log = LoggerFactory
45 .getLogger(StateMachineProxyBuilder.class);
46
47 private static final Object[] EMPTY_ARGUMENTS = new Object[0];
48
49 private StateContextLookup contextLookup = new SingletonStateContextLookup();
50
51 private EventFactory eventFactory = new DefaultEventFactory();
52
53 private EventArgumentsInterceptor interceptor = null;
54
55 private boolean ignoreUnhandledEvents = false;
56
57 private boolean ignoreStateContextLookupFailure = false;
58
59 private String name = null;
60
61
62
63
64
65 private ClassLoader defaultCl = null;
66
67 public StateMachineProxyBuilder() {
68 }
69
70
71
72
73
74
75
76
77
78 public StateMachineProxyBuilder setName(String name) {
79 this.name = name;
80 return this;
81 }
82
83
84
85
86
87
88
89
90 public StateMachineProxyBuilder setStateContextLookup(
91 StateContextLookup contextLookup) {
92 this.contextLookup = contextLookup;
93 return this;
94 }
95
96
97
98
99
100
101
102
103 public StateMachineProxyBuilder setEventFactory(EventFactory eventFactory) {
104 this.eventFactory = eventFactory;
105 return this;
106 }
107
108
109
110
111
112
113
114
115 public StateMachineProxyBuilder setEventArgumentsInterceptor(
116 EventArgumentsInterceptor interceptor) {
117 this.interceptor = interceptor;
118 return this;
119 }
120
121
122
123
124
125
126
127
128
129 public StateMachineProxyBuilder setIgnoreUnhandledEvents(boolean b) {
130 this.ignoreUnhandledEvents = b;
131 return this;
132 }
133
134
135
136
137
138
139
140
141
142 public StateMachineProxyBuilder setIgnoreStateContextLookupFailure(boolean b) {
143 this.ignoreStateContextLookupFailure = b;
144 return this;
145 }
146
147
148
149
150
151
152
153
154
155 public StateMachineProxyBuilder setClassLoader(ClassLoader cl) {
156 this.defaultCl = cl;
157 return this;
158 }
159
160
161
162
163
164
165
166
167
168
169 @SuppressWarnings("unchecked")
170 public <T> T create(Class<T> iface, StateMachine sm) {
171 return (T) create(new Class[] { iface }, sm);
172 }
173
174
175
176
177
178
179
180
181
182
183 public Object create(Class<?>[] ifaces, StateMachine sm) {
184 ClassLoader cl = defaultCl;
185 if (cl == null) {
186 cl = Thread.currentThread().getContextClassLoader();
187 }
188
189 InvocationHandler handler = new MethodInvocationHandler(sm,
190 contextLookup, interceptor, eventFactory,
191 ignoreUnhandledEvents, ignoreStateContextLookupFailure, name);
192
193 return Proxy.newProxyInstance(cl, ifaces, handler);
194 }
195
196 private static class MethodInvocationHandler implements InvocationHandler {
197 private final StateMachine sm;
198 private final StateContextLookup contextLookup;
199 private final EventArgumentsInterceptor interceptor;
200 private final EventFactory eventFactory;
201 private final boolean ignoreUnhandledEvents;
202 private final boolean ignoreStateContextLookupFailure;
203 private final String name;
204
205 public MethodInvocationHandler(StateMachine sm, StateContextLookup contextLookup,
206 EventArgumentsInterceptor interceptor, EventFactory eventFactory,
207 boolean ignoreUnhandledEvents, boolean ignoreStateContextLookupFailure,
208 String name) {
209
210 this.contextLookup = contextLookup;
211 this.sm = sm;
212 this.interceptor = interceptor;
213 this.eventFactory = eventFactory;
214 this.ignoreUnhandledEvents = ignoreUnhandledEvents;
215 this.ignoreStateContextLookupFailure = ignoreStateContextLookupFailure;
216 this.name = name;
217 }
218
219 public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
220 if ("hashCode".equals(method.getName()) && args == null) {
221 return new Integer(System.identityHashCode(proxy));
222 }
223 if ("equals".equals(method.getName()) && args.length == 1) {
224 return Boolean.valueOf(proxy == args[0]);
225 }
226 if ("toString".equals(method.getName()) && args == null) {
227 return (name != null ? name : proxy.getClass().getName()) + "@"
228 + Integer.toHexString(System.identityHashCode(proxy));
229 }
230
231 if (log.isDebugEnabled()) {
232 log.debug("Method invoked: " + method);
233 }
234
235 args = args == null ? EMPTY_ARGUMENTS : args;
236 if (interceptor != null) {
237 args = interceptor.modify(args);
238 }
239
240 StateContext context = contextLookup.lookup(args);
241
242 if (context == null) {
243 if (ignoreStateContextLookupFailure) {
244 return null;
245 }
246 throw new IllegalStateException("Cannot determine state "
247 + "context for method invocation: " + method);
248 }
249
250 Event event = eventFactory.create(context, method, args);
251
252 try {
253 sm.handle(event);
254 } catch (UnhandledEventException uee) {
255 if (!ignoreUnhandledEvents) {
256 throw uee;
257 }
258 }
259
260 return null;
261 }
262 }
263 }