KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > nanocontainer > script > rhino > JavascriptContainerBuilder


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

10 package org.nanocontainer.script.rhino;
11
12 import java.io.IOException JavaDoc;
13 import java.io.Reader JavaDoc;
14 import java.net.URL JavaDoc;
15
16 import org.mozilla.javascript.Context;
17 import org.mozilla.javascript.DefiningClassLoader;
18 import org.mozilla.javascript.GeneratedClassLoader;
19 import org.mozilla.javascript.ImporterTopLevel;
20 import org.mozilla.javascript.JavaScriptException;
21 import org.mozilla.javascript.NativeJavaObject;
22 import org.mozilla.javascript.NativeJavaPackage;
23 import org.mozilla.javascript.Script;
24 import org.mozilla.javascript.Scriptable;
25 import org.nanocontainer.script.NanoContainerMarkupException;
26 import org.nanocontainer.script.ScriptedContainerBuilder;
27 import org.picocontainer.PicoContainer;
28
29 /**
30  * {@inheritDoc}
31  * The script has to assign a "pico" variable with an instance of
32  * {@link PicoContainer}.
33  * There is an implicit variable named "parent" that may contain a reference to a parent
34  * container. It is recommended to use this as a constructor argument to the instantiated
35  * PicoContainer.
36  *
37  * @author Paul Hammant
38  * @author Aslak Hellesøy
39  * @author Mauro Talevi
40  */

41 public class JavascriptContainerBuilder extends ScriptedContainerBuilder {
42
43     public JavascriptContainerBuilder(Reader JavaDoc script, ClassLoader JavaDoc classLoader) {
44         super(script, classLoader);
45     }
46
47     public JavascriptContainerBuilder(URL JavaDoc script, ClassLoader JavaDoc classLoader) {
48         super(script, classLoader);
49     }
50
51     protected PicoContainer createContainerFromScript(PicoContainer parentContainer, Object JavaDoc assemblyScope) {
52         final ClassLoader JavaDoc loader = getClassLoader();
53         Context cx = new Context() {
54             public GeneratedClassLoader createClassLoader(ClassLoader JavaDoc parent) {
55                 return new DefiningClassLoader(loader);
56             }
57         };
58         cx = Context.enter(cx);
59
60         try {
61             Scriptable scope = new ImporterTopLevel(cx);
62             scope.put("parent", scope, parentContainer);
63             scope.put("assemblyScope", scope, assemblyScope);
64             ImporterTopLevel.importPackage(cx,
65                     scope, new NativeJavaPackage[]{
66                         new NativeJavaPackage("org.picocontainer.defaults", loader),
67                         new NativeJavaPackage("org.nanocontainer", loader),
68                         new NativeJavaPackage("org.nanocontainer.reflection", loader),
69                         // File, URL and URLClassLoader will be frequently used by scripts.
70
new NativeJavaPackage("java.net", loader),
71                         new NativeJavaPackage("java.io", loader),
72                     },
73                     null);
74             Script scriptObject = cx.compileReader(scope, getScriptReader(), "javascript", 1, null);
75             scriptObject.exec(cx, scope);
76             Object JavaDoc pico = scope.get("pico", scope);
77
78             if (pico == null) {
79                 throw new NanoContainerMarkupException("The script must define a variable named 'pico'");
80             }
81             if (!(pico instanceof NativeJavaObject)) {
82                 throw new NanoContainerMarkupException("The 'pico' variable must be of type " + NativeJavaObject.class.getName());
83             }
84             Object JavaDoc javaObject = ((NativeJavaObject) pico).unwrap();
85             if (!(javaObject instanceof PicoContainer)) {
86                 throw new NanoContainerMarkupException("The 'pico' variable must be of type " + PicoContainer.class.getName());
87             }
88             return (PicoContainer) javaObject;
89         } catch (NanoContainerMarkupException e) {
90             throw e;
91         } catch (JavaScriptException e) {
92             Object JavaDoc value = e.getValue();
93             if (value instanceof Throwable JavaDoc) {
94                 throw new NanoContainerMarkupException((Throwable JavaDoc) value);
95             } else {
96                 throw new NanoContainerMarkupException(e);
97             }
98         } catch (IOException JavaDoc e) {
99             throw new NanoContainerMarkupException("IOException encountered, message -'" + e.getMessage() + "'", e);
100         } finally {
101             Context.exit();
102         }
103     }
104 }
105
Popular Tags