View Javadoc

1   /*
2    * $Id: DefaultMuleConnection.java 7976 2007-08-21 14:26:13Z dirk.olmes $
3    * --------------------------------------------------------------------------------------
4    * Copyright (c) MuleSource, Inc.  All rights reserved.  http://www.mulesource.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.ra;
12  
13  import org.mule.config.MuleProperties;
14  import org.mule.config.i18n.CoreMessages;
15  import org.mule.extras.client.i18n.ClientMessages;
16  import org.mule.impl.MuleEvent;
17  import org.mule.impl.MuleMessage;
18  import org.mule.impl.MuleSession;
19  import org.mule.impl.endpoint.MuleEndpoint;
20  import org.mule.impl.endpoint.MuleEndpointURI;
21  import org.mule.impl.security.MuleCredentials;
22  import org.mule.providers.AbstractConnector;
23  import org.mule.ra.i18n.JcaMessages;
24  import org.mule.umo.UMOEvent;
25  import org.mule.umo.UMOException;
26  import org.mule.umo.UMOMessage;
27  import org.mule.umo.UMOSession;
28  import org.mule.umo.endpoint.UMOEndpoint;
29  import org.mule.umo.endpoint.UMOEndpointURI;
30  import org.mule.umo.manager.UMOManager;
31  import org.mule.umo.provider.DispatchException;
32  import org.mule.umo.provider.ReceiveException;
33  import org.mule.umo.provider.UMOConnector;
34  
35  import java.util.Map;
36  
37  import javax.resource.ResourceException;
38  
39  /**
40   * <code>MuleConnection</code> TODO
41   */
42  public class DefaultMuleConnection implements MuleConnection
43  {
44      private final MuleCredentials credentials;
45      private final UMOManager manager;
46      private MuleManagedConnection managedConnection;
47  
48      public DefaultMuleConnection(MuleManagedConnection managedConnection,
49                                   UMOManager manager,
50                                   MuleCredentials credentials)
51      {
52          this.manager = manager;
53          this.credentials = credentials;
54          this.managedConnection = managedConnection;
55      }
56  
57      /**
58       * Dispatches an event asynchronously to a endpointUri via a mule server. the Url
59       * determines where to dispathc the event to, this can be in the form of
60       * 
61       * @param url the Mule url used to determine the destination and transport of the
62       *            message
63       * @param payload the object that is the payload of the event
64       * @param messageProperties any properties to be associated with the payload. In
65       *            the case of Jms you could set the JMSReplyTo property in these
66       *            properties.
67       * @throws org.mule.umo.UMOException
68       */
69      public void dispatch(String url, Object payload, Map messageProperties) throws UMOException
70      {
71          UMOEndpointURI muleEndpoint = new MuleEndpointURI(url);
72          UMOMessage message = new MuleMessage(payload, messageProperties);
73          UMOEvent event = getEvent(message, muleEndpoint, false);
74          try
75          {
76              event.getSession().dispatchEvent(event);
77          }
78          catch (UMOException e)
79          {
80              throw e;
81          }
82          catch (Exception e)
83          {
84              throw new DispatchException(
85                  ClientMessages.failedToDispatchClientEvent(),
86                  event.getMessage(), event.getEndpoint(), e);
87          }
88      }
89  
90      /**
91       * Sends an object (payload) synchronous to the given url and returns a
92       * UMOMessage response back.
93       * 
94       * @param url the Mule url used to determine the destination and transport of the
95       *            message
96       * @param payload the object that is the payload of the event
97       * @param messageProperties any properties to be associated with the payload. In
98       *            the case of Jms you could set the JMSReplyTo property in these
99       *            properties.
100      * @return a umomessage response.
101      * @throws org.mule.umo.UMOException
102      */
103     public UMOMessage send(String url, Object payload, Map messageProperties) throws UMOException
104     {
105         UMOEndpointURI muleEndpoint = new MuleEndpointURI(url);
106         UMOMessage message = new MuleMessage(payload, messageProperties);
107         UMOEvent event = getEvent(message, muleEndpoint, true);
108 
109         UMOMessage response;
110         try
111         {
112             response = event.getSession().sendEvent(event);
113         }
114         catch (UMOException e)
115         {
116             throw e;
117         }
118         catch (Exception e)
119         {
120             throw new DispatchException(
121                 ClientMessages.failedToDispatchClientEvent(), 
122                 event.getMessage(), event.getEndpoint(), e);
123         }
124         return response;
125     }
126 
127     /**
128      * Will receive an event from an endpointUri determined by the url
129      * 
130      * @param url the Mule url used to determine the destination and transport of the
131      *            message
132      * @param timeout how long to block waiting to receive the event, if set to 0 the
133      *            receive will not wait at all and if set to -1 the receive will wait
134      *            forever
135      * @return the message received or null if no message was received
136      * @throws org.mule.umo.UMOException
137      */
138     public UMOMessage receive(String url, long timeout) throws UMOException
139     {
140         MuleEndpointURI muleEndpoint = new MuleEndpointURI(url);
141 
142         UMOEndpoint endpoint = MuleEndpoint.getOrCreateEndpointForUri(muleEndpoint,
143             UMOEndpoint.ENDPOINT_TYPE_SENDER);
144 
145         try
146         {
147             return endpoint.receive(timeout);
148         }
149         catch (Exception e)
150         {
151             throw new ReceiveException(endpoint, timeout, e);
152         }
153     }
154 
155     /**
156      * Packages a mule event for the current request
157      * 
158      * @param message the event payload
159      * @param uri the destination endpointUri
160      * @param synchronous whether the event will be synchronously processed
161      * @return the UMOEvent
162      * @throws UMOException in case of Mule error
163      */
164     protected UMOEvent getEvent(UMOMessage message, UMOEndpointURI uri, boolean synchronous)
165         throws UMOException
166     {
167         UMOEndpoint endpoint = MuleEndpoint.getOrCreateEndpointForUri(uri, UMOEndpoint.ENDPOINT_TYPE_SENDER);
168         UMOConnector connector = endpoint.getConnector();
169 
170         if (!connector.isStarted() && manager.isStarted())
171         {
172             connector.startConnector();
173         }
174 
175         try
176         {
177             UMOSession session = new MuleSession(message,
178                 ((AbstractConnector)endpoint.getConnector()).getSessionHandler());
179 
180             if (credentials != null)
181             {
182                 message.setProperty(MuleProperties.MULE_USER_PROPERTY, "Plain " + credentials.getToken());
183             }
184 
185             return new MuleEvent(message, endpoint, session, synchronous);
186         }
187         catch (Exception e)
188         {
189             throw new DispatchException(
190                 CoreMessages.failedToCreate("Client event"), message, endpoint, e);
191         }
192     }
193 
194     /**
195      * Retrieves a ManagedConnection.
196      * 
197      * @return a ManagedConnection instance representing the physical connection to
198      *         the EIS
199      */
200 
201     public MuleManagedConnection getManagedConnection()
202     {
203         return managedConnection;
204     }
205 
206     /**
207      * Closes the connection.
208      */
209     public void close() throws ResourceException
210     {
211         if (managedConnection == null)
212         {
213             return; // connection is already closed
214         }
215         managedConnection.removeConnection(this);
216 
217         // Send a close event to the App Server
218         managedConnection.fireCloseEvent(this);
219         managedConnection = null;
220     }
221 
222     /**
223      * Associates connection handle with new managed connection.
224      * 
225      * @param newMc new managed connection
226      */
227 
228     public void associateConnection(MuleManagedConnection newMc) throws ResourceException
229     {
230         checkIfValid();
231         // dissociate handle from current managed connection
232         managedConnection.removeConnection(this);
233         // associate handle with new managed connection
234         newMc.addConnection(this);
235         managedConnection = newMc;
236     }
237 
238     /**
239      * Checks the validity of the physical connection to the EIS.
240      * 
241      * @throws javax.resource.ResourceException in case of any error
242      */
243 
244     void checkIfValid() throws ResourceException
245     {
246         if (managedConnection == null)
247         {
248             throw new ResourceException(
249                 JcaMessages.objectMarkedInvalid("muleManagedConnection").toString());
250         }
251     }
252 
253     /**
254      * Sets the physical connection to the EIS as invalid. The physical connection to
255      * the EIS cannot be used any more.
256      */
257 
258     void invalidate()
259     {
260         managedConnection = null;
261     }
262 }