Coverage Report - org.mule.routing.outbound.AbstractOutboundRouter
 
Classes in this File Line Coverage Branch Coverage Complexity
AbstractOutboundRouter
48%
56/117
42%
25/60
2.905
AbstractOutboundRouter$1
100%
3/3
N/A
2.905
AbstractOutboundRouter$2
100%
2/2
N/A
2.905
 
 1  
 /*
 2  
  * $Id: AbstractOutboundRouter.java 12181 2008-06-26 20:05:55Z 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.routing.outbound;
 12  
 
 13  
 import org.mule.api.MuleException;
 14  
 import org.mule.api.MuleMessage;
 15  
 import org.mule.api.MuleSession;
 16  
 import org.mule.api.config.MuleProperties;
 17  
 import org.mule.api.endpoint.ImmutableEndpoint;
 18  
 import org.mule.api.endpoint.InvalidEndpointTypeException;
 19  
 import org.mule.api.endpoint.OutboundEndpoint;
 20  
 import org.mule.api.routing.MessageInfoMapping;
 21  
 import org.mule.api.routing.OutboundRouter;
 22  
 import org.mule.api.routing.RoutingException;
 23  
 import org.mule.api.transaction.TransactionCallback;
 24  
 import org.mule.api.transaction.TransactionConfig;
 25  
 import org.mule.config.i18n.CoreMessages;
 26  
 import org.mule.routing.AbstractRouter;
 27  
 import org.mule.routing.MuleMessageInfoMapping;
 28  
 import org.mule.transaction.TransactionTemplate;
 29  
 import org.mule.util.StringMessageUtils;
 30  
 import org.mule.util.SystemUtils;
 31  
 
 32  
 import java.util.Iterator;
 33  
 import java.util.List;
 34  
 
 35  
 import edu.emory.mathcs.backport.java.util.concurrent.CopyOnWriteArrayList;
 36  
 
 37  
 import org.apache.commons.logging.Log;
 38  
 import org.apache.commons.logging.LogFactory;
 39  
 
 40  
 /**
 41  
  * <code>AbstractOutboundRouter</code> is a base router class that tracks
 42  
  * statistics about message processing through the router.
 43  
  *
 44  
  */
 45  442
 public abstract class AbstractOutboundRouter extends AbstractRouter implements OutboundRouter
 46  
 {
 47  
     public static final int ENABLE_CORRELATION_IF_NOT_SET = 0;
 48  
     public static final int ENABLE_CORRELATION_ALWAYS = 1;
 49  
     public static final int ENABLE_CORRELATION_NEVER = 2;
 50  
     /**
 51  
      * logger used by this class
 52  
      */
 53  442
     protected transient Log logger = LogFactory.getLog(getClass());
 54  
 
 55  442
     protected List endpoints = new CopyOnWriteArrayList();
 56  
 
 57  442
     protected String replyTo = null;
 58  
 
 59  442
     protected int enableCorrelation = ENABLE_CORRELATION_IF_NOT_SET;
 60  
 
 61  442
     protected MessageInfoMapping messageInfoMapping = new MuleMessageInfoMapping();
 62  
 
 63  
     protected TransactionConfig transactionConfig;
 64  
 
 65  
     public void dispatch(final MuleSession session, final MuleMessage message, final OutboundEndpoint endpoint) throws MuleException
 66  
     {
 67  42
         setMessageProperties(session, message, endpoint);
 68  
 
 69  42
         if (logger.isDebugEnabled())
 70  
         {
 71  
             try
 72  
             {
 73  0
                 logger.debug("Message being sent to: " + endpoint.getEndpointURI() + " Message payload: \n"
 74  
                              + StringMessageUtils.truncate(message.getPayloadAsString(), 100, false)
 75  
                              + "\n outbound transformer is: " + endpoint.getTransformers());
 76  
             }
 77  0
             catch (Exception e)
 78  
             {
 79  0
                 logger.debug("Message being sent to: " + endpoint.getEndpointURI()
 80  
                              + " Message payload: \n(unable to retrieve payload: " + e.getMessage()
 81  
                              + "\n outbound transformer is: " + endpoint.getTransformers());
 82  0
             }
 83  
         }
 84  
 
 85  42
         TransactionTemplate tt = createTransactionTemplate(session, endpoint);
 86  42
         TransactionCallback cb = new TransactionCallback()
 87  
         {
 88  42
             public Object doInTransaction() throws Exception
 89  
             {
 90  42
                 session.dispatchEvent(message, endpoint);
 91  40
                 return null;
 92  
             }
 93  
         };
 94  
         
 95  
         try
 96  
         {
 97  42
             tt.execute(cb);
 98  
         }
 99  2
         catch (Exception e)
 100  
         {
 101  2
             throw new RoutingException(message, null, e);
 102  40
         }
 103  
         
 104  
         
 105  40
         if (getRouterStatistics() != null)
 106  
         {
 107  8
             if (getRouterStatistics().isEnabled())
 108  
             {
 109  0
                 getRouterStatistics().incrementRoutedMessage(endpoint);
 110  
             }
 111  
         }
 112  40
     }
 113  
 
 114  
     public MuleMessage send(final MuleSession session, final MuleMessage message, final OutboundEndpoint endpoint) throws MuleException
 115  
     {
 116  64
         if (replyTo != null)
 117  
         {
 118  0
             logger.debug("event was dispatched synchronously, but there is a ReplyTo endpoint set, so using asynchronous dispatch");
 119  0
             dispatch(session, message, endpoint);
 120  0
             return null;
 121  
         }
 122  
 
 123  64
         this.setMessageProperties(session, message, endpoint);
 124  
 
 125  64
         if (logger.isDebugEnabled())
 126  
         {
 127  0
             logger.debug("Message being sent to: " + endpoint.getEndpointURI());
 128  0
             logger.debug(message);
 129  
         }
 130  
 
 131  64
         if (logger.isTraceEnabled())
 132  
         {
 133  
             try
 134  
             {
 135  0
                 logger.trace("Message payload: \n" + message.getPayloadAsString());
 136  
             }
 137  0
             catch (Exception e)
 138  
             {
 139  
                 // ignore
 140  0
             }
 141  
         }
 142  
 
 143  64
         TransactionTemplate tt = createTransactionTemplate(session, endpoint);
 144  64
         TransactionCallback cb = new TransactionCallback()
 145  
         {
 146  64
             public Object doInTransaction() throws Exception
 147  
             {
 148  64
                 return session.sendEvent(message, endpoint);
 149  
             }
 150  
         };
 151  
         
 152  
         MuleMessage result;
 153  
         try
 154  
         {
 155  64
             result = (MuleMessage) tt.execute(cb);
 156  
         }
 157  6
         catch (Exception e)
 158  
         {
 159  6
             throw new RoutingException(message, null, e);
 160  58
         }
 161  
         
 162  58
         if (getRouterStatistics() != null)
 163  
         {
 164  0
             if (getRouterStatistics().isEnabled())
 165  
             {
 166  0
                 getRouterStatistics().incrementRoutedMessage(endpoint);
 167  
             }
 168  
         }
 169  
 
 170  58
         if (logger.isDebugEnabled())
 171  
         {
 172  0
             logger.debug("Response message from sending to: " + endpoint.getEndpointURI());
 173  0
             logger.debug(result);
 174  
         }
 175  
 
 176  58
         if (logger.isTraceEnabled())
 177  
         {
 178  
             try
 179  
             {
 180  0
                 logger.trace("Message payload: \n" + result.getPayloadAsString());
 181  
             }
 182  0
             catch (Exception e)
 183  
             {
 184  
                 // ignore
 185  0
             }
 186  
         }
 187  
 
 188  58
         return result;
 189  
     }
 190  
     
 191  
     protected TransactionTemplate createTransactionTemplate(MuleSession session, ImmutableEndpoint endpoint)
 192  
     {
 193  86
         return new TransactionTemplate(endpoint.getTransactionConfig(),
 194  
             session.getService().getExceptionListener(), muleContext);
 195  
     }
 196  
 
 197  
     protected void setMessageProperties(MuleSession session, MuleMessage message, OutboundEndpoint endpoint)
 198  
     {
 199  108
         if (replyTo != null)
 200  
         {
 201  
             // if replyTo is set we'll probably want the correlationId set as
 202  
             // well
 203  0
             message.setReplyTo(replyTo);
 204  0
             message.setProperty(MuleProperties.MULE_REPLY_TO_REQUESTOR_PROPERTY, session.getService().getName());
 205  0
             if (logger.isDebugEnabled())
 206  
             {
 207  0
                 logger.debug("Setting replyTo=" + replyTo + " for outbound endpoint: "
 208  
                              + endpoint.getEndpointURI());
 209  
             }
 210  
         }
 211  108
         if (enableCorrelation != ENABLE_CORRELATION_NEVER)
 212  
         {
 213  108
             boolean correlationSet = message.getCorrelationId() != null;
 214  108
             if (correlationSet && (enableCorrelation == ENABLE_CORRELATION_IF_NOT_SET))
 215  
             {
 216  58
                 if (logger.isDebugEnabled())
 217  
                 {
 218  0
                     logger.debug("CorrelationId is already set to '" + message.getCorrelationId()
 219  
                                  + "' , not setting it again");
 220  
                 }
 221  58
                 return;
 222  
             }
 223  50
             else if (correlationSet)
 224  
             {
 225  0
                 if (logger.isDebugEnabled())
 226  
                 {
 227  0
                     logger.debug("CorrelationId is already set to '" + message.getCorrelationId()
 228  
                                  + "', but router is configured to overwrite it");
 229  
                 }
 230  
             }
 231  
             else
 232  
             {
 233  50
                 if (logger.isDebugEnabled())
 234  
                 {
 235  0
                     logger.debug("No CorrelationId is set on the message, will set a new Id");
 236  
                 }
 237  
             }
 238  
 
 239  
             String correlation;
 240  50
             correlation = messageInfoMapping.getCorrelationId(message);
 241  50
             if (logger.isDebugEnabled())
 242  
             {
 243  0
                 logger.debug("Extracted correlation Id as: " + correlation);
 244  
             }
 245  
 
 246  50
             if (logger.isDebugEnabled())
 247  
             {
 248  0
                 StringBuffer buf = new StringBuffer();
 249  0
                 buf.append("Setting Correlation info on Outbound router for endpoint: ").append(
 250  
                     endpoint.getEndpointURI());
 251  0
                 buf.append(SystemUtils.LINE_SEPARATOR).append("Id=").append(correlation);
 252  
                 // buf.append(", ").append("Seq=").append(seq);
 253  
                 // buf.append(", ").append("Group Size=").append(group);
 254  0
                 logger.debug(buf.toString());
 255  
             }
 256  50
             message.setCorrelationId(correlation);
 257  
             // message.setCorrelationGroupSize(group);
 258  
             // message.setCorrelationSequence(seq);
 259  
         }
 260  50
     }
 261  
 
 262  
     public List getEndpoints()
 263  
     {
 264  16
         return endpoints;
 265  
     }
 266  
 
 267  
     public void setEndpoints(List endpoints)
 268  
     {
 269  38
         this.endpoints.clear();
 270  
         // this.endpoints = new CopyOnWriteArrayList(endpoints);
 271  
         // Ensure all endpoints are outbound endpoints
 272  
         // This will go when we start dropping support for 1.4 and start using 1.5
 273  38
         for (Iterator iterator = endpoints.iterator(); iterator.hasNext();)
 274  
         {
 275  74
             ImmutableEndpoint umoEndpoint = (ImmutableEndpoint) iterator.next();
 276  74
             if (!(umoEndpoint instanceof OutboundEndpoint))
 277  
             {
 278  0
                 throw new InvalidEndpointTypeException(CoreMessages.outboundRouterMustUseOutboudEndpoints(
 279  
                     this, umoEndpoint));
 280  
             }
 281  
             else
 282  
             {
 283  74
                 addEndpoint((OutboundEndpoint) umoEndpoint);
 284  
             }
 285  74
         }
 286  38
     }
 287  
 
 288  
     public void addEndpoint(OutboundEndpoint endpoint)
 289  
     {
 290  96
         endpoints.add(endpoint);
 291  96
     }
 292  
 
 293  
     public boolean removeEndpoint(OutboundEndpoint endpoint)
 294  
     {
 295  0
         return endpoints.remove(endpoint);
 296  
     }
 297  
 
 298  
     public String getReplyTo()
 299  
     {
 300  0
         return replyTo;
 301  
     }
 302  
 
 303  
     public void setReplyTo(String replyTo)
 304  
     {
 305  0
         this.replyTo = replyTo;
 306  0
     }
 307  
 
 308  
     public int getEnableCorrelation()
 309  
     {
 310  0
         return enableCorrelation;
 311  
     }
 312  
 
 313  
     public void setEnableCorrelation(int enableCorrelation)
 314  
     {
 315  0
         this.enableCorrelation = enableCorrelation;
 316  0
     }
 317  
 
 318  
     public void setEnableCorrelationAsString(String enableCorrelation)
 319  
     {
 320  0
         if (enableCorrelation != null)
 321  
         {
 322  0
             if (enableCorrelation.equals("ALWAYS"))
 323  
             {
 324  0
                 this.enableCorrelation = ENABLE_CORRELATION_ALWAYS;
 325  
             }
 326  0
             else if (enableCorrelation.equals("NEVER"))
 327  
             {
 328  0
                 this.enableCorrelation = ENABLE_CORRELATION_NEVER;
 329  
             }
 330  0
             else if (enableCorrelation.equals("IF_NOT_SET"))
 331  
             {
 332  0
                 this.enableCorrelation = ENABLE_CORRELATION_IF_NOT_SET;
 333  
             }
 334  
             else
 335  
             {
 336  0
                 throw new IllegalArgumentException("Value for enableCorrelation not recognised: "
 337  
                                                    + enableCorrelation);
 338  
             }
 339  
         }
 340  0
     }
 341  
 
 342  
     public MessageInfoMapping getMessageInfoMapping()
 343  
     {
 344  0
         return messageInfoMapping;
 345  
     }
 346  
 
 347  
     public void setMessageInfoMapping(MessageInfoMapping messageInfoMapping)
 348  
     {
 349  0
         this.messageInfoMapping = messageInfoMapping;
 350  0
     }
 351  
 
 352  
     public TransactionConfig getTransactionConfig()
 353  
     {
 354  16
         return transactionConfig;
 355  
     }
 356  
 
 357  
     public void setTransactionConfig(TransactionConfig transactionConfig)
 358  
     {
 359  0
         this.transactionConfig = transactionConfig;
 360  0
     }
 361  
 
 362  
     public boolean isDynamicEndpoints()
 363  
     {
 364  0
         return false;
 365  
     }
 366  
 
 367  
     /**
 368  
      * @param name the Endpoint identifier
 369  
      * @return the Endpoint or null if the endpointUri is not registered
 370  
      * @see org.mule.api.routing.InboundRouterCollection
 371  
      */
 372  
     public OutboundEndpoint getEndpoint(String name)
 373  
     {
 374  
         OutboundEndpoint endpointDescriptor;
 375  0
         for (Iterator iterator = endpoints.iterator(); iterator.hasNext();)
 376  
         {
 377  0
             endpointDescriptor = (OutboundEndpoint) iterator.next();
 378  0
             if (endpointDescriptor.getName().equals(name))
 379  
             {
 380  0
                 return endpointDescriptor;
 381  
             }
 382  
         }
 383  0
         return null;
 384  
     }
 385  
 }