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.monitors;
009    
010    import org.picocontainer.ComponentMonitor;
011    import org.picocontainer.MutablePicoContainer;
012    import org.picocontainer.PicoContainer;
013    
014    /**
015     * The first of a list of composers passed in that responds with an instance for a missing component will
016     * be used.
017     */
018    public class ComposingMonitor extends AbstractComponentMonitor {
019        private Composer[] composers;
020    
021        public ComposingMonitor(ComponentMonitor delegate, Composer... composers) {
022            super(delegate);
023            this.composers = composers;
024        }
025    
026        public ComposingMonitor(Composer... composers) {
027            this.composers = composers;
028        }
029    
030        @Override
031        public Object noComponentFound(MutablePicoContainer container, Object componentKey) {
032            for (Composer composer : composers) {
033                Object retVal = composer.compose(container, componentKey);
034                if (retVal != null) {
035                    return retVal;
036                }
037            }
038            return super.noComponentFound(container, componentKey);
039        }
040    
041        /**
042         * A Composer can be used to make components that are otherwise missing.
043         */
044        public static interface Composer {
045            public Object compose(PicoContainer container, Object componentKey);
046        }
047    
048    
049    }