View Javadoc

1   /*
2    * $Id: AbstractRouterCollection.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;
12  
13  import org.mule.management.stats.RouterStatistics;
14  import org.mule.umo.routing.UMORouter;
15  import org.mule.umo.routing.UMORouterCatchAllStrategy;
16  import org.mule.umo.routing.UMORouterCollection;
17  
18  import java.util.Iterator;
19  import java.util.List;
20  
21  import edu.emory.mathcs.backport.java.util.concurrent.CopyOnWriteArrayList;
22  import org.apache.commons.logging.Log;
23  import org.apache.commons.logging.LogFactory;
24  
25  /**
26   * <code>AbstractRouterCollection</code> provides common method implementations of
27   * router collections for in and outbound routers.
28   */
29  
30  public abstract class AbstractRouterCollection implements UMORouterCollection
31  {
32      /**
33       * logger used by this class
34       */
35      protected final transient Log logger = LogFactory.getLog(getClass());
36  
37      protected boolean matchAll = false;
38  
39      protected List routers = new CopyOnWriteArrayList();
40  
41      private RouterStatistics statistics;
42  
43      private UMORouterCatchAllStrategy catchAllStrategy;
44  
45      public AbstractRouterCollection(int type)
46      {
47          statistics = new RouterStatistics(type);
48      }
49  
50      public void setRouters(List routers)
51      {
52          for (Iterator iterator = routers.iterator(); iterator.hasNext();)
53          {
54              addRouter((UMORouter) iterator.next());
55          }
56      }
57  
58      public void addRouter(UMORouter router)
59      {
60          router.setRouterStatistics(getStatistics());
61          routers.add(router);
62      }
63  
64      public UMORouter removeRouter(UMORouter router)
65      {
66          if (routers.remove(router))
67          {
68              return router;
69          }
70          else
71          {
72              return null;
73          }
74      }
75  
76      public List getRouters()
77      {
78          return routers;
79      }
80  
81      public UMORouterCatchAllStrategy getCatchAllStrategy()
82      {
83          return catchAllStrategy;
84      }
85  
86      public void setCatchAllStrategy(UMORouterCatchAllStrategy catchAllStrategy)
87      {
88          this.catchAllStrategy = catchAllStrategy;
89          if (this.catchAllStrategy != null && catchAllStrategy instanceof AbstractCatchAllStrategy)
90          {
91              ((AbstractCatchAllStrategy) this.catchAllStrategy).setStatistics(statistics);
92          }
93      }
94  
95      public boolean isMatchAll()
96      {
97          return matchAll;
98      }
99  
100     public void setMatchAll(boolean matchAll)
101     {
102         this.matchAll = matchAll;
103     }
104 
105     public RouterStatistics getStatistics()
106     {
107         return statistics;
108     }
109 
110     public void setStatistics(RouterStatistics stat)
111     {
112         this.statistics = stat;
113     }
114 }