KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > mule > routing > outbound > OutboundMessageRouter


1 /*
2  * $Id: OutboundMessageRouter.java 3798 2006-11-04 04:07:14Z aperepel $
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.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 JavaDoc;
28 import java.util.Iterator JavaDoc;
29 import java.util.List JavaDoc;
30
31 /**
32  * <code>OutboundMessageRouter</code> is a container of routers. An
33  * OutboundMessageRouter must have atleast one router. By default the first matching
34  * router is used to route an event though it is possible to match on all routers
35  * meaning that the message will get sent over all matching routers.
36  *
37  * @author <a HREF="mailto:ross.mason@symphonysoft.com">Ross Mason </a>
38  * @version $Revision: 3798 $
39  */

40
41 public class OutboundMessageRouter extends AbstractRouterCollection implements UMOOutboundMessageRouter
42 {
43
44     /**
45      * logger used by this class
46      */

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 JavaDoc iterator = getRouters().iterator(); iterator.hasNext();)
62         {
63             UMOOutboundRouter umoOutboundRouter = (UMOOutboundRouter)iterator.next();
64             if (umoOutboundRouter.isMatch(message))
65             {
66                 matchfound = true;
67                 // Manage outbound only transactions here
68
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 JavaDoc doInTransaction() throws Exception JavaDoc
75                     {
76                         return router.route(message, session, synchronous);
77                     }
78                 };
79                 try
80                 {
81                     result = (UMOMessage)tt.execute(cb);
82                 }
83                 catch (Exception JavaDoc 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     /**
115      * A helper method for finding out which endpoints a message would be routed to
116      * without actually routing the the message
117      *
118      * @param message the message to retrieve endpoints for
119      * @return an array of UMOEndpoint objects or an empty array
120      * @throws RoutingException
121      */

122     public UMOEndpoint[] getEndpointsForMessage(UMOMessage message) throws MessagingException
123     {
124         List JavaDoc endpoints = new ArrayList JavaDoc();
125         for (Iterator JavaDoc 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 JavaDoc 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