KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > picocontainer > defaults > CachingComponentAdapter


1 /*****************************************************************************
2  * Copyright (c) PicoContainer Organization. All rights reserved. *
3  * ------------------------------------------------------------------------- *
4  * The software in this package is published under the terms of the BSD *
5  * style license a copy of which has been included with this distribution in *
6  * the LICENSE.txt file. *
7  * *
8  * Idea by Rachel Davies, Original code by Aslak Hellesoy and Paul Hammant *
9  *****************************************************************************/

10
11 package org.picocontainer.defaults;
12
13 import org.picocontainer.ComponentAdapter;
14 import org.picocontainer.PicoInitializationException;
15 import org.picocontainer.PicoIntrospectionException;
16 import org.picocontainer.PicoContainer;
17
18 /**
19  * This ComponentAdapter caches the instance.
20  * @version $Revision: 1570 $
21  */

22 public class CachingComponentAdapter extends DecoratingComponentAdapter {
23
24     private ObjectReference instanceReference;
25
26     public CachingComponentAdapter(ComponentAdapter delegate) {
27         this(delegate, new SimpleReference());
28     }
29
30     public CachingComponentAdapter(ComponentAdapter delegate, ObjectReference instanceReference) {
31         super(delegate);
32         this.instanceReference = instanceReference;
33     }
34
35     public Object JavaDoc getComponentInstance(PicoContainer container)
36             throws PicoInitializationException, PicoIntrospectionException, AssignabilityRegistrationException, NotConcreteRegistrationException {
37         if (instanceReference.get() == null) {
38             instanceReference.set(super.getComponentInstance(container));
39         }
40         return instanceReference.get();
41     }
42 }
43
Popular Tags