    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 Paul Hammant                                             *
    009     *****************************************************************************/
    010
    011    package org.nanocontainer.reflection;
    012
    013    import java.io.Serializable;
    014    import org.nanocontainer.NanoPicoContainer;
    015    import org.picocontainer.ComponentMonitor;
    016    import org.picocontainer.MutablePicoContainer;
    017    import org.picocontainer.PicoContainer;
    018    import org.picocontainer.defaults.ComponentAdapterFactory;
    019    import org.picocontainer.defaults.ComponentMonitorStrategy;
    020    import org.picocontainer.defaults.DefaultComponentAdapterFactory;
    021    import org.picocontainer.defaults.DefaultPicoContainer;
    022    import org.picocontainer.defaults.LifecycleStrategy;
    023
    024    /**
    025     * This is a MutablePicoContainer that also supports soft composition. i.e. assembly by class name rather that class
    026     * reference.
    027     * <p>
    028     * In terms of implementation it adopts the behaviour of DefaultPicoContainer and DefaultNanoContainer.</p>
    029     *
    030     * @author Paul Hammant
    031     * @author Mauro Talevi
    032     * @author Michael Rimov
    033     * @version $Revision: 2964 $
    034     */
    035    public class DefaultNanoPicoContainer extends AbstractNanoPicoContainer implements NanoPicoContainer, Serializable,
    036        ComponentMonitorStrategy {
    037
    038        public DefaultNanoPicoContainer(ClassLoader classLoader, ComponentAdapterFactory caf, PicoContainer parent) {
    039            super(new DefaultPicoContainer(caf, parent), classLoader);
    040        }
    041
    042        public DefaultNanoPicoContainer(ClassLoader classLoader, PicoContainer parent) {
    043            super(new DefaultPicoContainer(new DefaultComponentAdapterFactory(), parent), classLoader);
    044        }
    045
    046        public DefaultNanoPicoContainer(ClassLoader classLoader, PicoContainer parent, ComponentMonitor componentMonitor) {
    047            super(new DefaultPicoContainer(new DefaultComponentAdapterFactory(componentMonitor), parent), classLoader);
    048        }
    049
    050        public DefaultNanoPicoContainer(ComponentAdapterFactory caf) {
    051            super(new DefaultPicoContainer(caf, null), DefaultNanoPicoContainer.class.getClassLoader());
    052        }
    053
    054        public DefaultNanoPicoContainer(PicoContainer pc) {
    055            super(new DefaultPicoContainer(pc), DefaultNanoPicoContainer.class.getClassLoader());
    056        }
    057
    058        public DefaultNanoPicoContainer(ClassLoader classLoader) {
    059            super(new DefaultPicoContainer(), classLoader);
    060        }
    061
    062        public DefaultNanoPicoContainer() {
    063            super(new DefaultPicoContainer(), DefaultNanoPicoContainer.class.getClassLoader());
    064        }
    065
    066
    067        /**
    068         * Constructor that provides the same control over the nanocontainer lifecycle strategies
    069         * as {@link DefaultPicoContainer(ComponentAdapterFactory, LifecycleStrategy, PicoContainer)}.
    070         * @param componentAdapterFactory ComponentAdapterFactory
    071         * @param lifecycleStrategyForInstanceRegistrations LifecycleStrategy
    072         * @param parent PicoContainer may be null if there is no parent.
    073         * @param cl the Classloader to use.  May be null, in which case DefaultNanoPicoContainer.class.getClassLoader()
    074         * will be called instead.
    075         */
    076        public DefaultNanoPicoContainer(ComponentAdapterFactory componentAdapterFactory,
    077            LifecycleStrategy lifecycleStrategyForInstanceRegistrations, PicoContainer parent, ClassLoader cl) {
    078
    079            super(new DefaultPicoContainer(componentAdapterFactory,
    080                lifecycleStrategyForInstanceRegistrations, parent),
    081                //Use a default classloader if none is specified.
    082                (cl != null) ? cl : DefaultNanoPicoContainer.class.getClassLoader());
    083        }
    084
    085        /**
    086         * Copy Constructor.  Makes a new DefaultNanoPicoContainer with the same
    087         * attributes - ClassLoader, child PicoContainer type, ComponentAdapterFactory -
    088         * as the parent.
    089         * <p><tt>Note:</tt> This constructor is protected because are existing scripts
    090         * that call <tt>new DefaultNanoPicoContainer(PicoContainer)</tt>, and they get this
    091         * constructor instead (which has different behavior).</p>
    092         * @param parent  The object to copy.
    093         */
    094        protected DefaultNanoPicoContainer(final DefaultNanoPicoContainer parent) {
    095            super(parent.getDelegate().makeChildContainer(),  parent.getComponentClassLoader());
    096            MutablePicoContainer parentDelegate = parent.getDelegate();
    097            parentDelegate.removeChildContainer(getDelegate());
    098            parentDelegate.addChildContainer(this);
    099        }
    100
    101
    102        protected AbstractNanoPicoContainer createChildContainer() {
    103            return new DefaultNanoPicoContainer(this);
    104         }
    105
    106        public void changeMonitor(ComponentMonitor monitor) {
    107            ((ComponentMonitorStrategy)getDelegate()).changeMonitor(monitor);
    108        }
    109
    110        public ComponentMonitor currentMonitor() {
    111            return ((ComponentMonitorStrategy)getDelegate()).currentMonitor();
    112        }
    113
    114    }