KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > nanocontainer > script > jython > JythonContainerBuilder


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.jython;
11
12 import java.io.IOException JavaDoc;
13 import java.io.Reader JavaDoc;
14 import java.net.URL JavaDoc;
15
16 import org.nanocontainer.script.NanoContainerMarkupException;
17 import org.nanocontainer.script.ScriptedContainerBuilder;
18 import org.picocontainer.PicoContainer;
19 import org.python.util.PythonInterpreter;
20
21 /**
22  * {@inheritDoc}
23  * The script has to assign a "pico" variable with an instance of
24  * {@link PicoContainer}.
25  * There is an implicit variable named "parent" that may contain a reference to a parent
26  * container. It is recommended to use this as a constructor argument to the instantiated
27  * PicoContainer.
28  *
29  * @author Paul Hammant
30  * @author Mike Royle
31  * @author Aslak Hellesøy
32  * @author Mauro Talevi
33  * @version $Revision: 3144 $
34  */

35 public class JythonContainerBuilder extends ScriptedContainerBuilder {
36
37     public JythonContainerBuilder(Reader JavaDoc script, ClassLoader JavaDoc classLoader) {
38         super(script, classLoader);
39     }
40
41     public JythonContainerBuilder(URL JavaDoc script, ClassLoader JavaDoc classLoader) {
42         super(script, classLoader);
43     }
44
45     protected PicoContainer createContainerFromScript(PicoContainer parentContainer, Object JavaDoc assemblyScope) {
46         try {
47             PythonInterpreter interpreter = new PythonInterpreter();
48             interpreter.exec("from org.picocontainer.defaults import *");
49             interpreter.exec("from org.nanocontainer import *");
50             interpreter.exec("from org.nanocontainer.reflection import *");
51             interpreter.exec("from java.net import *");
52             interpreter.set("parent", parentContainer);
53             interpreter.set("assemblyScope", assemblyScope);
54             interpreter.execfile(getScriptInputStream(), "nanocontainer.py");
55             return (PicoContainer) interpreter.get("pico", PicoContainer.class);
56         } catch (IOException JavaDoc e) {
57             throw new NanoContainerMarkupException(e);
58         }
59     }
60 }
61
Popular Tags