diff -rupN trunk/jython/src/org/python/util/PyServlet.java new/jython/src/org/python/util/PyServlet.java --- trunk/jython/src/org/python/util/PyServlet.java 2009-08-02 00:25:55.000000000 +0000 +++ new/jython/src/org/python/util/PyServlet.java 2009-08-02 00:41:11.000000000 +0000 @@ -151,7 +151,10 @@ public class PyServlet extends HttpServl spath = ((HttpServletRequest)req).getPathInfo(); } } - String rpath = getServletContext().getRealPath(spath); + String rpath = spath.replace('/', '.'); + if (rpath.charAt(0) == '.') { + rpath = rpath.substring(1); + } getServlet(rpath).service(req, res); } @@ -174,55 +177,71 @@ public class PyServlet extends HttpServl throws ServletException, IOException { CacheEntry entry = cache.get(path); - if (entry == null || new File(path).lastModified() > entry.date) { - return loadServlet(path); + + PyStringMap sysModules = (PyStringMap) Py.getSystemState().modules; + String moduleFile = null; + if (sysModules.has_key(path)) { + PyObject module = sysModules.get(new PyString(path)); + moduleFile = module.__getattr__(new PyString("__file__")).asString(); + moduleFile = moduleFile.replace("$py.class", ".py"); + } + + if (entry == null || new File(moduleFile).lastModified() > entry.date) { + if (sysModules.has_key(new PyString(path))) { + sysModules.pop(new PyString(path)); + } + return loadServlet(path, moduleFile); } return entry.servlet; } - private HttpServlet loadServlet(String path) + private HttpServlet loadServlet(String path, String moduleFile) throws ServletException, IOException { - File file = new File(path); - - HttpServlet servlet = createInstance(interp, file, HttpServlet.class); + try { + interp.exec("import " + path); + } catch (PyException e) { + throw new ServletException("No module '" + path + "' found", e); + } + PyObject servletModule = interp.eval(path); + + String className = path.substring(path.lastIndexOf('.') + 1); + PyObject servletClass; try { - servlet.init(getServletConfig()); + servletClass = interp.eval(path + "." + className); } catch (PyException e) { - throw new ServletException(e); + throw new ServletException("No class '" + className + "' defined in module '" + path + "'", e); } - cache.put(path, new CacheEntry(servlet, file.lastModified())); - return servlet; - } - - protected static T createInstance(PythonInterpreter interp, File file, Class type) - throws ServletException { - Matcher m = FIND_NAME.matcher(file.getName()); - if (!m.find()) { - throw new ServletException("I can't guess the name of the class from " - + file.getAbsolutePath()); + + if (moduleFile == null) { + moduleFile = servletModule.__getattr__(new PyString("__file__")).asString(); + moduleFile = moduleFile.replace("$py.class", ".py"); } - String name = m.group(1); + + HttpServlet servlet = createInstance(servletClass, HttpServlet.class); try { - interp.set("__file__", file.getAbsolutePath()); - interp.execfile(file.getAbsolutePath()); - PyObject cls = interp.get(name); - if (cls == null) { - throw new ServletException("No callable (class or function) named " + name + " in " - + file.getAbsolutePath()); - } - PyObject pyServlet = cls.__call__(); - Object o = pyServlet.__tojava__(type); - if (o == Py.NoConversion) { - throw new ServletException("The value from " + name + " must extend " - + type.getSimpleName()); - } - @SuppressWarnings("unchecked") - T asT = (T)o; - return asT; + servlet.init(getServletConfig()); } catch (PyException e) { throw new ServletException(e); } + cache.put(path, new CacheEntry(servlet, new File(moduleFile).lastModified())); + return servlet; + } + + protected static T createInstance(PyObject cls, Class type) + throws ServletException { + try { + PyObject pyServlet = cls.__call__(); + Object o = pyServlet.__tojava__(type); + if (o == Py.NoConversion) { + throw new ServletException("Servlet classes must extend " + type.getSimpleName()); + } + @SuppressWarnings("unchecked") + T asT = (T) o; + return asT; + } catch (PyException e) { + throw new ServletException(e); + } } private void destroyCache() { @@ -242,8 +261,6 @@ public class PyServlet extends HttpServl } } - private static final Pattern FIND_NAME = Pattern.compile("([^/]+)\\.py$"); - private PythonInterpreter interp; private Map cache = Generic.map(); } \ No newline at end of file