KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > nanocontainer > DefaultNanoContainer


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 Aslak Hellesoy and Paul Hammant *
9  *****************************************************************************/

10
11 package org.nanocontainer;
12
13 import java.net.URL JavaDoc;
14 import java.security.AccessController JavaDoc;
15 import java.security.PermissionCollection JavaDoc;
16 import java.security.PrivilegedAction JavaDoc;
17 import java.util.ArrayList JavaDoc;
18 import java.util.HashMap JavaDoc;
19 import java.util.List JavaDoc;
20 import java.util.Map JavaDoc;
21
22 import org.nanocontainer.script.NanoContainerMarkupException;
23 import org.picocontainer.ComponentAdapter;
24 import org.picocontainer.MutablePicoContainer;
25 import org.picocontainer.Parameter;
26 import org.picocontainer.PicoIntrospectionException;
27 import org.picocontainer.PicoRegistrationException;
28 import org.picocontainer.defaults.BeanPropertyComponentAdapter;
29 import org.picocontainer.defaults.ConstantParameter;
30 import org.picocontainer.defaults.CustomPermissionsURLClassLoader;
31 import org.picocontainer.defaults.DefaultPicoContainer;
32
33 /**
34  * The default implementation of {@link NanoContainer}.
35  *
36  * @author Paul Hammant
37  * @author Aslak Hellesøy
38  */

39 public class DefaultNanoContainer implements NanoContainer {
40     private static final Map JavaDoc primitiveNameToBoxedName = new HashMap JavaDoc();
41     static {
42         primitiveNameToBoxedName.put("int", Integer JavaDoc.class.getName());
43         primitiveNameToBoxedName.put("byte", Byte JavaDoc.class.getName());
44         primitiveNameToBoxedName.put("short", Short JavaDoc.class.getName());
45         primitiveNameToBoxedName.put("long", Long JavaDoc.class.getName());
46         primitiveNameToBoxedName.put("float", Float JavaDoc.class.getName());
47         primitiveNameToBoxedName.put("double", Double JavaDoc.class.getName());
48         primitiveNameToBoxedName.put("boolean", Boolean JavaDoc.class.getName());
49     }
50
51     private final List JavaDoc classPathElements = new ArrayList JavaDoc();
52     private MutablePicoContainer picoContainer;
53     private final ClassLoader JavaDoc parentClassLoader;
54
55     private ClassLoader JavaDoc componentClassLoader;
56     private boolean componentClassLoaderLocked;
57
58     private static String JavaDoc getClassName(String JavaDoc primitiveOrClass) {
59         String JavaDoc fromMap = (String JavaDoc) primitiveNameToBoxedName.get(primitiveOrClass);
60         return fromMap != null ? fromMap : primitiveOrClass;
61     }
62
63     public DefaultNanoContainer(ClassLoader JavaDoc parentClassLoader, MutablePicoContainer picoContainer) {
64         this.parentClassLoader = parentClassLoader;
65         if (picoContainer == null) {
66             throw new NullPointerException JavaDoc("picoContainer");
67         }
68         this.picoContainer = picoContainer;
69     }
70
71     public DefaultNanoContainer(ClassLoader JavaDoc parentClassLoader) {
72         this(parentClassLoader, new DefaultPicoContainer());
73     }
74
75     public DefaultNanoContainer(MutablePicoContainer picoContainer) {
76         this(Thread.currentThread().getContextClassLoader(), picoContainer);
77     }
78
79     public DefaultNanoContainer(NanoContainer parent) {
80         this(parent.getComponentClassLoader(), new DefaultPicoContainer(parent.getPico()));
81     }
82
83     /**
84      * Beware - no parent container and no parent classloader.
85      */

86     public DefaultNanoContainer() {
87         this(Thread.currentThread().getContextClassLoader(), new DefaultPicoContainer());
88     }
89
90     public ComponentAdapter registerComponentImplementation(String JavaDoc componentImplementationClassName) throws PicoRegistrationException, ClassNotFoundException JavaDoc, PicoIntrospectionException {
91         return picoContainer.registerComponentImplementation(loadClass(componentImplementationClassName));
92     }
93
94     public ComponentAdapter registerComponentImplementation(Object JavaDoc key, String JavaDoc componentImplementationClassName) throws ClassNotFoundException JavaDoc {
95         Class JavaDoc componentImplementation = loadClass(componentImplementationClassName);
96         if (key instanceof ClassNameKey) {
97             key = loadClass(((ClassNameKey) key).getClassName());
98         }
99         return picoContainer.registerComponentImplementation(key, componentImplementation);
100     }
101
102
103     public ComponentAdapter registerComponentImplementation(Object JavaDoc key, String JavaDoc componentImplementationClassName, Parameter[] parameters) throws ClassNotFoundException JavaDoc {
104         Class JavaDoc componentImplementation = loadClass(componentImplementationClassName);
105         if (key instanceof ClassNameKey) {
106             key = loadClass(((ClassNameKey) key).getClassName());
107
108         }
109         return picoContainer.registerComponentImplementation(key, componentImplementation, parameters);
110     }
111
112     public ComponentAdapter registerComponentImplementation(Object JavaDoc key,
113                                                             String JavaDoc componentImplementationClassName,
114                                                             String JavaDoc[] parameterTypesAsString,
115                                                             String JavaDoc[] parameterValuesAsString) throws PicoRegistrationException, ClassNotFoundException JavaDoc, PicoIntrospectionException {
116         Class JavaDoc componentImplementation = getComponentClassLoader().loadClass(componentImplementationClassName);
117         if (key instanceof ClassNameKey) {
118             key = loadClass(((ClassNameKey) key).getClassName());
119
120         }
121         return registerComponentImplementation(parameterTypesAsString, parameterValuesAsString, key, componentImplementation);
122     }
123
124     public ComponentAdapter registerComponentImplementation(String JavaDoc componentImplementationClassName,
125                                                             String JavaDoc[] parameterTypesAsString,
126                                                             String JavaDoc[] parameterValuesAsString) throws PicoRegistrationException, ClassNotFoundException JavaDoc, PicoIntrospectionException {
127         Class JavaDoc componentImplementation = getComponentClassLoader().loadClass(componentImplementationClassName);
128         return registerComponentImplementation(parameterTypesAsString, parameterValuesAsString, componentImplementation, componentImplementation);
129     }
130
131     private ComponentAdapter registerComponentImplementation(String JavaDoc[] parameterTypesAsString, String JavaDoc[] parameterValuesAsString, Object JavaDoc key, Class JavaDoc componentImplementation) throws ClassNotFoundException JavaDoc {
132         Parameter[] parameters = new Parameter[parameterTypesAsString.length];
133         for (int i = 0; i < parameters.length; i++) {
134             Object JavaDoc value = BeanPropertyComponentAdapter.convert(parameterTypesAsString[i], parameterValuesAsString[i], getComponentClassLoader());
135             parameters[i] = new ConstantParameter(value);
136         }
137         return picoContainer.registerComponentImplementation(key, componentImplementation, parameters);
138     }
139
140     private Class JavaDoc loadClass(final String JavaDoc className) throws ClassNotFoundException JavaDoc {
141         ClassLoader JavaDoc classLoader = getComponentClassLoader();
142         String JavaDoc cn = getClassName(className);
143         return classLoader.loadClass(cn);
144     }
145
146     public ClassPathElement addClassLoaderURL(URL JavaDoc url) {
147         if (componentClassLoaderLocked) throw new IllegalStateException JavaDoc("ClassLoader URLs cannot be added once this instance is locked");
148
149         ClassPathElement classPathElement = new ClassPathElement(url);
150         classPathElements.add(classPathElement);
151         return classPathElement;
152     }
153
154     public ClassLoader JavaDoc getComponentClassLoader() {
155         if (componentClassLoader == null) {
156             componentClassLoaderLocked = true;
157             componentClassLoader = (ClassLoader JavaDoc) AccessController.doPrivileged(new PrivilegedAction JavaDoc() {
158                 public Object JavaDoc run() {
159                     return new CustomPermissionsURLClassLoader(getURLs(classPathElements), makePermissions(), parentClassLoader);
160                 }
161             });
162         }
163         return componentClassLoader;
164     }
165
166     public MutablePicoContainer getPico() {
167         return picoContainer;
168     }
169
170     private Map JavaDoc makePermissions() {
171         Map JavaDoc permissionsMap = new HashMap JavaDoc();
172         for (int i = 0; i < classPathElements.size(); i++) {
173             ClassPathElement cpe = (ClassPathElement) classPathElements.get(i);
174             PermissionCollection JavaDoc permissionCollection = cpe.getPermissionCollection();
175             permissionsMap.put(cpe.getUrl(), permissionCollection);
176         }
177         return permissionsMap;
178     }
179
180     private URL JavaDoc[] getURLs(List JavaDoc classPathElemelements) {
181         final URL JavaDoc[] urls = new URL JavaDoc[classPathElemelements.size()];
182         for(int i = 0; i < urls.length; i++) {
183             urls[i] = ((ClassPathElement) classPathElemelements.get(i)).getUrl();
184         }
185         return urls;
186     }
187
188     public Object JavaDoc getComponentInstanceOfType(String JavaDoc componentType) {
189         try {
190             Class JavaDoc compType = getComponentClassLoader().loadClass(componentType);
191             return picoContainer.getComponentInstanceOfType(compType);
192         } catch (ClassNotFoundException JavaDoc e) {
193             throw new NanoContainerMarkupException("Can't resolve class as type '" + componentType + "'");
194         }
195     }
196
197     public MutablePicoContainer addDecoratingPicoContainer(Class JavaDoc picoContainerClass) {
198         DefaultPicoContainer pico = new DefaultPicoContainer();
199         pico.registerComponentImplementation(MutablePicoContainer.class, picoContainerClass, new Parameter[] { new ConstantParameter(picoContainer) });
200         picoContainer = (MutablePicoContainer) pico.getComponentInstanceOfType(MutablePicoContainer.class);
201         return picoContainer;
202     }
203
204 }
205
Popular Tags