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     * @author Aslak Hellesøy
026     * @author <a href="Rafal.Krzewski">rafal@caltha.pl</a>
027     */
028    @SuppressWarnings("serial")
029    public class OptInCaching extends AbstractBehaviorFactory {
030    
031        public ComponentAdapter createComponentAdapter(ComponentMonitor componentMonitor, LifecycleStrategy lifecycleStrategy, Properties componentProperties, Object componentKey, Class componentImplementation, Parameter... parameters)
032                throws PicoCompositionException {
033            if (AbstractBehaviorFactory.removePropertiesIfPresent(componentProperties, Characteristics.CACHE)) {
034                return new Cached(super.createComponentAdapter(componentMonitor,
035                                                                                            lifecycleStrategy,
036                                                                                            componentProperties,
037                                                                                            componentKey,
038                                                                                            componentImplementation,
039                                                                                            parameters));
040            }
041            AbstractBehaviorFactory.removePropertiesIfPresent(componentProperties, Characteristics.NO_CACHE);
042            return super.createComponentAdapter(componentMonitor, lifecycleStrategy,
043                                                componentProperties, componentKey, componentImplementation, parameters);
044        }
045    
046    
047        public ComponentAdapter addComponentAdapter(ComponentMonitor componentMonitor,
048                                                    LifecycleStrategy lifecycleStrategy,
049                                                    Properties componentProperties,
050                                                    ComponentAdapter adapter) {
051            if (AbstractBehaviorFactory.removePropertiesIfPresent(componentProperties, Characteristics.CACHE)) {
052                return new Cached(super.addComponentAdapter(componentMonitor,
053                                                                     lifecycleStrategy,
054                                                                     componentProperties,
055                                                                     adapter));
056            }
057            AbstractBehaviorFactory.removePropertiesIfPresent(componentProperties, Characteristics.NO_CACHE);
058            return super.addComponentAdapter(componentMonitor,
059                                             lifecycleStrategy,
060                                             componentProperties,
061                                             adapter);
062        }
063    }