1 | |
|
2 | |
|
3 | |
|
4 | |
|
5 | |
|
6 | |
|
7 | |
|
8 | |
|
9 | |
|
10 | |
|
11 | |
package org.mule.util; |
12 | |
|
13 | |
import java.lang.reflect.InvocationHandler; |
14 | |
import java.lang.reflect.Method; |
15 | |
import java.lang.reflect.Proxy; |
16 | |
import java.util.ArrayList; |
17 | |
import java.util.Collection; |
18 | |
import java.util.Iterator; |
19 | |
import java.util.List; |
20 | |
|
21 | |
|
22 | |
|
23 | |
|
24 | |
|
25 | |
|
26 | |
|
27 | |
|
28 | |
public final class Multicaster |
29 | |
{ |
30 | |
|
31 | |
private Multicaster () |
32 | 0 | { |
33 | |
|
34 | 0 | } |
35 | |
|
36 | |
public static Object create(Class theInterface, Collection objects) |
37 | |
{ |
38 | 2 | return create(new Class[]{theInterface}, objects); |
39 | |
} |
40 | |
|
41 | |
public static Object create(Class theInterface, Collection objects, InvokeListener listener) |
42 | |
{ |
43 | 0 | return create(new Class[]{theInterface}, objects, listener); |
44 | |
} |
45 | |
|
46 | |
public static Object create(Class[] interfaces, Collection objects) |
47 | |
{ |
48 | 2 | return create(interfaces, objects, null); |
49 | |
} |
50 | |
|
51 | |
public static Object create(Class[] interfaces, Collection objects, InvokeListener listener) |
52 | |
{ |
53 | 2 | return Proxy.newProxyInstance(Thread.currentThread().getContextClassLoader(), interfaces, |
54 | |
new CastingHandler(objects, listener)); |
55 | |
} |
56 | |
|
57 | |
private static class CastingHandler implements InvocationHandler |
58 | |
{ |
59 | |
private final Collection objects; |
60 | |
private final InvokeListener listener; |
61 | |
|
62 | |
public CastingHandler(Collection objects) |
63 | |
{ |
64 | 0 | this(objects, null); |
65 | 0 | } |
66 | |
|
67 | |
public CastingHandler(Collection objects, InvokeListener listener) |
68 | 2 | { |
69 | 2 | this.objects = objects; |
70 | 2 | this.listener = listener; |
71 | 2 | } |
72 | |
|
73 | |
public Object invoke(Object proxy, Method method, Object[] args) throws Throwable |
74 | |
{ |
75 | 2 | List results = new ArrayList(); |
76 | 2 | Object item = null; |
77 | |
Object result; |
78 | |
|
79 | 2 | for (Iterator iterator = objects.iterator(); iterator.hasNext();) |
80 | |
{ |
81 | |
try |
82 | |
{ |
83 | 6 | item = iterator.next(); |
84 | 6 | result = method.invoke(item, args); |
85 | 6 | if (listener != null) |
86 | |
{ |
87 | 0 | listener.afterExecute(item, method, args); |
88 | |
} |
89 | 6 | if (result != null) |
90 | |
{ |
91 | 0 | results.add(result); |
92 | |
} |
93 | |
} |
94 | 0 | catch (Throwable t) |
95 | |
{ |
96 | |
|
97 | 0 | if (listener != null) |
98 | |
{ |
99 | 0 | t = listener.onException(item, method, args, t); |
100 | 0 | if (t != null) |
101 | |
{ |
102 | 0 | throw t; |
103 | |
} |
104 | |
} |
105 | 6 | } |
106 | |
} |
107 | 2 | return results; |
108 | |
} |
109 | |
} |
110 | |
|
111 | |
public static interface InvokeListener |
112 | |
{ |
113 | |
void afterExecute(Object object, Method method, Object[] args); |
114 | |
|
115 | |
Throwable onException(Object object, Method method, Object[] args, Throwable t); |
116 | |
} |
117 | |
|
118 | |
} |