View Javadoc

1   /*
2    * $Id: DefaultReplyToHandler.java 7963 2007-08-21 08:53:15Z 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.providers;
12  
13  import org.mule.MuleManager;
14  import org.mule.config.MuleProperties;
15  import org.mule.config.i18n.CoreMessages;
16  import org.mule.impl.MuleEvent;
17  import org.mule.impl.endpoint.MuleEndpoint;
18  import org.mule.impl.endpoint.MuleEndpointURI;
19  import org.mule.impl.model.AbstractComponent;
20  import org.mule.umo.UMOEvent;
21  import org.mule.umo.UMOException;
22  import org.mule.umo.UMOMessage;
23  import org.mule.umo.endpoint.UMOEndpoint;
24  import org.mule.umo.endpoint.UMOEndpointURI;
25  import org.mule.umo.provider.DispatchException;
26  import org.mule.umo.transformer.UMOTransformer;
27  
28  import java.util.HashMap;
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 UMOTransformer transformer;
47      private final Map endpointCache = new HashMap();
48  
49      public DefaultReplyToHandler(UMOTransformer transformer)
50      {
51          this.transformer = transformer;
52      }
53  
54      public void processReplyTo(UMOEvent event, UMOMessage returnMessage, Object replyTo) throws UMOException
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          UMOEndpoint endpoint = getEndpoint(event, replyToEndpoint);
65  
66          if (transformer == null)
67          {
68              transformer = event.getEndpoint().getResponseTransformer();
69          }
70  
71          if (transformer != null)
72          {
73              endpoint.setTransformer(transformer);
74          }
75  
76          // make sure remove the replyTo property as not cause a a forever
77          // replyto loop
78          returnMessage.removeProperty(MuleProperties.MULE_REPLY_TO_PROPERTY);
79  
80          // Create the replyTo event asynchronous
81          UMOEvent replyToEvent = new MuleEvent(returnMessage, endpoint, event.getSession(), false);
82  
83          // dispatch the event
84          try
85          {
86              endpoint.dispatch(replyToEvent);
87              if (logger.isInfoEnabled())
88              {
89                  logger.info("reply to sent: " + endpoint);
90              }
91              ((AbstractComponent) event.getComponent()).getStatistics().incSentReplyToEvent();
92          }
93          catch (Exception e)
94          {
95              throw new DispatchException(
96                  CoreMessages.failedToDispatchToReplyto(endpoint),
97                  replyToEvent.getMessage(), replyToEvent.getEndpoint(), e);
98          }
99  
100     }
101 
102     protected synchronized UMOEndpoint getEndpoint(UMOEvent event, String endpointUri) throws UMOException
103     {
104         UMOEndpoint endpoint = (UMOEndpoint) endpointCache.get(endpointUri);
105         if (endpoint == null)
106         {
107             endpoint = MuleManager.getInstance().lookupEndpoint(endpointUri);
108             if (endpoint == null)
109             {
110                 UMOEndpointURI ep = new MuleEndpointURI(endpointUri);
111                 endpoint = MuleEndpoint.getOrCreateEndpointForUri(ep, UMOEndpoint.ENDPOINT_TYPE_SENDER);
112                 endpointCache.put(endpointUri, endpoint);
113             }
114         }
115         return endpoint;
116     }
117 
118     public UMOTransformer getTransformer()
119     {
120         return transformer;
121     }
122 
123     public void setTransformer(UMOTransformer transformer)
124     {
125         this.transformer = transformer;
126     }
127 
128 }