001: /*
002: * $Id: DefaultMuleConnection.java 11343 2008-03-13 10:58:26Z tcarlson $
003: * --------------------------------------------------------------------------------------
004: * Copyright (c) MuleSource, Inc. All rights reserved. http://www.mulesource.com
005: *
006: * The software in this package is published under the terms of the CPAL v1.0
007: * license, a copy of which has been included with this distribution in the
008: * LICENSE.txt file.
009: */
010:
011: package org.mule.module.jca;
012:
013: import org.mule.DefaultMuleEvent;
014: import org.mule.DefaultMuleMessage;
015: import org.mule.DefaultMuleSession;
016: import org.mule.api.MuleContext;
017: import org.mule.api.MuleEvent;
018: import org.mule.api.MuleException;
019: import org.mule.api.MuleMessage;
020: import org.mule.api.MuleSession;
021: import org.mule.api.config.MuleProperties;
022: import org.mule.api.endpoint.ImmutableEndpoint;
023: import org.mule.api.endpoint.InboundEndpoint;
024: import org.mule.api.transport.DispatchException;
025: import org.mule.api.transport.ReceiveException;
026: import org.mule.config.i18n.CoreMessages;
027: import org.mule.module.client.i18n.ClientMessages;
028: import org.mule.module.jca.i18n.JcaMessages;
029: import org.mule.security.MuleCredentials;
030: import org.mule.transport.AbstractConnector;
031:
032: import java.util.Map;
033:
034: import javax.resource.ResourceException;
035:
036: /**
037: * <code>MuleConnection</code> TODO
038: */
039: public class DefaultMuleConnection implements MuleConnection {
040: private final MuleCredentials credentials;
041: private final MuleContext muleContext;
042: private MuleManagedConnection managedConnection;
043:
044: public DefaultMuleConnection(
045: MuleManagedConnection managedConnection,
046: MuleContext muleContext, MuleCredentials credentials) {
047: this .muleContext = muleContext;
048: this .credentials = credentials;
049: this .managedConnection = managedConnection;
050: }
051:
052: /**
053: * Dispatches an event asynchronously to a endpointUri via a mule server. the Url
054: * determines where to dispathc the event to, this can be in the form of
055: *
056: * @param url the Mule url used to determine the destination and transport of the
057: * message
058: * @param payload the object that is the payload of the event
059: * @param messageProperties any properties to be associated with the payload. In
060: * the case of Jms you could set the JMSReplyTo property in these
061: * properties.
062: * @throws org.mule.api.MuleException
063: */
064: public void dispatch(String url, Object payload,
065: Map messageProperties) throws MuleException {
066: MuleMessage message = new DefaultMuleMessage(payload,
067: messageProperties);
068: MuleEvent event = getEvent(message, url, false);
069: try {
070: event.getSession().dispatchEvent(event);
071: } catch (MuleException e) {
072: throw e;
073: } catch (Exception e) {
074: throw new DispatchException(ClientMessages
075: .failedToDispatchClientEvent(), event.getMessage(),
076: event.getEndpoint(), e);
077: }
078: }
079:
080: /**
081: * Sends an object (payload) synchronous to the given url and returns a
082: * MuleMessage response back.
083: *
084: * @param url the Mule url used to determine the destination and transport of the
085: * message
086: * @param payload the object that is the payload of the event
087: * @param messageProperties any properties to be associated with the payload. In
088: * the case of Jms you could set the JMSReplyTo property in these
089: * properties.
090: * @return a umomessage response.
091: * @throws org.mule.api.MuleException
092: */
093: public MuleMessage send(String url, Object payload,
094: Map messageProperties) throws MuleException {
095: MuleMessage message = new DefaultMuleMessage(payload,
096: messageProperties);
097: MuleEvent event = getEvent(message, url, true);
098:
099: MuleMessage response;
100: try {
101: response = event.getSession().sendEvent(event);
102: } catch (MuleException e) {
103: throw e;
104: } catch (Exception e) {
105: throw new DispatchException(ClientMessages
106: .failedToDispatchClientEvent(), event.getMessage(),
107: event.getEndpoint(), e);
108: }
109: return response;
110: }
111:
112: /**
113: * Will receive an event from an endpointUri determined by the url
114: *
115: * @param url the Mule url used to determine the destination and transport of the
116: * message
117: * @param timeout how long to block waiting to receive the event, if set to 0 the
118: * receive will not wait at all and if set to -1 the receive will wait
119: * forever
120: * @return the message received or null if no message was received
121: * @throws org.mule.api.MuleException
122: */
123: public MuleMessage request(String url, long timeout)
124: throws MuleException {
125: InboundEndpoint endpoint = muleContext.getRegistry()
126: .lookupEndpointFactory().getInboundEndpoint(url);
127:
128: try {
129: return endpoint.request(timeout);
130: } catch (Exception e) {
131: throw new ReceiveException(endpoint, timeout, e);
132: }
133: }
134:
135: /**
136: * Packages a mule event for the current request
137: *
138: * @param message the event payload
139: * @param uri the destination endpointUri
140: * @param synchronous whether the event will be synchronously processed
141: * @return the MuleEvent
142: * @throws MuleException in case of Mule error
143: */
144: protected MuleEvent getEvent(MuleMessage message, String uri,
145: boolean synchronous) throws MuleException {
146: ImmutableEndpoint endpoint = muleContext.getRegistry()
147: .lookupEndpointFactory().getOutboundEndpoint(uri);
148: //Connector connector = endpoint.getConnector();
149:
150: // if (!connector.isStarted() && manager.isStarted())
151: // {
152: // connector.start();
153: // }
154:
155: try {
156: MuleSession session = new DefaultMuleSession(message,
157: ((AbstractConnector) endpoint.getConnector())
158: .getSessionHandler(), muleContext);
159:
160: if (credentials != null) {
161: message.setProperty(MuleProperties.MULE_USER_PROPERTY,
162: "Plain " + credentials.getToken());
163: }
164:
165: return new DefaultMuleEvent(message, endpoint, session,
166: synchronous);
167: } catch (Exception e) {
168: throw new DispatchException(CoreMessages
169: .failedToCreate("Client event"), message, endpoint,
170: e);
171: }
172: }
173:
174: /**
175: * Retrieves a ManagedConnection.
176: *
177: * @return a ManagedConnection instance representing the physical connection to
178: * the EIS
179: */
180:
181: public MuleManagedConnection getManagedConnection() {
182: return managedConnection;
183: }
184:
185: /**
186: * Closes the connection.
187: */
188: public void close() throws ResourceException {
189: if (managedConnection == null) {
190: return; // connection is already closed
191: }
192: managedConnection.removeConnection(this );
193:
194: // Send a close event to the App Server
195: managedConnection.fireCloseEvent(this );
196: managedConnection = null;
197: }
198:
199: /**
200: * Associates connection handle with new managed connection.
201: *
202: * @param newMc new managed connection
203: */
204:
205: public void associateConnection(MuleManagedConnection newMc)
206: throws ResourceException {
207: checkIfValid();
208: // dissociate handle from current managed connection
209: managedConnection.removeConnection(this );
210: // associate handle with new managed connection
211: newMc.addConnection(this );
212: managedConnection = newMc;
213: }
214:
215: /**
216: * Checks the validity of the physical connection to the EIS.
217: *
218: * @throws javax.resource.ResourceException in case of any error
219: */
220:
221: void checkIfValid() throws ResourceException {
222: if (managedConnection == null) {
223: throw new ResourceException(JcaMessages
224: .objectMarkedInvalid("muleManagedConnection")
225: .toString());
226: }
227: }
228:
229: /**
230: * Sets the physical connection to the EIS as invalid. The physical connection to
231: * the EIS cannot be used any more.
232: */
233:
234: void invalidate() {
235: managedConnection = null;
236: }
237: }
|