1 10 package org.nanocontainer.script.rhino; 11 12 import java.io.IOException ; 13 import java.io.Reader ; 14 import java.net.URL ; 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 41 public class JavascriptContainerBuilder extends ScriptedContainerBuilder { 42 43 public JavascriptContainerBuilder(Reader script, ClassLoader classLoader) { 44 super(script, classLoader); 45 } 46 47 public JavascriptContainerBuilder(URL script, ClassLoader classLoader) { 48 super(script, classLoader); 49 } 50 51 protected PicoContainer createContainerFromScript(PicoContainer parentContainer, Object assemblyScope) { 52 final ClassLoader loader = getClassLoader(); 53 Context cx = new Context() { 54 public GeneratedClassLoader createClassLoader(ClassLoader 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 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 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 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 value = e.getValue(); 93 if (value instanceof Throwable ) { 94 throw new NanoContainerMarkupException((Throwable ) value); 95 } else { 96 throw new NanoContainerMarkupException(e); 97 } 98 } catch (IOException e) { 99 throw new NanoContainerMarkupException("IOException encountered, message -'" + e.getMessage() + "'", e); 100 } finally { 101 Context.exit(); 102 } 103 } 104 } 105 | Popular Tags |