KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > mule > ra > DefaultMuleConnection


1 /*
2  * $Id: DefaultMuleConnection.java 3798 2006-11-04 04:07:14Z aperepel $
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 MuleSource MPL
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 java.util.Map JavaDoc;
14
15 import javax.resource.ResourceException JavaDoc;
16
17 import org.mule.config.MuleProperties;
18 import org.mule.config.i18n.Message;
19 import org.mule.config.i18n.Messages;
20 import org.mule.impl.MuleEvent;
21 import org.mule.impl.MuleMessage;
22 import org.mule.impl.MuleSession;
23 import org.mule.impl.endpoint.MuleEndpoint;
24 import org.mule.impl.endpoint.MuleEndpointURI;
25 import org.mule.impl.security.MuleCredentials;
26 import org.mule.providers.AbstractConnector;
27 import org.mule.umo.UMOEvent;
28 import org.mule.umo.UMOException;
29 import org.mule.umo.UMOMessage;
30 import org.mule.umo.UMOSession;
31 import org.mule.umo.endpoint.UMOEndpoint;
32 import org.mule.umo.endpoint.UMOEndpointURI;
33 import org.mule.umo.manager.UMOManager;
34 import org.mule.umo.provider.DispatchException;
35 import org.mule.umo.provider.ReceiveException;
36 import org.mule.umo.provider.UMOConnector;
37
38 /**
39  * <code>MuleConnection</code> TODO
40  */

41 public class DefaultMuleConnection implements MuleConnection
42 {
43     private final MuleCredentials credentials;
44     private final UMOManager manager;
45     private MuleManagedConnection managedConnection;
46
47     public DefaultMuleConnection(MuleManagedConnection managedConnection,
48                                  UMOManager manager,
49                                  MuleCredentials credentials)
50     {
51         this.manager = manager;
52         this.credentials = credentials;
53         this.managedConnection = managedConnection;
54     }
55
56     /**
57      * Dispatches an event asynchronously to a endpointUri via a mule server. the Url
58      * determines where to dispathc the event to, this can be in the form of
59      *
60      * @param url the Mule url used to determine the destination and transport of the
61      * message
62      * @param payload the object that is the payload of the event
63      * @param messageProperties any properties to be associated with the payload. In
64      * the case of Jms you could set the JMSReplyTo property in these
65      * properties.
66      * @throws org.mule.umo.UMOException
67      */

68     public void dispatch(String JavaDoc url, Object JavaDoc payload, Map JavaDoc messageProperties) throws UMOException
69     {
70         UMOEndpointURI muleEndpoint = new MuleEndpointURI(url);
71         UMOMessage message = new MuleMessage(payload, messageProperties);
72         UMOEvent event = getEvent(message, muleEndpoint, false);
73         try
74         {
75             event.getSession().dispatchEvent(event);
76         }
77         catch (UMOException e)
78         {
79             throw e;
80         }
81         catch (Exception JavaDoc e)
82         {
83             throw new DispatchException(new Message("client", 1), event.getMessage(), event.getEndpoint(), e);
84         }
85     }
86
87     /**
88      * Sends an object (payload) synchronous to the given url and returns a
89      * UMOMessage 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 umomessage response.
98      * @throws org.mule.umo.UMOException
99      */

100     public UMOMessage send(String JavaDoc url, Object JavaDoc payload, Map JavaDoc messageProperties) throws UMOException
101     {
102         UMOEndpointURI muleEndpoint = new MuleEndpointURI(url);
103         UMOMessage message = new MuleMessage(payload, messageProperties);
104         UMOEvent event = getEvent(message, muleEndpoint, true);
105
106         UMOMessage response;
107         try
108         {
109             response = event.getSession().sendEvent(event);
110         }
111         catch (UMOException e)
112         {
113             throw e;
114         }
115         catch (Exception JavaDoc e)
116         {
117             throw new DispatchException(new Message("client", 1), event.getMessage(), event.getEndpoint(), e);
118         }
119         return response;
120     }
121
122     /**
123      * Will receive an event from an endpointUri determined by the url
124      *
125      * @param url the Mule url used to determine the destination and transport of the
126      * message
127      * @param timeout how long to block waiting to receive the event, if set to 0 the
128      * receive will not wait at all and if set to -1 the receive will wait
129      * forever
130      * @return the message received or null if no message was received
131      * @throws org.mule.umo.UMOException
132      */

133     public UMOMessage receive(String JavaDoc url, long timeout) throws UMOException
134     {
135         MuleEndpointURI muleEndpoint = new MuleEndpointURI(url);
136
137         UMOEndpoint endpoint = MuleEndpoint.getOrCreateEndpointForUri(muleEndpoint,
138             UMOEndpoint.ENDPOINT_TYPE_SENDER);
139
140         try
141         {
142             return endpoint.getConnector().getDispatcher(endpoint).receive(endpoint, timeout);
143         }
144         catch (Exception JavaDoc e)
145         {
146             throw new ReceiveException(endpoint, timeout, e);
147         }
148     }
149
150     /**
151      * Packages a mule event for the current request
152      *
153      * @param message the event payload
154      * @param uri the destination endpointUri
155      * @param synchronous whether the event will be synchronously processed
156      * @return the UMOEvent
157      * @throws UMOException in case of Mule error
158      */

159     protected UMOEvent getEvent(UMOMessage message, UMOEndpointURI uri, boolean synchronous)
160         throws UMOException
161     {
162         UMOEndpoint endpoint = MuleEndpoint.getOrCreateEndpointForUri(uri, UMOEndpoint.ENDPOINT_TYPE_SENDER);
163         UMOConnector connector = endpoint.getConnector();
164
165         if (!connector.isStarted() && manager.isStarted())
166         {
167             connector.startConnector();
168         }
169
170         try
171         {
172             UMOSession session = new MuleSession(message,
173                 ((AbstractConnector)endpoint.getConnector()).getSessionHandler());
174
175             if (credentials != null)
176             {
177                 message.setProperty(MuleProperties.MULE_USER_PROPERTY, "Plain " + credentials.getToken());
178             }
179
180             return new MuleEvent(message, endpoint, session, synchronous);
181         }
182         catch (Exception JavaDoc e)
183         {
184             throw new DispatchException(new Message(Messages.FAILED_TO_CREATE_X, "Client event"), message,
185                 endpoint, e);
186         }
187     }
188
189     /**
190      * Retrieves a ManagedConnection.
191      *
192      * @return a ManagedConnection instance representing the physical connection to
193      * the EIS
194      */

195
196     public MuleManagedConnection getManagedConnection()
197     {
198         return managedConnection;
199     }
200
201     /**
202      * Closes the connection.
203      */

204     public void close() throws ResourceException JavaDoc
205     {
206         if (managedConnection == null)
207         {
208             return; // connection is already closed
209
}
210         managedConnection.removeConnection(this);
211
212         // Send a close event to the App Server
213
managedConnection.fireCloseEvent(this);
214         managedConnection = null;
215     }
216
217     /**
218      * Associates connection handle with new managed connection.
219      *
220      * @param newMc new managed connection
221      */

222
223     public void associateConnection(MuleManagedConnection newMc) throws ResourceException JavaDoc
224     {
225         checkIfValid();
226         // dissociate handle from current managed connection
227
managedConnection.removeConnection(this);
228         // associate handle with new managed connection
229
newMc.addConnection(this);
230         managedConnection = newMc;
231     }
232
233     /**
234      * Checks the validity of the physical connection to the EIS.
235      *
236      * @throws javax.resource.ResourceException in case of any error
237      */

238
239     void checkIfValid() throws ResourceException JavaDoc
240     {
241         if (managedConnection == null)
242         {
243             throw new ResourceException JavaDoc(
244                 new Message(Messages.OBJECT_X_MARKED_INVALID, "muleManagedConnection").toString());
245         }
246     }
247
248     /**
249      * Sets the physical connection to the EIS as invalid. The physical connection to
250      * the EIS cannot be used any more.
251      */

252
253     void invalidate()
254     {
255         managedConnection = null;
256     }
257 }
258
Popular Tags