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.ComponentAdapter;
014    import org.picocontainer.Parameter;
015    import org.picocontainer.PicoCompositionException;
016    import org.picocontainer.Characteristics;
017    import org.picocontainer.ComponentMonitor;
018    import org.picocontainer.behaviors.Cached;
019    import org.picocontainer.behaviors.AbstractBehaviorFactory;
020    import org.picocontainer.LifecycleStrategy;
021    
022    import java.util.Properties;
023    
024    /**
025     * Behavior that turns off Caching behavior by default.  
026     * <p>Example:</p>
027     * <pre>
028     *              import org.picocontainer.*;
029     *              import static org.picocontainer.Characteristics.*;
030     * 
031     *              MutablePicoContainer mpc = new PicoBuilder().withBehaviors(new OptInCaching()).build();
032     *              mpc.addComponent(Map.class, HashMap.class) //Multiple Instances, no Caching.
033     *              mpc.as(CACHE).addComponent(Set.class, HashSet.class) //Single Cached Instance.          
034     * </pre>
035     * @author Aslak Helles&oslash;y
036     * @author <a href="Rafal.Krzewski">rafal@caltha.pl</a>
037     */
038    @SuppressWarnings("serial")
039    public class OptInCaching extends AbstractBehaviorFactory {
040    
041        public <T> ComponentAdapter<T> createComponentAdapter(ComponentMonitor componentMonitor, LifecycleStrategy lifecycleStrategy, Properties componentProperties, Object componentKey, 
042                            Class<T> componentImplementation, Parameter... parameters)
043                throws PicoCompositionException {
044            if (AbstractBehaviorFactory.removePropertiesIfPresent(componentProperties, Characteristics.CACHE)) {
045                return componentMonitor.newBehavior(new Cached<T>(super.createComponentAdapter(componentMonitor,
046                                                                                            lifecycleStrategy,
047                                                                                            componentProperties,
048                                                                                            componentKey,
049                                                                                            componentImplementation,
050                                                                                            parameters)));
051            }
052            AbstractBehaviorFactory.removePropertiesIfPresent(componentProperties, Characteristics.NO_CACHE);
053            return super.createComponentAdapter(componentMonitor, lifecycleStrategy,
054                                                componentProperties, componentKey, componentImplementation, parameters);
055        }
056    
057    
058        public <T> ComponentAdapter<T> addComponentAdapter(ComponentMonitor componentMonitor,
059                                                    LifecycleStrategy lifecycleStrategy,
060                                                    Properties componentProperties,
061                                                    ComponentAdapter<T> adapter) {
062            if (AbstractBehaviorFactory.removePropertiesIfPresent(componentProperties, Characteristics.CACHE)) {
063                return componentMonitor.newBehavior(new Cached<T>(super.addComponentAdapter(componentMonitor,
064                                                                     lifecycleStrategy,
065                                                                     componentProperties,
066                                                                     adapter)));
067            }
068            AbstractBehaviorFactory.removePropertiesIfPresent(componentProperties, Characteristics.NO_CACHE);
069            return super.addComponentAdapter(componentMonitor,
070                                             lifecycleStrategy,
071                                             componentProperties,
072                                             adapter);
073        }
074    }