Coverage Report - org.mule.module.ibeans.spi.MuleCallAnnotationHandler
 
Classes in this File Line Coverage Branch Coverage Complexity
MuleCallAnnotationHandler
0%
0/43
0%
0/14
0
 
 1  
 /*
 2  
  * Copyright (c) MuleSoft, Inc.  All rights reserved.  http://www.mulesoft.com
 3  
  * The software in this package is published under the terms of the CPAL v1.0
 4  
  * license, a copy of which has been included with this distribution in the
 5  
  * LICENSE.txt file.
 6  
  */
 7  
 package org.mule.module.ibeans.spi;
 8  
 
 9  
 import org.mule.DefaultMuleEvent;
 10  
 import org.mule.DefaultMuleMessage;
 11  
 import org.mule.api.MuleContext;
 12  
 import org.mule.api.MuleEvent;
 13  
 import org.mule.api.MuleMessage;
 14  
 import org.mule.api.MuleSession;
 15  
 import org.mule.api.component.InterfaceBinding;
 16  
 import org.mule.api.context.MuleContextAware;
 17  
 import org.mule.api.endpoint.ImmutableEndpoint;
 18  
 import org.mule.config.i18n.CoreMessages;
 19  
 import org.mule.message.DefaultExceptionPayload;
 20  
 import org.mule.module.ibeans.config.IBeanFlowConstruct;
 21  
 import org.mule.session.DefaultMuleSession;
 22  
 import org.mule.transport.NullPayload;
 23  
 import org.mule.util.StringMessageUtils;
 24  
 
 25  
 import java.lang.reflect.Method;
 26  
 import java.util.HashMap;
 27  
 import java.util.Map;
 28  
 
 29  
 import org.apache.commons.logging.Log;
 30  
 import org.apache.commons.logging.LogFactory;
 31  
 import org.ibeans.api.ClientAnnotationHandler;
 32  
 import org.ibeans.api.InvocationContext;
 33  
 import org.ibeans.api.Request;
 34  
 import org.ibeans.api.Response;
 35  
 
 36  
 /**
 37  
  * Used to Handle {@link org.ibeans.annotation.Call} annotated method calls.
 38  
  */
 39  
 public class MuleCallAnnotationHandler implements ClientAnnotationHandler
 40  
 {
 41  
     public static final String DEFAULT_METHOD_NAME_TOKEN = "default";
 42  
 
 43  0
     protected static Log logger = LogFactory.getLog(MuleCallAnnotationHandler.class);
 44  
 
 45  
     private MuleContext muleContext;
 46  
 
 47  
     private IBeanFlowConstruct flow;
 48  
 
 49  0
     protected Map<String, InterfaceBinding> routers = new HashMap<String, InterfaceBinding>();
 50  
 
 51  
     public MuleCallAnnotationHandler(MuleContext muleContext)
 52  0
     {
 53  0
         this.muleContext = muleContext;
 54  0
     }
 55  
 
 56  
     public void setFlow(IBeanFlowConstruct flow)
 57  
     {
 58  0
         this.flow = flow;
 59  0
     }
 60  
 
 61  
     public void addRouterForInterface(InterfaceBinding router)
 62  
     {
 63  0
         if (router instanceof MuleContextAware)
 64  
         {
 65  0
             ((MuleContextAware) router).setMuleContext(muleContext);
 66  
         }
 67  0
         if (router.getMethod() == null)
 68  
         {
 69  0
             if (routers.size() == 0)
 70  
             {
 71  0
                 routers.put(DEFAULT_METHOD_NAME_TOKEN, router);
 72  
             }
 73  
             else
 74  
             {
 75  0
                 throw new IllegalArgumentException(CoreMessages.mustSetMethodNamesOnBinding().getMessage());
 76  
             }
 77  
         }
 78  
         else
 79  
         {
 80  0
             routers.put(router.getMethod(), router);
 81  
         }
 82  
 
 83  
 
 84  0
     }
 85  
 
 86  
     public Response invoke(InvocationContext ctx) throws Exception
 87  
     {
 88  0
         InterfaceBinding router = routers.get(ctx.getMethod().toString());
 89  0
         if (router == null)
 90  
         {
 91  0
             throw new IllegalArgumentException(CoreMessages.cannotFindBindingForMethod(ctx.getMethod().getName()).toString());
 92  
         }
 93  0
         router.getEndpoint().getProperties().putAll(ctx.getIBeanDefaultConfig().getPropertyParams());
 94  0
         Request req = ctx.getRequest();
 95  0
         MuleMessage message = ((MuleRequestMessage)req).getMessage();
 96  
 
 97  0
         if (logger.isTraceEnabled())
 98  
         {
 99  
             try
 100  
             {
 101  0
                 logger.trace("Message Before invoking "
 102  
                         + ctx.getMethod()
 103  
                         + ": \n"
 104  
                         + StringMessageUtils.truncate(
 105  
                         StringMessageUtils.toString(message.getPayload()),
 106  
                         2000, false));
 107  0
                 logger.trace("Message Headers: \n"
 108  
                         + StringMessageUtils.headersToString(message));
 109  
             }
 110  0
             catch (Exception e)
 111  
             {
 112  
                 // ignore
 113  0
             }
 114  
         }
 115  
 
 116  
 //        MuleMessage message = new DefaultMuleMessage(req.getPayload(), muleContext);
 117  
 //
 118  
 //        message.addProperties(router.getEndpoint().getProperties(), PropertyScope.INVOCATION);
 119  
 //        for (String s : req.getHeaderNames())
 120  
 //        {
 121  
 //            message.setOutboundProperty(s, req.getHeader(s));
 122  
 //        }
 123  
 //        for (String s : req.getAttachmentNames())
 124  
 //        {
 125  
 //            message.addAttachment(s, req.getAttachment(s));
 126  
 //        }
 127  
        
 128  
 
 129  0
         MuleEvent replyEvent = null;
 130  
         MuleMessage reply;
 131  0
         MuleSession session = new DefaultMuleSession(flow, muleContext);
 132  
 
 133  
         try
 134  
         {
 135  0
             replyEvent = router.process(new DefaultMuleEvent(message, router.getEndpoint(), session));
 136  
         }
 137  0
         catch (Throwable e)
 138  
         {
 139  
             //Make all exceptions go through the CallException handler
 140  0
             reply = new DefaultMuleMessage(NullPayload.getInstance(), muleContext);
 141  0
             reply.setExceptionPayload(new DefaultExceptionPayload(e));
 142  0
             return new MuleResponseMessage(reply);
 143  0
         }
 144  0
         return new MuleResponseMessage(replyEvent.getMessage());
 145  
     }
 146  
 
 147  
     public String getScheme(Method method)
 148  
     {
 149  0
         InterfaceBinding router = routers.get(method.toString());
 150  0
         if (router == null)
 151  
         {
 152  0
             throw new IllegalArgumentException(CoreMessages.cannotFindBindingForMethod(method.getName()).toString());
 153  
         }
 154  0
         return router.getEndpoint().getEndpointURI().getScheme();
 155  
     }
 156  
 
 157  
     ImmutableEndpoint getEndpointForMethod(Method method)
 158  
     {
 159  0
         InterfaceBinding router = routers.get(method.toString());
 160  0
         if (router != null)
 161  
         {
 162  0
             return router.getEndpoint();
 163  
         }
 164  0
         return null;
 165  
     }
 166  
 }