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 Joerg Schaibe                                            *
009     *****************************************************************************/
010    
011    package org.picocontainer.gems.adapters;
012    
013    import com.thoughtworks.proxy.ProxyFactory;
014    import com.thoughtworks.proxy.factory.StandardProxyFactory;
015    
016    import org.picocontainer.ComponentAdapter;
017    import org.picocontainer.Parameter;
018    import org.picocontainer.PicoIntrospectionException;
019    import org.picocontainer.defaults.AssignabilityRegistrationException;
020    import org.picocontainer.defaults.ComponentAdapterFactory;
021    import org.picocontainer.defaults.DecoratingComponentAdapterFactory;
022    import org.picocontainer.defaults.NotConcreteRegistrationException;
023    
024    
025    /**
026     * Factory for the AssimilatingComponentAdapter. This factory will create {@link AssimilatingComponentAdapter} instances for all
027     * {@link ComponentAdapter} instances created by the delegate. This will assimilate every component for a specific type.
028     * 
029     * @author Jörg Schaible
030     * @since 1.2
031     */
032    public class AssimilatingComponentAdapterFactory extends DecoratingComponentAdapterFactory {
033    
034        private final ProxyFactory proxyFactory;
035        private final Class assimilationType;
036    
037        /**
038         * Construct an AssimilatingComponentAdapterFactory. The instance will use the {@link StandardProxyFactory} using the JDK
039         * implementation.
040         * 
041         * @param delegate The delegated {@link ComponentAdapterFactory}.
042         * @param type The assimilated type.
043         */
044        public AssimilatingComponentAdapterFactory(final ComponentAdapterFactory delegate, final Class type) {
045            this(delegate, type, new StandardProxyFactory());
046        }
047    
048        /**
049         * Construct an AssimilatingComponentAdapterFactory using a special {@link ProxyFactory}.
050         * 
051         * @param delegate The delegated {@link ComponentAdapterFactory}.
052         * @param type The assimilated type.
053         * @param proxyFactory The proxy factory to use.
054         */
055        public AssimilatingComponentAdapterFactory(
056                final ComponentAdapterFactory delegate, final Class type, final ProxyFactory proxyFactory) {
057            super(delegate);
058            this.assimilationType = type;
059            this.proxyFactory = proxyFactory;
060        }
061    
062        /**
063         * Create a {@link AssimilatingComponentAdapter}. This adapter will wrap the returned {@link ComponentAdapter} of the
064         * deleated {@link ComponentAdapterFactory}.
065         * 
066         * @see org.picocontainer.defaults.DecoratingComponentAdapterFactory#createComponentAdapter(java.lang.Object,
067         *      java.lang.Class, org.picocontainer.Parameter[])
068         */
069        public ComponentAdapter createComponentAdapter(
070                final Object componentKey, final Class componentImplementation, final Parameter[] parameters)
071                throws PicoIntrospectionException, AssignabilityRegistrationException, NotConcreteRegistrationException {
072            return new AssimilatingComponentAdapter(assimilationType, super.createComponentAdapter(
073                    componentKey, componentImplementation, parameters), proxyFactory);
074        }
075    }