View Javadoc

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      private ConcurrentMap inboundEndpointCache = new ConcurrentHashMap();
44      private ConcurrentMap outboundEndpointCache = new ConcurrentHashMap();
45  
46      public DefaultLocalMuleClient(MuleContext muleContext)
47      {
48          this.muleContext = muleContext;
49      }
50  
51      public MuleMessage process(OutboundEndpoint endpoint,
52                                 Object payload,
53                                 Map<String, Object> messageProperties) throws MuleException
54      {
55          return process(endpoint, new DefaultMuleMessage(payload, messageProperties, muleContext));
56  
57      }
58  
59      public MuleMessage process(OutboundEndpoint endpoint, MuleMessage message) throws MuleException
60      {
61          return returnMessage(endpoint.process(createMuleEvent(message, endpoint)));
62      }
63  
64      public MuleMessage request(InboundEndpoint endpoint, long timeout) throws MuleException
65      {
66          try
67          {
68              return endpoint.request(timeout);
69          }
70          catch (Exception e)
71          {
72              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          dispatch(url, new DefaultMuleMessage(payload, messageProperties, muleContext));
80      }
81  
82      public MuleMessage send(String url, Object payload, Map<String, Object> messageProperties)
83          throws MuleException
84      {
85          return send(url, new DefaultMuleMessage(payload, messageProperties, muleContext));
86      }
87  
88      public MuleMessage send(String url, MuleMessage message) throws MuleException
89      {
90          OutboundEndpoint endpoint = getOutboundEndpoint(url, MessageExchangePattern.REQUEST_RESPONSE, null);
91          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          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         OutboundEndpoint endpoint = getOutboundEndpoint(url, MessageExchangePattern.REQUEST_RESPONSE, timeout);
104         return returnMessage(endpoint.process(createMuleEvent(message, endpoint)));
105     }
106 
107     public void dispatch(String url, MuleMessage message) throws MuleException
108     {
109         OutboundEndpoint endpoint = getOutboundEndpoint(url, MessageExchangePattern.ONE_WAY, null);
110         endpoint.process(createMuleEvent(message, endpoint));
111     }
112 
113     public MuleMessage request(String url, long timeout) throws MuleException
114     {
115         InboundEndpoint endpoint = getInboundEndpoint(url, MessageExchangePattern.ONE_WAY);
116         try
117         {
118             return endpoint.request(timeout);
119         }
120         catch (Exception e)
121         {
122             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         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         OutboundEndpoint endpoint = getOutboundEndpoint(uri, mep, null);
138         return returnMessage(endpoint.process(createMuleEvent(message, endpoint)));
139     }
140 
141     protected MuleEvent createMuleEvent(MuleMessage message, OutboundEndpoint endpoint)
142     {
143         DefaultMuleSession session = new DefaultMuleSession(new MuleClientFlowConstruct(muleContext), muleContext);
144         return new DefaultMuleEvent(message, endpoint, session);
145     }
146 
147     protected MuleMessage returnMessage(MuleEvent event)
148     {
149         if (event != null)
150         {
151             return event.getMessage();
152         }
153         else
154         {
155             return null;
156         }
157     }
158 
159     protected OutboundEndpoint getOutboundEndpoint(String uri,
160                                                    MessageExchangePattern mep,
161                                                    Long responseTimeout) throws MuleException
162     {
163         OutboundEndpoint endpoint = (OutboundEndpoint) outboundEndpointCache.get(uri + ":" + mep.toString()
164                                                                                  + ":" + responseTimeout);
165         if (endpoint == null)
166         {
167             EndpointBuilder endpointBuilder = muleContext.getRegistry()
168                 .lookupEndpointFactory()
169                 .getEndpointBuilder(uri);
170             endpointBuilder.setExchangePattern(mep);
171             if (responseTimeout != null && responseTimeout > 0)
172             {
173                 endpointBuilder.setResponseTimeout(responseTimeout.intValue());
174             }
175             endpoint = muleContext.getEndpointFactory().getOutboundEndpoint(endpointBuilder);
176             OutboundEndpoint concurrentlyAddedEndpoint = (OutboundEndpoint) outboundEndpointCache.putIfAbsent(
177                 uri + ":" + mep.toString() + ":" + responseTimeout, endpoint);
178             if (concurrentlyAddedEndpoint != null)
179             {
180                 return concurrentlyAddedEndpoint;
181             }
182         }
183         return endpoint;
184     }
185 
186     protected InboundEndpoint getInboundEndpoint(String uri, MessageExchangePattern mep) throws MuleException
187     {
188         InboundEndpoint endpoint = (InboundEndpoint) inboundEndpointCache.get(uri + ":" + mep.toString());
189         if (endpoint == null)
190         {
191             EndpointBuilder endpointBuilder = muleContext.getRegistry()
192                 .lookupEndpointFactory()
193                 .getEndpointBuilder(uri);
194             endpointBuilder.setExchangePattern(mep);
195             endpoint = muleContext.getEndpointFactory().getInboundEndpoint(endpointBuilder);
196             InboundEndpoint concurrentlyAddedEndpoint = (InboundEndpoint) inboundEndpointCache.putIfAbsent(
197                 uri + ":" + mep.toString(), endpoint);
198             if (concurrentlyAddedEndpoint != null)
199             {
200                 return concurrentlyAddedEndpoint;
201             }
202         }
203         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         {
215             this.muleContext = muleContext;
216         }
217 
218         public String getName()
219         {
220             return "MuleClient";
221         }
222 
223         public MessagingExceptionHandler getExceptionListener()
224         {
225             return new DefaultServiceExceptionStrategy(muleContext);
226         }
227 
228         public LifecycleState getLifecycleState()
229         {
230             return null;
231         }
232 
233         public FlowConstructStatistics getStatistics()
234         {
235             return null;
236         }
237 
238         public MuleContext getMuleContext()
239         {
240             return muleContext;
241         }
242 
243         public MessageInfoMapping getMessageInfoMapping()
244         {
245             return null;
246         }
247 
248         public MessageProcessorChain getMessageProcessorChain()
249         {
250             return null;
251         }
252     };
253 }