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