View Javadoc

1   /*
2    * $Id: EndpointSelector.java 7976 2007-08-21 14:26:13Z 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  public class EndpointSelector extends FilteringOutboundRouter
43  {
44      private String selectorProperty = "endpoint";
45  
46      public UMOMessage route(UMOMessage message, UMOSession session, boolean synchronous)
47          throws RoutingException
48      {
49          String endpointName = message.getStringProperty(getSelectorProperty(), null);
50          if (endpointName == null)
51          {
52              throw new IllegalArgumentException("selectorProperty '" + getSelectorProperty()
53                                                 + "' must be set on message in order to route it.");
54          }
55  
56          UMOEndpoint ep = lookupEndpoint(endpointName);
57          if (ep == null)
58          {
59              throw new CouldNotRouteOutboundMessageException(
60                  MessageFactory.createStaticMessage("No endpoint found with the name " + endpointName), message, ep);
61          }
62  
63          try
64          {
65              if (synchronous)
66              {
67                  return send(session, message, ep);
68              }
69              else
70              {
71                  dispatch(session, message, ep);
72                  return null;
73              }
74          }
75          catch (UMOException e)
76          {
77              throw new CouldNotRouteOutboundMessageException(message, ep, e);
78          }
79      }
80  
81      protected UMOEndpoint lookupEndpoint(String endpointName)
82      {
83          UMOEndpoint ep;
84          Iterator iterator = endpoints.iterator();
85          while (iterator.hasNext())
86          {
87              ep = (UMOEndpoint) iterator.next();
88              // Endpoint identifier (deprecated)
89              if (endpointName.equals(ep.getEndpointURI().getEndpointName()))
90              {
91                  return ep;
92              }
93              // Global endpoint
94              else if (endpointName.equals(ep.getName()))
95              {
96                  return ep;
97              }
98              else if (endpointName.equals(ep.getEndpointURI().getUri().toString()))
99              {
100                 return ep;
101             }
102         }
103         return null;
104     }
105 
106     public String getSelectorProperty()
107     {
108         return selectorProperty;
109     }
110 
111     public void setSelectorProperty(String selectorProperty)
112     {
113         this.selectorProperty = selectorProperty;
114     }
115 }