Coverage Report - org.mule.routing.response.ResponseRouterCollection
 
Classes in this File Line Coverage Branch Coverage Complexity
ResponseRouterCollection
0%
0/54
0%
0/12
2.143
 
 1  
 /*
 2  
  * $Id: ResponseRouterCollection.java 7976 2007-08-21 14:26:13Z dirk.olmes $
 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.config.MuleConfiguration;
 14  
 import org.mule.management.stats.RouterStatistics;
 15  
 import org.mule.routing.AbstractRouterCollection;
 16  
 import org.mule.umo.UMOEvent;
 17  
 import org.mule.umo.UMOMessage;
 18  
 import org.mule.umo.endpoint.UMOEndpoint;
 19  
 import org.mule.umo.routing.RoutingException;
 20  
 import org.mule.umo.routing.UMOResponseRouter;
 21  
 import org.mule.umo.routing.UMOResponseRouterCollection;
 22  
 import org.mule.umo.routing.UMORouter;
 23  
 
 24  
 import java.util.Iterator;
 25  
 import java.util.List;
 26  
 
 27  
 import edu.emory.mathcs.backport.java.util.concurrent.CopyOnWriteArrayList;
 28  
 
 29  
 /**
 30  
  * <code>ResponseRouterCollection</code> is a router that can be used to control how
 31  
  * the response in a request/response message flow is created. Main usecase is to
 32  
  * aggregate a set of asynchonous events into a single response
 33  
  */
 34  
 public class ResponseRouterCollection extends AbstractRouterCollection implements UMOResponseRouterCollection
 35  
 {
 36  0
     private volatile List endpoints = new CopyOnWriteArrayList();
 37  0
     private volatile int timeout = MuleConfiguration.DEFAULT_TIMEOUT;
 38  0
     private volatile boolean failOnTimeout = true;
 39  
 
 40  
     public ResponseRouterCollection()
 41  
     {
 42  0
         super(RouterStatistics.TYPE_RESPONSE);
 43  0
     }
 44  
 
 45  
     public void route(UMOEvent event) throws RoutingException
 46  
     {
 47  
         UMOResponseRouter router;
 48  0
         for (Iterator iterator = getRouters().iterator(); iterator.hasNext();)
 49  
         {
 50  0
             router = (UMOResponseRouter) iterator.next();
 51  0
             router.process(event);
 52  
             // Update stats
 53  0
             if (getStatistics().isEnabled())
 54  
             {
 55  0
                 getStatistics().incrementRoutedMessage(event.getEndpoint());
 56  
             }
 57  
         }
 58  0
     }
 59  
 
 60  
     public UMOMessage getResponse(UMOMessage message) throws RoutingException
 61  
     {
 62  0
         UMOMessage result = null;
 63  0
         if (routers.size() == 0)
 64  
         {
 65  0
             logger.warn("There are no routers configured on the response router. Returning the current message");
 66  0
             result = message;
 67  
         }
 68  
         else
 69  
         {
 70  
             UMOResponseRouter router;
 71  0
             for (Iterator iterator = getRouters().iterator(); iterator.hasNext();)
 72  
             {
 73  0
                 router = (UMOResponseRouter) iterator.next();
 74  0
                 result = router.getResponse(message);
 75  
             }
 76  
 
 77  0
             if (result == null)
 78  
             {
 79  
                 // Update stats
 80  0
                 if (getStatistics().isEnabled())
 81  
                 {
 82  0
                     getStatistics().incrementNoRoutedMessage();
 83  
                 }
 84  
             }
 85  
         }
 86  
 
 87  
         // if (result != null && transformer != null) {
 88  
         // try {
 89  
         // result = new MuleMessage(transformer.transform(result.getPayload()),
 90  
         // result.getProperties());
 91  
         // } catch (TransformerException e) {
 92  
         // throw new RoutingException(result, null);
 93  
         // }
 94  
         // }
 95  0
         return result;
 96  
 
 97  
     }
 98  
 
 99  
     public void addRouter(UMORouter router)
 100  
     {
 101  0
         ((UMOResponseRouter) router).setTimeout(getTimeout());
 102  0
         ((UMOResponseRouter) router).setFailOnTimeout(isFailOnTimeout());
 103  0
         routers.add(router);
 104  0
     }
 105  
 
 106  
     public UMOResponseRouter removeRouter(UMOResponseRouter router)
 107  
     {
 108  0
         if (routers.remove(router))
 109  
         {
 110  0
             return router;
 111  
         }
 112  
         else
 113  
         {
 114  0
             return null;
 115  
         }
 116  
     }
 117  
 
 118  
     public void addEndpoint(UMOEndpoint endpoint)
 119  
     {
 120  0
         if (endpoint != null)
 121  
         {
 122  0
             endpoint.setType(UMOEndpoint.ENDPOINT_TYPE_RESPONSE);
 123  0
             endpoints.add(endpoint);
 124  
         }
 125  
         else
 126  
         {
 127  0
             throw new IllegalArgumentException("endpoint = null");
 128  
         }
 129  0
     }
 130  
 
 131  
     public boolean removeEndpoint(UMOEndpoint endpoint)
 132  
     {
 133  0
         return endpoints.remove(endpoint);
 134  
     }
 135  
 
 136  
     public List getEndpoints()
 137  
     {
 138  0
         return endpoints;
 139  
     }
 140  
 
 141  
     public void setEndpoints(List endpoints)
 142  
     {
 143  0
         if (endpoints != null)
 144  
         {
 145  0
             this.endpoints.clear();
 146  0
             this.endpoints.addAll(endpoints);
 147  
 
 148  
             // Force all endpoints' type to RESPONSE just in case.
 149  0
             for (Iterator it = this.endpoints.iterator(); it.hasNext();)
 150  
             {
 151  0
                 ((UMOEndpoint) it.next()).setType(UMOEndpoint.ENDPOINT_TYPE_RESPONSE);
 152  
             }
 153  
         }
 154  
         else
 155  
         {
 156  0
             throw new IllegalArgumentException("List of endpoints = null");
 157  
         }
 158  0
     }
 159  
 
 160  
     /**
 161  
      * @param name the Endpoint identifier
 162  
      * @return the Endpoint or null if the endpointUri is not registered
 163  
      * @see org.mule.umo.routing.UMOInboundRouterCollection
 164  
      */
 165  
     public UMOEndpoint getEndpoint(String name)
 166  
     {
 167  
         UMOEndpoint endpointDescriptor;
 168  0
         for (Iterator iterator = endpoints.iterator(); iterator.hasNext();)
 169  
         {
 170  0
             endpointDescriptor = (UMOEndpoint) iterator.next();
 171  0
             if (endpointDescriptor.getName().equals(name))
 172  
             {
 173  0
                 return endpointDescriptor;
 174  
             }
 175  
         }
 176  0
         return null;
 177  
     }
 178  
 
 179  
     public int getTimeout()
 180  
     {
 181  0
         return timeout;
 182  
     }
 183  
 
 184  
     public void setTimeout(int timeout)
 185  
     {
 186  0
         this.timeout = timeout;
 187  0
     }
 188  
 
 189  
 
 190  
     public boolean isFailOnTimeout()
 191  
     {
 192  0
         return failOnTimeout;
 193  
     }
 194  
 
 195  
     public void setFailOnTimeout(boolean failOnTimeout)
 196  
     {
 197  0
         this.failOnTimeout = failOnTimeout;
 198  0
     }
 199  
 }