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     *****************************************************************************/
009    package org.picocontainer.behaviors;
010    
011    import org.picocontainer.ComponentAdapter;
012    import org.picocontainer.LifecycleStrategy;
013    import org.picocontainer.ObjectReference;
014    import org.picocontainer.PicoCompositionException;
015    import org.picocontainer.PicoContainer;
016    import org.picocontainer.ComponentLifecycle;
017    
018    import java.lang.reflect.Type;
019    import java.io.Serializable;
020    
021    /**
022     * behaviour for allows components to be guarded by another component
023     *
024     * @author Paul Hammant
025     * @param <T>
026     */
027    @SuppressWarnings("serial")
028    public class Guarded<T> extends AbstractBehavior<T> {
029        private final String guard;
030    
031        public Guarded(ComponentAdapter delegate, String guard) {
032            super(delegate);
033            this.guard = guard;
034        }
035    
036        public T getComponentInstance(PicoContainer container, Type into) throws PicoCompositionException {
037            container.getComponent(guard);
038            return super.getComponentInstance(container, into);
039        }
040    
041        public String getDescriptor() {
042            return "Guarded(with " + guard + ")";
043        }
044    
045    
046    }