    001    package org.nanocontainer.script.bsh;
    002
    003    import java.io.IOException;
    004    import java.io.Reader;
    005    import java.net.URL;
    006
    007    import org.nanocontainer.script.NanoContainerMarkupException;
    008    import org.nanocontainer.script.ScriptedContainerBuilder;
    009    import org.picocontainer.PicoContainer;
    010
    011    import bsh.EvalError;
    012    import bsh.Interpreter;
    013
    014    /**
    015     * {@inheritDoc}
    016     * The script has to assign a "pico" variable with an instance of
    017     * {@link org.picocontainer.PicoContainer}.
    018     * There is an implicit variable named "parent" that may contain a reference to a parent
    019     * container. It is recommended to use this as a constructor argument to the instantiated
    020     * PicoContainer.
    021     *
    022     * @author Aslak Helles&oslash;y
    023     * @author Michael Rimov
    024     * @author Mauro Talevi
    025     * @version $Revision: 3144 $
    026     */
    027    public class BeanShellContainerBuilder extends ScriptedContainerBuilder {
    028
    029        public BeanShellContainerBuilder(Reader script, ClassLoader classLoader) {
    030            super(script, classLoader);
    031        }
    032
    033        public BeanShellContainerBuilder(URL script, ClassLoader classLoader) {
    034            super(script, classLoader);
    035        }
    036
    037        protected PicoContainer createContainerFromScript(PicoContainer parentContainer, Object assemblyScope) {
    038            Interpreter i = new Interpreter();
    039            try {
    040                i.set("parent", parentContainer);
    041                i.set("assemblyScope", assemblyScope);
    042                i.setClassLoader(this.getClassLoader());
    043                i.eval(getScriptReader(), i.getNameSpace(), "nanocontainer.bsh");
    044                return (PicoContainer) i.get("pico");
    045            } catch (EvalError e) {
    046                throw new NanoContainerMarkupException(e);
    047            } catch (IOException e) {
    048                throw new NanoContainerMarkupException(e);
    049            }
    050        }
    051    }