001 /***************************************************************************** 002 * Copyright (C) PicoContainer 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.picocontainer.script.jython; 011 012 import java.io.IOException; 013 import java.io.Reader; 014 import java.net.URL; 015 016 import org.picocontainer.PicoContainer; 017 import org.picocontainer.script.LifecycleMode; 018 import org.picocontainer.script.ScriptedPicoContainerMarkupException; 019 import org.picocontainer.script.ScriptedContainerBuilder; 020 import org.python.util.PythonInterpreter; 021 022 /** 023 * {@inheritDoc} 024 * The script has to assign a "pico" variable with an instance of 025 * {@link PicoContainer}. 026 * There is an implicit variable named "parent" that may contain a reference to a parent 027 * container. It is recommended to use this as a constructor argument to the instantiated 028 * PicoContainer. 029 * 030 * @author Paul Hammant 031 * @author Mike Royle 032 * @author Aslak Hellesøy 033 * @author Mauro Talevi 034 */ 035 public class JythonContainerBuilder extends ScriptedContainerBuilder { 036 037 public JythonContainerBuilder(Reader script, ClassLoader classLoader, LifecycleMode lifecycleMode ) { 038 super(script, classLoader, lifecycleMode); 039 } 040 041 public JythonContainerBuilder(Reader script, ClassLoader classLoader) { 042 this(script,classLoader, LifecycleMode.AUTO_LIFECYCLE); 043 } 044 045 public JythonContainerBuilder(URL script, ClassLoader classLoader) { 046 this(script, classLoader, LifecycleMode.AUTO_LIFECYCLE); 047 } 048 049 public JythonContainerBuilder(URL script, ClassLoader classLoader, LifecycleMode lifecycleMode) { 050 super(script, classLoader, lifecycleMode); 051 } 052 053 protected PicoContainer createContainerFromScript(PicoContainer parentContainer, Object assemblyScope) { 054 try { 055 PythonInterpreter interpreter = new PythonInterpreter(); 056 interpreter.set("parent", parentContainer); 057 interpreter.set("assemblyScope", assemblyScope); 058 interpreter.execfile(getScriptInputStream(), "picocontainer.py"); 059 return (PicoContainer) interpreter.get("pico", PicoContainer.class); 060 } catch (IOException e) { 061 throw new ScriptedPicoContainerMarkupException(e); 062 } 063 } 064 }