    001    /*****************************************************************************
    002     * Copyright (C) PicoContainer 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                                                          *
    009     *****************************************************************************/
    010    package org.picocontainer.gems.behaviors;
    011
    012    import org.picocontainer.behaviors.AbstractBehaviorFactory;
    013    import org.picocontainer.gems.GemsCharacteristics;
    014    import org.picocontainer.ComponentAdapter;
    015    import org.picocontainer.ComponentMonitor;
    016    import org.picocontainer.LifecycleStrategy;
    017    import org.picocontainer.Parameter;
    018    import org.picocontainer.PicoCompositionException;
    019
    020    import java.util.Properties;
    021
    022    @SuppressWarnings("serial")
    023    public class Pooling extends AbstractBehaviorFactory {
    024
    025            private final Pooled.Context poolContext;
    026
    027        public Pooling(final Pooled.Context poolContext) {
    028            this.poolContext = poolContext;
    029        }
    030
    031        public Pooling() {
    032            poolContext = new Pooled.DefaultContext();
    033        }
    034
    035        @Override
    036            public ComponentAdapter createComponentAdapter(final ComponentMonitor componentMonitor, final LifecycleStrategy lifecycleStrategy, final Properties componentProperties, final Object componentKey, final Class componentImplementation, final Parameter... parameters)
    037                throws PicoCompositionException {
    038            ComponentAdapter delegate = super.createComponentAdapter(componentMonitor, lifecycleStrategy,
    039                                                                             componentProperties, componentKey, componentImplementation, parameters);
    040
    041            if (AbstractBehaviorFactory.removePropertiesIfPresent(componentProperties, GemsCharacteristics.NO_POOL)) {
    042                    return delegate;
    043                    }
    044
    045            AbstractBehaviorFactory.removePropertiesIfPresent(componentProperties, GemsCharacteristics.POOL);
    046            Pooled behavior = new Pooled(delegate, poolContext);
    047            //TODO
    048            //Characteristics.HIDE.setProcessedIn(componentCharacteristics);
    049            return componentMonitor.newBehavior(behavior);
    050        }
    051
    052        @Override
    053            public ComponentAdapter addComponentAdapter(final ComponentMonitor componentMonitor,
    054                                                    final LifecycleStrategy lifecycleStrategy,
    055                                                    final Properties componentProperties,
    056                                                    final ComponentAdapter adapter) {
    057
    058            if (AbstractBehaviorFactory.removePropertiesIfPresent(componentProperties, GemsCharacteristics.NO_POOL)) {
    059                    return super.addComponentAdapter(componentMonitor,
    060                        lifecycleStrategy,
    061                        componentProperties,
    062                        adapter);
    063                    }
    064
    065            AbstractBehaviorFactory.removePropertiesIfPresent(componentProperties, GemsCharacteristics.POOL);
    066            return componentMonitor.newBehavior(new Pooled(super.addComponentAdapter(componentMonitor,
    067                                             lifecycleStrategy,
    068                                             componentProperties,
    069                                             adapter), poolContext));
    070        }
    071    }