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 * Original code by                                                          *
009 *****************************************************************************/
010package org.picocontainer.behaviors;
011
012import org.picocontainer.ComponentAdapter;
013import org.picocontainer.Parameter;
014import org.picocontainer.ComponentMonitor;
015import org.picocontainer.LifecycleStrategy;
016import org.picocontainer.Characteristics;
017import org.picocontainer.behaviors.AbstractBehaviorFactory;
018
019import java.util.Properties;
020
021/**
022 * This behavior factory provides java.util.concurrent locks.  It is recommended to be used instead
023 * of {@link org.picocontainer.behaviors.Synchronizing} since it results in better performance.
024 * @author Aslak Hellesøy
025 * @author Paul Hammant.
026 */
027@SuppressWarnings("serial")
028public class Locking extends AbstractBehaviorFactory {
029
030    /** {@inheritDoc} **/
031        public <T> ComponentAdapter<T> createComponentAdapter(ComponentMonitor componentMonitor,
032                                                   LifecycleStrategy lifecycleStrategy,
033                                                   Properties componentProperties,
034                                                   Object componentKey,
035                                                   Class<T> componentImplementation,
036                                                   Parameter... parameters) {
037        
038        if (removePropertiesIfPresent(componentProperties, Characteristics.NO_LOCK)) {
039           return super.createComponentAdapter(
040                    componentMonitor,
041                    lifecycleStrategy,
042                    componentProperties,
043                    componentKey,
044                    componentImplementation,
045                    parameters);
046        }
047        
048        removePropertiesIfPresent(componentProperties, Characteristics.LOCK);
049        return componentMonitor.newBehavior(new Locked<T>(super.createComponentAdapter(
050            componentMonitor,
051            lifecycleStrategy,
052            componentProperties,
053            componentKey,
054            componentImplementation,
055            parameters)));
056    }
057
058    /** {@inheritDoc} **/
059        public <T> ComponentAdapter<T> addComponentAdapter(ComponentMonitor componentMonitor,
060                                                LifecycleStrategy lifecycleStrategy,
061                                                Properties componentProperties,
062                                                ComponentAdapter<T> adapter) {
063        if (removePropertiesIfPresent(componentProperties, Characteristics.NO_LOCK)) {
064                return super.addComponentAdapter(componentMonitor,
065                    lifecycleStrategy,
066                    componentProperties,
067                    adapter);
068        }       
069        
070        removePropertiesIfPresent(componentProperties, Characteristics.LOCK);
071        return componentMonitor.newBehavior(new Locked<T>(super.addComponentAdapter(componentMonitor,
072                                                          lifecycleStrategy,
073                                                          componentProperties,
074                                                          adapter)));
075    }
076}