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.AbstractBehaviorFactory;
019    import org.picocontainer.references.SimpleReference;
020    import org.picocontainer.LifecycleStrategy;
021    
022    import java.util.Properties;
023    
024    /**
025     * factory class creating cached behaviours
026     * @author Aslak Hellesøy
027     * @author <a href="Rafal.Krzewski">rafal@caltha.pl</a>
028     * @author Konstantin Pribluda
029     */
030    @SuppressWarnings("serial")
031    public class Caching extends AbstractBehaviorFactory {
032    
033        public <T> ComponentAdapter<T> createComponentAdapter(
034                            ComponentMonitor componentMonitor,
035                            LifecycleStrategy lifecycleStrategy,
036                            Properties componentProperties, Object componentKey,
037                            Class<T> componentImplementation, Parameter... parameters)
038                            throws PicoCompositionException {
039                    if (removePropertiesIfPresent(componentProperties,
040                                    Characteristics.NO_CACHE)) {
041                            return super.createComponentAdapter(componentMonitor,
042                                            lifecycleStrategy, componentProperties, componentKey,
043                                            componentImplementation, parameters);
044                    }
045                    removePropertiesIfPresent(componentProperties, Characteristics.CACHE);
046            return componentMonitor.newBehavior(new Cached<T>(super.createComponentAdapter(componentMonitor,
047                                    lifecycleStrategy, componentProperties, componentKey,
048                                    componentImplementation, parameters),
049                    new SimpleReference<Stored.Instance<T>>()));
050    
051            }
052    
053            public <T> ComponentAdapter<T> addComponentAdapter(
054                            ComponentMonitor componentMonitor,
055                            LifecycleStrategy lifecycleStrategy,
056                            Properties componentProperties, ComponentAdapter<T> adapter) {
057                    if (removePropertiesIfPresent(componentProperties,
058                                    Characteristics.NO_CACHE)) {
059                            return super.addComponentAdapter(componentMonitor,
060                                            lifecycleStrategy, componentProperties, adapter);
061                    }
062                    removePropertiesIfPresent(componentProperties, Characteristics.CACHE);
063            ComponentAdapter<T> delegate = super.addComponentAdapter(componentMonitor, lifecycleStrategy, componentProperties, adapter);
064            return componentMonitor.newBehavior(componentMonitor.newBehavior(new Cached<T>(delegate, new SimpleReference<Stored.Instance<T>>())));
065            }
066    
067    }