Coverage Report - org.mule.components.builder.ReflectionMessageBuilder
 
Classes in this File Line Coverage Branch Coverage Complexity
ReflectionMessageBuilder
0%
0/19
0%
0/3
9
 
 1  
 /*
 2  
  * $Id: ReflectionMessageBuilder.java 7976 2007-08-21 14:26:13Z 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  0
 public class ReflectionMessageBuilder extends AbstractMessageBuilder
 30  
 {
 31  
 
 32  
     // we don't want to match these methods when looking for a method
 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  
 }