1 10 11 package org.mule.routing.inbound; 12 13 import edu.emory.mathcs.backport.java.util.concurrent.CopyOnWriteArrayList; 14 import org.apache.commons.logging.Log; 15 import org.apache.commons.logging.LogFactory; 16 import org.mule.config.MuleProperties; 17 import org.mule.management.stats.RouterStatistics; 18 import org.mule.routing.AbstractRouterCollection; 19 import org.mule.umo.MessagingException; 20 import org.mule.umo.UMOEvent; 21 import org.mule.umo.UMOException; 22 import org.mule.umo.UMOMessage; 23 import org.mule.umo.endpoint.UMOEndpoint; 24 import org.mule.umo.routing.RoutingException; 25 import org.mule.umo.routing.UMOInboundMessageRouter; 26 import org.mule.umo.routing.UMOInboundRouter; 27 import org.mule.util.StringMessageUtils; 28 import org.mule.util.StringUtils; 29 30 import java.util.Iterator ; 31 import java.util.List ; 32 33 42 43 public class InboundMessageRouter extends AbstractRouterCollection implements UMOInboundMessageRouter 44 { 45 48 protected static Log logger = LogFactory.getLog(InboundMessageRouter.class); 49 50 private List endpoints = new CopyOnWriteArrayList(); 51 52 public InboundMessageRouter() 53 { 54 super(RouterStatistics.TYPE_INBOUND); 55 } 56 57 public UMOMessage route(UMOEvent event) throws MessagingException 58 { 59 String inboundEndpoint = event.getEndpoint().getName(); 60 if (StringUtils.isBlank(inboundEndpoint)) { 61 inboundEndpoint = event.getEndpoint().getEndpointURI().getAddress(); 62 } 63 event.getMessage().setProperty(MuleProperties.MULE_ORIGINATING_ENDPOINT_PROPERTY, inboundEndpoint); 64 65 if (endpoints.size() > 0 && routers.size() == 0) 66 { 67 addRouter(new InboundPassThroughRouter()); 68 } 69 70 String componentName = event.getSession().getComponent().getDescriptor().getName(); 71 72 UMOEvent[] eventsToRoute = null; 73 boolean noRoute = true; 74 boolean match = false; 75 UMOInboundRouter umoInboundRouter = null; 76 77 for (Iterator iterator = getRouters().iterator(); iterator.hasNext();) 78 { 79 umoInboundRouter = (UMOInboundRouter)iterator.next(); 80 81 if (umoInboundRouter.isMatch(event)) 82 { 83 match = true; 84 eventsToRoute = umoInboundRouter.process(event); 85 noRoute = (eventsToRoute == null); 86 if (!matchAll) 87 { 88 break; 89 } 90 } 91 } 92 93 if (!event.isStopFurtherProcessing()) 98 { 99 if (noRoute) 100 { 101 if (getStatistics().isEnabled()) 103 { 104 getStatistics().incrementNoRoutedMessage(); 105 } 106 if (!match) 107 { 108 if (getCatchAllStrategy() != null) 109 { 110 if (logger.isDebugEnabled()) 111 { 112 logger.debug("Message did not match any routers on: " + componentName 113 + " - invoking catch all strategy"); 114 } 115 if (getStatistics().isEnabled()) 116 { 117 getStatistics().incrementCaughtMessage(); 118 } 119 return getCatchAllStrategy().catchMessage(event.getMessage(), event.getSession(), 120 event.isSynchronous()); 121 122 } 123 else 124 { 125 logger.warn("Message did not match any routers on: " 126 + componentName 127 + " and there is no catch all strategy configured on this router. Disposing message."); 128 if (logger.isDebugEnabled()) 129 { 130 try 131 { 132 logger.warn("Message fragment is: " 133 + StringMessageUtils.truncate(event.getMessageAsString(), 100, 134 true)); 135 } 136 catch (UMOException e) 137 { 138 } 140 } 141 } 142 } 143 } 144 else 145 { 146 try 147 { 148 UMOMessage messageResult = null; 149 if (eventsToRoute != null) 150 { 151 for (int i = 0; i < eventsToRoute.length; i++) 152 { 153 if (event.getMessage().getProperty(MuleProperties.MULE_ORIGINATING_ENDPOINT_PROPERTY) == null) { 155 event.getMessage().setProperty(MuleProperties.MULE_ORIGINATING_ENDPOINT_PROPERTY, inboundEndpoint); 156 } 157 158 if (event.isSynchronous()) 159 { 160 messageResult = send(eventsToRoute[i]); 161 } 162 else 163 { 164 dispatch(eventsToRoute[i]); 165 } 166 if (getStatistics().isEnabled()) 168 { 169 getStatistics().incrementRoutedMessage(eventsToRoute[i].getEndpoint()); 170 } 171 } 172 } 173 return messageResult; 174 } 175 catch (UMOException e) 176 { 177 throw new RoutingException(event.getMessage(), event.getEndpoint(), e); 178 } 179 } 180 } 181 return (eventsToRoute != null && eventsToRoute.length > 0 182 ? eventsToRoute[eventsToRoute.length - 1].getMessage() : null); 183 184 } 185 186 public void dispatch(UMOEvent event) throws UMOException 187 { 188 event.getSession().dispatchEvent(event); 189 } 190 191 public UMOMessage send(UMOEvent event) throws UMOException 192 { 193 194 return event.getSession().sendEvent(event); 195 } 196 197 public void addRouter(UMOInboundRouter router) 198 { 199 routers.add(router); 200 } 201 202 public UMOInboundRouter removeRouter(UMOInboundRouter router) 203 { 204 if (routers.remove(router)) 205 { 206 return router; 207 } 208 else 209 { 210 return null; 211 } 212 } 213 214 public void addEndpoint(UMOEndpoint endpoint) 215 { 216 if (endpoint != null) 217 { 218 endpoint.setType(UMOEndpoint.ENDPOINT_TYPE_RECEIVER); 219 endpoints.add(endpoint); 220 } 221 else 222 { 223 throw new NullPointerException ("endpoint = null"); 224 } 225 } 226 227 public boolean removeEndpoint(UMOEndpoint endpoint) 228 { 229 return endpoints.remove(endpoint); 230 } 231 232 public List getEndpoints() 233 { 234 return endpoints; 235 } 236 237 public void setEndpoints(List endpoints) 238 { 239 if (endpoints != null) 240 { 241 this.endpoints.clear(); 242 this.endpoints.addAll(endpoints); 243 244 for (Iterator it = this.endpoints.iterator(); it.hasNext();) 246 { 247 ((UMOEndpoint)it.next()).setType(UMOEndpoint.ENDPOINT_TYPE_RECEIVER); 248 } 249 } 250 else 251 { 252 throw new NullPointerException ("List of endpoints = null"); 253 } 254 } 255 256 261 public UMOEndpoint getEndpoint(String name) 262 { 263 UMOEndpoint endpointDescriptor; 264 for (Iterator iterator = endpoints.iterator(); iterator.hasNext();) 265 { 266 endpointDescriptor = (UMOEndpoint)iterator.next(); 267 if (endpointDescriptor.getName().equals(name)) 268 { 269 return endpointDescriptor; 270 } 271 } 272 return null; 273 } 274 } 275 | Popular Tags |