KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > mule > routing > outbound > EndpointSelector


1 /*
2  * $Id: EndpointSelector.java 4219 2006-12-09 10:15:14Z lajos $
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 MuleSource MPL
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 java.util.Iterator JavaDoc;
14
15 import org.mule.config.i18n.Message;
16 import org.mule.umo.UMOException;
17 import org.mule.umo.UMOMessage;
18 import org.mule.umo.UMOSession;
19 import org.mule.umo.endpoint.UMOEndpoint;
20 import org.mule.umo.routing.CouldNotRouteOutboundMessageException;
21 import org.mule.umo.routing.RoutingException;
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  *
28  * <outbound-router>
29  * <router className="org.mule.routing.outbound.EndpointSelector">
30  * <endpoint name="dest1" address="jms://queue1" />
31  * <endpoint name="dest2" address="jms://queue2" />
32  * <endpoint name="dest3" address="jms://queue3" />
33  * <properties>
34  * <property name="selector" value="endpoint" />
35  * </properties>
36  * </router>
37  * </outbound-router>
38  */

39 public class EndpointSelector extends FilteringOutboundRouter
40 {
41     private String JavaDoc selectorProperty = "endpoint";
42
43     public UMOMessage route(UMOMessage message, UMOSession session, boolean synchronous)
44         throws RoutingException
45     {
46         String JavaDoc endpointName = message.getStringProperty(getSelectorProperty(), null);
47         if (endpointName == null)
48         {
49             throw new IllegalArgumentException JavaDoc("selectorProperty '" + getSelectorProperty()
50                                                + "' must be set on message in order to route it.");
51         }
52
53         UMOEndpoint ep = lookupEndpoint(endpointName);
54         if (ep == null)
55         {
56             throw new CouldNotRouteOutboundMessageException(
57                 Message.createStaticMessage("No endpoint found with the name " + endpointName), message, ep);
58         }
59
60         try
61         {
62             if (synchronous)
63             {
64                 return send(session, message, ep);
65             }
66             else
67             {
68                 dispatch(session, message, ep);
69                 return null;
70             }
71         }
72         catch (UMOException e)
73         {
74             throw new CouldNotRouteOutboundMessageException(message, ep, e);
75         }
76     }
77
78     protected UMOEndpoint lookupEndpoint(String JavaDoc endpointName)
79     {
80         UMOEndpoint ep;
81         Iterator JavaDoc iterator = endpoints.iterator();
82         while (iterator.hasNext())
83         {
84             ep = (UMOEndpoint)iterator.next();
85             if (endpointName.equals(ep.getName()))
86             {
87                 return ep;
88             }
89             else if (endpointName.equals(ep.getEndpointURI().getAddress()))
90             {
91                 return ep;
92             }
93         }
94         return null;
95     }
96
97     public String JavaDoc getSelectorProperty()
98     {
99         return selectorProperty;
100     }
101
102     public void setSelectorProperty(String JavaDoc selectorProperty)
103     {
104         this.selectorProperty = selectorProperty;
105     }
106 }
107
Popular Tags