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     *****************************************************************************/
010    package org.picocontainer.behaviors;
011    
012    
013    import org.picocontainer.ComponentAdapter;
014    import org.picocontainer.PicoContainer;
015    import org.picocontainer.PicoCompositionException;
016    
017    import java.util.concurrent.locks.Lock;
018    import java.util.concurrent.locks.ReentrantLock;
019    import java.lang.reflect.Type;
020    
021    /**
022     * @author Paul Hammant
023     */
024    @SuppressWarnings("serial")
025    public class Locked<T> extends AbstractBehavior<T> {
026            
027            /**
028             * Reentrant lock.
029             */
030            private Lock lock = new ReentrantLock();
031    
032        public Locked(ComponentAdapter<T> delegate) {
033            super(delegate);
034        }
035    
036        public T getComponentInstance(PicoContainer container, Type into) throws PicoCompositionException {
037            T retVal = null;
038            lock.lock();
039            try {
040              retVal = super.getComponentInstance(container, into);
041            }
042            finally {
043              lock.unlock();
044            }
045            return retVal;
046        }
047    
048        public String getDescriptor() {
049            return "Locked";
050        }
051    
052    }