    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    package org.picocontainer.lifecycle;
    009
    010    import org.picocontainer.LifecycleStrategy;
    011
    012    /**
    013     * Allow for use of alternate LifecycleStrategy strategies to be used
    014     * at the same time. A component can be started/stopped/disposed according
    015     * to *any* of the supplied LifecycleStrategy instances.
    016     *
    017     * @author Paul Hammant
    018     */
    019    public class CompositeLifecycleStrategy implements LifecycleStrategy {
    020
    021        private final StartableLifecycleStrategy[] alternateStrategies;
    022
    023        public CompositeLifecycleStrategy(StartableLifecycleStrategy... alternateStrategies) {
    024            this.alternateStrategies = alternateStrategies;
    025        }
    026
    027        public void start(Object component) {
    028            for (StartableLifecycleStrategy lifecycleStrategy : alternateStrategies) {
    029                lifecycleStrategy.start(component);
    030            }
    031        }
    032
    033        public void stop(Object component) {
    034            for (StartableLifecycleStrategy lifecycleStrategy : alternateStrategies) {
    035                lifecycleStrategy.stop(component);
    036            }
    037        }
    038
    039        public void dispose(Object component) {
    040            for (StartableLifecycleStrategy lifecycleStrategy : alternateStrategies) {
    041                lifecycleStrategy.dispose(component);
    042            }
    043        }
    044
    045        public boolean hasLifecycle(Class<?> type) {
    046            for (StartableLifecycleStrategy lifecycleStrategy : alternateStrategies) {
    047                if (lifecycleStrategy.hasLifecycle(type)) {
    048                    return true;
    049                }
    050            }
    051            return false;
    052        }
    053    }