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