View Javadoc

1   /*
2    * $Id: ReflectionMessageBuilder.java 7963 2007-08-21 08:53:15Z dirk.olmes $
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 CPAL v1.0
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.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   * Will try and set the result of an invocation as a bean property on the request
27   * message using reflection.
28   */
29  public class ReflectionMessageBuilder extends AbstractMessageBuilder
30  {
31  
32      // we don't want to match these methods when looking for a method
33      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          Object master = request.getPayload();
38          Object property = response.getPayload();
39          List methods = null;
40          try
41          {
42              methods = ClassUtils.getSatisfiableMethods(master.getClass(), new Class[]{property.getClass()},
43                  true, false, ignoreMethods);
44          }
45          catch (Exception e)
46          {
47              throw new MessageBuilderException(request, e);
48          }
49          if (methods.size() == 0)
50          {
51              throw new MessageBuilderException(request,
52                      new NoSatisfiableMethodsException(master, new Class[]{property.getClass()}));
53          }
54          else if (methods.size() > 1)
55          {
56              throw new MessageBuilderException(request,
57                      new TooManySatisfiableMethodsException(master,
58                              new String[]{property.getClass().getName()}));
59          }
60          else
61          {
62              Method m = (Method) methods.get(0);
63              try
64              {
65                  m.invoke(master,
66                      (property.getClass().isArray() ? (Object[]) property : new Object[]{property}));
67              }
68              catch (Exception e)
69              {
70                  throw new MessageBuilderException(
71                      CoreMessages.failedToInvoke(m.getName() + " on " + master.getClass().getName()), 
72                      request, e);
73              }
74          }
75          return master;
76      }
77  }