1 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 ; 33 34 41 42 public class DefaultReplyToHandler implements ReplyToHandler 43 { 44 private UMOTransformer transformer; 45 46 private Map endpointCache = new ConcurrentHashMap(); 47 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 replyTo) throws UMOException 58 { 59 if (logger.isDebugEnabled()) 60 { 61 logger.debug("sending reply to: " + returnMessage.getReplyTo()); 62 } 63 String replyToEndpoint = replyTo.toString(); 64 65 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 returnMessage.removeProperty(MuleProperties.MULE_REPLY_TO_PROPERTY); 79 80 UMOEvent replyToEvent = new MuleEvent(returnMessage, endpoint, event.getSession(), false); 82 83 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 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 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 |