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.LifecycleStrategy;
020    import org.picocontainer.ObjectReference;
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    public class Caching extends AbstractBehaviorFactory {
031    
032            public <T> ComponentAdapter<T> createComponentAdapter(
033                            ComponentMonitor componentMonitor,
034                            LifecycleStrategy lifecycleStrategy,
035                            Properties componentProperties, Object componentKey,
036                            Class<T> componentImplementation, Parameter... parameters)
037                            throws PicoCompositionException {
038                    if (removePropertiesIfPresent(componentProperties,
039                                    Characteristics.NO_CACHE)) {
040                            return super.createComponentAdapter(componentMonitor,
041                                            lifecycleStrategy, componentProperties, componentKey,
042                                            componentImplementation, parameters);
043                    }
044                    removePropertiesIfPresent(componentProperties, Characteristics.CACHE);
045                    return new Cached<T>(super.createComponentAdapter(componentMonitor,
046                                    lifecycleStrategy, componentProperties, componentKey,
047                                    componentImplementation, parameters), newObjectReference());
048    
049            }
050    
051            public <T> ComponentAdapter<T> addComponentAdapter(
052                            ComponentMonitor componentMonitor,
053                            LifecycleStrategy lifecycleStrategy,
054                            Properties componentProperties, ComponentAdapter<T> adapter) {
055                    if (removePropertiesIfPresent(componentProperties,
056                                    Characteristics.NO_CACHE)) {
057                            return super.addComponentAdapter(componentMonitor,
058                                            lifecycleStrategy, componentProperties, adapter);
059                    }
060                    removePropertiesIfPresent(componentProperties, Characteristics.CACHE);
061                    return new Cached<T>(super.addComponentAdapter(componentMonitor,
062                                    lifecycleStrategy, componentProperties, adapter),
063                                    newObjectReference());
064            }
065    
066            protected <T> ObjectReference<T> newObjectReference() {
067                    return new Cached.SimpleReference<T>();
068            }
069    }