Coverage Report - org.mule.routing.nested.NestedInvocationHandler
 
Classes in this File Line Coverage Branch Coverage Complexity
NestedInvocationHandler
0%
0/33
0%
0/16
4.25
 
 1  
 /*
 2  
  * $Id: NestedInvocationHandler.java 12075 2008-06-17 11:12:10Z rossmason $
 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.routing.nested;
 12  
 
 13  
 import org.mule.DefaultMuleMessage;
 14  
 import org.mule.RequestContext;
 15  
 import org.mule.api.MuleEvent;
 16  
 import org.mule.api.MuleMessage;
 17  
 import org.mule.api.routing.NestedRouter;
 18  
 import org.mule.config.i18n.CoreMessages;
 19  
 import org.mule.util.StringMessageUtils;
 20  
 
 21  
 import java.lang.reflect.InvocationHandler;
 22  
 import java.lang.reflect.Method;
 23  
 import java.util.Map;
 24  
 
 25  
 import edu.emory.mathcs.backport.java.util.concurrent.ConcurrentHashMap;
 26  
 import org.apache.commons.logging.Log;
 27  
 import org.apache.commons.logging.LogFactory;
 28  
 
 29  
 public class NestedInvocationHandler implements InvocationHandler
 30  
 {
 31  
 
 32  
     public static final String DEFAULT_METHOD_NAME_TOKEN = "default";
 33  
 
 34  0
     protected static Log logger = LogFactory.getLog(NestedInvocationHandler.class);
 35  
 
 36  0
     protected Map routers = new ConcurrentHashMap();
 37  
 
 38  
     public NestedInvocationHandler(NestedRouter router)
 39  0
     {
 40  0
         addRouterForInterface(router);
 41  0
     }
 42  
 
 43  
     public void addRouterForInterface(NestedRouter router)
 44  
     {
 45  0
         if (router.getMethod() == null)
 46  
         {
 47  0
             if (routers.size() == 0)
 48  
             {
 49  0
                 routers.put(DEFAULT_METHOD_NAME_TOKEN, router);
 50  
             }
 51  
             else
 52  
             {
 53  0
                 throw new IllegalArgumentException(CoreMessages.mustSetMethodNamesOnBinding().getMessage());
 54  
             }
 55  
         }
 56  
         else
 57  
         {
 58  0
             routers.put(router.getMethod(), router);
 59  
         }
 60  0
     }
 61  
 
 62  
     public Object invoke(Object proxy, Method method, Object[] args) throws Throwable
 63  
     {
 64  0
         if (method.getName().equals("toString"))
 65  
         {
 66  0
             return toString();
 67  
         }
 68  
 
 69  
         MuleMessage message;
 70  
 
 71  0
         if (args.length == 1)
 72  
         {
 73  0
             message = new DefaultMuleMessage(args[0]);
 74  
         }
 75  
         else
 76  
         {
 77  0
             message = new DefaultMuleMessage(args);
 78  
         }
 79  
 
 80  
 
 81  0
         NestedRouter router = (NestedRouter) routers.get(method.getName());
 82  0
         if (router == null)
 83  
         {
 84  0
             router = (NestedRouter) routers.get(DEFAULT_METHOD_NAME_TOKEN);
 85  
         }
 86  
 
 87  0
         if (router == null)
 88  
         {
 89  0
             throw new IllegalArgumentException(CoreMessages.cannotFindBindingForMethod(method.getName()).toString());
 90  
         }
 91  
 
 92  
         MuleMessage reply;
 93  0
         MuleEvent currentEvent = RequestContext.getEvent();
 94  0
         reply = router.route(message, currentEvent.getSession(), router.getEndpoint().isSynchronous());
 95  
 
 96  0
         if (reply != null)
 97  
         {
 98  0
             if (reply.getExceptionPayload() != null)
 99  
             {
 100  0
                 throw reply.getExceptionPayload().getException();
 101  
             }
 102  
             else
 103  
             {
 104  0
                 return reply.getPayload();
 105  
             }
 106  
         }
 107  
         else
 108  
         {
 109  0
             return null;
 110  
         }
 111  
     }
 112  
 
 113  
     public String toString()
 114  
     {
 115  0
         final StringBuffer sb = new StringBuffer();
 116  0
         sb.append("NestedInvocation");
 117  0
         sb.append("{routers='").append(StringMessageUtils.toString(routers));
 118  0
         sb.append('}');
 119  0
         return sb.toString();
 120  
     }
 121  
 }