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 <strong>synchronized</strong> wrappers to control access to a particular component.
023 *  It is recommended that you use {@link org.picocontainer.behaviors.Locking} instead since it results in better performance
024 *  and does the same job.
025 * @author Aslak Helles&oslash;y
026 */
027@SuppressWarnings("serial")
028public class Synchronizing extends AbstractBehaviorFactory {
029
030        
031    /** {@inheritDoc} **/
032        public <T> ComponentAdapter<T> createComponentAdapter(ComponentMonitor componentMonitor, LifecycleStrategy lifecycleStrategy, Properties componentProperties, Object componentKey, Class<T> componentImplementation, Parameter... parameters) {
033       if (removePropertiesIfPresent(componentProperties, Characteristics.NO_SYNCHRONIZE)) {
034           return super.createComponentAdapter(
035                    componentMonitor,
036                    lifecycleStrategy,
037                    componentProperties,
038                    componentKey,
039                    componentImplementation,
040                    parameters);
041       }
042        
043        removePropertiesIfPresent(componentProperties, Characteristics.SYNCHRONIZE);
044        return componentMonitor.newBehavior(new Synchronized<T>(super.createComponentAdapter(
045            componentMonitor,
046            lifecycleStrategy,
047            componentProperties,
048            componentKey,
049            componentImplementation,
050            parameters)));
051    }
052
053    /** {@inheritDoc} **/
054    public <T> ComponentAdapter<T> addComponentAdapter(ComponentMonitor componentMonitor,
055                                                LifecycleStrategy lifecycleStrategy,
056                                                Properties componentProperties,
057                                                ComponentAdapter<T> adapter) {
058        if (removePropertiesIfPresent(componentProperties, Characteristics.NO_SYNCHRONIZE)) {
059                return super.addComponentAdapter(componentMonitor,
060                    lifecycleStrategy,
061                    componentProperties,
062                    adapter);
063        }
064        
065        removePropertiesIfPresent(componentProperties, Characteristics.SYNCHRONIZE);
066        return componentMonitor.newBehavior(new Synchronized<T>(super.addComponentAdapter(componentMonitor,
067                                         lifecycleStrategy,
068                                         componentProperties,
069                                         adapter)));
070    }
071}