Coverage Report - org.mule.client.DefaultLocalMuleClient
 
Classes in this File Line Coverage Branch Coverage Complexity
DefaultLocalMuleClient
0%
0/53
0%
0/14
0
DefaultLocalMuleClient$MuleClientFlowConstruct
0%
0/10
N/A
0
 
 1  
 /*
 2  
  * $Id: DefaultLocalMuleClient.java 20385 2010-11-29 20:25:26Z dfeist $
 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  
 
 11  
 package org.mule.client;
 12  
 
 13  
 import org.mule.DefaultMuleEvent;
 14  
 import org.mule.DefaultMuleMessage;
 15  
 import org.mule.MessageExchangePattern;
 16  
 import org.mule.api.MuleContext;
 17  
 import org.mule.api.MuleEvent;
 18  
 import org.mule.api.MuleException;
 19  
 import org.mule.api.MuleMessage;
 20  
 import org.mule.api.client.LocalMuleClient;
 21  
 import org.mule.api.construct.FlowConstruct;
 22  
 import org.mule.api.endpoint.EndpointBuilder;
 23  
 import org.mule.api.endpoint.InboundEndpoint;
 24  
 import org.mule.api.endpoint.OutboundEndpoint;
 25  
 import org.mule.api.exception.MessagingExceptionHandler;
 26  
 import org.mule.api.lifecycle.LifecycleState;
 27  
 import org.mule.api.processor.MessageProcessorChain;
 28  
 import org.mule.api.routing.MessageInfoMapping;
 29  
 import org.mule.api.transport.ReceiveException;
 30  
 import org.mule.exception.DefaultServiceExceptionStrategy;
 31  
 import org.mule.management.stats.FlowConstructStatistics;
 32  
 import org.mule.session.DefaultMuleSession;
 33  
 
 34  
 import java.util.Map;
 35  
 
 36  
 import edu.emory.mathcs.backport.java.util.concurrent.ConcurrentHashMap;
 37  
 import edu.emory.mathcs.backport.java.util.concurrent.ConcurrentMap;
 38  
 
 39  
 public class DefaultLocalMuleClient implements LocalMuleClient
 40  
 {
 41  
 
 42  
     protected MuleContext muleContext;
 43  0
     private ConcurrentMap inboundEndpointCache = new ConcurrentHashMap();
 44  0
     private ConcurrentMap outboundEndpointCache = new ConcurrentHashMap();
 45  
 
 46  
     public DefaultLocalMuleClient(MuleContext muleContext)
 47  0
     {
 48  0
         this.muleContext = muleContext;
 49  0
     }
 50  
 
 51  
     public MuleMessage process(OutboundEndpoint endpoint,
 52  
                                Object payload,
 53  
                                Map<String, Object> messageProperties) throws MuleException
 54  
     {
 55  0
         return process(endpoint, new DefaultMuleMessage(payload, messageProperties, muleContext));
 56  
 
 57  
     }
 58  
 
 59  
     public MuleMessage process(OutboundEndpoint endpoint, MuleMessage message) throws MuleException
 60  
     {
 61  0
         return returnMessage(endpoint.process(createMuleEvent(message, endpoint)));
 62  
     }
 63  
 
 64  
     public MuleMessage request(InboundEndpoint endpoint, long timeout) throws MuleException
 65  
     {
 66  
         try
 67  
         {
 68  0
             return endpoint.request(timeout);
 69  
         }
 70  0
         catch (Exception e)
 71  
         {
 72  0
             throw new ReceiveException(endpoint, timeout, e);
 73  
         }
 74  
     }
 75  
 
 76  
     public void dispatch(String url, Object payload, Map<String, Object> messageProperties)
 77  
         throws MuleException
 78  
     {
 79  0
         dispatch(url, new DefaultMuleMessage(payload, messageProperties, muleContext));
 80  0
     }
 81  
 
 82  
     public MuleMessage send(String url, Object payload, Map<String, Object> messageProperties)
 83  
         throws MuleException
 84  
     {
 85  0
         return send(url, new DefaultMuleMessage(payload, messageProperties, muleContext));
 86  
     }
 87  
 
 88  
     public MuleMessage send(String url, MuleMessage message) throws MuleException
 89  
     {
 90  0
         OutboundEndpoint endpoint = getOutboundEndpoint(url, MessageExchangePattern.REQUEST_RESPONSE, null);
 91  0
         return returnMessage(endpoint.process(createMuleEvent(message, endpoint)));
 92  
     }
 93  
 
 94  
     public MuleMessage send(String url, Object payload, Map<String, Object> messageProperties, long timeout)
 95  
         throws MuleException
 96  
     {
 97  0
         return send(url, new DefaultMuleMessage(payload, messageProperties, muleContext), timeout);
 98  
 
 99  
     }
 100  
 
 101  
     public MuleMessage send(String url, MuleMessage message, long timeout) throws MuleException
 102  
     {
 103  0
         OutboundEndpoint endpoint = getOutboundEndpoint(url, MessageExchangePattern.REQUEST_RESPONSE, timeout);
 104  0
         return returnMessage(endpoint.process(createMuleEvent(message, endpoint)));
 105  
     }
 106  
 
 107  
     public void dispatch(String url, MuleMessage message) throws MuleException
 108  
     {
 109  0
         OutboundEndpoint endpoint = getOutboundEndpoint(url, MessageExchangePattern.ONE_WAY, null);
 110  0
         endpoint.process(createMuleEvent(message, endpoint));
 111  0
     }
 112  
 
 113  
     public MuleMessage request(String url, long timeout) throws MuleException
 114  
     {
 115  0
         InboundEndpoint endpoint = getInboundEndpoint(url, MessageExchangePattern.ONE_WAY);
 116  
         try
 117  
         {
 118  0
             return endpoint.request(timeout);
 119  
         }
 120  0
         catch (Exception e)
 121  
         {
 122  0
             throw new ReceiveException(endpoint, timeout, e);
 123  
         }
 124  
     }
 125  
 
 126  
     public MuleMessage process(String uri,
 127  
                                MessageExchangePattern mep,
 128  
                                Object payload,
 129  
                                Map<String, Object> messageProperties) throws MuleException
 130  
     {
 131  0
         return process(uri, mep, new DefaultMuleMessage(payload, messageProperties, muleContext));
 132  
     }
 133  
 
 134  
     public MuleMessage process(String uri, MessageExchangePattern mep, MuleMessage message)
 135  
         throws MuleException
 136  
     {
 137  0
         OutboundEndpoint endpoint = getOutboundEndpoint(uri, mep, null);
 138  0
         return returnMessage(endpoint.process(createMuleEvent(message, endpoint)));
 139  
     }
 140  
 
 141  
     protected MuleEvent createMuleEvent(MuleMessage message, OutboundEndpoint endpoint)
 142  
     {
 143  0
         DefaultMuleSession session = new DefaultMuleSession(new MuleClientFlowConstruct(muleContext), muleContext);
 144  0
         return new DefaultMuleEvent(message, endpoint, session);
 145  
     }
 146  
 
 147  
     protected MuleMessage returnMessage(MuleEvent event)
 148  
     {
 149  0
         if (event != null)
 150  
         {
 151  0
             return event.getMessage();
 152  
         }
 153  
         else
 154  
         {
 155  0
             return null;
 156  
         }
 157  
     }
 158  
 
 159  
     protected OutboundEndpoint getOutboundEndpoint(String uri,
 160  
                                                    MessageExchangePattern mep,
 161  
                                                    Long responseTimeout) throws MuleException
 162  
     {
 163  0
         OutboundEndpoint endpoint = (OutboundEndpoint) outboundEndpointCache.get(uri + ":" + mep.toString()
 164  
                                                                                  + ":" + responseTimeout);
 165  0
         if (endpoint == null)
 166  
         {
 167  0
             EndpointBuilder endpointBuilder = muleContext.getRegistry()
 168  
                 .lookupEndpointFactory()
 169  
                 .getEndpointBuilder(uri);
 170  0
             endpointBuilder.setExchangePattern(mep);
 171  0
             if (responseTimeout != null && responseTimeout > 0)
 172  
             {
 173  0
                 endpointBuilder.setResponseTimeout(responseTimeout.intValue());
 174  
             }
 175  0
             endpoint = muleContext.getEndpointFactory().getOutboundEndpoint(endpointBuilder);
 176  0
             OutboundEndpoint concurrentlyAddedEndpoint = (OutboundEndpoint) outboundEndpointCache.putIfAbsent(
 177  
                 uri + ":" + mep.toString() + ":" + responseTimeout, endpoint);
 178  0
             if (concurrentlyAddedEndpoint != null)
 179  
             {
 180  0
                 return concurrentlyAddedEndpoint;
 181  
             }
 182  
         }
 183  0
         return endpoint;
 184  
     }
 185  
 
 186  
     protected InboundEndpoint getInboundEndpoint(String uri, MessageExchangePattern mep) throws MuleException
 187  
     {
 188  0
         InboundEndpoint endpoint = (InboundEndpoint) inboundEndpointCache.get(uri + ":" + mep.toString());
 189  0
         if (endpoint == null)
 190  
         {
 191  0
             EndpointBuilder endpointBuilder = muleContext.getRegistry()
 192  
                 .lookupEndpointFactory()
 193  
                 .getEndpointBuilder(uri);
 194  0
             endpointBuilder.setExchangePattern(mep);
 195  0
             endpoint = muleContext.getEndpointFactory().getInboundEndpoint(endpointBuilder);
 196  0
             InboundEndpoint concurrentlyAddedEndpoint = (InboundEndpoint) inboundEndpointCache.putIfAbsent(
 197  
                 uri + ":" + mep.toString(), endpoint);
 198  0
             if (concurrentlyAddedEndpoint != null)
 199  
             {
 200  0
                 return concurrentlyAddedEndpoint;
 201  
             }
 202  
         }
 203  0
         return endpoint;
 204  
     }
 205  
 
 206  
     /**
 207  
      * Placeholder class which makes the default exception handler available.
 208  
      */
 209  
     static public class MuleClientFlowConstruct implements FlowConstruct
 210  
     {
 211  
         MuleContext muleContext;
 212  
 
 213  
         public MuleClientFlowConstruct(MuleContext muleContext)
 214  0
         {
 215  0
             this.muleContext = muleContext;
 216  0
         }
 217  
 
 218  
         public String getName()
 219  
         {
 220  0
             return "MuleClient";
 221  
         }
 222  
 
 223  
         public MessagingExceptionHandler getExceptionListener()
 224  
         {
 225  0
             return new DefaultServiceExceptionStrategy(muleContext);
 226  
         }
 227  
 
 228  
         public LifecycleState getLifecycleState()
 229  
         {
 230  0
             return null;
 231  
         }
 232  
 
 233  
         public FlowConstructStatistics getStatistics()
 234  
         {
 235  0
             return null;
 236  
         }
 237  
 
 238  
         public MuleContext getMuleContext()
 239  
         {
 240  0
             return muleContext;
 241  
         }
 242  
 
 243  
         public MessageInfoMapping getMessageInfoMapping()
 244  
         {
 245  0
             return null;
 246  
         }
 247  
 
 248  
         public MessageProcessorChain getMessageProcessorChain()
 249  
         {
 250  0
             return null;
 251  
         }
 252  
     };
 253  
 }