    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     * Idea by Rachel Davies, Original code by Aslak Hellesoy and Paul Hammant   *
    009     *****************************************************************************/
    010
    011    package org.picocontainer.behaviors;
    012
    013    import org.picocontainer.Characteristics;
    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    /** @author Paul Hammant */
    023    @SuppressWarnings("serial")
    024    public class ThreadCaching extends AbstractBehaviorFactory {
    025
    026        public <T> ComponentAdapter<T> createComponentAdapter(ComponentMonitor componentMonitor,
    027                                                              LifecycleStrategy lifecycleStrategy,
    028                                                              Properties componentProperties,
    029                                                              Object componentKey,
    030                                                              Class<T> componentImplementation,
    031                                                              Parameter... parameters)
    032            throws PicoCompositionException
    033        {
    034            if (removePropertiesIfPresent(componentProperties, Characteristics.NO_CACHE)) {
    035                return super.createComponentAdapter(componentMonitor,
    036                                                    lifecycleStrategy,
    037                                                    componentProperties,
    038                                                    componentKey,
    039                                                    componentImplementation,
    040                                                    parameters);
    041            }
    042            removePropertiesIfPresent(componentProperties, Characteristics.CACHE);
    043            return componentMonitor.newBehavior(new ThreadCached<T>(super.createComponentAdapter(componentMonitor,
    044                                                                    lifecycleStrategy,
    045                                                                    componentProperties,
    046                                                                    componentKey,
    047                                                                    componentImplementation,
    048                                                                    parameters)));
    049
    050        }
    051
    052        public <T> ComponentAdapter<T> addComponentAdapter(ComponentMonitor componentMonitor,
    053                                                           LifecycleStrategy lifecycleStrategy,
    054                                                           Properties componentProperties,
    055                                                           ComponentAdapter<T> adapter) {
    056            if (removePropertiesIfPresent(componentProperties, Characteristics.NO_CACHE)) {
    057                return super.addComponentAdapter(componentMonitor, lifecycleStrategy, componentProperties, adapter);
    058            }
    059            removePropertiesIfPresent(componentProperties, Characteristics.CACHE);
    060            return componentMonitor.newBehavior(new ThreadCached<T>(super.addComponentAdapter(componentMonitor,
    061                                                                 lifecycleStrategy,
    062                                                                 componentProperties,
    063                                                                 adapter)));
    064        }
    065    }