Coverage Report - org.mule.routing.outbound.EndpointSelector
 
Classes in this File Line Coverage Branch Coverage Complexity
EndpointSelector
81%
22/27
79%
11/14
5
 
 1  
 /*
 2  
  * $Id: EndpointSelector.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.routing.outbound;
 12  
 
 13  
 import org.mule.config.i18n.MessageFactory;
 14  
 import org.mule.umo.UMOException;
 15  
 import org.mule.umo.UMOMessage;
 16  
 import org.mule.umo.UMOSession;
 17  
 import org.mule.umo.endpoint.UMOEndpoint;
 18  
 import org.mule.umo.routing.CouldNotRouteOutboundMessageException;
 19  
 import org.mule.umo.routing.RoutingException;
 20  
 
 21  
 import java.util.Iterator;
 22  
 
 23  
 /**
 24  
  * <code>EndpointSelector</code> selects the outgoing endpoint based on a
 25  
  * message property ("endpoint" by default).  It will first try to match the
 26  
  * endpoint by name and then by address.
 27  
  * <pre>
 28  
  *
 29  
  * &lt;outbound-router&gt;
 30  
  *      &lt;router className="org.mule.routing.outbound.EndpointSelector"&gt;
 31  
  *          &lt;endpoint name="dest1" address="jms://queue1" /&gt;
 32  
  *          &lt;endpoint name="dest2" address="jms://queue2" /&gt;
 33  
  *          &lt;endpoint name="dest3" address="jms://queue3" /&gt;
 34  
  *          &lt;properties&gt;
 35  
  *              &lt;property name="selector" value="endpoint" /&gt;
 36  
  *          &lt;/properties&gt;
 37  
  *      &lt;/router&gt;
 38  
  * &lt;/outbound-router&gt;
 39  
  *
 40  
  * </pre>
 41  
  */
 42  8
 public class EndpointSelector extends FilteringOutboundRouter
 43  
 {
 44  8
     private String selectorProperty = "endpoint";
 45  
 
 46  
     public UMOMessage route(UMOMessage message, UMOSession session, boolean synchronous)
 47  
         throws RoutingException
 48  
     {
 49  8
         String endpointName = message.getStringProperty(getSelectorProperty(), null);
 50  8
         if (endpointName == null)
 51  
         {
 52  2
             throw new IllegalArgumentException("selectorProperty '" + getSelectorProperty()
 53  
                                                + "' must be set on message in order to route it.");
 54  
         }
 55  
 
 56  6
         UMOEndpoint ep = lookupEndpoint(endpointName);
 57  6
         if (ep == null)
 58  
         {
 59  2
             throw new CouldNotRouteOutboundMessageException(
 60  
                 MessageFactory.createStaticMessage("No endpoint found with the name " + endpointName), message, ep);
 61  
         }
 62  
 
 63  
         try
 64  
         {
 65  4
             if (synchronous)
 66  
             {
 67  0
                 return send(session, message, ep);
 68  
             }
 69  
             else
 70  
             {
 71  4
                 dispatch(session, message, ep);
 72  4
                 return null;
 73  
             }
 74  
         }
 75  0
         catch (UMOException e)
 76  
         {
 77  0
             throw new CouldNotRouteOutboundMessageException(message, ep, e);
 78  
         }
 79  
     }
 80  
 
 81  
     protected UMOEndpoint lookupEndpoint(String endpointName)
 82  
     {
 83  
         UMOEndpoint ep;
 84  6
         Iterator iterator = endpoints.iterator();
 85  18
         while (iterator.hasNext())
 86  
         {
 87  16
             ep = (UMOEndpoint) iterator.next();
 88  
             // Endpoint identifier (deprecated)
 89  16
             if (endpointName.equals(ep.getEndpointURI().getEndpointName()))
 90  
             {
 91  0
                 return ep;
 92  
             }
 93  
             // Global endpoint
 94  16
             else if (endpointName.equals(ep.getName()))
 95  
             {
 96  4
                 return ep;
 97  
             }
 98  12
             else if (endpointName.equals(ep.getEndpointURI().getUri().toString()))
 99  
             {
 100  0
                 return ep;
 101  
             }
 102  
         }
 103  2
         return null;
 104  
     }
 105  
 
 106  
     public String getSelectorProperty()
 107  
     {
 108  14
         return selectorProperty;
 109  
     }
 110  
 
 111  
     public void setSelectorProperty(String selectorProperty)
 112  
     {
 113  2
         this.selectorProperty = selectorProperty;
 114  2
     }
 115  
 }