Coverage Report - org.mule.routing.outbound.EndpointSelector
 
Classes in this File Line Coverage Branch Coverage Complexity
EndpointSelector
0%
0/64
0%
0/30
0
 
 1  
 /*
 2  
  * Copyright (c) MuleSoft, Inc.  All rights reserved.  http://www.mulesoft.com
 3  
  * The software in this package is published under the terms of the CPAL v1.0
 4  
  * license, a copy of which has been included with this distribution in the
 5  
  * LICENSE.txt file.
 6  
  */
 7  
 package org.mule.routing.outbound;
 8  
 
 9  
 import org.mule.api.MuleEvent;
 10  
 import org.mule.api.MuleException;
 11  
 import org.mule.api.MuleMessage;
 12  
 import org.mule.api.endpoint.EndpointNotFoundException;
 13  
 import org.mule.api.endpoint.ImmutableEndpoint;
 14  
 import org.mule.api.endpoint.MalformedEndpointException;
 15  
 import org.mule.api.expression.ExpressionRuntimeException;
 16  
 import org.mule.api.processor.MessageProcessor;
 17  
 import org.mule.api.routing.CouldNotRouteOutboundMessageException;
 18  
 import org.mule.api.routing.RoutingException;
 19  
 import org.mule.config.i18n.CoreMessages;
 20  
 import org.mule.expression.ExpressionConfig;
 21  
 import org.mule.util.StringUtils;
 22  
 
 23  
 import java.util.ArrayList;
 24  
 import java.util.Iterator;
 25  
 import java.util.List;
 26  
 
 27  
 /**
 28  
  * <code>EndpointSelector</code> selects the outgoing endpoint based on a
 29  
  * an expression evaluator  ("header:endpoint" by default).  It will first try to match the
 30  
  * endpoint by name and then by address.
 31  
  * The targets to use can be set on the router itself or be global endpoint definitions.
 32  
  * <pre>
 33  
  * <p/>
 34  
  * &lt;outbound&gt;
 35  
  *      &lt;endpoint-selector-router evaluator="xpath" expression="/MSG/HEADER/NEXT-ADDRESS"&gt;
 36  
  *          &lt;endpoint name="dest1" address="jms://queue1" /&gt;
 37  
  *          &lt;endpoint name="dest2" address="jms://queue2" /&gt;
 38  
  *          &lt;endpoint name="dest3" address="jms://queue3" /&gt;
 39  
  *      &lt;/endpoint-selector-router&gt;
 40  
  * &lt;/outbound&gt;
 41  
  * <p/>
 42  
  * </pre>
 43  
  */
 44  0
 public class EndpointSelector extends FilteringOutboundRouter
 45  
 {
 46  
     public static final String DEFAULT_SELECTOR_EVALUATOR = "header";
 47  
     public static final String DEFAULT_SELECTOR_EXPRESSION = "endpoint";
 48  
 
 49  
     private String defaultEndpointName;
 50  
 
 51  0
     private ExpressionConfig expressionConfig = new ExpressionConfig(DEFAULT_SELECTOR_EXPRESSION, DEFAULT_SELECTOR_EVALUATOR, null);
 52  
 
 53  
     @Override
 54  
     public MuleEvent route(MuleEvent event) throws RoutingException
 55  
     {
 56  0
         MuleMessage message = event.getMessage();
 57  
 
 58  
         List<Object> endpoints;
 59  
         String endpointName;
 60  
 
 61  0
         String prop = expressionConfig.getFullExpression(expressionManager);
 62  0
         if (!expressionManager.isValidExpression(prop))
 63  
         {
 64  0
             throw new CouldNotRouteOutboundMessageException(
 65  
                     CoreMessages.expressionInvalidForProperty("expression", prop), event, null);
 66  
         }
 67  
 
 68  0
         Object property = null;
 69  
         try
 70  
         {
 71  0
             property = expressionManager.evaluate(prop, message);
 72  
         }
 73  0
         catch (ExpressionRuntimeException e)
 74  
         {
 75  0
             logger.error(e.getMessage());
 76  0
         }
 77  
 
 78  0
         if (property == null && getDefaultEndpointName() == null)
 79  
         {
 80  0
             throw new CouldNotRouteOutboundMessageException(
 81  
                     CoreMessages.expressionResultWasNull(
 82  
                         expressionConfig.getFullExpression(expressionManager)), event, null);
 83  
         }
 84  0
         else if (property == null)
 85  
         {
 86  0
             logger.info("Expression: " + prop + " returned null, using default endpoint: " + getDefaultEndpointName());
 87  0
             property = getDefaultEndpointName();
 88  
         }
 89  
 
 90  0
         if (property instanceof String)
 91  
         {
 92  0
             endpoints = new ArrayList<Object>(1);
 93  0
             endpoints.add(property);
 94  
         }
 95  0
         else if (property instanceof List)
 96  
         {
 97  0
             endpoints = (List<Object>) property;
 98  
         }
 99  
         else
 100  
         {
 101  0
             throw new CouldNotRouteOutboundMessageException(CoreMessages.propertyIsNotSupportedType(
 102  
                     expressionConfig.getFullExpression(expressionManager),
 103  
                     new Class[]{String.class, List.class}, property.getClass()), event, null);
 104  
         }
 105  
 
 106  0
         List<MuleEvent> results = new ArrayList<MuleEvent>(endpoints.size());
 107  
 
 108  0
         for (Iterator<Object> iterator = endpoints.iterator(); iterator.hasNext();)
 109  
         {
 110  0
             endpointName = iterator.next().toString();
 111  
 
 112  0
             if (StringUtils.isEmpty(endpointName))
 113  
             {
 114  0
                 throw new CouldNotRouteOutboundMessageException(
 115  
                         CoreMessages.objectIsNull("Endpoint Name: " + expressionConfig.getFullExpression(expressionManager)), event, null);
 116  
             }
 117  0
             MessageProcessor ep = null;
 118  
             try
 119  
             {
 120  0
                 ep = lookupEndpoint(endpointName);
 121  0
                 if (ep == null)
 122  
                 {
 123  0
                     throw new CouldNotRouteOutboundMessageException(CoreMessages.objectNotFound("Endpoint",
 124  
                             endpointName), event, null);
 125  
                 }
 126  0
                 MuleEvent result = sendRequest(event, message, ep, true);
 127  0
                 if (result != null)
 128  
                 {
 129  0
                     results.add(result);
 130  
                 }
 131  
             }
 132  0
             catch (MuleException e)
 133  
             {
 134  0
                 throw new CouldNotRouteOutboundMessageException(event, ep, e);
 135  0
             }
 136  0
         }
 137  0
         return resultsHandler.aggregateResults(results, event, muleContext);
 138  
     }
 139  
 
 140  
     protected MessageProcessor lookupEndpoint(String endpointName) throws MuleException
 141  
     {
 142  0
         for (MessageProcessor target : routes)
 143  
         {
 144  0
             if (target instanceof ImmutableEndpoint)
 145  
             {
 146  0
                 ImmutableEndpoint ep = (ImmutableEndpoint) target;
 147  
                 // Endpoint identifier (deprecated)
 148  0
                 if (endpointName.equals(ep.getName()))
 149  
                 {
 150  0
                     return target;
 151  
                 }
 152  
                 // Global endpoint
 153  0
                 else if (endpointName.equals(ep.getName()))
 154  
                 {
 155  0
                     return target;
 156  
                 }
 157  0
                 else if (endpointName.equals(ep.getEndpointURI().getUri().toString()))
 158  
                     {
 159  0
                         return target;
 160  
                     }
 161  0
             }
 162  
         }
 163  
         try
 164  
         {
 165  0
             return getMuleContext().getEndpointFactory().getOutboundEndpoint(endpointName);
 166  
         }
 167  0
         catch (MalformedEndpointException e)
 168  
         {
 169  0
             throw new EndpointNotFoundException(CoreMessages.endpointNotFound(endpointName), e);
 170  
         }
 171  
     }
 172  
 
 173  
     public String getExpression()
 174  
     {
 175  0
         return expressionConfig.getExpression();
 176  
     }
 177  
 
 178  
     public void setExpression(String expression)
 179  
     {
 180  0
         expressionConfig.setExpression(expression);
 181  0
     }
 182  
 
 183  
     public String getCustomEvaluator()
 184  
     {
 185  0
         return expressionConfig.getCustomEvaluator();
 186  
     }
 187  
 
 188  
     public void setCustomEvaluator(String customEvaluator)
 189  
     {
 190  0
         expressionConfig.setCustomEvaluator(customEvaluator);
 191  0
     }
 192  
 
 193  
     public String getEvaluator()
 194  
     {
 195  0
         return expressionConfig.getEvaluator();
 196  
     }
 197  
 
 198  
     public void setEvaluator(String evaluator)
 199  
     {
 200  0
         expressionConfig.setEvaluator(evaluator);
 201  0
     }
 202  
 
 203  
     public String getDefaultEndpointName()
 204  
     {
 205  0
         return defaultEndpointName;
 206  
     }
 207  
 
 208  
     public void setDefaultEndpointName(String defaultEndpointName)
 209  
     {
 210  0
         this.defaultEndpointName = defaultEndpointName;
 211  0
     }
 212  
 }