001: /*
002: * $Id: AbstractRouterCollection.java 11149 2008-03-03 19:41:55Z acooke $
003: * --------------------------------------------------------------------------------------
004: * Copyright (c) MuleSource, Inc. All rights reserved. http://www.mulesource.com
005: *
006: * The software in this package is published under the terms of the CPAL v1.0
007: * license, a copy of which has been included with this distribution in the
008: * LICENSE.txt file.
009: */
010:
011: package org.mule.routing;
012:
013: import org.mule.api.MuleContext;
014: import org.mule.api.context.MuleContextAware;
015: import org.mule.api.lifecycle.Initialisable;
016: import org.mule.api.lifecycle.InitialisationException;
017: import org.mule.api.lifecycle.LifecycleTransitionResult;
018: import org.mule.api.routing.Router;
019: import org.mule.api.routing.RouterCatchAllStrategy;
020: import org.mule.api.routing.RouterCollection;
021: import org.mule.management.stats.RouterStatistics;
022:
023: import java.util.Iterator;
024: import java.util.List;
025:
026: import edu.emory.mathcs.backport.java.util.concurrent.CopyOnWriteArrayList;
027:
028: import org.apache.commons.logging.Log;
029: import org.apache.commons.logging.LogFactory;
030:
031: /**
032: * <code>AbstractRouterCollection</code> provides common method implementations of
033: * router collections for in and outbound routers.
034: */
035:
036: public abstract class AbstractRouterCollection implements
037: RouterCollection, Initialisable, MuleContextAware {
038: /**
039: * logger used by this class
040: */
041: protected final transient Log logger = LogFactory
042: .getLog(getClass());
043:
044: protected boolean matchAll = false;
045:
046: protected List routers = new CopyOnWriteArrayList();
047:
048: private RouterStatistics statistics;
049:
050: private RouterCatchAllStrategy catchAllStrategy;
051:
052: protected MuleContext muleContext;
053:
054: public AbstractRouterCollection(int type) {
055: statistics = new RouterStatistics(type);
056: }
057:
058: public LifecycleTransitionResult initialise()
059: throws InitialisationException {
060: return LifecycleTransitionResult.initialiseAll(routers
061: .iterator());
062: }
063:
064: public void dispose() {
065: for (Iterator iterator = routers.iterator(); iterator.hasNext();) {
066: Router router = (Router) iterator.next();
067: router.dispose();
068: }
069: }
070:
071: public void setRouters(List routers) {
072: for (Iterator iterator = routers.iterator(); iterator.hasNext();) {
073: addRouter((Router) iterator.next());
074: }
075: }
076:
077: public void addRouter(Router router) {
078: router.setRouterStatistics(getStatistics());
079: routers.add(router);
080: }
081:
082: public Router removeRouter(Router router) {
083: if (routers.remove(router)) {
084: return router;
085: } else {
086: return null;
087: }
088: }
089:
090: public List getRouters() {
091: return routers;
092: }
093:
094: public RouterCatchAllStrategy getCatchAllStrategy() {
095: return catchAllStrategy;
096: }
097:
098: public void setCatchAllStrategy(
099: RouterCatchAllStrategy catchAllStrategy) {
100: this .catchAllStrategy = catchAllStrategy;
101: if (this .catchAllStrategy != null
102: && catchAllStrategy instanceof AbstractCatchAllStrategy) {
103: ((AbstractCatchAllStrategy) this .catchAllStrategy)
104: .setStatistics(statistics);
105: }
106: }
107:
108: public boolean isMatchAll() {
109: return matchAll;
110: }
111:
112: public void setMatchAll(boolean matchAll) {
113: this .matchAll = matchAll;
114: }
115:
116: public RouterStatistics getStatistics() {
117: return statistics;
118: }
119:
120: public void setStatistics(RouterStatistics stat) {
121: this .statistics = stat;
122: }
123:
124: public void setMuleContext(MuleContext context) {
125: this.muleContext = context;
126: }
127: }
|