KickJava   Java API By Example, From Geeks To Geeks.

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


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  *****************************************************************************/

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

33 public class ImplementationHidingPicoContainer extends AbstractDelegatingMutablePicoContainer implements Serializable JavaDoc {
34
35     private ComponentAdapterFactory caf;
36     private LifecycleManager lifecycleManager;
37
38     /**
39      * Creates a new container with a parent container.
40      */

41     public ImplementationHidingPicoContainer(ComponentAdapterFactory caf, PicoContainer parent, LifecycleManager lifecycleManager) {
42         super(new DefaultPicoContainer(caf, parent, lifecycleManager));
43         this.caf = caf;
44         this.lifecycleManager = lifecycleManager;
45     }
46
47
48     public ImplementationHidingPicoContainer(ComponentAdapterFactory caf, PicoContainer parent) {
49         this(caf, parent, new DefaultLifecycleManager());
50     }
51
52     /**
53      * Creates a new container with a parent container.
54      */

55     public ImplementationHidingPicoContainer(PicoContainer parent) {
56         this(new DefaultComponentAdapterFactory(), parent);
57     }
58
59
60     /**
61      * Creates a new container with no parent container.
62      */

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