Coverage Report - org.mule.routing.AbstractSelectiveRouter
 
Classes in this File Line Coverage Branch Coverage Complexity
AbstractSelectiveRouter
0%
0/111
0%
0/64
0
AbstractSelectiveRouter$1
0%
0/4
N/A
0
AbstractSelectiveRouter$2
0%
0/5
N/A
0
AbstractSelectiveRouter$RoutesUpdater
N/A
N/A
0
 
 1  
 /*
 2  
  * $Id: AbstractSelectiveRouter.java 20592 2010-12-10 03:55:14Z mike.schilling $
 3  
  * --------------------------------------------------------------------------------------
 4  
  * Copyright (c) MuleSoft, Inc.  All rights reserved.  http://www.mulesoft.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;
 12  
 
 13  
 import java.util.ArrayList;
 14  
 import java.util.Collection;
 15  
 import java.util.Collections;
 16  
 import java.util.List;
 17  
 
 18  
 import org.apache.commons.collections.ListUtils;
 19  
 import org.mule.DefaultMuleEvent;
 20  
 import org.mule.api.MuleContext;
 21  
 import org.mule.api.MuleEvent;
 22  
 import org.mule.api.MuleException;
 23  
 import org.mule.api.MuleRuntimeException;
 24  
 import org.mule.api.construct.FlowConstruct;
 25  
 import org.mule.api.construct.FlowConstructAware;
 26  
 import org.mule.api.context.MuleContextAware;
 27  
 import org.mule.api.endpoint.OutboundEndpoint;
 28  
 import org.mule.api.lifecycle.Disposable;
 29  
 import org.mule.api.lifecycle.Initialisable;
 30  
 import org.mule.api.lifecycle.InitialisationException;
 31  
 import org.mule.api.lifecycle.Lifecycle;
 32  
 import org.mule.api.lifecycle.Startable;
 33  
 import org.mule.api.lifecycle.Stoppable;
 34  
 import org.mule.api.processor.MessageProcessor;
 35  
 import org.mule.api.routing.RoutePathNotFoundException;
 36  
 import org.mule.api.routing.RouterResultsHandler;
 37  
 import org.mule.api.routing.RouterStatisticsRecorder;
 38  
 import org.mule.api.routing.SelectiveRouter;
 39  
 import org.mule.api.routing.filter.Filter;
 40  
 import org.mule.config.i18n.MessageFactory;
 41  
 import org.mule.management.stats.RouterStatistics;
 42  
 
 43  
 import edu.emory.mathcs.backport.java.util.concurrent.atomic.AtomicBoolean;
 44  
 
 45  0
 public abstract class AbstractSelectiveRouter
 46  
     implements SelectiveRouter, RouterStatisticsRecorder, Lifecycle, FlowConstructAware, MuleContextAware
 47  
 {
 48  0
     private final List<MessageProcessorFilterPair> conditionalMessageProcessors = new ArrayList<MessageProcessorFilterPair>();
 49  
     private MessageProcessor defaultProcessor;
 50  0
     private final RouterResultsHandler resultsHandler = new DefaultRouterResultsHandler();
 51  
     private RouterStatistics routerStatistics;
 52  
 
 53  0
     final AtomicBoolean initialised = new AtomicBoolean(false);
 54  0
     final AtomicBoolean starting = new AtomicBoolean(false);
 55  0
     final AtomicBoolean started = new AtomicBoolean(false);
 56  
     private FlowConstruct flowConstruct;
 57  
     private MuleContext muleContext;
 58  
 
 59  
     public AbstractSelectiveRouter()
 60  0
     {
 61  0
         routerStatistics = new RouterStatistics(RouterStatistics.TYPE_OUTBOUND);
 62  0
     }
 63  
 
 64  
     public void setFlowConstruct(FlowConstruct flowConstruct)
 65  
     {
 66  0
         this.flowConstruct = flowConstruct;
 67  0
     }
 68  
 
 69  
     public void setMuleContext(MuleContext context)
 70  
     {
 71  0
         this.muleContext = context;
 72  0
     }
 73  
 
 74  
     public void initialise() throws InitialisationException
 75  
     {
 76  0
         synchronized (conditionalMessageProcessors)
 77  
         {
 78  0
             for (Object o : getLifecycleManagedObjects())
 79  
             {
 80  0
                 if (o instanceof FlowConstructAware)
 81  
                 {
 82  0
                     ((FlowConstructAware) o).setFlowConstruct(flowConstruct);
 83  
                 }
 84  0
                 if (o instanceof MuleContextAware)
 85  
                 {
 86  0
                     ((MuleContextAware) o).setMuleContext(muleContext);
 87  
                 }
 88  0
                 if (o instanceof Initialisable)
 89  
                 {
 90  0
                     ((Initialisable) o).initialise();
 91  
                 }
 92  
             }
 93  0
         }
 94  0
         initialised.set(true);
 95  0
     }
 96  
 
 97  
     public void start() throws MuleException
 98  
     {
 99  0
         synchronized (conditionalMessageProcessors)
 100  
         {
 101  0
             starting.set(true);
 102  0
             for (Object o : getLifecycleManagedObjects())
 103  
             {
 104  0
                 if (o instanceof Startable)
 105  
                 {
 106  0
                     ((Startable) o).start();
 107  
                 }
 108  
             }
 109  
 
 110  0
             started.set(true);
 111  0
             starting.set(false);
 112  0
         }
 113  0
     }
 114  
 
 115  
     public void stop() throws MuleException
 116  
     {
 117  0
         synchronized (conditionalMessageProcessors)
 118  
         {
 119  0
             for (Object o : getLifecycleManagedObjects())
 120  
             {
 121  0
                 if (o instanceof Stoppable)
 122  
                 {
 123  0
                     ((Stoppable) o).stop();
 124  
                 }
 125  
             }
 126  
 
 127  0
             started.set(false);
 128  0
         }
 129  0
     }
 130  
 
 131  
     public void dispose()
 132  
     {
 133  0
         synchronized (conditionalMessageProcessors)
 134  
         {
 135  0
             for (Object o : getLifecycleManagedObjects())
 136  
             {
 137  0
                 if (o instanceof Disposable)
 138  
                 {
 139  0
                     ((Disposable) o).dispose();
 140  
                 }
 141  
             }
 142  0
         }
 143  0
     }
 144  
 
 145  
     public void addRoute(MessageProcessor processor, Filter filter)
 146  
     {
 147  0
         synchronized (conditionalMessageProcessors)
 148  
         {
 149  0
             MessageProcessorFilterPair addedPair = new MessageProcessorFilterPair(processor, filter);
 150  0
             conditionalMessageProcessors.add(transitionLifecycleManagedObjectForAddition(addedPair));
 151  0
         }
 152  0
     }
 153  
 
 154  
     public void removeRoute(MessageProcessor processor)
 155  
     {
 156  0
         updateRoute(processor, new RoutesUpdater()
 157  0
         {
 158  
             public void updateAt(int index)
 159  
             {
 160  0
                 MessageProcessorFilterPair removedPair = conditionalMessageProcessors.remove(index);
 161  
 
 162  0
                 transitionLifecycleManagedObjectForRemoval(removedPair);
 163  0
             }
 164  
         });
 165  0
     }
 166  
 
 167  
     public void updateRoute(final MessageProcessor processor, final Filter filter)
 168  
     {
 169  0
         updateRoute(processor, new RoutesUpdater()
 170  0
         {
 171  
             public void updateAt(int index)
 172  
             {
 173  0
                 MessageProcessorFilterPair addedPair = new MessageProcessorFilterPair(processor, filter);
 174  
 
 175  0
                 MessageProcessorFilterPair removedPair = conditionalMessageProcessors.set(index,
 176  
                     transitionLifecycleManagedObjectForAddition(addedPair));
 177  
 
 178  0
                 transitionLifecycleManagedObjectForRemoval(removedPair);
 179  0
             }
 180  
         });
 181  0
     }
 182  
 
 183  
     public void setDefaultRoute(MessageProcessor processor)
 184  
     {
 185  0
         defaultProcessor = processor;
 186  0
     }
 187  
 
 188  
     public MuleEvent process(MuleEvent event) throws MuleException
 189  
     {
 190  0
         Collection<MessageProcessor> selectedProcessors = selectProcessors(event);
 191  
 
 192  0
         if (!selectedProcessors.isEmpty())
 193  
         {
 194  0
             return routeWithProcessors(selectedProcessors, event);
 195  
         }
 196  
 
 197  0
         if (defaultProcessor != null)
 198  
         {
 199  0
             return routeWithProcessor(defaultProcessor, event);
 200  
         }
 201  
 
 202  0
         if (getRouterStatistics() != null  && getRouterStatistics().isEnabled())
 203  
         {
 204  0
             getRouterStatistics().incrementNoRoutedMessage();
 205  
         }
 206  
 
 207  0
         throw new RoutePathNotFoundException(
 208  
             MessageFactory.createStaticMessage("Can't process message because no route has been found matching any filter and no default route is defined"),
 209  
             event, this);
 210  
     }
 211  
 
 212  
     /**
 213  
      * @return the processors selected according to the specific router strategy or
 214  
      *         an empty collection (not null).
 215  
      */
 216  
     protected abstract Collection<MessageProcessor> selectProcessors(MuleEvent event);
 217  
 
 218  
     private Collection<?> getLifecycleManagedObjects()
 219  
     {
 220  0
         if (defaultProcessor == null)
 221  
         {
 222  0
             return conditionalMessageProcessors;
 223  
         }
 224  
 
 225  0
         return ListUtils.union(conditionalMessageProcessors, Collections.singletonList(defaultProcessor));
 226  
     }
 227  
 
 228  
     private <O> O transitionLifecycleManagedObjectForAddition(O managedObject)
 229  
     {
 230  
         try
 231  
         {
 232  0
             if ((flowConstruct != null) && (managedObject instanceof FlowConstructAware))
 233  
             {
 234  0
                 ((FlowConstructAware) managedObject).setFlowConstruct(flowConstruct);
 235  
             }
 236  
 
 237  0
             if ((muleContext != null) && (managedObject instanceof MuleContextAware))
 238  
             {
 239  0
                 ((MuleContextAware) managedObject).setMuleContext(muleContext);
 240  
             }
 241  
 
 242  0
             if ((initialised.get()) && (managedObject instanceof Initialisable))
 243  
             {
 244  0
                 ((Initialisable) managedObject).initialise();
 245  
             }
 246  
 
 247  0
             if ((started.get()) && (managedObject instanceof Startable))
 248  
             {
 249  0
                 ((Startable) managedObject).start();
 250  
             }
 251  
         }
 252  0
         catch (MuleException me)
 253  
         {
 254  0
             throw new MuleRuntimeException(me);
 255  0
         }
 256  
 
 257  0
         return managedObject;
 258  
     }
 259  
 
 260  
     private <O> O transitionLifecycleManagedObjectForRemoval(O managedObject)
 261  
     {
 262  
         try
 263  
         {
 264  0
             if (managedObject instanceof Stoppable)
 265  
             {
 266  0
                 ((Stoppable) managedObject).stop();
 267  
             }
 268  
 
 269  0
             if (managedObject instanceof Disposable)
 270  
             {
 271  0
                 ((Disposable) managedObject).dispose();
 272  
             }
 273  
         }
 274  0
         catch (MuleException me)
 275  
         {
 276  0
             throw new MuleRuntimeException(me);
 277  0
         }
 278  
 
 279  0
         return managedObject;
 280  
     }
 281  
 
 282  
     private MuleEvent routeWithProcessor(MessageProcessor processor, MuleEvent event) throws MuleException
 283  
     {
 284  0
         return routeWithProcessors(Collections.singleton(processor), event);
 285  
     }
 286  
 
 287  
     private MuleEvent routeWithProcessors(Collection<MessageProcessor> processors, MuleEvent event)
 288  
         throws MuleException
 289  
     {
 290  0
         List<MuleEvent> results = new ArrayList<MuleEvent>();
 291  
 
 292  0
         for (MessageProcessor processor : processors)
 293  
         {
 294  0
             processEventWithProcessor(event, processor, results);
 295  
         }
 296  
 
 297  0
         return resultsHandler.aggregateResults(results, event, event.getMuleContext());
 298  
     }
 299  
 
 300  
     private void processEventWithProcessor(MuleEvent event,
 301  
                                            MessageProcessor processor,
 302  
                                            List<MuleEvent> results) throws MuleException
 303  
     {
 304  0
         MuleEvent processedEvent = event;
 305  
 
 306  0
         if (processor instanceof OutboundEndpoint)
 307  
         {
 308  0
             processedEvent = new DefaultMuleEvent(event.getMessage(), (OutboundEndpoint) processor,
 309  
                 event.getSession());
 310  
         }
 311  
 
 312  0
         results.add(processor.process(processedEvent));
 313  
 
 314  0
         if (getRouterStatistics() != null && getRouterStatistics().isEnabled())
 315  
         {
 316  0
             getRouterStatistics().incrementRoutedMessage(event.getEndpoint());
 317  
         }
 318  0
     }
 319  
 
 320  
     protected List<MessageProcessorFilterPair> getConditionalMessageProcessors()
 321  
     {
 322  0
         return Collections.unmodifiableList(conditionalMessageProcessors);
 323  
     }
 324  
 
 325  
     private interface RoutesUpdater
 326  
     {
 327  
         void updateAt(int index);
 328  
     }
 329  
 
 330  
     private void updateRoute(MessageProcessor processor, RoutesUpdater routesUpdater)
 331  
     {
 332  0
         synchronized (conditionalMessageProcessors)
 333  
         {
 334  0
             for (int i = 0; i < conditionalMessageProcessors.size(); i++)
 335  
             {
 336  0
                 if (conditionalMessageProcessors.get(i).getMessageProcessor().equals(processor))
 337  
                 {
 338  0
                     routesUpdater.updateAt(i);
 339  
                 }
 340  
             }
 341  0
         }
 342  0
     }
 343  
 
 344  
     public RouterStatistics getRouterStatistics()
 345  
     {
 346  0
         return routerStatistics;
 347  
     }
 348  
 
 349  
     public void setRouterStatistics(RouterStatistics routerStatistics)
 350  
     {
 351  0
         this.routerStatistics = routerStatistics;
 352  0
     }
 353  
 
 354  
     @Override
 355  
     public String toString()
 356  
     {
 357  0
         return String.format("%s [flow-construct=%s, started=%s]", getClass().getSimpleName(),
 358  
             flowConstruct != null ? flowConstruct.getName() : null, started);
 359  
     }
 360  
 }