001    /*****************************************************************************
002     * Copyright (C) PicoContainer Organization. All rights reserved.            *
003     * ------------------------------------------------------------------------- *
004     * The software in this package is published under the terms of the BSD      *
005     * style license a copy of which has been included with this distribution in *
006     * the LICENSE.txt file.                                                     *
007     *                                                                           *
008     * Original code by Konstantin Pribluda                                      *
009     *****************************************************************************/
010    package org.picocontainer.web.chain;
011    
012    import java.util.ArrayList;
013    import java.util.List;
014    
015    import org.picocontainer.PicoContainer;
016    import org.picocontainer.Startable;
017    
018    /**
019     * Represents chain of containers, which can be started and stopped at once. 
020     * 
021     * @author Konstantin Pribluda
022     */
023    public final class ContainerChain implements Startable {
024    
025            private final List chain = new ArrayList();
026    
027            private PicoContainer last;
028    
029            /**
030             * Returns last container in chain.
031             * 
032             * @return last container in chain or null
033             */
034            public PicoContainer getLast() {
035                    return last;
036            }
037    
038            /**
039             * add new container to the end of chain.
040             * 
041             * @param container
042             */
043            public void addContainer(PicoContainer container) {
044                    chain.add(container);
045                    last = container;
046            }
047    
048            /**
049             * start each container in the chain
050         */
051            public void start() {
052            for (Object aChain : chain) {
053                ((Startable)aChain).start();
054            }
055        }
056    
057            /**
058         * stop each container in the chain
059             */
060            public void stop() {
061                    for (int i = chain.size() - 1; i >= 0; i--) {
062                            ((Startable) chain.get(i)).stop();
063                    }
064            }
065    
066    }