    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     *****************************************************************************/
    009    package org.picocontainer.injectors;
    010
    011    import java.lang.reflect.Type;
    012
    013    import org.picocontainer.ComponentMonitor;
    014    import org.picocontainer.Injector;
    015    import org.picocontainer.LifecycleStrategy;
    016    import org.picocontainer.Parameter;
    017    import org.picocontainer.PicoCompositionException;
    018    import org.picocontainer.PicoContainer;
    019    import org.picocontainer.PicoVisitor;
    020    import org.picocontainer.ComponentAdapter;
    021
    022    @SuppressWarnings("serial")
    023    public class CompositeInjector<T> extends AbstractInjector<T> {
    024
    025        private final Injector<T>[] injectors;
    026
    027        public CompositeInjector(Object componentKey, Class<?> componentImplementation, Parameter[] parameters, ComponentMonitor monitor, LifecycleStrategy lifecycleStrategy,
    028                                 boolean useNames, Injector... injectors) {
    029            super(componentKey, componentImplementation, parameters, monitor, lifecycleStrategy, useNames);
    030            this.injectors = injectors;
    031        }
    032
    033
    034        public T getComponentInstance(PicoContainer container) throws PicoCompositionException {
    035            return getComponentInstance(container, NOTHING.class);
    036        }
    037
    038        public T getComponentInstance(PicoContainer container, Type into) throws PicoCompositionException {
    039            T instance = null;
    040            for (int i = 0; i < injectors.length; i++) {
    041                Injector<T> injector = injectors[i];
    042                if (instance == null) {
    043                    instance = injector.getComponentInstance(container, ComponentAdapter.NOTHING.class);
    044                } else {
    045                    injector.decorateComponentInstance(container, into, instance);
    046                }
    047            }
    048            return (T) instance;
    049        }
    050
    051
    052        /**
    053         * @return the object returned is the result of the last of the injectors delegated to
    054         */
    055        @Override
    056        public Object decorateComponentInstance(PicoContainer container, Type into, T instance) {
    057            Object result = null;
    058            for (int i = 0; i < injectors.length; i++) {
    059                result = injectors[i].decorateComponentInstance(container, into, instance);
    060            }
    061            return result;
    062        }
    063
    064        public void verify(PicoContainer container) throws PicoCompositionException {
    065            for (int i = 0; i < injectors.length; i++) {
    066                injectors[i].verify(container);
    067            }
    068        }
    069
    070        public final void accept(PicoVisitor visitor) {
    071            super.accept(visitor);
    072            for (int i = 0; i < injectors.length; i++) {
    073                injectors[i].accept(visitor);
    074            }
    075        }
    076
    077        public String getDescriptor() {
    078            return "CompositeInjector";
    079        }
    080    }