KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > nanocontainer > script > ScriptedContainerBuilderFactory


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

9
10 package org.nanocontainer.script;
11
12 import java.io.File JavaDoc;
13 import java.io.FileNotFoundException JavaDoc;
14 import java.io.FileReader JavaDoc;
15 import java.io.IOException JavaDoc;
16 import java.io.Reader JavaDoc;
17 import java.net.URL JavaDoc;
18 import org.nanocontainer.DefaultNanoContainer;
19 import org.picocontainer.ComponentAdapter;
20 import org.picocontainer.defaults.DefaultPicoContainer;
21
22 /**
23  * The main class for configuration of PicoContainer with various scripting languages.
24  * When using the constructors taking a file, the extensions must be one of the following:
25  * <ul>
26  * <li>.groovy</li>
27  * <li>.bsh</li>
28  * <li>.js</li>
29  * <li>.py</li>
30  * <li>.xml</li>
31  * </ul>
32  * -And the content of the file likewise. See <a HREF="http://docs.codehaus.org/display/NANO/NanoContainer">NanoContainer documentation</a>
33  * for details.
34  *
35  * @author Paul Hammant
36  * @author Aslak Helles&oslah;y
37  * @author Obie Fernandez
38  * @author Michael Rimov
39  */

40 public class ScriptedContainerBuilderFactory {
41
42     /**
43      * @deprecated Since NanoContainer RC-2. (14-Dec-2005). Use ScriptBuilderResolver.GROOVY
44      * instead.
45      */

46     public static final String JavaDoc GROOVY = ".groovy";
47
48     /**
49      * @deprecated Since NanoContainer RC-2. (14-Dec-2005). Use ScriptBuilderResolver.BEANSHELL
50      * instead.
51      */

52     public static final String JavaDoc BEANSHELL = ".bsh";
53
54     /**
55      * @deprecated Since NanoContainer RC-2. (14-Dec-2005). Use ScriptBuilderResolver.JAVASCRIPT
56      * instead.
57      */

58     public static final String JavaDoc JAVASCRIPT = ".js";
59
60     /**
61      * @deprecated Since NanoContainer RC-2. (14-Dec-2005). Use ScriptBuilderResolver.JYTHON
62      * instead.
63      */

64     public static final String JavaDoc JYTHON = ".py";
65
66     /**
67      * @deprecated Since NanoContainer RC-2. (14-Dec-2005). Use ScriptBuilderResolver.XML
68      * instead.
69      */

70     public static final String JavaDoc XML = ".xml";
71
72
73     private ScriptedContainerBuilder containerBuilder;
74
75
76
77     public ScriptedContainerBuilderFactory(File JavaDoc compositionFile, ClassLoader JavaDoc classLoader) throws IOException JavaDoc, ClassNotFoundException JavaDoc {
78         this(compositionFile, classLoader, new ScriptBuilderResolver());
79     }
80
81     /**
82      * Added since Nano RC-2. This allows you to add/modify registered builders to replace
83      * script handling or add new extensions by modifying a constructed ScriptBuilderResolver
84      * before constructing this object.
85      * @param compositionFile File The script file.
86      * @param classLoader ClassLoader for class resolution once we resolve what the name of the
87      * builder should be..
88      * @param builderClassResolver ScriptBuilderResolver the resolver for figuring out
89      * file names to container builder class names.
90      * @throws IOException upon java.io.File name resolution error.
91      * @throws ClassNotFoundException If there is an error loading
92      * the specified builder using the specified classloader.
93      * @throws UnsupportedScriptTypeException if the extension of the file
94      * does not match that of any known script.
95      */

96     public ScriptedContainerBuilderFactory(File JavaDoc compositionFile, ClassLoader JavaDoc classLoader, ScriptBuilderResolver builderClassResolver) throws IOException JavaDoc, ClassNotFoundException JavaDoc ,UnsupportedScriptTypeException {
97         this(new FileReader JavaDoc(fileExists(compositionFile)), builderClassResolver.getBuilderClassName(compositionFile), classLoader);
98     }
99
100
101     public ScriptedContainerBuilderFactory(File JavaDoc compositionFile) throws IOException JavaDoc, ClassNotFoundException JavaDoc {
102         this(compositionFile, Thread.currentThread().getContextClassLoader());
103     }
104
105     public ScriptedContainerBuilderFactory(URL JavaDoc compositionURL) throws ClassNotFoundException JavaDoc {
106         this(compositionURL, Thread.currentThread().getContextClassLoader(),new ScriptBuilderResolver());
107     }
108
109     /**
110      *
111      * Added since Nano RC-2. This allows you to add/modify registered builders to replace
112      * script handling or add new extensions by modifying a constructed ScriptBuilderResolver
113      * before constructing this object.
114      * @param compositionURL The script URL.
115      * @param builderClassResolver ScriptBuilderResolver the resolver for figuring out
116      * file names to container builder class names.
117      * @param classLoader ClassLoader for class resolution once we resolve what the name of the
118      * builder should be..
119      * @throws ClassNotFoundException If there is an error loading
120      * the specified builder using the specified classloader.
121      * @throws UnsupportedScriptTypeException if the extension of the file
122      * does not match that of any known script.
123      */

124     public ScriptedContainerBuilderFactory(URL JavaDoc compositionURL, ClassLoader JavaDoc classLoader, ScriptBuilderResolver builderClassResolver) throws ClassNotFoundException JavaDoc ,UnsupportedScriptTypeException {
125         this(compositionURL, builderClassResolver.getBuilderClassName(compositionURL), classLoader);
126     }
127
128
129     public ScriptedContainerBuilderFactory(URL JavaDoc compositionURL, String JavaDoc builderClassName, ClassLoader JavaDoc contextClassLoader) throws ClassNotFoundException JavaDoc {
130         createContainerBuilder(compositionURL, contextClassLoader, builderClassName);
131     }
132
133
134     public ScriptedContainerBuilderFactory(Reader JavaDoc composition, String JavaDoc builderClass) throws ClassNotFoundException JavaDoc {
135         this(composition, builderClass, Thread.currentThread().getContextClassLoader());
136     }
137
138     /**
139      * Allows you to create a factory that isntantiats the builder class you desire.
140      * @param composition Reader the script you wish to create the builder for.
141      * @param builderClass String the builder class that instantiate.
142      * @param classLoader ClassLoader the classloader to use for instantiation.
143      * @throws ClassNotFoundException if the specified class cannot be found.
144      */

145     public ScriptedContainerBuilderFactory(Reader JavaDoc composition, String JavaDoc builderClass, ClassLoader JavaDoc classLoader) throws ClassNotFoundException JavaDoc {
146         createContainerBuilder(composition, classLoader, builderClass);
147     }
148
149
150     /**
151      * Performs the actual instantiation of the builder.
152      * @param composition Object Either a URL or a File, it doesn't matter which here.
153      * @param classLoader ClassLoader the classloader ot use for classname resolution
154      * and loading.
155      * @param builderClass String the builder class to load.
156      * @throws ClassNotFoundException if the specified builder class cannot be loaded.
157      */

158     private void createContainerBuilder(Object JavaDoc composition, ClassLoader JavaDoc classLoader, String JavaDoc builderClass) throws ClassNotFoundException JavaDoc {
159         DefaultNanoContainer defaultNanoContainer;
160         {
161             // transient.
162
DefaultPicoContainer factory = new DefaultPicoContainer();
163             if(composition == null) {
164                 throw new NullPointerException JavaDoc("composition can't be null");
165             }
166             factory.registerComponentInstance(composition);
167
168             if(classLoader == null) {
169                 // on some weird JVMs (like jeode) Thread.currentThread().getContextClassLoader() returns null !?!?
170
//Found out on JDK 1.5 javadocs that Thread.currentThread().getContextClassLoader() MAY return null
171
//while Class.getClassLoader() should NEVER return null. -MR
172
//
173
//
174
classLoader = getClass().getClassLoader();
175             }
176             factory.registerComponentInstance(classLoader);
177
178             //
179
//If we don't specify the classloader here, some of the things that make
180
//up a nanocontainer may bomb. And we're only talking a reload
181
//within a webapp! -MR
182
//
183
defaultNanoContainer = new DefaultNanoContainer(classLoader,factory);
184         }
185         ComponentAdapter componentAdapter = defaultNanoContainer.registerComponentImplementation(builderClass);
186         containerBuilder = (ScriptedContainerBuilder) componentAdapter.getComponentInstance(defaultNanoContainer.getPico());
187     }
188
189     private static File JavaDoc fileExists(final File JavaDoc file) throws FileNotFoundException JavaDoc {
190         if (file.exists()) {
191             return file;
192         } else {
193             //todo a proper exception.
194
throw new FileNotFoundException JavaDoc("File " + file.getAbsolutePath() + " does not exist.");
195         }
196     }
197
198     /**
199      * This function does not support custom file type resolving -- for backwards
200      * compatibility, it uses a fresh instance of ScriptBuilderResolver for
201      * each invocation. Use ScriptBuilderResolver instead.
202      * @param extension String the file extension to res
203      * @return String the classname to use for the specified extension.
204      * @deprecated Since NanoContainer 1.0 RC-2. Use the class ScriptBuilderResolver
205      * for this functionality.
206      */

207     public static String JavaDoc getBuilderClassName(final String JavaDoc extension) {
208         return new ScriptBuilderResolver().getBuilderClassName(extension);
209     }
210
211
212     /**
213      * Retrieve the created container builder instance.
214      * @return ScriptedContainerBuilder instance, never null.
215      */

216     public ScriptedContainerBuilder getContainerBuilder() {
217         return containerBuilder;
218     }
219
220 }
221
Popular Tags