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 *****************************************************************************/
009package org.picocontainer.behaviors;
010
011import org.picocontainer.ComponentAdapter;
012import org.picocontainer.LifecycleStrategy;
013import org.picocontainer.ObjectReference;
014import org.picocontainer.PicoCompositionException;
015import org.picocontainer.PicoContainer;
016import org.picocontainer.ComponentLifecycle;
017
018import java.lang.reflect.Type;
019import 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")
028public 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}