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 public class OptInCaching extends AbstractBehaviorFactory {
029
030 public ComponentAdapter createComponentAdapter(ComponentMonitor componentMonitor, LifecycleStrategy lifecycleStrategy, Properties componentProperties, Object componentKey, Class componentImplementation, Parameter... parameters)
031 throws PicoCompositionException {
032 if (AbstractBehaviorFactory.removePropertiesIfPresent(componentProperties, Characteristics.CACHE)) {
033 return new Cached(super.createComponentAdapter(componentMonitor,
034 lifecycleStrategy,
035 componentProperties,
036 componentKey,
037 componentImplementation,
038 parameters));
039 }
040 AbstractBehaviorFactory.removePropertiesIfPresent(componentProperties, Characteristics.NO_CACHE);
041 return super.createComponentAdapter(componentMonitor, lifecycleStrategy,
042 componentProperties, componentKey, componentImplementation, parameters);
043 }
044
045
046 public ComponentAdapter addComponentAdapter(ComponentMonitor componentMonitor,
047 LifecycleStrategy lifecycleStrategy,
048 Properties componentProperties,
049 ComponentAdapter adapter) {
050 if (AbstractBehaviorFactory.removePropertiesIfPresent(componentProperties, Characteristics.CACHE)) {
051 return new Cached(super.addComponentAdapter(componentMonitor,
052 lifecycleStrategy,
053 componentProperties,
054 adapter));
055 }
056 AbstractBehaviorFactory.removePropertiesIfPresent(componentProperties, Characteristics.NO_CACHE);
057 return super.addComponentAdapter(componentMonitor,
058 lifecycleStrategy,
059 componentProperties,
060 adapter);
061 }
062 }