View Javadoc

1   /*
2    * $Id: DefaultResponseRouterCollection.java 11998 2008-06-10 16:51:33Z rossmason $
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 CPAL v1.0
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 org.mule.api.MuleEvent;
14  import org.mule.api.MuleMessage;
15  import org.mule.api.endpoint.ImmutableEndpoint;
16  import org.mule.api.endpoint.InboundEndpoint;
17  import org.mule.api.endpoint.InvalidEndpointTypeException;
18  import org.mule.api.lifecycle.InitialisationException;
19  import org.mule.api.routing.ResponseRouter;
20  import org.mule.api.routing.ResponseRouterCollection;
21  import org.mule.api.routing.Router;
22  import org.mule.api.routing.RoutingException;
23  import org.mule.config.i18n.CoreMessages;
24  import org.mule.management.stats.RouterStatistics;
25  import org.mule.routing.AbstractRouterCollection;
26  
27  import java.util.Iterator;
28  import java.util.List;
29  
30  import edu.emory.mathcs.backport.java.util.concurrent.CopyOnWriteArrayList;
31  
32  /**
33   * <code>DefaultResponseRouterCollection</code> is a router that can be used to control how
34   * the response in a request/response message flow is created. Main usecase is to
35   * aggregate a set of asynchonous events into a single response
36   */
37  public class DefaultResponseRouterCollection extends AbstractRouterCollection implements ResponseRouterCollection
38  {
39      private volatile List endpoints = new CopyOnWriteArrayList();
40      private volatile int timeout = -1; // undefined
41      private volatile boolean failOnTimeout = true;
42  
43      public DefaultResponseRouterCollection()
44      {
45          super(RouterStatistics.TYPE_RESPONSE);
46      }
47  
48  
49      public void initialise() throws InitialisationException
50      {
51          if (timeout == -1) // undefined
52          {
53              setTimeout(muleContext.getConfiguration().getDefaultSynchronousEventTimeout());
54          }
55          super.initialise();
56      }
57  
58      public void route(MuleEvent event) throws RoutingException
59      {
60          ResponseRouter router;
61          for (Iterator iterator = getRouters().iterator(); iterator.hasNext();)
62          {
63              router = (ResponseRouter) iterator.next();
64              router.process(event);
65              // Update stats
66              if (getStatistics().isEnabled())
67              {
68                  getStatistics().incrementRoutedMessage(event.getEndpoint());
69              }
70          }
71      }
72  
73      public MuleMessage getResponse(MuleMessage message) throws RoutingException
74      {
75          MuleMessage result = null;
76          if (routers.size() == 0)
77          {
78              if(logger.isDebugEnabled())
79              {
80                  logger.error("There are no routers configured on the response router. Returning the current message");
81              }
82              result = message;
83          }
84          else
85          {
86              ResponseRouter router;
87              for (Iterator iterator = getRouters().iterator(); iterator.hasNext();)
88              {
89                  router = (ResponseRouter) iterator.next();
90                  result = router.getResponse(message);
91              }
92  
93              if (result == null)
94              {
95                  // Update stats
96                  if (getStatistics().isEnabled())
97                  {
98                      getStatistics().incrementNoRoutedMessage();
99                  }
100             }
101         }
102 
103         return result;
104 
105     }
106 
107     public void addRouter(Router router)
108     {
109         ((ResponseRouter) router).setTimeout(getTimeout());
110         ((ResponseRouter) router).setFailOnTimeout(isFailOnTimeout());
111         routers.add(router);
112     }
113 
114     public ResponseRouter removeRouter(ResponseRouter router)
115     {
116         if (routers.remove(router))
117         {
118             return router;
119         }
120         else
121         {
122             return null;
123         }
124     }
125 
126     public void addEndpoint(InboundEndpoint endpoint)
127     {
128         if (endpoint != null)
129         {
130             endpoints.add(endpoint);
131         }
132         else
133         {
134             throw new IllegalArgumentException("endpoint = null");
135         }
136     }
137 
138     public boolean removeEndpoint(InboundEndpoint endpoint)
139     {
140         return endpoints.remove(endpoint);
141     }
142 
143     public List getEndpoints()
144     {
145         return endpoints;
146     }
147 
148     public void setEndpoints(List endpoints)
149     {
150         if (endpoints != null)
151         {
152             this.endpoints.clear();
153             // Ensure all endpoints are response endpoints
154 			// This will go when we start dropping suport for 1.4 and start using 1.5
155             for (Iterator it = endpoints.iterator(); it.hasNext();)
156             {
157                 ImmutableEndpoint endpoint=(ImmutableEndpoint) it.next();
158                 if (!(endpoint instanceof InboundEndpoint))
159                 {
160                     throw new InvalidEndpointTypeException(CoreMessages.responseRouterMustUseInboundEndpoints(
161                         this, endpoint));
162                 }
163             }
164             this.endpoints.addAll(endpoints);
165         }
166         else
167         {
168             throw new IllegalArgumentException("List of endpoints = null");
169         }
170     }
171 
172     /**
173      * @param name the Endpoint identifier
174      * @return the Endpoint or null if the endpointUri is not registered
175      * @see org.mule.api.routing.InboundRouterCollection
176      */
177     public InboundEndpoint getEndpoint(String name)
178     {
179         InboundEndpoint endpointDescriptor;
180         for (Iterator iterator = endpoints.iterator(); iterator.hasNext();)
181         {
182             endpointDescriptor = (InboundEndpoint) iterator.next();
183             if (endpointDescriptor.getName().equals(name))
184             {
185                 return endpointDescriptor;
186             }
187         }
188         return null;
189     }
190 
191     public int getTimeout()
192     {
193         return timeout;
194     }
195 
196     public void setTimeout(int timeout)
197     {
198         this.timeout = timeout;
199     }
200 
201 
202     public boolean isFailOnTimeout()
203     {
204         return failOnTimeout;
205     }
206 
207     public void setFailOnTimeout(boolean failOnTimeout)
208     {
209         this.failOnTimeout = failOnTimeout;
210     }
211 
212 
213     public boolean hasEndpoints()
214     {
215         return !getEndpoints().isEmpty();
216     }
217 }