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