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     * @since 1.2
028     */
029    public class Multicaster {
030        /**
031         * Create a {@link Multicasting} proxy for the components of a {@link PicoContainer}.
032         * 
033         * @param pico the container
034         * @param callInInstantiationOrder <code>true</code> if the components will be called in instantiation order
035         * @param proxyFactory the ProxyFactory to use
036         * @return the Multicasting proxy
037         * @since 1.2
038         */
039        public static Object object(PicoContainer pico, boolean callInInstantiationOrder, ProxyFactory proxyFactory) {
040            List copy = new ArrayList(pico.getComponentInstances());
041    
042            if (!callInInstantiationOrder) {
043                // reverse the list
044                Collections.reverse(copy);
045            }
046            Object[] targets = copy.toArray();
047            return Multicasting.object(proxyFactory, targets);
048        }
049    }