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    import org.picocontainer.ObjectReference;
022    
023    import java.util.Properties;
024    
025    /**
026     * factory class creating cached behaviours
027     * @author Aslak Hellesøy
028     * @author <a href="Rafal.Krzewski">rafal@caltha.pl</a>
029     * @author Konstantin Pribluda
030     */
031    @SuppressWarnings("serial")
032    public class Caching extends AbstractBehaviorFactory {
033    
034        public <T> ComponentAdapter<T> createComponentAdapter(
035                            ComponentMonitor componentMonitor,
036                            LifecycleStrategy lifecycleStrategy,
037                            Properties componentProperties, Object componentKey,
038                            Class<T> componentImplementation, Parameter... parameters)
039                            throws PicoCompositionException {
040                    if (removePropertiesIfPresent(componentProperties,
041                                    Characteristics.NO_CACHE)) {
042                            return super.createComponentAdapter(componentMonitor,
043                                            lifecycleStrategy, componentProperties, componentKey,
044                                            componentImplementation, parameters);
045                    }
046                    removePropertiesIfPresent(componentProperties, Characteristics.CACHE);
047                    return new Cached<T>(super.createComponentAdapter(componentMonitor,
048                                    lifecycleStrategy, componentProperties, componentKey,
049                                    componentImplementation, parameters), newObjectReference());
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                    return new Cached<T>(super.addComponentAdapter(componentMonitor,
064                                    lifecycleStrategy, componentProperties, adapter),
065                                    newObjectReference());
066            }
067    
068            protected <T> ObjectReference<T> newObjectReference() {
069                    return new SimpleReference<T>();
070            }
071    }