From: <cg...@us...> - 2009-06-23 06:19:30
|
Revision: 6496 http://jython.svn.sourceforge.net/jython/?rev=6496&view=rev Author: cgroves Date: 2009-06-23 06:17:57 +0000 (Tue, 23 Jun 2009) Log Message: ----------- Break an initializer clas out of PyServlet to keep PyServlet from being a ServletContextListener. Modified Paths: -------------- trunk/jython/src/org/python/util/PyFilter.java trunk/jython/src/org/python/util/PyServlet.java Added Paths: ----------- trunk/jython/src/org/python/util/PyServletInitializer.java Modified: trunk/jython/src/org/python/util/PyFilter.java =================================================================== --- trunk/jython/src/org/python/util/PyFilter.java 2009-06-23 02:20:07 UTC (rev 6495) +++ trunk/jython/src/org/python/util/PyFilter.java 2009-06-23 06:17:57 UTC (rev 6496) @@ -43,17 +43,16 @@ * </p> * * <pre> - * <!-- PyFilter depends on PyServlet --> - * <servlet> - * <servlet-name>PyServlet</servlet-name> - * <servlet-class>org.python.util.PyServlet</servlet-class> - * <load-on-startup>1</load-on-startup> - * </servlet> + * <!-- Initialize the Jython runtime --> + * <listener> + * <listener-class>org.python.util.PyServletInitializer</listener-class> + * <load-on-startup>1</load-on-startup> + * </listener> * * <!-- Declare a uniquely-named PyFilter --> * <filter> * <filter-name>HeaderFilter</filter-name> - * <filter-class>org.jython.util.PyFilter</filter-class> + * <filter-class>org.python.util.PyFilter</filter-class> * * <!-- The special param __filter__ gives the context-relative path to the Jython source file --> * <init-param> @@ -72,20 +71,6 @@ * <url-pattern>/*</url-pattern> * </filter-mapping> * </pre> - * - * <p> - * PyFilter depends on initialization code from PyServlet being run. If PyServlet is used to serve - * pages, this code will be executed and PyFilter will work properly. However, if you aren't using - * PyServlet, the initialization code can be invoked as a ServletContextListener instead of as an - * HttpServlet. Use the following in web.xml instead of a servlet tag: - * - * <pre> - * <listener> - * <listener-class>org.python.util.PyServlet</listener-class> - * <load-on-startup>1</load-on-startup> - * </listener> - * </pre> - * */ public class PyFilter implements Filter { public static final String FILTER_PATH_PARAM = "__filter__"; Modified: trunk/jython/src/org/python/util/PyServlet.java =================================================================== --- trunk/jython/src/org/python/util/PyServlet.java 2009-06-23 02:20:07 UTC (rev 6495) +++ trunk/jython/src/org/python/util/PyServlet.java 2009-06-23 06:17:57 UTC (rev 6496) @@ -9,8 +9,6 @@ import java.util.regex.Pattern; import javax.servlet.ServletContext; -import javax.servlet.ServletContextEvent; -import javax.servlet.ServletContextListener; import javax.servlet.ServletException; import javax.servlet.ServletRequest; import javax.servlet.ServletResponse; @@ -62,18 +60,12 @@ * * </pre> */ -public class PyServlet extends HttpServlet implements ServletContextListener { +public class PyServlet extends HttpServlet { public static final String SKIP_INIT_NAME = "skip_jython_initialization"; protected static final String INIT_ATTR = "__jython_initialized__"; - public void contextInitialized(ServletContextEvent event) { - init(new Properties(), event.getServletContext(), "a ServletContextListener", true); - } - - public void contextDestroyed(ServletContextEvent event) {} - @Override public void init() { Properties props = new Properties(); @@ -83,8 +75,18 @@ String name = (String)e.nextElement(); props.put(name, getInitParameter(name)); } - init(props, getServletContext(), "the servlet " + getServletConfig().getServletName(), - getServletConfig().getInitParameter(SKIP_INIT_NAME) != null); + boolean initialize = getServletConfig().getInitParameter(SKIP_INIT_NAME) != null; + if (getServletContext().getAttribute(INIT_ATTR) != null) { + if (initialize) { + System.err.println("Jython has already been initialized in this context, not " + + "initializing for " + getServletName() + ". Add " + SKIP_INIT_NAME + + " to as an init param to this servlet's configuration to indicate this " + + "is expected."); + } + } else if (initialize) { + init(props, getServletContext()); + } + reset(); } /** @@ -92,46 +94,37 @@ * servlet, and this is the shared init code. If both initializations are used in a single * context, the system state initialization code only runs once. */ - private void init(Properties props, ServletContext context, String initializerName, - boolean initialize) { - rootPath = context.getRealPath("/"); + protected static void init(Properties props, ServletContext context) { + String rootPath = getRootPath(context); + context.setAttribute(INIT_ATTR, true); + Properties baseProps = PySystemState.getBaseProperties(); + // Context parameters + Enumeration<?> e = context.getInitParameterNames(); + while (e.hasMoreElements()) { + String name = (String)e.nextElement(); + props.put(name, context.getInitParameter(name)); + } + if (props.getProperty("python.home") == null + && baseProps.getProperty("python.home") == null) { + props.put("python.home", rootPath + "WEB-INF" + File.separator + "lib"); + } + PySystemState.initialize(baseProps, props, new String[0]); + PySystemState.add_package("javax.servlet"); + PySystemState.add_package("javax.servlet.http"); + PySystemState.add_package("javax.servlet.jsp"); + PySystemState.add_package("javax.servlet.jsp.tagext"); + PySystemState.add_classdir(rootPath + "WEB-INF" + File.separator + "classes"); + PySystemState.add_extdir(rootPath + "WEB-INF" + File.separator + "lib", true); + } + + protected static String getRootPath(ServletContext context) { + String rootPath = context.getRealPath("/"); if (!rootPath.endsWith(File.separator)) { rootPath += File.separator; } - if (context.getAttribute(INIT_ATTR) != null) { - if (initialize) { - System.err.println("Jython has already been initialized by " - + context.getAttribute(INIT_ATTR) - + " in this context, not initializing for " + initializerName + ". Add " - + SKIP_INIT_NAME - + " to as an init param to this servlet's configuration to indicate this " - + "is expected."); - } - } else if (initialize) { - context.setAttribute(INIT_ATTR, initializerName); - Properties baseProps = PySystemState.getBaseProperties(); - // Context parameters - Enumeration<?> e = context.getInitParameterNames(); - while (e.hasMoreElements()) { - String name = (String)e.nextElement(); - props.put(name, context.getInitParameter(name)); - } - if (props.getProperty("python.home") == null - && baseProps.getProperty("python.home") == null) { - props.put("python.home", rootPath + "WEB-INF" + File.separator + "lib"); - } - PySystemState.initialize(baseProps, props, new String[0]); - PySystemState.add_package("javax.servlet"); - PySystemState.add_package("javax.servlet.http"); - PySystemState.add_package("javax.servlet.jsp"); - PySystemState.add_package("javax.servlet.jsp.tagext"); - PySystemState.add_classdir(rootPath + "WEB-INF" + File.separator + "classes"); - PySystemState.add_extdir(rootPath + "WEB-INF" + File.separator + "lib", true); - } - reset(); + return rootPath; } - @Override public void service(ServletRequest req, ServletResponse res) throws ServletException, IOException @@ -161,6 +154,7 @@ * requests. */ public void reset() { + String rootPath = getRootPath(getServletContext()); destroyCache(); interp = new PythonInterpreter(null, new PySystemState()); @@ -246,6 +240,5 @@ private static final Pattern FIND_NAME = Pattern.compile("([^/]+)\\.py$"); private PythonInterpreter interp; - private String rootPath; private Map<String, CacheEntry> cache = Generic.map(); } \ No newline at end of file Added: trunk/jython/src/org/python/util/PyServletInitializer.java =================================================================== --- trunk/jython/src/org/python/util/PyServletInitializer.java (rev 0) +++ trunk/jython/src/org/python/util/PyServletInitializer.java 2009-06-23 06:17:57 UTC (rev 6496) @@ -0,0 +1,26 @@ +package org.python.util; + +import java.util.Properties; + +import javax.servlet.ServletContextEvent; +import javax.servlet.ServletContextListener; + +/** + * Initializes the jython runtime inside a servlet engine. Should be used with {@link PyFilter} to + * initialize the system before the filter starts. Add the following to web.xml to run the + * initializer: + * + * <pre> + * <listener> + * <listener-class>org.python.util.PyServletInitializer</listener-class> + * <load-on-startup>1</load-on-startup> + * </listener> + *</pre> + */ +public class PyServletInitializer implements ServletContextListener { + public void contextInitialized(ServletContextEvent evt) { + PyServlet.init(new Properties(), evt.getServletContext()); + } + + public void contextDestroyed(ServletContextEvent evt) {} +} This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |