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