01: /*****************************************************************************
02: * Copyright (C) PicoContainer Organization. All rights reserved. *
03: * ------------------------------------------------------------------------- *
04: * The software in this package is published under the terms of the BSD *
05: * style license a copy of which has been included with this distribution in *
06: * the LICENSE.txt file. *
07: * *
08: * Original code by *
09: *****************************************************************************/package org.picocontainer.behaviors;
10:
11: import org.picocontainer.ComponentAdapter;
12: import org.picocontainer.PicoContainer;
13: import org.picocontainer.PicoCompositionException;
14:
15: import java.util.concurrent.locks.Lock;
16: import java.util.concurrent.locks.ReentrantLock;
17:
18: /**
19: * @author Paul Hammant
20: */
21: public class Locked<T> extends AbstractBehavior<T> {
22: /**
23: * Serialization UUID.
24: */
25: private static final long serialVersionUID = 2355716085957171021L;
26:
27: /**
28: * Reentrant lock.
29: */
30: private Lock lock = new ReentrantLock();
31:
32: public Locked(ComponentAdapter<T> delegate) {
33: super (delegate);
34: }
35:
36: public T getComponentInstance(PicoContainer container)
37: throws PicoCompositionException {
38: T retVal = null;
39: lock.lock();
40: try {
41: retVal = super .getComponentInstance(container);
42: } finally {
43: lock.unlock();
44: }
45: return retVal;
46: }
47:
48: public String getDescriptor() {
49: return "Locked";
50: }
51:
52: }
|