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
021 import java.util.Properties;
022
023 /**
024 * @author Paul Hammant
025 */
026 public class ThreadCaching extends AbstractBehaviorFactory {
027
028
029 public ComponentAdapter createComponentAdapter(ComponentMonitor componentMonitor, LifecycleStrategy lifecycleStrategy, Properties componentProperties, Object componentKey, Class componentImplementation, Parameter... parameters)
030 throws PicoCompositionException {
031 if (removePropertiesIfPresent(componentProperties, Characteristics.NO_CACHE)) {
032 return super.createComponentAdapter(componentMonitor,
033 lifecycleStrategy,
034 componentProperties,
035 componentKey,
036 componentImplementation,
037 parameters);
038 }
039 removePropertiesIfPresent(componentProperties, Characteristics.CACHE);
040 return new ThreadCached(super.createComponentAdapter(componentMonitor, lifecycleStrategy,
041 componentProperties, componentKey, componentImplementation, parameters));
042
043 }
044
045 public ComponentAdapter addComponentAdapter(ComponentMonitor componentMonitor,
046 LifecycleStrategy lifecycleStrategy,
047 Properties componentProperties,
048 ComponentAdapter adapter) {
049 if (removePropertiesIfPresent(componentProperties, Characteristics.NO_CACHE)) {
050 return super.addComponentAdapter(componentMonitor, lifecycleStrategy, componentProperties, adapter);
051 }
052 removePropertiesIfPresent(componentProperties, Characteristics.CACHE);
053 return new ThreadCached(super.addComponentAdapter(componentMonitor, lifecycleStrategy, componentProperties, adapter));
054 }
055 }