KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > mule > routing > inbound > InboundMessageRouter


1 /*
2  * $Id: InboundMessageRouter.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.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 JavaDoc;
31 import java.util.List JavaDoc;
32
33 /**
34  * <code>InboundMessageRouter</code> is a collection of routers that will be
35  * invoked when an event is received It is responsible for manageing a collection of
36  * routers and also executing the routing logic. Each router must match against the
37  * current event for the event to be routed.
38  *
39  * @author <a HREF="mailto:ross.mason@symphonysoft.com">Ross Mason </a>
40  * @version $Revision: 4219 $
41  */

42
43 public class InboundMessageRouter extends AbstractRouterCollection implements UMOInboundMessageRouter
44 {
45     /**
46      * logger used by this class
47      */

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 JavaDoc 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 JavaDoc 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 JavaDoc 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 the stopFurtherProcessing flag has been set
94
// do not route events to the component.
95
// This is the case when using a ForwardingConsumer
96
// inbound router for example.
97
if (!event.isStopFurtherProcessing())
98         {
99             if (noRoute)
100             {
101                 // Update stats
102
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                                 // ignore
139
}
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                             // Set the originating endpoint so we'll know where this event came from further down the pipeline.
154
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                             // Update stats
167
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 JavaDoc("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             // Force all endpoints' type to RECEIVER just in case.
245
for (Iterator JavaDoc 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 JavaDoc("List of endpoints = null");
253         }
254     }
255
256     /**
257      * @param name the Endpoint identifier
258      * @return the Endpoint or null if the endpointUri is not registered
259      * @see org.mule.umo.routing.UMOInboundMessageRouter
260      */

261     public UMOEndpoint getEndpoint(String JavaDoc name)
262     {
263         UMOEndpoint endpointDescriptor;
264         for (Iterator JavaDoc 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