KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > mule > routing > response > ResponseMessageRouter


1 /*
2  * $Id: ResponseMessageRouter.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.response;
12
13 import java.util.Iterator JavaDoc;
14 import java.util.List JavaDoc;
15
16 import org.apache.commons.logging.Log;
17 import org.apache.commons.logging.LogFactory;
18 import org.mule.config.MuleConfiguration;
19 import org.mule.management.stats.RouterStatistics;
20 import org.mule.routing.AbstractRouterCollection;
21 import org.mule.umo.UMOEvent;
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.UMOResponseMessageRouter;
26 import org.mule.umo.routing.UMOResponseRouter;
27 import org.mule.umo.routing.UMORouter;
28
29 import edu.emory.mathcs.backport.java.util.concurrent.CopyOnWriteArrayList;
30
31 /**
32  * <code>ResponseMessageRouter</code> is a router that can be used to control how
33  * the response in a request/response message flow is created. Main usecase is to
34  * aggregate a set of asynchonous events into a single response
35  *
36  * @author <a HREF="mailto:ross.mason@symphonysoft.com">Ross Mason</a>
37  * @version $Revision: 3798 $
38  */

39 public class ResponseMessageRouter extends AbstractRouterCollection implements UMOResponseMessageRouter
40 {
41     /**
42      * logger used by this class
43      */

44     protected static Log logger = LogFactory.getLog(ResponseMessageRouter.class);
45
46     private List JavaDoc endpoints = new CopyOnWriteArrayList();
47
48     private int timeout = MuleConfiguration.DEFAULT_TIMEOUT;
49
50     public ResponseMessageRouter()
51     {
52         super(RouterStatistics.TYPE_RESPONSE);
53     }
54
55     public void route(UMOEvent event) throws RoutingException
56     {
57         UMOResponseRouter router = null;
58         for (Iterator JavaDoc iterator = getRouters().iterator(); iterator.hasNext();)
59         {
60             router = (UMOResponseRouter)iterator.next();
61             router.process(event);
62             // Update stats
63
if (getStatistics().isEnabled())
64             {
65                 getStatistics().incrementRoutedMessage(event.getEndpoint());
66             }
67         }
68     }
69
70     public UMOMessage getResponse(UMOMessage message) throws RoutingException
71     {
72         UMOMessage result = null;
73         if (routers.size() == 0)
74         {
75             logger.warn("There are no routers configured on the response router. Returning the current message");
76             result = message;
77         }
78         else
79         {
80             UMOResponseRouter router = null;
81             for (Iterator JavaDoc iterator = getRouters().iterator(); iterator.hasNext();)
82             {
83                 router = (UMOResponseRouter)iterator.next();
84                 result = router.getResponse(message);
85             }
86
87             if (result == null)
88             {
89                 // Update stats
90
if (getStatistics().isEnabled())
91                 {
92                     getStatistics().incrementNoRoutedMessage();
93                 }
94             }
95         }
96
97         // if (result != null && transformer != null) {
98
// try {
99
// result = new MuleMessage(transformer.transform(result.getPayload()),
100
// result.getProperties());
101
// } catch (TransformerException e) {
102
// throw new RoutingException(result, null);
103
// }
104
// }
105
return result;
106
107     }
108
109     public void addRouter(UMORouter router)
110     {
111         ((UMOResponseRouter)router).setTimeout(getTimeout());
112         routers.add(router);
113     }
114
115     public UMOResponseRouter removeRouter(UMOResponseRouter router)
116     {
117         if (routers.remove(router))
118         {
119             return router;
120         }
121         else
122         {
123             return null;
124         }
125     }
126
127     public void addEndpoint(UMOEndpoint endpoint)
128     {
129         if (endpoint != null)
130         {
131             endpoint.setType(UMOEndpoint.ENDPOINT_TYPE_RESPONSE);
132             endpoints.add(endpoint);
133         }
134         else
135         {
136             throw new NullPointerException JavaDoc("endpoint = null");
137         }
138     }
139
140     public boolean removeEndpoint(UMOEndpoint endpoint)
141     {
142         return endpoints.remove(endpoint);
143     }
144
145     public List JavaDoc getEndpoints()
146     {
147         return endpoints;
148     }
149
150     public void setEndpoints(List JavaDoc endpoints)
151     {
152         if (endpoints != null)
153         {
154             this.endpoints.clear();
155             this.endpoints.addAll(endpoints);
156
157             // Force all endpoints' type to RESPONSE just in case.
158
for (Iterator JavaDoc it = this.endpoints.iterator(); it.hasNext();)
159             {
160                 ((UMOEndpoint)it.next()).setType(UMOEndpoint.ENDPOINT_TYPE_RESPONSE);
161             }
162         }
163         else
164         {
165             throw new NullPointerException JavaDoc("List of endpoints = null");
166         }
167     }
168
169     /**
170      * @param name the Endpoint identifier
171      * @return the Endpoint or null if the endpointUri is not registered
172      * @see org.mule.umo.routing.UMOInboundMessageRouter
173      */

174     public UMOEndpoint getEndpoint(String JavaDoc name)
175     {
176         UMOEndpoint endpointDescriptor;
177         for (Iterator JavaDoc iterator = endpoints.iterator(); iterator.hasNext();)
178         {
179             endpointDescriptor = (UMOEndpoint)iterator.next();
180             if (endpointDescriptor.getName().equals(name))
181             {
182                 return endpointDescriptor;
183             }
184         }
185         return null;
186     }
187
188     public int getTimeout()
189     {
190         return timeout;
191     }
192
193     public void setTimeout(int timeout)
194     {
195         this.timeout = timeout;
196     }
197
198 }
199
Popular Tags