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