Coverage Report - org.mule.routing.response.DefaultResponseRouterCollection
 
Classes in this File Line Coverage Branch Coverage Complexity
DefaultResponseRouterCollection
29%
18/62
19%
6/32
2.25
 
 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  418
     private volatile List endpoints = new CopyOnWriteArrayList();
 40  418
     private volatile int timeout = -1; // undefined
 41  418
     private volatile boolean failOnTimeout = true;
 42  
 
 43  
     public DefaultResponseRouterCollection()
 44  
     {
 45  418
         super(RouterStatistics.TYPE_RESPONSE);
 46  418
     }
 47  
 
 48  
 
 49  
     public void initialise() throws InitialisationException
 50  
     {
 51  0
         if (timeout == -1) // undefined
 52  
         {
 53  0
             setTimeout(muleContext.getConfiguration().getDefaultSynchronousEventTimeout());
 54  
         }
 55  0
         super.initialise();
 56  0
     }
 57  
 
 58  
     public void route(MuleEvent event) throws RoutingException
 59  
     {
 60  
         ResponseRouter router;
 61  0
         for (Iterator iterator = getRouters().iterator(); iterator.hasNext();)
 62  
         {
 63  0
             router = (ResponseRouter) iterator.next();
 64  0
             router.process(event);
 65  
             // Update stats
 66  0
             if (getStatistics().isEnabled())
 67  
             {
 68  0
                 getStatistics().incrementRoutedMessage(event.getEndpoint());
 69  
             }
 70  
         }
 71  0
     }
 72  
 
 73  
     public MuleMessage getResponse(MuleMessage message) throws RoutingException
 74  
     {
 75  0
         MuleMessage result = null;
 76  0
         if (routers.size() == 0)
 77  
         {
 78  0
             if(logger.isDebugEnabled())
 79  
             {
 80  0
                 logger.error("There are no routers configured on the response router. Returning the current message");
 81  
             }
 82  0
             result = message;
 83  
         }
 84  
         else
 85  
         {
 86  
             ResponseRouter router;
 87  0
             for (Iterator iterator = getRouters().iterator(); iterator.hasNext();)
 88  
             {
 89  0
                 router = (ResponseRouter) iterator.next();
 90  0
                 result = router.getResponse(message);
 91  
             }
 92  
 
 93  0
             if (result == null)
 94  
             {
 95  
                 // Update stats
 96  0
                 if (getStatistics().isEnabled())
 97  
                 {
 98  0
                     getStatistics().incrementNoRoutedMessage();
 99  
                 }
 100  
             }
 101  
         }
 102  
 
 103  0
         return result;
 104  
 
 105  
     }
 106  
 
 107  
     public void addRouter(Router router)
 108  
     {
 109  0
         ((ResponseRouter) router).setTimeout(getTimeout());
 110  0
         ((ResponseRouter) router).setFailOnTimeout(isFailOnTimeout());
 111  0
         routers.add(router);
 112  0
     }
 113  
 
 114  
     public ResponseRouter removeRouter(ResponseRouter router)
 115  
     {
 116  0
         if (routers.remove(router))
 117  
         {
 118  0
             return router;
 119  
         }
 120  
         else
 121  
         {
 122  0
             return null;
 123  
         }
 124  
     }
 125  
 
 126  
     public void addEndpoint(InboundEndpoint endpoint)
 127  
     {
 128  4
         if (endpoint != null)
 129  
         {
 130  4
             endpoints.add(endpoint);
 131  
         }
 132  
         else
 133  
         {
 134  0
             throw new IllegalArgumentException("endpoint = null");
 135  
         }
 136  4
     }
 137  
 
 138  
     public boolean removeEndpoint(InboundEndpoint endpoint)
 139  
     {
 140  0
         return endpoints.remove(endpoint);
 141  
     }
 142  
 
 143  
     public List getEndpoints()
 144  
     {
 145  182
         return endpoints;
 146  
     }
 147  
 
 148  
     public void setEndpoints(List endpoints)
 149  
     {
 150  6
         if (endpoints != null)
 151  
         {
 152  6
             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  6
             for (Iterator it = endpoints.iterator(); it.hasNext();)
 156  
             {
 157  12
                 ImmutableEndpoint endpoint=(ImmutableEndpoint) it.next();
 158  12
                 if (!(endpoint instanceof InboundEndpoint))
 159  
                 {
 160  4
                     throw new InvalidEndpointTypeException(CoreMessages.responseRouterMustUseInboundEndpoints(
 161  
                         this, endpoint));
 162  
                 }
 163  8
             }
 164  2
             this.endpoints.addAll(endpoints);
 165  
         }
 166  
         else
 167  
         {
 168  0
             throw new IllegalArgumentException("List of endpoints = null");
 169  
         }
 170  2
     }
 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  0
         for (Iterator iterator = endpoints.iterator(); iterator.hasNext();)
 181  
         {
 182  0
             endpointDescriptor = (InboundEndpoint) iterator.next();
 183  0
             if (endpointDescriptor.getName().equals(name))
 184  
             {
 185  0
                 return endpointDescriptor;
 186  
             }
 187  
         }
 188  0
         return null;
 189  
     }
 190  
 
 191  
     public int getTimeout()
 192  
     {
 193  0
         return timeout;
 194  
     }
 195  
 
 196  
     public void setTimeout(int timeout)
 197  
     {
 198  0
         this.timeout = timeout;
 199  0
     }
 200  
 
 201  
 
 202  
     public boolean isFailOnTimeout()
 203  
     {
 204  0
         return failOnTimeout;
 205  
     }
 206  
 
 207  
     public void setFailOnTimeout(boolean failOnTimeout)
 208  
     {
 209  0
         this.failOnTimeout = failOnTimeout;
 210  0
     }
 211  
 
 212  
 
 213  
     public boolean hasEndpoints()
 214  
     {
 215  0
         return !getEndpoints().isEmpty();
 216  
     }
 217  
 }