001    /*****************************************************************************
002     * Copyright (C) NanoContainer 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     * Original code by Aslak Hellesoy & Joerg Schaible                                       *
009     *****************************************************************************/
010    package org.picocontainer.gems.util;
011    
012    import com.thoughtworks.proxy.ProxyFactory;
013    import com.thoughtworks.proxy.toys.multicast.Multicasting;
014    import org.picocontainer.PicoContainer;
015    
016    import java.util.ArrayList;
017    import java.util.Collections;
018    import java.util.List;
019    
020    /**
021     * Factory for creating a multicaster object that multicasts calls to all
022     * components in a PicoContainer instance.
023     *
024     * @author Aslak Hellesøy
025     * @author Chris Stevenson
026     * @author Paul Hammant
027     */
028    public class Multicaster {
029        /**
030         * Create a {@link Multicasting} proxy for the components of a {@link PicoContainer}.
031         * 
032         * @param pico the container
033         * @param callInInstantiationOrder <code>true</code> if the components will be called in instantiation order
034         * @param proxyFactory the ProxyFactory to use
035         * @return the Multicasting proxy
036         */
037        public static Object object(final PicoContainer pico, boolean callInInstantiationOrder, final ProxyFactory proxyFactory) {
038            List copy = new ArrayList(pico.getComponents());
039    
040            if (!callInInstantiationOrder) {
041                // reverse the list
042                Collections.reverse(copy);
043            }
044            Object[] targets = copy.toArray();
045            return Multicasting.object(proxyFactory, targets);
046        }
047    }