KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > nanocontainer > reflection > AbstractNanoPicoContainer


1 /*****************************************************************************
2  * Copyright (C) NanoContainer 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 Paul Hammant *
9  *****************************************************************************/

10
11 package org.nanocontainer.reflection;
12
13 import java.io.Serializable JavaDoc;
14 import java.net.URL JavaDoc;
15 import java.util.Collection JavaDoc;
16 import java.util.HashMap JavaDoc;
17 import java.util.Iterator JavaDoc;
18 import java.util.Map JavaDoc;
19 import org.nanocontainer.ClassPathElement;
20 import org.nanocontainer.DefaultNanoContainer;
21 import org.nanocontainer.NanoContainer;
22 import org.nanocontainer.NanoPicoContainer;
23 import org.picocontainer.ComponentAdapter;
24 import org.picocontainer.MutablePicoContainer;
25 import org.picocontainer.Parameter;
26 import org.picocontainer.PicoContainer;
27 import org.picocontainer.PicoException;
28 import org.picocontainer.PicoIntrospectionException;
29 import org.picocontainer.PicoRegistrationException;
30 import org.picocontainer.alternatives.AbstractDelegatingMutablePicoContainer;
31
32 /**
33  * A base class for NanoPicoContainers. As well as the functionality indicated by the interface it
34  * implements, extenders of this class will have named child component capability.
35  *
36  * @author Paul Hammant
37  * @version $Revision: 2964 $
38  */

39 public abstract class AbstractNanoPicoContainer extends AbstractDelegatingMutablePicoContainer implements NanoPicoContainer, Serializable JavaDoc {
40
41     protected Map JavaDoc namedChildContainers = new HashMap JavaDoc();
42
43     // Serializable cannot be cascaded into DefaultNanoContainer's referenced classes
44
// need to implement custom Externalisable regime.
45
protected transient NanoContainer container;
46
47
48     protected AbstractNanoPicoContainer(MutablePicoContainer delegate, ClassLoader JavaDoc classLoader) {
49         super(delegate);
50         container = new DefaultNanoContainer(classLoader, delegate);
51     }
52
53     public final Object JavaDoc getComponentInstance(Object JavaDoc componentKey) throws PicoException {
54
55         Object JavaDoc instance = getDelegate().getComponentInstance(componentKey);
56
57         if (instance != null) {
58             return instance;
59         }
60
61         ComponentAdapter componentAdapter = null;
62         if (componentKey.toString().startsWith("*")) {
63             String JavaDoc candidateClassName = componentKey.toString().substring(1);
64             Collection JavaDoc cas = getComponentAdapters();
65             for (Iterator JavaDoc it = cas.iterator(); it.hasNext();) {
66                 ComponentAdapter ca = (ComponentAdapter) it.next();
67                 Object JavaDoc key = ca.getComponentKey();
68                 if (key instanceof Class JavaDoc && candidateClassName.equals(((Class JavaDoc) key).getName())) {
69                     componentAdapter = ca;
70                     break;
71                 }
72             }
73         }
74         if (componentAdapter != null) {
75             return componentAdapter.getComponentInstance(this);
76         } else {
77             return getComponentInstanceFromChildren(componentKey);
78         }
79     }
80
81     private Object JavaDoc getComponentInstanceFromChildren(Object JavaDoc componentKey) {
82         String JavaDoc componentKeyPath = componentKey.toString();
83         int ix = componentKeyPath.indexOf('/');
84         if (ix != -1) {
85             String JavaDoc firstElement = componentKeyPath.substring(0, ix);
86             String JavaDoc remainder = componentKeyPath.substring(ix + 1, componentKeyPath.length());
87             Object JavaDoc o = getNamedContainers().get(firstElement);
88             if (o != null) {
89                 MutablePicoContainer child = (MutablePicoContainer) o;
90                 return child.getComponentInstance(remainder);
91             }
92         }
93         return null;
94     }
95
96     public final MutablePicoContainer makeChildContainer() {
97         return makeChildContainer("containers" + namedChildContainers.size());
98     }
99
100     /**
101      * Makes a child container with the same basic characteristics of <tt>this</tt>
102      * object (ComponentAdapterFactory, PicoContainer type, LifecycleManager, etc)
103      * @param name the name of the child container
104      * @return The child MutablePicoContainer
105      */

106     public MutablePicoContainer makeChildContainer(String JavaDoc name) {
107         AbstractNanoPicoContainer child = createChildContainer();
108         MutablePicoContainer parentDelegate = getDelegate();
109         parentDelegate.removeChildContainer(child.getDelegate());
110         parentDelegate.addChildContainer(child);
111         namedChildContainers.put(name, child);
112         return child;
113     }
114
115     protected abstract AbstractNanoPicoContainer createChildContainer();
116
117     public boolean removeChildContainer(PicoContainer child) {
118         boolean result = getDelegate().removeChildContainer(child);
119         Iterator JavaDoc children = namedChildContainers.entrySet().iterator();
120         while (children.hasNext()) {
121             Map.Entry JavaDoc e = (Map.Entry JavaDoc) children.next();
122             PicoContainer pc = (PicoContainer) e.getValue();
123             if (pc == child) {
124                 children.remove();
125             }
126         }
127         return result;
128     }
129
130     protected final Map JavaDoc getNamedContainers() {
131         return namedChildContainers;
132     }
133
134     public Object JavaDoc getComponentInstanceOfType(String JavaDoc componentType) {
135         return container.getComponentInstanceOfType(componentType);
136     }
137
138     public MutablePicoContainer addDecoratingPicoContainer(Class JavaDoc picoContainerClass) {
139         return container.addDecoratingPicoContainer(picoContainerClass);
140     }
141
142
143     public ClassPathElement addClassLoaderURL(URL JavaDoc url) {
144         return container.addClassLoaderURL(url);
145     }
146
147     public ComponentAdapter registerComponentImplementation(String JavaDoc componentImplementationClassName) throws PicoRegistrationException, ClassNotFoundException JavaDoc, PicoIntrospectionException {
148         return container.registerComponentImplementation(componentImplementationClassName);
149     }
150
151     public ComponentAdapter registerComponentImplementation(Object JavaDoc key, String JavaDoc componentImplementationClassName) throws ClassNotFoundException JavaDoc {
152         return container.registerComponentImplementation(key, componentImplementationClassName);
153     }
154
155     public ComponentAdapter registerComponentImplementation(Object JavaDoc key, String JavaDoc componentImplementationClassName, Parameter[] parameters) throws ClassNotFoundException JavaDoc {
156         return container.registerComponentImplementation(key, componentImplementationClassName, parameters);
157     }
158
159     public ComponentAdapter registerComponentImplementation(Object JavaDoc key, String JavaDoc componentImplementationClassName, String JavaDoc[] parameterTypesAsString, String JavaDoc[] parameterValuesAsString) throws PicoRegistrationException, ClassNotFoundException JavaDoc, PicoIntrospectionException {
160         return container.registerComponentImplementation(key, componentImplementationClassName, parameterTypesAsString, parameterValuesAsString);
161     }
162
163     public ComponentAdapter registerComponentImplementation(String JavaDoc componentImplementationClassName, String JavaDoc[] parameterTypesAsString, String JavaDoc[] parameterValuesAsString) throws PicoRegistrationException, ClassNotFoundException JavaDoc, PicoIntrospectionException {
164         return container.registerComponentImplementation(componentImplementationClassName, parameterTypesAsString, parameterValuesAsString);
165     }
166
167
168     //TODO Should this method be the NanoContainer interface only?
169
public MutablePicoContainer getPico() {
170         return this;
171     }
172
173     public ClassLoader JavaDoc getComponentClassLoader() {
174         return container.getComponentClassLoader();
175     }
176
177     public boolean addChildContainer(PicoContainer child) {
178         boolean result = getDelegate().addChildContainer(child);
179
180
181         namedChildContainers.put("containers" + namedChildContainers.size(), child);
182         return result;
183     }
184
185     public void addChildContainer(String JavaDoc name, PicoContainer child) {
186
187         super.addChildContainer(child);
188
189         namedChildContainers.put(name, child);
190     }
191
192 }
193
Popular Tags