View Javadoc

1   /*
2    * $Id: AbstractRouterCollection.java 12269 2008-07-10 04:19:03Z dfeist $
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.api.MuleContext;
14  import org.mule.api.context.MuleContextAware;
15  import org.mule.api.lifecycle.InitialisationException;
16  import org.mule.api.lifecycle.LifecycleTransitionResult;
17  import org.mule.api.routing.Router;
18  import org.mule.api.routing.RouterCatchAllStrategy;
19  import org.mule.api.routing.RouterCollection;
20  import org.mule.management.stats.RouterStatistics;
21  
22  import java.util.Iterator;
23  import java.util.List;
24  
25  import edu.emory.mathcs.backport.java.util.concurrent.CopyOnWriteArrayList;
26  
27  import org.apache.commons.logging.Log;
28  import org.apache.commons.logging.LogFactory;
29  
30  /**
31   * <code>AbstractRouterCollection</code> provides common method implementations of
32   * router collections for in and outbound routers.
33   */
34  
35  public abstract class AbstractRouterCollection implements RouterCollection, MuleContextAware
36  {
37      /**
38       * logger used by this class
39       */
40      protected final transient Log logger = LogFactory.getLog(getClass());
41  
42      protected boolean matchAll = false;
43  
44      protected List routers = new CopyOnWriteArrayList();
45  
46      private RouterStatistics statistics;
47  
48      private RouterCatchAllStrategy catchAllStrategy;
49      
50      protected MuleContext muleContext;
51  
52      public AbstractRouterCollection(int type)
53      {
54          statistics = new RouterStatistics(type);
55      }
56  
57      public void initialise() throws InitialisationException
58      {
59          LifecycleTransitionResult.initialiseAll(routers.iterator());
60      }
61  
62      public void dispose()
63      {
64          for (Iterator iterator = routers.iterator(); iterator.hasNext();)
65          {
66              Router router = (Router) iterator.next();
67              router.dispose();
68          }
69      }
70  
71      public void setRouters(List routers)
72      {
73          for (Iterator iterator = routers.iterator(); iterator.hasNext();)
74          {
75              addRouter((Router) iterator.next());
76          }
77      }
78  
79      public void addRouter(Router router)
80      {
81          router.setRouterStatistics(getStatistics());
82          routers.add(router);
83      }
84  
85      public Router removeRouter(Router router)
86      {
87          if (routers.remove(router))
88          {
89              return router;
90          }
91          else
92          {
93              return null;
94          }
95      }
96  
97      public List getRouters()
98      {
99          return routers;
100     }
101 
102     public RouterCatchAllStrategy getCatchAllStrategy()
103     {
104         return catchAllStrategy;
105     }
106 
107     public void setCatchAllStrategy(RouterCatchAllStrategy catchAllStrategy)
108     {
109         this.catchAllStrategy = catchAllStrategy;
110         if (this.catchAllStrategy != null && catchAllStrategy instanceof AbstractCatchAllStrategy)
111         {
112             ((AbstractCatchAllStrategy) this.catchAllStrategy).setStatistics(statistics);
113         }
114     }
115 
116     public boolean isMatchAll()
117     {
118         return matchAll;
119     }
120 
121     public void setMatchAll(boolean matchAll)
122     {
123         this.matchAll = matchAll;
124     }
125 
126     public RouterStatistics getStatistics()
127     {
128         return statistics;
129     }
130 
131     public void setStatistics(RouterStatistics stat)
132     {
133         this.statistics = stat;
134     }
135 
136     public void setMuleContext(MuleContext context)
137     {
138         this.muleContext = context;
139     }
140 }