1 10 11 package org.mule.routing.outbound; 12 13 import org.apache.commons.logging.Log; 14 import org.apache.commons.logging.LogFactory; 15 import org.mule.management.stats.RouterStatistics; 16 import org.mule.routing.AbstractRouterCollection; 17 import org.mule.transaction.TransactionCallback; 18 import org.mule.transaction.TransactionTemplate; 19 import org.mule.umo.MessagingException; 20 import org.mule.umo.UMOMessage; 21 import org.mule.umo.UMOSession; 22 import org.mule.umo.endpoint.UMOEndpoint; 23 import org.mule.umo.routing.RoutingException; 24 import org.mule.umo.routing.UMOOutboundMessageRouter; 25 import org.mule.umo.routing.UMOOutboundRouter; 26 27 import java.util.ArrayList ; 28 import java.util.Iterator ; 29 import java.util.List ; 30 31 40 41 public class OutboundMessageRouter extends AbstractRouterCollection implements UMOOutboundMessageRouter 42 { 43 44 47 protected static Log logger = LogFactory.getLog(OutboundMessageRouter.class); 48 49 public OutboundMessageRouter() 50 { 51 super(RouterStatistics.TYPE_OUTBOUND); 52 } 53 54 public UMOMessage route(final UMOMessage message, final UMOSession session, final boolean synchronous) 55 throws MessagingException 56 { 57 58 UMOMessage result; 59 boolean matchfound = false; 60 61 for (Iterator iterator = getRouters().iterator(); iterator.hasNext();) 62 { 63 UMOOutboundRouter umoOutboundRouter = (UMOOutboundRouter)iterator.next(); 64 if (umoOutboundRouter.isMatch(message)) 65 { 66 matchfound = true; 67 final UMOOutboundRouter router = umoOutboundRouter; 69 TransactionTemplate tt = new TransactionTemplate(umoOutboundRouter.getTransactionConfig(), 70 session.getComponent().getDescriptor().getExceptionListener()); 71 72 TransactionCallback cb = new TransactionCallback() 73 { 74 public Object doInTransaction() throws Exception 75 { 76 return router.route(message, session, synchronous); 77 } 78 }; 79 try 80 { 81 result = (UMOMessage)tt.execute(cb); 82 } 83 catch (Exception e) 84 { 85 throw new RoutingException(message, null, e); 86 } 87 88 if (!isMatchAll()) 89 { 90 return result; 91 } 92 } 93 } 94 95 if (!matchfound && getCatchAllStrategy() != null) 96 { 97 if (logger.isDebugEnabled()) 98 { 99 logger.debug("Message did not match any routers on: " 100 + session.getComponent().getDescriptor().getName() 101 + " invoking catch all strategy"); 102 } 103 return catchAll(message, session, synchronous); 104 } 105 else if (!matchfound) 106 { 107 logger.warn("Message did not match any routers on: " 108 + session.getComponent().getDescriptor().getName() 109 + " and there is no catch all strategy configured on this router. Disposing message."); 110 } 111 return message; 112 } 113 114 122 public UMOEndpoint[] getEndpointsForMessage(UMOMessage message) throws MessagingException 123 { 124 List endpoints = new ArrayList (); 125 for (Iterator iterator = getRouters().iterator(); iterator.hasNext();) 126 { 127 UMOOutboundRouter umoOutboundRouter = (UMOOutboundRouter)iterator.next(); 128 if (umoOutboundRouter.isMatch(message)) 129 { 130 endpoints.addAll(umoOutboundRouter.getEndpoints()); 131 if (!isMatchAll()) 132 { 133 break; 134 } 135 } 136 } 137 138 UMOEndpoint[] result = new UMOEndpoint[endpoints.size()]; 139 return (UMOEndpoint[])endpoints.toArray(result); 140 } 141 142 protected UMOMessage catchAll(UMOMessage message, UMOSession session, boolean synchronous) 143 throws RoutingException 144 { 145 if (getStatistics().isEnabled()) 146 { 147 getStatistics().incrementCaughtMessage(); 148 } 149 150 return getCatchAllStrategy().catchMessage(message, session, synchronous); 151 } 152 153 public boolean hasEndpoints() 154 { 155 for (Iterator iterator = routers.iterator(); iterator.hasNext();) 156 { 157 UMOOutboundRouter router = (UMOOutboundRouter)iterator.next(); 158 if (router.getEndpoints().size() > 0 || router.isDynamicEndpoints()) 159 { 160 return true; 161 } 162 } 163 return false; 164 } 165 166 } 167 | Popular Tags |