    001    /*****************************************************************************
    002     * Copyright (C) NanoContainer Organization. All rights reserved.            *
    003     * ------------------------------------------------------------------------- *
    004     * The software in this package is published under the terms of the BSD      *
    005     * style license a copy of which has been included with this distribution in *
    006     * the LICENSE.txt file.                                                     *
    007     *                                                                           *
    008     * Original code by                                                          *
    009     *****************************************************************************/
    010    package org.nanocontainer.script.jython;
    011
    012    import java.io.IOException;
    013    import java.io.Reader;
    014    import java.net.URL;
    015
    016    import org.nanocontainer.script.NanoContainerMarkupException;
    017    import org.nanocontainer.script.ScriptedContainerBuilder;
    018    import org.picocontainer.PicoContainer;
    019    import org.python.util.PythonInterpreter;
    020
    021    /**
    022     * {@inheritDoc}
    023     * The script has to assign a "pico" variable with an instance of
    024     * {@link PicoContainer}.
    025     * There is an implicit variable named "parent" that may contain a reference to a parent
    026     * container. It is recommended to use this as a constructor argument to the instantiated
    027     * PicoContainer.
    028     *
    029     * @author Paul Hammant
    030     * @author Mike Royle
    031     * @author Aslak Helles&oslash;y
    032     * @author Mauro Talevi
    033     * @version $Revision: 3144 $
    034     */
    035    public class JythonContainerBuilder extends ScriptedContainerBuilder {
    036
    037        public JythonContainerBuilder(Reader script, ClassLoader classLoader) {
    038            super(script, classLoader);
    039        }
    040
    041        public JythonContainerBuilder(URL script, ClassLoader classLoader) {
    042            super(script, classLoader);
    043        }
    044
    045        protected PicoContainer createContainerFromScript(PicoContainer parentContainer, Object assemblyScope) {
    046            try {
    047                PythonInterpreter interpreter = new PythonInterpreter();
    048                interpreter.exec("from org.picocontainer.defaults import *");
    049                interpreter.exec("from org.nanocontainer import *");
    050                interpreter.exec("from org.nanocontainer.reflection import *");
    051                interpreter.exec("from java.net import *");
    052                interpreter.set("parent", parentContainer);
    053                interpreter.set("assemblyScope", assemblyScope);
    054                interpreter.execfile(getScriptInputStream(), "nanocontainer.py");
    055                return (PicoContainer) interpreter.get("pico", PicoContainer.class);
    056            } catch (IOException e) {
    057                throw new NanoContainerMarkupException(e);
    058            }
    059        }
    060    }