KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > mule > components > builder > ReflectionMessageBuilder


1 /*
2  * $Id: ReflectionMessageBuilder.java 3798 2006-11-04 04:07:14Z aperepel $
3  * --------------------------------------------------------------------------------------
4  * Copyright (c) MuleSource, Inc. All rights reserved. http://www.mulesource.com
5  *
6  * The software in this package is published under the terms of the MuleSource MPL
7  * license, a copy of which has been included with this distribution in the
8  * LICENSE.txt file.
9  */

10
11 package org.mule.components.builder;
12
13 import org.mule.config.i18n.Message;
14 import org.mule.config.i18n.Messages;
15 import org.mule.umo.UMOMessage;
16 import org.mule.util.ClassUtils;
17
18 import java.lang.reflect.Method JavaDoc;
19 import java.util.List JavaDoc;
20
21 /**
22  * Will try and set the result of an invocation as a bean property on the request
23  * message using reflection
24  *
25  * @author <a HREF="mailto:ross.mason@symphonysoft.com">Ross Mason</a>
26  * @version $Revision: 3798 $
27  */

28 public class ReflectionMessageBuilder extends AbstractMessageBuilder
29 {
30
31     // we don't want to match these methods when looking for a method
32
protected String JavaDoc[] ignoreMethods = new String JavaDoc[]{"equals", "getInvocationHandler"};
33
34     public Object JavaDoc buildMessage(UMOMessage request, UMOMessage response) throws MessageBuilderException
35     {
36         Object JavaDoc master = request.getPayload();
37         Object JavaDoc property = response.getPayload();
38         List JavaDoc methods = null;
39         try
40         {
41             methods = ClassUtils.getSatisfiableMethods(master.getClass(), new Class JavaDoc[]{property.getClass()},
42                 true, false, ignoreMethods);
43         }
44         catch (Exception JavaDoc e)
45         {
46             throw new MessageBuilderException(request, e);
47         }
48         if (methods.size() == 0)
49         {
50             throw new MessageBuilderException(new Message(Messages.NO_MATCHING_METHODS_FOR_X_ON_X,
51                 property.getClass().getName(), master.getClass().getName()), request);
52         }
53         else if (methods.size() > 1)
54         {
55             throw new MessageBuilderException(new Message(Messages.TOO_MANY_MATCHING_METHODS_FOR_X_ON_X,
56                 property.getClass().getName(), master.getClass().getName()), request);
57         }
58         else
59         {
60             Method JavaDoc m = (Method JavaDoc)methods.get(0);
61             try
62             {
63                 m.invoke(master,
64                     (property.getClass().isArray() ? (Object JavaDoc[])property : new Object JavaDoc[]{property}));
65             }
66             catch (Exception JavaDoc e)
67             {
68                 throw new MessageBuilderException(new Message(Messages.FAILED_TO_INVOKE_X,
69                     m.getName() + " on " + master.getClass().getName()), request);
70
71             }
72         }
73         return master;
74     }
75 }
76
Popular Tags