View Javadoc

1   /*
2    * $Id: DefaultLocalMuleClient.java 22772 2011-08-27 15:20:15Z 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.EndpointCache;
23  import org.mule.api.endpoint.EndpointException;
24  import org.mule.api.endpoint.InboundEndpoint;
25  import org.mule.api.endpoint.OutboundEndpoint;
26  import org.mule.api.exception.MessagingExceptionHandler;
27  import org.mule.api.lifecycle.LifecycleState;
28  import org.mule.api.processor.MessageProcessorChain;
29  import org.mule.api.routing.MessageInfoMapping;
30  import org.mule.api.transport.ReceiveException;
31  import org.mule.endpoint.SimpleEndpointCache;
32  import org.mule.exception.DefaultMessagingExceptionStrategy;
33  import org.mule.management.stats.FlowConstructStatistics;
34  import org.mule.session.DefaultMuleSession;
35  
36  import java.util.Map;
37  
38  public class DefaultLocalMuleClient implements LocalMuleClient
39  {
40      protected final MuleContext muleContext;
41      private final EndpointCache endpointCache;
42  
43      public DefaultLocalMuleClient(MuleContext muleContext)
44      {
45          this.muleContext = muleContext;
46          this.endpointCache = new SimpleEndpointCache(muleContext);
47      }
48  
49      public MuleMessage process(OutboundEndpoint endpoint,
50                                 Object payload,
51                                 Map<String, Object> messageProperties) throws MuleException
52      {
53          return process(endpoint, new DefaultMuleMessage(payload, messageProperties, muleContext));
54  
55      }
56  
57      public MuleMessage process(OutboundEndpoint endpoint, MuleMessage message) throws MuleException
58      {
59          return returnMessage(endpoint.process(createMuleEvent(message, endpoint)));
60      }
61  
62      public MuleMessage request(InboundEndpoint endpoint, long timeout) throws MuleException
63      {
64          try
65          {
66              return endpoint.request(timeout);
67          }
68          catch (Exception e)
69          {
70              throw new ReceiveException(endpoint, timeout, e);
71          }
72      }
73  
74      public void dispatch(String url, Object payload, Map<String, Object> messageProperties)
75          throws MuleException
76      {
77          dispatch(url, new DefaultMuleMessage(payload, messageProperties, muleContext));
78      }
79  
80      public MuleMessage send(String url, Object payload, Map<String, Object> messageProperties)
81          throws MuleException
82      {
83          return send(url, new DefaultMuleMessage(payload, messageProperties, muleContext));
84      }
85  
86      public MuleMessage send(String url, MuleMessage message) throws MuleException
87      {
88          OutboundEndpoint endpoint = endpointCache.getOutboundEndpoint(url, MessageExchangePattern.REQUEST_RESPONSE, null);
89          return returnMessage(endpoint.process(createMuleEvent(message, endpoint)));
90      }
91  
92      public MuleMessage send(String url, Object payload, Map<String, Object> messageProperties, long timeout)
93          throws MuleException
94      {
95          return send(url, new DefaultMuleMessage(payload, messageProperties, muleContext), timeout);
96  
97      }
98  
99      public MuleMessage send(String url, MuleMessage message, long timeout) throws MuleException
100     {
101         OutboundEndpoint endpoint = endpointCache.getOutboundEndpoint(url, MessageExchangePattern.REQUEST_RESPONSE, timeout);
102         return returnMessage(endpoint.process(createMuleEvent(message, endpoint)));
103     }
104 
105     public void dispatch(String url, MuleMessage message) throws MuleException
106     {
107         OutboundEndpoint endpoint = endpointCache.getOutboundEndpoint(url, MessageExchangePattern.ONE_WAY, null);
108         endpoint.process(createMuleEvent(message, endpoint));
109     }
110 
111     public MuleMessage request(String url, long timeout) throws MuleException
112     {
113         InboundEndpoint endpoint = endpointCache.getInboundEndpoint(url, MessageExchangePattern.ONE_WAY);
114         try
115         {
116             return endpoint.request(timeout);
117         }
118         catch (Exception e)
119         {
120             throw new ReceiveException(endpoint, timeout, e);
121         }
122     }
123 
124     public MuleMessage process(String uri,
125                                MessageExchangePattern mep,
126                                Object payload,
127                                Map<String, Object> messageProperties) throws MuleException
128     {
129         return process(uri, mep, new DefaultMuleMessage(payload, messageProperties, muleContext));
130     }
131 
132     public MuleMessage process(String uri, MessageExchangePattern mep, MuleMessage message)
133         throws MuleException
134     {
135         OutboundEndpoint endpoint = endpointCache.getOutboundEndpoint(uri, mep, null);
136         return returnMessage(endpoint.process(createMuleEvent(message, endpoint)));
137     }
138 
139     protected MuleEvent createMuleEvent(MuleMessage message, OutboundEndpoint endpoint) throws EndpointException
140     {
141         DefaultMuleSession session = new DefaultMuleSession(new MuleClientFlowConstruct(muleContext), muleContext);
142         return new DefaultMuleEvent(message, endpoint.getExchangePattern(), session);
143     }
144 
145     protected MuleMessage returnMessage(MuleEvent event)
146     {
147         if (event != null)
148         {
149             return event.getMessage();
150         }
151         else
152         {
153             return null;
154         }
155     }
156 
157     /**
158      * Placeholder class which makes the default exception handler available.
159      */
160     static public class MuleClientFlowConstruct implements FlowConstruct
161     {
162         MuleContext muleContext;
163 
164         public MuleClientFlowConstruct(MuleContext muleContext)
165         {
166             this.muleContext = muleContext;
167         }
168 
169         public String getName()
170         {
171             return "MuleClient";
172         }
173 
174         public MessagingExceptionHandler getExceptionListener()
175         {
176             return new DefaultMessagingExceptionStrategy(muleContext);
177         }
178 
179         public LifecycleState getLifecycleState()
180         {
181             return null;
182         }
183 
184         public FlowConstructStatistics getStatistics()
185         {
186             return null;
187         }
188 
189         public MuleContext getMuleContext()
190         {
191             return muleContext;
192         }
193 
194         public MessageInfoMapping getMessageInfoMapping()
195         {
196             return null;
197         }
198 
199         public MessageProcessorChain getMessageProcessorChain()
200         {
201             return null;
202         }
203     }
204 }