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.util.Properties;
012    
013    import org.picocontainer.Characteristics;
014    import org.picocontainer.ComponentAdapter;
015    import org.picocontainer.ComponentMonitor;
016    import org.picocontainer.InjectionFactory;
017    import org.picocontainer.Injector;
018    import org.picocontainer.LifecycleStrategy;
019    import org.picocontainer.Parameter;
020    import org.picocontainer.PicoCompositionException;
021    import org.picocontainer.behaviors.AbstractBehaviorFactory;
022    
023    /**
024     * A Composite of other types on InjectionFactories - pass them into the varargs constructor.
025     * 
026     * @author Paul Hammant
027     */
028    @SuppressWarnings("serial")
029    public class CompositeInjection extends AbstractInjectionFactory {
030    
031        private final InjectionFactory[] injectionFactories;
032    
033        public CompositeInjection(InjectionFactory... injectionFactories) {
034            this.injectionFactories = injectionFactories;
035        }
036    
037        public <T> ComponentAdapter<T> createComponentAdapter(ComponentMonitor monitor,
038                                                              LifecycleStrategy lifecycleStrategy,
039                                                              Properties componentProperties,
040                                                              Object componentKey,
041                                                              Class<T> componentImplementation,
042                                                              Parameter... parameters) throws PicoCompositionException {
043    
044            Injector[] injectors = new Injector[injectionFactories.length];
045    
046            for (int i = 0; i < injectionFactories.length; i++) {
047                InjectionFactory injectionFactory = injectionFactories[i];
048                injectors[i] = (Injector) injectionFactory.createComponentAdapter(monitor,
049                        lifecycleStrategy, componentProperties, componentKey, componentImplementation, parameters);
050            }
051    
052            boolean useNames = AbstractBehaviorFactory.arePropertiesPresent(componentProperties, Characteristics.USE_NAMES, true);
053            return wrapLifeCycle(monitor.newInjector(new CompositeInjector(componentKey, componentImplementation, parameters,
054                    monitor, useNames, injectors)), lifecycleStrategy);
055        }
056    }