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
011package org.picocontainer.gems.behaviors;
012
013import com.thoughtworks.proxy.ProxyFactory;
014import com.thoughtworks.proxy.factory.StandardProxyFactory;
015
016import org.picocontainer.ComponentAdapter;
017import org.picocontainer.Parameter;
018import org.picocontainer.PicoCompositionException;
019import org.picocontainer.ComponentMonitor;
020import org.picocontainer.LifecycleStrategy;
021import org.picocontainer.behaviors.AbstractBehaviorFactory;
022import org.picocontainer.ComponentFactory;
023
024import java.util.Properties;
025
026
027/**
028 * Factory for the Assimilated. This factory will create {@link Assimilated} instances for all
029 * {@link ComponentAdapter} instances created by the delegate. This will assimilate every component for a specific type.
030 * (TODO Since assimilating is taking types that are not the result type, does this mean that we cannot use generics)
031 * 
032 * @author Jörg Schaible
033 * for this type?  I've been unable to actually get it working.
034 */
035@SuppressWarnings("serial")
036public class Assimilating extends AbstractBehaviorFactory {
037
038        private final ProxyFactory proxyFactory;
039    private final Class<?> assimilationType;
040
041    /**
042     * Construct an Assimilating. The instance will use the {@link StandardProxyFactory} using the JDK
043     * implementation.
044     * 
045     * @param type The assimilated type.
046     */
047    public Assimilating(final Class<?> type) {
048        this(type, new StandardProxyFactory());
049    }
050
051    /**
052     * Construct an Assimilating using a special {@link ProxyFactory}.
053     * 
054     * @param type The assimilated type.
055     * @param proxyFactory The proxy factory to use.
056     */
057    public Assimilating(final Class<?> type, final ProxyFactory proxyFactory) {
058        this.assimilationType = type;
059        this.proxyFactory = proxyFactory;
060    }
061
062    /**
063     * Create a {@link Assimilated}. This adapter will wrap the returned {@link ComponentAdapter} of the
064     * deleated {@link ComponentFactory}.
065     * 
066     * @see ComponentFactory#createComponentAdapter(ComponentMonitor,LifecycleStrategy,Properties,Object,Class,Parameter...)
067     */
068        @Override
069        public ComponentAdapter createComponentAdapter(
070            final ComponentMonitor componentMonitor, final LifecycleStrategy lifecycleStrategy, final Properties componentProperties, final Object componentKey, final Class componentImplementation, final Parameter... parameters)
071            throws PicoCompositionException {
072        return componentMonitor.newBehavior(new Assimilated(assimilationType, super.createComponentAdapter(
073                componentMonitor, lifecycleStrategy, componentProperties, componentKey, componentImplementation, parameters), proxyFactory));
074    }
075
076
077    @Override
078        public ComponentAdapter addComponentAdapter(final ComponentMonitor componentMonitor,
079                                                final LifecycleStrategy lifecycleStrategy,
080                                                final Properties componentProperties,
081                                                final ComponentAdapter adapter) {
082        return componentMonitor.newBehavior(new Assimilated(assimilationType, super.addComponentAdapter(componentMonitor,
083                                         lifecycleStrategy,
084                                         componentProperties,
085                                         adapter)));
086    }
087}
088