001: /*
002: * $Id: DefaultOutboundRouterCollection.java 10961 2008-02-22 19:01:02Z dfeist $
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.MessagingException;
014: import org.mule.api.MuleMessage;
015: import org.mule.api.MuleSession;
016: import org.mule.api.routing.OutboundRouter;
017: import org.mule.api.routing.OutboundRouterCollection;
018: import org.mule.api.routing.RoutingException;
019: import org.mule.api.service.Service;
020: import org.mule.api.transaction.TransactionCallback;
021: import org.mule.management.stats.RouterStatistics;
022: import org.mule.routing.AbstractRouterCollection;
023: import org.mule.transaction.TransactionTemplate;
024:
025: import java.util.Iterator;
026:
027: /**
028: * <code>DefaultOutboundRouterCollection</code> is a container of routers. An
029: * DefaultOutboundRouterCollection must have atleast one router. By default the first matching
030: * router is used to route an event though it is possible to match on all routers
031: * meaning that the message will get sent over all matching routers.
032: */
033:
034: public class DefaultOutboundRouterCollection extends
035: AbstractRouterCollection implements OutboundRouterCollection {
036:
037: public DefaultOutboundRouterCollection() {
038: super (RouterStatistics.TYPE_OUTBOUND);
039: }
040:
041: public MuleMessage route(final MuleMessage message,
042: final MuleSession session, final boolean synchronous)
043: throws MessagingException {
044:
045: MuleMessage result;
046: boolean matchfound = false;
047:
048: for (Iterator iterator = getRouters().iterator(); iterator
049: .hasNext();) {
050: OutboundRouter umoOutboundRouter = (OutboundRouter) iterator
051: .next();
052: if (umoOutboundRouter.isMatch(message)) {
053: matchfound = true;
054: // Manage outbound only transactions here
055: final OutboundRouter router = umoOutboundRouter;
056: Service service = session.getService();
057: TransactionTemplate tt = new TransactionTemplate(
058: umoOutboundRouter.getTransactionConfig(),
059: service.getExceptionListener(), muleContext);
060:
061: TransactionCallback cb = new TransactionCallback() {
062: public Object doInTransaction() throws Exception {
063: return router.route(message, session,
064: synchronous);
065: }
066: };
067: try {
068: result = (MuleMessage) tt.execute(cb);
069: } catch (Exception e) {
070: throw new RoutingException(message, null, e);
071: }
072:
073: if (!isMatchAll()) {
074: return result;
075: }
076: }
077: }
078:
079: if (!matchfound && getCatchAllStrategy() != null) {
080: if (logger.isDebugEnabled()) {
081: logger.debug("Message did not match any routers on: "
082: + session.getService().getName()
083: + " invoking catch all strategy");
084: }
085: return catchAll(message, session, synchronous);
086: } else if (!matchfound) {
087: logger
088: .warn("Message did not match any routers on: "
089: + session.getService().getName()
090: + " and there is no catch all strategy configured on this router. Disposing message "
091: + message);
092: }
093: return message;
094: }
095:
096: protected MuleMessage catchAll(MuleMessage message,
097: MuleSession session, boolean synchronous)
098: throws RoutingException {
099: if (getStatistics().isEnabled()) {
100: getStatistics().incrementCaughtMessage();
101: }
102:
103: return getCatchAllStrategy().catchMessage(message, session,
104: synchronous);
105: }
106:
107: public boolean hasEndpoints() {
108: for (Iterator iterator = routers.iterator(); iterator.hasNext();) {
109: OutboundRouter router = (OutboundRouter) iterator.next();
110: if (router.getEndpoints().size() > 0
111: || router.isDynamicEndpoints()) {
112: return true;
113: }
114: }
115: return false;
116: }
117:
118: }
|