1 | |
|
2 | |
|
3 | |
|
4 | |
|
5 | |
|
6 | |
|
7 | |
|
8 | |
|
9 | |
|
10 | |
|
11 | |
package org.mule.components.builder; |
12 | |
|
13 | |
import org.mule.config.i18n.CoreMessages; |
14 | |
import org.mule.impl.NoSatisfiableMethodsException; |
15 | |
import org.mule.impl.TooManySatisfiableMethodsException; |
16 | |
import org.mule.umo.UMOMessage; |
17 | |
import org.mule.util.ClassUtils; |
18 | |
|
19 | |
import java.lang.reflect.Method; |
20 | |
import java.util.Arrays; |
21 | |
import java.util.HashSet; |
22 | |
import java.util.List; |
23 | |
import java.util.Set; |
24 | |
|
25 | |
|
26 | |
|
27 | |
|
28 | |
|
29 | 0 | public class ReflectionMessageBuilder extends AbstractMessageBuilder |
30 | |
{ |
31 | |
|
32 | |
|
33 | 0 | protected final Set ignoreMethods = new HashSet(Arrays.asList(new String[]{"equals", "getInvocationHandler"})); |
34 | |
|
35 | |
public Object buildMessage(UMOMessage request, UMOMessage response) throws MessageBuilderException |
36 | |
{ |
37 | 0 | Object master = request.getPayload(); |
38 | 0 | Object property = response.getPayload(); |
39 | 0 | List methods = null; |
40 | |
try |
41 | |
{ |
42 | 0 | methods = ClassUtils.getSatisfiableMethods(master.getClass(), new Class[]{property.getClass()}, |
43 | |
true, false, ignoreMethods); |
44 | |
} |
45 | 0 | catch (Exception e) |
46 | |
{ |
47 | 0 | throw new MessageBuilderException(request, e); |
48 | 0 | } |
49 | 0 | if (methods.size() == 0) |
50 | |
{ |
51 | 0 | throw new MessageBuilderException(request, |
52 | |
new NoSatisfiableMethodsException(master, new Class[]{property.getClass()})); |
53 | |
} |
54 | 0 | else if (methods.size() > 1) |
55 | |
{ |
56 | 0 | throw new MessageBuilderException(request, |
57 | |
new TooManySatisfiableMethodsException(master, |
58 | |
new String[]{property.getClass().getName()})); |
59 | |
} |
60 | |
else |
61 | |
{ |
62 | 0 | Method m = (Method) methods.get(0); |
63 | |
try |
64 | |
{ |
65 | 0 | m.invoke(master, |
66 | |
(property.getClass().isArray() ? (Object[]) property : new Object[]{property})); |
67 | |
} |
68 | 0 | catch (Exception e) |
69 | |
{ |
70 | 0 | throw new MessageBuilderException( |
71 | |
CoreMessages.failedToInvoke(m.getName() + " on " + master.getClass().getName()), |
72 | |
request, e); |
73 | 0 | } |
74 | |
} |
75 | 0 | return master; |
76 | |
} |
77 | |
} |