View Javadoc

1   /*
2    * $Id: DefaultMuleConnection.java 22162 2011-06-09 18:23:00Z 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.module.jca;
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.MuleSession;
21  import org.mule.api.config.MuleProperties;
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.transport.DispatchException;
26  import org.mule.api.transport.ReceiveException;
27  import org.mule.module.client.i18n.ClientMessages;
28  import org.mule.module.jca.i18n.JcaMessages;
29  import org.mule.security.MuleCredentials;
30  import org.mule.session.DefaultMuleSession;
31  import org.mule.transport.AbstractConnector;
32  
33  import java.util.Map;
34  
35  import javax.resource.ResourceException;
36  
37  /**
38   * <code>MuleConnection</code> TODO
39   */
40  public class DefaultMuleConnection implements MuleConnection
41  {
42      private final MuleCredentials credentials;
43      private final MuleContext muleContext;
44      private MuleManagedConnection managedConnection;
45  
46      public DefaultMuleConnection(MuleManagedConnection managedConnection,
47                                   MuleContext muleContext,
48                                   MuleCredentials credentials)
49      {
50          this.muleContext = muleContext;
51          this.credentials = credentials;
52          this.managedConnection = managedConnection;
53      }
54  
55      /**
56       * Dispatches an event asynchronously to a endpointUri via a mule server. the Url
57       * determines where to dispathc the event to, this can be in the form of
58       *
59       * @param url the Mule url used to determine the destination and transport of the
60       *            message
61       * @param payload the object that is the payload of the event
62       * @param messageProperties any properties to be associated with the payload. In
63       *            the case of Jms you could set the JMSReplyTo property in these
64       *            properties.
65       * @throws org.mule.api.MuleException
66       */
67      public void dispatch(String url, Object payload, Map messageProperties) throws MuleException
68      {
69          MuleMessage message = new DefaultMuleMessage(payload, messageProperties, muleContext);
70          OutboundEndpoint endpoint = muleContext.getEndpointFactory().getOutboundEndpoint(url);
71          MuleEvent event = getEvent(message,endpoint);
72          try
73          {
74              endpoint.process(event);
75          }
76          catch (MuleException e)
77          {
78              throw e;
79          }
80          catch (Exception e)
81          {
82              throw new DispatchException(ClientMessages.failedToDispatchClientEvent(), event,
83                  endpoint, e);
84          }
85      }
86  
87      /**
88       * Sends an object (payload) synchronous to the given url and returns a
89       * MuleMessage response back.
90       *
91       * @param url the Mule url used to determine the destination and transport of the
92       *            message
93       * @param payload the object that is the payload of the event
94       * @param messageProperties any properties to be associated with the payload. In
95       *            the case of Jms you could set the JMSReplyTo property in these
96       *            properties.
97       * @return a response.
98       * @throws org.mule.api.MuleException
99       */
100     public MuleMessage send(String url, Object payload, Map messageProperties) throws MuleException
101     {
102         MuleMessage message = new DefaultMuleMessage(payload, messageProperties, muleContext);
103         OutboundEndpoint endpoint = getOutboundEndpoint(url, MessageExchangePattern.REQUEST_RESPONSE);
104         MuleEvent event = getEvent(message, endpoint);
105 
106         try
107         {
108             MuleEvent resultEvent = endpoint.process(event);
109             if (resultEvent != null)
110             {
111                 return resultEvent.getMessage();
112             }
113             else
114             {
115                 return null;
116             }
117         }
118         catch (MuleException e)
119         {
120             throw e;
121         }
122         catch (Exception e)
123         {
124             throw new DispatchException(ClientMessages.failedToDispatchClientEvent(), event,
125                 endpoint, e);
126         }
127     }
128 
129     /**
130      * Will receive an event from an endpointUri determined by the url
131      *
132      * @param url the Mule url used to determine the destination and transport of the
133      *            message
134      * @param timeout how long to block waiting to receive the event, if set to 0 the
135      *            receive will not wait at all and if set to -1 the receive will wait
136      *            forever
137      * @return the message received or null if no message was received
138      * @throws org.mule.api.MuleException
139      */
140     public MuleMessage request(String url, long timeout) throws MuleException
141     {
142         InboundEndpoint endpoint = muleContext.getEndpointFactory().getInboundEndpoint(url);
143 
144         try
145         {
146             return endpoint.request(timeout);
147         }
148         catch (Exception e)
149         {
150             throw new ReceiveException(endpoint, timeout, e);
151         }
152     }
153 
154     protected OutboundEndpoint getOutboundEndpoint(String uri, MessageExchangePattern exchangePattern)
155         throws MuleException
156     {
157         EndpointBuilder endpointBuilder = muleContext.getEndpointFactory().getEndpointBuilder(uri);
158         endpointBuilder.setExchangePattern(exchangePattern);
159         return muleContext.getEndpointFactory().getOutboundEndpoint(endpointBuilder);
160     }
161 
162     /**
163      * Packages a mule event for the current request
164      */
165     protected MuleEvent getEvent(MuleMessage message, OutboundEndpoint endpoint)
166         throws MuleException
167     {
168         MuleSession session = new DefaultMuleSession(message, ((AbstractConnector)endpoint.getConnector()).getSessionHandler(), muleContext);
169 
170         if (credentials != null)
171         {
172             message.setOutboundProperty(MuleProperties.MULE_USER_PROPERTY, "Plain " + credentials.getToken());
173         }
174 
175         return new DefaultMuleEvent(message, endpoint.getExchangePattern(), session);
176     }
177 
178     /**
179      * Retrieves a ManagedConnection.
180      *
181      * @return a ManagedConnection instance representing the physical connection to
182      *         the EIS
183      */
184 
185     public MuleManagedConnection getManagedConnection()
186     {
187         return managedConnection;
188     }
189 
190     /**
191      * Closes the connection.
192      */
193     public void close() throws ResourceException
194     {
195         if (managedConnection == null)
196         {
197             return; // connection is already closed
198         }
199         managedConnection.removeConnection(this);
200 
201         // Send a close event to the App Server
202         managedConnection.fireCloseEvent(this);
203         managedConnection = null;
204     }
205 
206     /**
207      * Associates connection handle with new managed connection.
208      *
209      * @param newMc new managed connection
210      */
211 
212     public void associateConnection(MuleManagedConnection newMc) throws ResourceException
213     {
214         checkIfValid();
215         // dissociate handle from current managed connection
216         managedConnection.removeConnection(this);
217         // associate handle with new managed connection
218         newMc.addConnection(this);
219         managedConnection = newMc;
220     }
221 
222     /**
223      * Checks the validity of the physical connection to the EIS.
224      *
225      * @throws javax.resource.ResourceException in case of any error
226      */
227 
228     void checkIfValid() throws ResourceException
229     {
230         if (managedConnection == null)
231         {
232             throw new ResourceException(
233                 JcaMessages.objectMarkedInvalid("muleManagedConnection").toString());
234         }
235     }
236 
237     /**
238      * Sets the physical connection to the EIS as invalid. The physical connection to
239      * the EIS cannot be used any more.
240      */
241 
242     void invalidate()
243     {
244         managedConnection = null;
245     }
246 }