001: /*
002: * $Id: EndpointSelector.java 11298 2008-03-09 20:48:15Z rossmason $
003: * --------------------------------------------------------------------------------------
004: * Copyright (c) MuleSource, Inc. All rights reserved. http://www.mulesource.com
005: *
006: * The software in this package is published under the terms of the CPAL v1.0
007: * license, a copy of which has been included with this distribution in the
008: * LICENSE.txt file.
009: */
010:
011: package org.mule.routing.outbound;
012:
013: import org.mule.api.MuleException;
014: import org.mule.api.MuleMessage;
015: import org.mule.api.MuleSession;
016: import org.mule.api.context.MuleContextAware;
017: import org.mule.api.endpoint.OutboundEndpoint;
018: import org.mule.api.routing.CouldNotRouteOutboundMessageException;
019: import org.mule.api.routing.RoutingException;
020: import org.mule.config.i18n.CoreMessages;
021: import org.mule.util.StringUtils;
022: import org.mule.util.expression.ExpressionEvaluatorManager;
023:
024: import java.util.ArrayList;
025: import java.util.Iterator;
026: import java.util.List;
027:
028: /**
029: * <code>EndpointSelector</code> selects the outgoing endpoint based on a
030: * message property ("endpoint" by default). It will first try to match the
031: * endpoint by name and then by address.
032: * <pre>
033: *
034: * <outbound-router>
035: * <router className="org.mule.routing.outbound.EndpointSelector">
036: * <endpoint name="dest1" address="jms://queue1" />
037: * <endpoint name="dest2" address="jms://queue2" />
038: * <endpoint name="dest3" address="jms://queue3" />
039: * <properties>
040: * <property name="selector" value="endpoint" />
041: * </properties>
042: * </router>
043: * </outbound-router>
044: *
045: * </pre>
046: */
047: public class EndpointSelector extends FilteringOutboundRouter implements
048: MuleContextAware {
049: public static final String DEFAULT_SELECTOR_PROPERTY = "endpoint";
050: public static final String DEFAULT_SELECTOR_EXPRESSION = "header:endpoint";
051:
052: private String selectorExpression = DEFAULT_SELECTOR_EXPRESSION;
053:
054: public MuleMessage route(MuleMessage message, MuleSession session,
055: boolean synchronous) throws RoutingException {
056: List endpoints;
057: String endpointName;
058:
059: String prop = getSelectorExpression();
060: if (!ExpressionEvaluatorManager.isValidExpression(prop)) {
061: throw new CouldNotRouteOutboundMessageException(
062: CoreMessages.expressionInvalidForProperty(
063: "selectorExpression", prop), message, null);
064: }
065:
066: Object property = ExpressionEvaluatorManager.evaluate(prop,
067: message);
068: if (property == null) {
069: throw new CouldNotRouteOutboundMessageException(
070: CoreMessages
071: .propertyIsNotSetOnEvent(getSelectorExpression()),
072: message, null);
073: }
074:
075: if (property instanceof String) {
076: endpoints = new ArrayList(1);
077: endpoints.add(property);
078: } else if (property instanceof List) {
079: endpoints = (List) property;
080: } else {
081: throw new CouldNotRouteOutboundMessageException(
082: CoreMessages.propertyIsNotSupportedType(
083: getSelectorExpression(), new Class[] {
084: String.class, List.class },
085: property.getClass()), message, null);
086: }
087:
088: MuleMessage result = null;
089: for (Iterator iterator = endpoints.iterator(); iterator
090: .hasNext();) {
091: endpointName = iterator.next().toString();
092:
093: if (StringUtils.isEmpty(endpointName)) {
094: throw new CouldNotRouteOutboundMessageException(
095: CoreMessages.objectIsNull("Endpoint Name: "
096: + getSelectorExpression()), message,
097: null);
098: }
099: OutboundEndpoint ep = null;
100: try {
101: ep = lookupEndpoint(endpointName);
102: if (ep == null) {
103: throw new CouldNotRouteOutboundMessageException(
104: CoreMessages.objectNotFound("Endpoint",
105: endpointName), message, ep);
106: }
107: if (synchronous) {
108: // TODO See MULE-2613, we only return the last message here
109: result = send(session, message, ep);
110: } else {
111: dispatch(session, message, ep);
112: }
113: } catch (MuleException e) {
114: throw new CouldNotRouteOutboundMessageException(
115: message, ep, e);
116: }
117: }
118: return result;
119: }
120:
121: protected OutboundEndpoint lookupEndpoint(String endpointName)
122: throws MuleException {
123: OutboundEndpoint ep;
124: Iterator iterator = endpoints.iterator();
125: while (iterator.hasNext()) {
126: ep = (OutboundEndpoint) iterator.next();
127: // Endpoint identifier (deprecated)
128: if (endpointName.equals(ep.getEndpointURI()
129: .getEndpointName())) {
130: return ep;
131: }
132: // Global endpoint
133: else if (endpointName.equals(ep.getName())) {
134: return ep;
135: } else if (endpointName.equals(ep.getEndpointURI().getUri()
136: .toString())) {
137: return ep;
138: }
139: }
140: return getMuleContext().getRegistry().lookupEndpointFactory()
141: .getOutboundEndpoint(endpointName);
142: }
143:
144: public String getSelectorExpression() {
145: return selectorExpression;
146: }
147:
148: public void setSelectorExpression(String selectorExpression) {
149: this.selectorExpression = selectorExpression;
150: }
151: }
|