KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > mule > providers > DefaultReplyToHandler


1 /*
2  * $Id: DefaultReplyToHandler.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.providers;
12
13 import edu.emory.mathcs.backport.java.util.concurrent.ConcurrentHashMap;
14 import org.apache.commons.logging.Log;
15 import org.apache.commons.logging.LogFactory;
16 import org.mule.MuleManager;
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.endpoint.MuleEndpoint;
22 import org.mule.impl.endpoint.MuleEndpointURI;
23 import org.mule.impl.model.AbstractComponent;
24 import org.mule.umo.UMOEvent;
25 import org.mule.umo.UMOException;
26 import org.mule.umo.UMOMessage;
27 import org.mule.umo.endpoint.UMOEndpoint;
28 import org.mule.umo.endpoint.UMOEndpointURI;
29 import org.mule.umo.provider.DispatchException;
30 import org.mule.umo.transformer.UMOTransformer;
31
32 import java.util.Map JavaDoc;
33
34 /**
35  * <code>DefaultReplyToHandler</code> is responsible for processing a message
36  * replyTo header.
37  *
38  * @author <a HREF="mailto:ross.mason@symphonysoft.com">Ross Mason</a>
39  * @version $Revision: 3798 $
40  */

41
42 public class DefaultReplyToHandler implements ReplyToHandler
43 {
44     private UMOTransformer transformer;
45
46     private Map endpointCache = new ConcurrentHashMap();
47     /**
48      * logger used by this class
49      */

50     protected static Log logger = LogFactory.getLog(DefaultReplyToHandler.class);
51
52     public DefaultReplyToHandler(UMOTransformer transformer)
53     {
54         this.transformer = transformer;
55     }
56
57     public void processReplyTo(UMOEvent event, UMOMessage returnMessage, Object JavaDoc replyTo) throws UMOException
58     {
59         if (logger.isDebugEnabled())
60         {
61             logger.debug("sending reply to: " + returnMessage.getReplyTo());
62         }
63         String JavaDoc replyToEndpoint = replyTo.toString();
64
65         // get the endpoint for this url
66
UMOEndpoint endpoint = getEndpoint(event, replyToEndpoint);
67         if (transformer == null)
68         {
69             transformer = event.getEndpoint().getResponseTransformer();
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.getConnector().getDispatcher(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 JavaDoc e)
94         {
95             throw new DispatchException(new Message(Messages.FAILED_TO_DISPATCH_TO_REPLYTO_X, endpoint),
96                 replyToEvent.getMessage(), replyToEvent.getEndpoint(), e);
97         }
98
99     }
100
101     protected UMOEndpoint getEndpoint(UMOEvent event, String JavaDoc endpointUri) throws UMOException
102     {
103         UMOEndpoint endpoint = (UMOEndpoint)endpointCache.get(endpointUri);
104         if (endpoint == null)
105         {
106             endpoint = MuleManager.getInstance().lookupEndpoint(endpointUri);
107             if (endpoint == null)
108             {
109                 UMOEndpointURI ep = new MuleEndpointURI(endpointUri);
110                 endpoint = MuleEndpoint.getOrCreateEndpointForUri(ep, UMOEndpoint.ENDPOINT_TYPE_SENDER);
111                 endpointCache.put(endpointUri, endpoint);
112             }
113         }
114         return endpoint;
115     }
116
117     public UMOTransformer getTransformer()
118     {
119         return transformer;
120     }
121
122     public void setTransformer(UMOTransformer transformer)
123     {
124         this.transformer = transformer;
125     }
126 }
127
Popular Tags