View Javadoc

1   /*
2    * $Id: DefaultReplyToHandler.java 11488 2008-03-24 14:38:49Z dfeist $
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.transport;
12  
13  import org.mule.DefaultMuleEvent;
14  import org.mule.RegistryContext;
15  import org.mule.api.MuleEvent;
16  import org.mule.api.MuleException;
17  import org.mule.api.MuleMessage;
18  import org.mule.api.config.MuleProperties;
19  import org.mule.api.endpoint.EndpointBuilder;
20  import org.mule.api.endpoint.EndpointFactory;
21  import org.mule.api.endpoint.OutboundEndpoint;
22  import org.mule.api.transport.DispatchException;
23  import org.mule.api.transport.ReplyToHandler;
24  import org.mule.config.i18n.CoreMessages;
25  import org.mule.service.AbstractService;
26  
27  import java.util.HashMap;
28  import java.util.List;
29  import java.util.Map;
30  
31  import org.apache.commons.logging.Log;
32  import org.apache.commons.logging.LogFactory;
33  
34  /**
35   * <code>DefaultReplyToHandler</code> is responsible for processing a message
36   * replyTo header.
37   */
38  
39  public class DefaultReplyToHandler implements ReplyToHandler
40  {
41      /**
42       * logger used by this class
43       */
44      protected static final Log logger = LogFactory.getLog(DefaultReplyToHandler.class);
45  
46      private volatile List transformers;
47      private final Map endpointCache = new HashMap();
48  
49      public DefaultReplyToHandler(List transformers)
50      {
51          this.transformers = transformers;
52      }
53  
54      public void processReplyTo(MuleEvent event, MuleMessage returnMessage, Object replyTo) throws MuleException
55      {
56          if (logger.isDebugEnabled())
57          {
58              logger.debug("sending reply to: " + returnMessage.getReplyTo());
59          }
60  
61          String replyToEndpoint = replyTo.toString();
62  
63          // get the endpoint for this url
64          OutboundEndpoint endpoint = getEndpoint(event, replyToEndpoint);
65  
66          // make sure remove the replyTo property as not cause a a forever
67          // replyto loop
68          returnMessage.removeProperty(MuleProperties.MULE_REPLY_TO_PROPERTY);
69  
70          // Create the replyTo event asynchronous
71          MuleEvent replyToEvent = new DefaultMuleEvent(returnMessage, endpoint, event.getSession(), false);
72  
73          // dispatch the event
74          try
75          {
76              endpoint.dispatch(replyToEvent);
77              if (logger.isInfoEnabled())
78              {
79                  logger.info("reply to sent: " + endpoint);
80              }
81              ((AbstractService) event.getService()).getStatistics().incSentReplyToEvent();
82          }
83          catch (Exception e)
84          {
85              throw new DispatchException(
86                  CoreMessages.failedToDispatchToReplyto(endpoint),
87                  replyToEvent.getMessage(), replyToEvent.getEndpoint(), e);
88          }
89  
90      }
91  
92      protected synchronized OutboundEndpoint getEndpoint(MuleEvent event, String endpointUri) throws MuleException
93      {
94          OutboundEndpoint endpoint = (OutboundEndpoint) endpointCache.get(endpointUri);
95          if (endpoint == null)
96          {
97              EndpointFactory endpointFactory = RegistryContext.getRegistry().lookupEndpointFactory();
98              EndpointBuilder endpointBuilder = endpointFactory.getEndpointBuilder(endpointUri);
99              if (transformers == null)
100             {
101                 endpointBuilder.setTransformers(event.getEndpoint().getResponseTransformers());
102             }
103             endpoint = endpointFactory.getOutboundEndpoint(endpointBuilder);
104             endpointCache.put(endpointUri, endpoint);
105         }
106         return endpoint;
107     }
108 
109     public List getTransformers()
110     {
111         return transformers;
112     }
113 
114     public void setTransformers(List transformers)
115     {
116         this.transformers = transformers;
117     }
118 
119 }