KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > picocontainer > alternatives > ImplementationHidingCachingPicoContainer


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  * Original code by the committers *
9  *****************************************************************************/

10 package org.picocontainer.alternatives;
11
12 import org.picocontainer.ComponentAdapter;
13 import org.picocontainer.MutablePicoContainer;
14 import org.picocontainer.Parameter;
15 import org.picocontainer.PicoContainer;
16 import org.picocontainer.PicoRegistrationException;
17 import org.picocontainer.LifecycleManager;
18 import org.picocontainer.defaults.CachingComponentAdapter;
19 import org.picocontainer.defaults.CachingComponentAdapterFactory;
20 import org.picocontainer.defaults.ComponentAdapterFactory;
21 import org.picocontainer.defaults.DefaultComponentAdapterFactory;
22 import org.picocontainer.defaults.DefaultLifecycleManager;
23
24 import java.io.Serializable JavaDoc;
25
26 /**
27  * This special MutablePicoContainer hides implementations of components if the key is an interface.
28  * It's very simple. Instances that are registered directly and components registered without key
29  * are not hidden.
30  *
31  * @author Paul Hammant
32  * @version $Revision: 1845 $
33  * @since 1.1
34  */

35 public class ImplementationHidingCachingPicoContainer extends AbstractDelegatingMutablePicoContainer implements Serializable JavaDoc {
36
37     private CachingComponentAdapterFactory caf;
38     private LifecycleManager lifecycleManager;
39
40     /**
41      * Creates a new container with a parent container.
42      */

43
44     public ImplementationHidingCachingPicoContainer(ComponentAdapterFactory caf, PicoContainer parent) {
45         this(parent, new CachingComponentAdapterFactory(caf), new DefaultLifecycleManager());
46     }
47
48     public ImplementationHidingCachingPicoContainer(ComponentAdapterFactory caf, PicoContainer parent, LifecycleManager lifecyleManager) {
49         this(parent, new CachingComponentAdapterFactory(caf), lifecyleManager);
50     }
51
52     private ImplementationHidingCachingPicoContainer(PicoContainer parent, CachingComponentAdapterFactory caf, LifecycleManager lifecycleManager) {
53         super(new ImplementationHidingPicoContainer(caf, parent, lifecycleManager));
54         this.caf = caf;
55         this.lifecycleManager = lifecycleManager;
56     }
57
58     /**
59      * Creates a new container with a parent container.
60      */

61     public ImplementationHidingCachingPicoContainer(PicoContainer parent) {
62         this(new DefaultComponentAdapterFactory(), parent);
63     }
64
65
66     /**
67      * Creates a new container with no parent container.
68      */

69     public ImplementationHidingCachingPicoContainer() {
70         this(null);
71     }
72
73     public ComponentAdapter registerComponentImplementation(Object JavaDoc componentKey, Class JavaDoc componentImplementation) throws PicoRegistrationException {
74         if (componentKey instanceof Class JavaDoc) {
75             Class JavaDoc clazz = (Class JavaDoc) componentKey;
76             if (clazz.isInterface()) {
77                 ComponentAdapter delegate = caf.createComponentAdapter(componentKey, componentImplementation, null);
78                 return getDelegate().registerComponent(new CachingComponentAdapter(new ImplementationHidingComponentAdapter(delegate, true)));
79             }
80         }
81         return getDelegate().registerComponentImplementation(componentKey, componentImplementation);
82     }
83
84     public ComponentAdapter registerComponentImplementation(Object JavaDoc componentKey, Class JavaDoc componentImplementation, Parameter[] parameters) throws PicoRegistrationException {
85         if (componentKey instanceof Class JavaDoc) {
86             Class JavaDoc clazz = (Class JavaDoc) componentKey;
87             if (clazz.isInterface()) {
88                 ComponentAdapter delegate = caf.createComponentAdapter(componentKey, componentImplementation, parameters);
89                 ImplementationHidingComponentAdapter ihDelegate = new ImplementationHidingComponentAdapter(delegate, true);
90                 return getDelegate().registerComponent(new CachingComponentAdapter(ihDelegate));
91             }
92         }
93         return getDelegate().registerComponentImplementation(componentKey, componentImplementation, parameters);
94     }
95
96
97     public MutablePicoContainer makeChildContainer() {
98         ImplementationHidingCachingPicoContainer pc = new ImplementationHidingCachingPicoContainer(this, caf, lifecycleManager);
99         getDelegate().addChildContainer(pc);
100         return pc;
101
102     }
103
104 }
105
Popular Tags