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     * factory class creating guard behaviour
025     *
026     * @author Paul Hammant
027     */
028    @SuppressWarnings("serial")
029    public class Guarding extends AbstractBehaviorFactory {
030    
031        public <T> ComponentAdapter<T> createComponentAdapter(
032                ComponentMonitor componentMonitor,
033                LifecycleStrategy lifecycleStrategy,
034                Properties componentProperties, Object componentKey,
035                Class<T> componentImplementation, Parameter... parameters)
036                throws PicoCompositionException {
037            String guard = getAndRemovePropertiesIfPresentByKey(componentProperties, Characteristics.GUARD);
038            ComponentAdapter<T> delegate = super.createComponentAdapter(componentMonitor, lifecycleStrategy,
039                    componentProperties, componentKey, componentImplementation, parameters);
040            if (guard == null) {
041                return delegate;
042            } else {
043                return componentMonitor.newBehavior(new Guarded<T>(delegate, guard));
044            }
045    
046        }
047    
048        public <T> ComponentAdapter<T> addComponentAdapter(
049                ComponentMonitor componentMonitor,
050                LifecycleStrategy lifecycleStrategy,
051                Properties componentProperties, ComponentAdapter<T> adapter) {
052            String guard = getAndRemovePropertiesIfPresentByKey(componentProperties, Characteristics.GUARD);
053            ComponentAdapter<T> delegate = super.addComponentAdapter(componentMonitor, lifecycleStrategy, componentProperties, adapter);
054            if (guard == null) {
055                return delegate;
056            } else {
057                return componentMonitor.newBehavior(componentMonitor.newBehavior(new Guarded<T>(delegate, guard)));
058            }
059        }
060    
061    }