001: /*
002: * $Id: DefaultReplyToHandler.java 11373 2008-03-15 05:03:10Z dfeist $
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.transport;
012:
013: import org.mule.DefaultMuleEvent;
014: import org.mule.RegistryContext;
015: import org.mule.api.MuleEvent;
016: import org.mule.api.MuleException;
017: import org.mule.api.MuleMessage;
018: import org.mule.api.config.MuleProperties;
019: import org.mule.api.endpoint.EndpointBuilder;
020: import org.mule.api.endpoint.EndpointFactory;
021: import org.mule.api.endpoint.OutboundEndpoint;
022: import org.mule.api.transport.DispatchException;
023: import org.mule.api.transport.ReplyToHandler;
024: import org.mule.config.i18n.CoreMessages;
025:
026: import java.util.HashMap;
027: import java.util.List;
028: import java.util.Map;
029:
030: import org.apache.commons.logging.Log;
031: import org.apache.commons.logging.LogFactory;
032:
033: /**
034: * <code>DefaultReplyToHandler</code> is responsible for processing a message
035: * replyTo header.
036: */
037:
038: public class DefaultReplyToHandler implements ReplyToHandler {
039: /**
040: * logger used by this class
041: */
042: protected static final Log logger = LogFactory
043: .getLog(DefaultReplyToHandler.class);
044:
045: private volatile List transformers;
046: private final Map endpointCache = new HashMap();
047:
048: public DefaultReplyToHandler(List transformers) {
049: this .transformers = transformers;
050: }
051:
052: public void processReplyTo(MuleEvent event,
053: MuleMessage returnMessage, Object replyTo)
054: throws MuleException {
055: if (logger.isDebugEnabled()) {
056: logger.debug("sending reply to: "
057: + returnMessage.getReplyTo());
058: }
059:
060: String replyToEndpoint = replyTo.toString();
061:
062: // get the endpoint for this url
063: OutboundEndpoint endpoint = getEndpoint(event, replyToEndpoint);
064:
065: // make sure remove the replyTo property as not cause a a forever
066: // replyto loop
067: returnMessage
068: .removeProperty(MuleProperties.MULE_REPLY_TO_PROPERTY);
069:
070: // Create the replyTo event asynchronous
071: MuleEvent replyToEvent = new DefaultMuleEvent(returnMessage,
072: endpoint, event.getSession(), false);
073:
074: // dispatch the event
075: try {
076: endpoint.dispatch(replyToEvent);
077: if (logger.isInfoEnabled()) {
078: logger.info("reply to sent: " + endpoint);
079: }
080: event.getService().getComponent().getStatistics()
081: .incSentReplyToEvent();
082: } catch (Exception e) {
083: throw new DispatchException(CoreMessages
084: .failedToDispatchToReplyto(endpoint), replyToEvent
085: .getMessage(), replyToEvent.getEndpoint(), e);
086: }
087:
088: }
089:
090: protected synchronized OutboundEndpoint getEndpoint(
091: MuleEvent event, String endpointUri) throws MuleException {
092: OutboundEndpoint endpoint = (OutboundEndpoint) endpointCache
093: .get(endpointUri);
094: if (endpoint == null) {
095: EndpointFactory endpointFactory = RegistryContext
096: .getRegistry().lookupEndpointFactory();
097: EndpointBuilder endpointBuilder = endpointFactory
098: .getEndpointBuilder(endpointUri);
099: if (transformers == null) {
100: endpointBuilder.setTransformers(event.getEndpoint()
101: .getResponseTransformers());
102: }
103: endpoint = endpointFactory
104: .getOutboundEndpoint(endpointBuilder);
105: endpointCache.put(endpointUri, endpoint);
106: }
107: return endpoint;
108: }
109:
110: public List getTransformers() {
111: return transformers;
112: }
113:
114: public void setTransformers(List transformers) {
115: this.transformers = transformers;
116: }
117:
118: }
|