From: <fwi...@us...> - 2008-12-12 15:47:03
|
Revision: 5751 http://jython.svn.sourceforge.net/jython/?rev=5751&view=rev Author: fwierzbicki Date: 2008-12-12 15:46:58 +0000 (Fri, 12 Dec 2008) Log Message: ----------- Dropping modjy code directly into Lib. Needed to move the code formerly in modjy.py into __init__.py in order to have a toplevel modjy/ dir (current modjy goes directly into a root namespace). Dropping com/xhaus/modjy/ModjyServlet.java into Jython's src dir, keeping com/xhaus/modjy packaging as requested by Alan Kennedy, the author of modjy. Added Paths: ----------- branches/modjy/Lib/modjy/ branches/modjy/Lib/modjy/__init__.py branches/modjy/Lib/modjy/modjy_exceptions.py branches/modjy/Lib/modjy/modjy_impl.py branches/modjy/Lib/modjy/modjy_log.py branches/modjy/Lib/modjy/modjy_params.py branches/modjy/Lib/modjy/modjy_publish.py branches/modjy/Lib/modjy/modjy_response.py branches/modjy/Lib/modjy/modjy_write.py branches/modjy/Lib/modjy/modjy_wsgi.py branches/modjy/src/com/xhaus/ branches/modjy/src/com/xhaus/modjy/ branches/modjy/src/com/xhaus/modjy/ModjyJServlet.java Added: branches/modjy/Lib/modjy/__init__.py =================================================================== --- branches/modjy/Lib/modjy/__init__.py (rev 0) +++ branches/modjy/Lib/modjy/__init__.py 2008-12-12 15:46:58 UTC (rev 5751) @@ -0,0 +1,119 @@ +### +# +# Copyright 2004-2008 Alan Kennedy. +# +# You may contact the copyright holder at this uri: +# +# http://www.xhaus.com/contact/modjy +# +# The licence under which this code is released is the Apache License v2.0. +# +# The terms and conditions of this license are listed in a file contained +# in the distribution that also contained this file, under the name +# LICENSE.txt. +# +# You may also read a copy of the license at the following web address. +# +# http://modjy.xhaus.com/LICENSE.txt +# +### + +__all__ = ['modjy', 'modjy_exceptions', 'modjy_impl', 'modjy_log', 'modjy_params', 'modjy_publish', 'modjy_response', 'modjy_write', 'modjy_wsgi',] + +import jarray +import synchronize +import sys +import types + +sys.add_package("javax.servlet") +sys.add_package("javax.servlet.http") +sys.add_package("org.python.core") + +from modjy_exceptions import * +from modjy_log import * +from modjy_params import modjy_param_mgr, modjy_servlet_params +from modjy_wsgi import modjy_wsgi +from modjy_response import start_response_object +from modjy_impl import modjy_impl +from modjy_publish import modjy_publisher + +from javax.servlet.http import HttpServlet + +class modjy_servlet(HttpServlet, modjy_publisher, modjy_wsgi, modjy_impl): + + def __init__(self): + HttpServlet.__init__(self) + + def do_param(self, name, value): + if name[:3] == 'log': + getattr(self.log, "set_%s" % name)(value) + else: + self.params[name] = value + + def get_params(self): + for pname in self.servlet_context.getInitParameterNames(): + self.do_param(pname, self.servlet_context.getInitParameter(pname)) + for pname in self.servlet.getInitParameterNames(): + self.do_param(pname, self.servlet.getInitParameter(pname)) + + def init(self, delegator): + self.servlet = delegator + self.servlet_context = self.servlet.getServletContext() + self.servlet_config = self.servlet.getServletConfig() + self.log = modjy_logger(self.servlet_context) + self.params = modjy_param_mgr(modjy_servlet_params) + self.get_params() + self.init_impl() + self.init_publisher() + import modjy_exceptions + self.exc_handler = getattr(modjy_exceptions, '%s_handler' % self.params['exc_handler'])() + + def service (self, req, resp): + wsgi_environ = {} + try: + self.dispatch_to_application(req, resp, wsgi_environ) + except ModjyException, mx: + self.log.error("Exception servicing request: %s" % str(mx)) + typ, value, tb = sys.exc_info()[:] + self.exc_handler.handle(req, resp, wsgi_environ, mx, (typ, value, tb) ) + + def get_j2ee_ns(self, req, resp): + return { + 'servlet': self.servlet, + 'servlet_context': self.servlet_context, + 'servlet_config': self.servlet_config, + 'request': req, + 'response': resp, + } + + def dispatch_to_application(self, req, resp, environ): + app_callable = self.get_app_object(req, environ) + self.set_wsgi_environment(req, resp, environ, self.params, self.get_j2ee_ns(req, resp)) + response_callable = start_response_object(req, resp) + try: + app_return = self.call_application(app_callable, environ, response_callable) + if app_return is None: + raise ReturnNotIterable("Application returned None: must return an iterable") + self.deal_with_app_return(environ, response_callable, app_return) + except ModjyException, mx: + self.raise_exc(mx.__class__, str(mx)) + except Exception, x: + self.raise_exc(ApplicationException, str(x)) + + def call_application(self, app_callable, environ, response_callable): + if self.params['multithread']: + return app_callable.__call__(environ, response_callable) + else: + return synchronize.apply_synchronized( \ + app_callable, \ + app_callable, \ + (environ, response_callable)) + + def expand_relative_path(self, path): + if path.startswith("$"): + return self.servlet.getServletContext().getRealPath(path[1:]) + return path + + def raise_exc(self, exc_class, message): + self.log.error(message) + raise exc_class(message) Added: branches/modjy/Lib/modjy/modjy_exceptions.py =================================================================== --- branches/modjy/Lib/modjy/modjy_exceptions.py (rev 0) +++ branches/modjy/Lib/modjy/modjy_exceptions.py 2008-12-12 15:46:58 UTC (rev 5751) @@ -0,0 +1,91 @@ +### +# +# Copyright 2004-2008 Alan Kennedy. +# +# You may contact the copyright holder at this uri: +# +# http://www.xhaus.com/contact/modjy +# +# The licence under which this code is released is the Apache License v2.0. +# +# The terms and conditions of this license are listed in a file contained +# in the distribution that also contained this file, under the name +# LICENSE.txt. +# +# You may also read a copy of the license at the following web address. +# +# http://modjy.xhaus.com/LICENSE.txt +# +### + +import sys +import StringIO +import traceback + +from java.lang import IllegalStateException +from java.io import IOException +from javax.servlet import ServletException + +class ModjyException(Exception): pass + +class ModjyIOException(ModjyException): pass + +class ConfigException(ModjyException): pass +class BadParameter(ConfigException): pass +class ApplicationNotFound(ConfigException): pass +class NoCallable(ConfigException): pass + +class RequestException(ModjyException): pass + +class ApplicationException(ModjyException): pass +class StartResponseNotCalled(ApplicationException): pass +class StartResponseCalledTwice(ApplicationException): pass +class ResponseCommitted(ApplicationException): pass +class HopByHopHeaderSet(ApplicationException): pass +class WrongLength(ApplicationException): pass +class BadArgument(ApplicationException): pass +class ReturnNotIterable(ApplicationException): pass +class NonStringOutput(ApplicationException): pass + +class exception_handler: + + def handle(self, req, resp, environ, exc, exc_info): + pass + + def get_status_and_message(self, req, resp, exc): + return resp.SC_INTERNAL_SERVER_ERROR, "Server configuration error" + +# +# Special exception handler for testing +# + +class testing_handler(exception_handler): + + def handle(self, req, resp, environ, exc, exc_info): + typ, value, tb = exc_info + err_msg = StringIO.StringIO() + err_msg.write("%s: %s\n" % (typ, value,) ) + err_msg.write(">Environment\n") + for k in environ.keys(): + err_msg.write("%s=%s\n" % (k, repr(environ[k])) ) + err_msg.write("<Environment\n") + err_msg.write(">TraceBack\n") + for line in traceback.format_exception(typ, value, tb): + err_msg.write(line) + err_msg.write("<TraceBack\n") + try: + status, message = self.get_status_and_message(req, resp, exc) + resp.setStatus(status) + resp.setContentLength(len(err_msg.getvalue())) + resp.getOutputStream().write(err_msg.getvalue()) + except IllegalStateException, ise: + raise exc # Let the container deal with it + +# +# Standard exception handler +# + +class standard_handler(exception_handler): + + def handle(self, req, resp, environ, exc, exc_info): + raise exc Added: branches/modjy/Lib/modjy/modjy_impl.py =================================================================== --- branches/modjy/Lib/modjy/modjy_impl.py (rev 0) +++ branches/modjy/Lib/modjy/modjy_impl.py 2008-12-12 15:46:58 UTC (rev 5751) @@ -0,0 +1,101 @@ +### +# +# Copyright 2004-2008 Alan Kennedy. +# +# You may contact the copyright holder at this uri: +# +# http://www.xhaus.com/contact/modjy +# +# The licence under which this code is released is the Apache License v2.0. +# +# The terms and conditions of this license are listed in a file contained +# in the distribution that also contained this file, under the name +# LICENSE.txt. +# +# You may also read a copy of the license at the following web address. +# +# http://modjy.xhaus.com/LICENSE.txt +# +### + +import types +import sys + +from modjy_exceptions import * + +class modjy_impl: + + def deal_with_app_return(self, environ, start_response_callable, app_return): + self.log.debug("Processing app return type: %s" % str(type(app_return))) + if isinstance(app_return, types.StringTypes): + raise ReturnNotIterable("Application returned object that was not an iterable: %s" % str(type(app_return))) + if type(app_return) is types.FileType: + pass # TBD: What to do here? can't call fileno() + if hasattr(app_return, '__len__') and callable(app_return.__len__): + expected_pieces = app_return.__len__() + else: + expected_pieces = -1 + try: + try: + ix = 0 + for next_piece in app_return: + if not isinstance(next_piece, types.StringType): + raise NonStringOutput("Application returned iterable containing non-strings: %s" % str(type(next_piece))) + if ix == 0: + # The application may have called start_response in the first iteration + if not start_response_callable.called: + raise StartResponseNotCalled("Start_response callable was never called.") + if not start_response_callable.content_length \ + and expected_pieces == 1 \ + and start_response_callable.write_callable.num_writes == 0: + # Take the length of the first piece + start_response_callable.set_content_length(len(next_piece)) + start_response_callable.write_callable(next_piece) + ix += 1 + if ix == expected_pieces: + break + if expected_pieces != -1 and ix != expected_pieces: + raise WrongLength("Iterator len() was wrong. Expected %d pieces: got %d" % (expected_pieces, ix) ) + except AttributeError, ax: + if str(ax) == "__getitem__": + raise ReturnNotIterable("Application returned object that was not an iterable: %s" % str(type(app_return))) + else: + raise ax + except TypeError, tx: + raise ReturnNotIterable("Application returned object that was not an iterable: %s" % str(type(app_return))) + except ModjyException, mx: + raise mx + except Exception, x: + raise ApplicationException(x) + finally: + if hasattr(app_return, 'close') and callable(app_return.close): + app_return.close() + + def init_impl(self): + self.do_j_env_params() + + def add_packages(self, package_list): + packages = [p.strip() for p in package_list.split(';')] + for p in packages: + self.log.info("Adding java package %s to jython" % p) + sys.add_package(p) + + def add_classdirs(self, classdir_list): + classdirs = [cd.strip() for cd in classdir_list.split(';')] + for cd in classdirs: + self.log.info("Adding directory %s to jython class file search path" % cd) + sys.add_classdir(cd) + + def add_extdirs(self, extdir_list): + extdirs = [ed.strip() for ed in extdir_list.split(';')] + for ed in extdirs: + self.log.info("Adding directory %s for .jars and .zips search path" % ed) + sys.add_extdir(self.expand_relative_path(ed)) + + def do_j_env_params(self): + if self.params['packages']: + self.add_packages(self.params['packages']) + if self.params['classdirs']: + self.add_classdirs(self.params['classdirs']) + if self.params['extdirs']: + self.add_extdirs(self.params['extdirs']) Added: branches/modjy/Lib/modjy/modjy_log.py =================================================================== --- branches/modjy/Lib/modjy/modjy_log.py (rev 0) +++ branches/modjy/Lib/modjy/modjy_log.py 2008-12-12 15:46:58 UTC (rev 5751) @@ -0,0 +1,80 @@ +### +# +# Copyright 2004-2008 Alan Kennedy. +# +# You may contact the copyright holder at this uri: +# +# http://www.xhaus.com/contact/modjy +# +# The licence under which this code is released is the Apache License v2.0. +# +# The terms and conditions of this license are listed in a file contained +# in the distribution that also contained this file, under the name +# LICENSE.txt. +# +# You may also read a copy of the license at the following web address. +# +# http://modjy.xhaus.com/LICENSE.txt +# +### + +import java + +import sys + +DEBUG = 'debug' +INFO = 'info' +WARN = 'warn' +ERROR = 'error' +FATAL = 'fatal' + +levels_dict = {} +ix = 0 +for level in [DEBUG, INFO, WARN, ERROR, FATAL, ]: + levels_dict[level]=ix + ix += 1 + +class modjy_logger: + + def __init__(self, context): + self.log_ctx = context + self.format_str = "%(lvl)s:\t%(msg)s" + self.log_level = levels_dict[DEBUG] + + def _log(self, level, level_str, msg, exc): + if level >= self.log_level: + msg = self.format_str % {'lvl': level_str, 'msg': msg, } + if exc: +# java.lang.System.err.println(msg, exc) + self.log_ctx.log(msg, exc) + else: +# java.lang.System.err.println(msg) + self.log_ctx.log(msg) + + def debug(self, msg, exc=None): + self._log(0, DEBUG, msg, exc) + + def info(self, msg, exc=None): + self._log(1, INFO, msg, exc) + + def warn(self, msg, exc=None): + self._log(2, WARN, msg, exc) + + def error(self, msg, exc=None): + self._log(3, ERROR, msg, exc) + + def fatal(self, msg, exc=None): + self._log(4, FATAL, msg, exc) + + def set_log_level(self, level_string): + try: + self.level = levels_dict[level_string] + except KeyError: + raise BadParameter("Invalid log level: '%s'" % level_string) + + def set_log_format(self, format_string): + # BUG! Format string never actually used in this function. + try: + self._log(debug, "This is a log formatting test", None) + except KeyError: + raise BadParameter("Bad format string: '%s'" % format_string) Added: branches/modjy/Lib/modjy/modjy_params.py =================================================================== --- branches/modjy/Lib/modjy/modjy_params.py (rev 0) +++ branches/modjy/Lib/modjy/modjy_params.py 2008-12-12 15:46:58 UTC (rev 5751) @@ -0,0 +1,84 @@ +### +# +# Copyright 2004-2008 Alan Kennedy. +# +# You may contact the copyright holder at this uri: +# +# http://www.xhaus.com/contact/modjy +# +# The licence under which this code is released is the Apache License v2.0. +# +# The terms and conditions of this license are listed in a file contained +# in the distribution that also contained this file, under the name +# LICENSE.txt. +# +# You may also read a copy of the license at the following web address. +# +# http://modjy.xhaus.com/LICENSE.txt +# +### + +from UserDict import UserDict + +BOOLEAN = ('boolean', int) +INTEGER = ('integer', int) +FLOAT = ('float', float) +STRING = ('string', None) + +modjy_servlet_params = { + + 'multithread': (BOOLEAN, 1), + 'cache_callables': (BOOLEAN, 1), + 'reload_on_mod': (BOOLEAN, 0), + + 'app_import_name': (STRING, None), + + 'app_directory': (STRING, None), + 'app_filename': (STRING, 'application.py'), + 'app_callable_name': (STRING, 'handler'), + 'callable_query_name': (STRING, None), + + 'exc_handler': (STRING, 'standard'), + + 'log_level': (STRING, 'info'), + + 'packages': (STRING, None), + 'classdirs': (STRING, None), + 'extdirs': (STRING, None), + + 'initial_env': (STRING, None), +} + +class modjy_param_mgr(UserDict): + + def __init__(self, param_types): + UserDict.__init__(self) + self.param_types = param_types + for pname in self.param_types.keys(): + typ, default = self.param_types[pname] + self.__setitem__(pname, default) + + def __getitem__(self, name): + return self._get_defaulted_value(name) + + def __setitem__(self, name, value): + self.data[name] = self._convert_value(name, value) + + def _convert_value(self, name, value): + if self.param_types.has_key(name): + typ, default = self.param_types[name] + typ_str, typ_func = typ + if typ_func: + try: + return typ_func(value) + except ValueError: + raise BadParameter("Illegal value for %s parameter '%s': %s" % (typ_str, name, value) ) + return value + + def _get_defaulted_value(self, name): + if self.data.has_key(name): + return self.data[name] + if self.param_types.has_key(name): + typ, default = self.param_types[name] + return default + raise KeyError(name) Added: branches/modjy/Lib/modjy/modjy_publish.py =================================================================== --- branches/modjy/Lib/modjy/modjy_publish.py (rev 0) +++ branches/modjy/Lib/modjy/modjy_publish.py 2008-12-12 15:46:58 UTC (rev 5751) @@ -0,0 +1,142 @@ +### +# +# Copyright 2004-2008 Alan Kennedy. +# +# You may contact the copyright holder at this uri: +# +# http://www.xhaus.com/contact/modjy +# +# The licence under which this code is released is the Apache License v2.0. +# +# The terms and conditions of this license are listed in a file contained +# in the distribution that also contained this file, under the name +# LICENSE.txt. +# +# You may also read a copy of the license at the following web address. +# +# http://modjy.xhaus.com/LICENSE.txt +# +### + +import sys +import synchronize + +from java.io import File + +from modjy_exceptions import * + +class modjy_publisher: + + def init_publisher(self): + self.cache = None + if self.params['app_directory']: + self.app_directory = self.expand_relative_path(self.params['app_directory']) + else: + self.app_directory = self.servlet_context.getRealPath('/') + self.params['app_directory'] = self.app_directory + if not self.app_directory in sys.path: + sys.path.append(self.app_directory) + + def map_uri(self, req, environ): + script_name = "%s%s" % (req.getContextPath(), req.getServletPath()) + path_info = req.getPathInfo() or "" + source_uri = '%s%s%s' % (self.app_directory, File.separator, self.params['app_filename']) + callable_name = self.params['app_callable_name'] + if self.params['callable_query_name']: + query_string = req.getQueryString() + if query_string and '=' in query_string: + for name_val in query_string.split('&'): + name, value = name_val.split('=') + if name == self.params['callable_query_name']: + callable_name = value + environ["SCRIPT_NAME"] = script_name + environ["PATH_INFO"] = path_info + environ["PATH_TRANSLATED"] = File(self.app_directory, path_info).getPath() + return source_uri, callable_name + + def get_app_object(self, req, environ): + if self.params['app_import_name'] is not None: + return self.get_app_object_importable(self.params['app_import_name']) + else: + if self.cache is None: + self.cache = {} + return self.get_app_object_old_style(req, environ) + + get_app_object = synchronize.make_synchronized(get_app_object) + + def get_app_object_importable(self, importable_name): + self.log.debug("Attempting to import application callable '%s'\n" % (importable_name, )) + # Under the importable mechanism, the cache contains a single object + if self.cache is None: + application, instantiable, method_name = self.load_importable(importable_name.strip()) + if instantiable and self.params['cache_callables']: + application = application() + self.cache = application, instantiable, method_name + application, instantiable, method_name = self.cache + self.log.debug("Application is " + str(application)) + if instantiable and not self.params['cache_callables']: + application = application() + self.log.debug("Instantiated application is " + str(application)) + if method_name is not None: + application = getattr(application, method_name) + self.log.debug("Application method is " + str(application)) + return application + + def load_importable(self, name): + try: + instantiable = False ; method_name = None + names = name.split('.') ; names.reverse() + imported = __import__(names.pop()) + while names: + n = names.pop() + if n.endswith("()"): + n = n[:-2] ; instantiable = True + self.log.debug("importable '%s' is instantiable." % n) + if len(names) > 1: + names.reverse() + message = "Failed to import '%s': '%s' is not a valid method name" % (name, ".".join(names)) + self.log.error(message) + self.raise_exc(ApplicationNotFound, message) + elif len(names) == 1: + method_name = names.pop() + else: + method_name = None + imported = getattr(imported, n) + self.log.debug("Imported '%s' from '%s'\n" % (n, str(imported))) + return imported, instantiable, method_name + except (ImportError, AttributeError), aix: + self.log.fatal("Import error import application callable '%s': %s\n" % (name, str(aix))) + self.raise_exc(ApplicationNotFound, "Failed to import app callable '%s': %s" % (name, str(aix))) + + def get_app_object_old_style(self, req, environ): + source_uri, callable_name = self.map_uri(req, environ) + source_filename = source_uri + if not self.params['cache_callables']: + self.log.debug("Caching of callables disabled") + return self.load_object(source_filename, callable_name) + if not self.cache.has_key( (source_filename, callable_name) ): + self.log.debug("Callable object not in cache: %s#%s" % (source_filename, callable_name) ) + return self.load_object(source_filename, callable_name) + app_callable, last_mod = self.cache.get( (source_filename, callable_name) ) + self.log.debug("Callable object was in cache: %s#%s" % (source_filename, callable_name) ) + if self.params['reload_on_mod']: + f = File(source_filename) + if f.lastModified() > last_mod: + self.log.info("Source file '%s' has been modified: reloading" % source_filename) + return self.load_object(source_filename, callable_name) + return app_callable + + def load_object(self, path, callable_name): + try: + app_ns = {} ; execfile(path, app_ns) + app_callable = app_ns[callable_name] + f = File(path) + self.cache[ (path, callable_name) ] = (app_callable, f.lastModified()) + return app_callable + except IOError, ioe: + self.raise_exc(ApplicationNotFound, "Application filename not found: %s" % path) + except KeyError, k: + self.raise_exc(NoCallable, "No callable named '%s' in %s" % (callable_name, path)) + except Exception, x: + self.raise_exc(NoCallable, "Error loading jython callable '%s': %s" % (callable_name, str(x)) ) + Added: branches/modjy/Lib/modjy/modjy_response.py =================================================================== --- branches/modjy/Lib/modjy/modjy_response.py (rev 0) +++ branches/modjy/Lib/modjy/modjy_response.py 2008-12-12 15:46:58 UTC (rev 5751) @@ -0,0 +1,113 @@ +### +# +# Copyright 2004-2008 Alan Kennedy. +# +# You may contact the copyright holder at this uri: +# +# http://www.xhaus.com/contact/modjy +# +# The licence under which this code is released is the Apache License v2.0. +# +# The terms and conditions of this license are listed in a file contained +# in the distribution that also contained this file, under the name +# LICENSE.txt. +# +# You may also read a copy of the license at the following web address. +# +# http://modjy.xhaus.com/LICENSE.txt +# +### + +import types + +from java.lang import System + +from modjy_exceptions import * +from modjy_write import write_object + +# From: http://www.w3.org/Protocols/rfc2616/rfc2616-sec13.html#sec13.5.1 + +hop_by_hop_headers = { + 'connection': None, + 'keep-alive': None, + 'proxy-authenticate': None, + 'proxy-authorization': None, + 'te': None, + 'trailers': None, + 'transfer-encoding': None, + 'upgrade': None, +} + +class start_response_object: + + def __init__(self, req, resp): + self.http_req = req + self.http_resp = resp + self.write_callable = None + self.called = 0 + self.content_length = None + + # I'm doing the parameters this way to facilitate porting back to java + def __call__(self, *args, **keywords): + if len(args) < 2 or len(args) > 3: + raise BadArgument("Start response callback requires either two or three arguments: got %s" % str(args)) + if len(args) == 3: + exc_info = args[2] + try: + try: + self.http_resp.reset() + except IllegalStateException, isx: + raise exc_info[0], exc_info[1], exc_info[2] + finally: + exc_info = None + else: + if self.called > 0: + raise StartResponseCalledTwice("Start response callback may only be called once, without exception information.") + status_str = args[0] + headers_list = args[1] + if not isinstance(status_str, types.StringType): + raise BadArgument("Start response callback requires string as first argument") + if not isinstance(headers_list, types.ListType): + raise BadArgument("Start response callback requires list as second argument") + try: + status_code, status_message_str = status_str.split(" ", 1) + self.http_resp.setStatus(int(status_code)) + except ValueError: + raise BadArgument("Status string must be of the form '<int> <string>'") + self.make_write_object() + try: + for header_name, header_value in headers_list: + header_name_lower = header_name.lower() + if hop_by_hop_headers.has_key(header_name_lower): + raise HopByHopHeaderSet("Under WSGI, it is illegal to set hop-by-hop headers, i.e. '%s'" % header_name) + if header_name_lower == "content-length": + try: + self.set_content_length(int(header_value)) + except ValueError, v: + raise BadArgument("Content-Length header value must be a string containing an integer, not '%s'" % header_value) + else: + final_value = header_value.encode('latin-1') + # Here would be the place to check for control characters, whitespace, etc + self.http_resp.addHeader(header_name, final_value) + except (AttributeError, TypeError), t: + raise BadArgument("Start response callback headers must contain a list of (<string>,<string>) tuples") + except UnicodeError, u: + raise BadArgument("Encoding error: header values may only contain latin-1 characters, not '%s'" % repr(header_value)) + except ValueError, v: + raise BadArgument("Headers list must contain 2-tuples") + self.called += 1 + return self.write_callable + + def set_content_length(self, length): + if self.write_callable.num_writes == 0: + self.content_length = length + self.http_resp.setContentLength(length) + else: + raise ResponseCommitted("Cannot set content-length: response is already commited.") + + def make_write_object(self): + try: + self.write_callable = write_object(self.http_resp.getOutputStream()) + except IOException, iox: + raise IOError(iox) + return self.write_callable Added: branches/modjy/Lib/modjy/modjy_write.py =================================================================== --- branches/modjy/Lib/modjy/modjy_write.py (rev 0) +++ branches/modjy/Lib/modjy/modjy_write.py 2008-12-12 15:46:58 UTC (rev 5751) @@ -0,0 +1,43 @@ +### +# +# Copyright 2004-2008 Alan Kennedy. +# +# You may contact the copyright holder at this uri: +# +# http://www.xhaus.com/contact/modjy +# +# The licence under which this code is released is the Apache License v2.0. +# +# The terms and conditions of this license are listed in a file contained +# in the distribution that also contained this file, under the name +# LICENSE.txt. +# +# You may also read a copy of the license at the following web address. +# +# http://modjy.xhaus.com/LICENSE.txt +# +### + +import types + +from modjy_exceptions import * + +class write_object: + + def __init__(self, ostream): + self.ostream = ostream + self.num_writes = 0 + + def __call__(self, *args, **keywords): + if len(args) != 1 or type(args[0]) is not types.StringType: + raise NonStringOutput("Invocation of write callable requires exactly one string argument") + try: + self.ostream.write(args[0]) # Jython implicitly converts the (binary) string to a byte array + # WSGI requires that all output be flushed before returning to the application + # According to the java docs: " The flush method of OutputStream does nothing." + # Still, leave it in place for now: it's in the right place should this + # code ever be ported to another platform. + self.ostream.flush() + self.num_writes += 1 + except Exception, x: + raise ModjyIOException(x) Added: branches/modjy/Lib/modjy/modjy_wsgi.py =================================================================== --- branches/modjy/Lib/modjy/modjy_wsgi.py (rev 0) +++ branches/modjy/Lib/modjy/modjy_wsgi.py 2008-12-12 15:46:58 UTC (rev 5751) @@ -0,0 +1,143 @@ +### +# +# Copyright 2004-2008 Alan Kennedy. +# +# You may contact the copyright holder at this uri: +# +# http://www.xhaus.com/contact/modjy +# +# The licence under which this code is released is the Apache License v2.0. +# +# The terms and conditions of this license are listed in a file contained +# in the distribution that also contained this file, under the name +# LICENSE.txt. +# +# You may also read a copy of the license at the following web address. +# +# http://modjy.xhaus.com/LICENSE.txt +# +### + +from java.lang import System +from org.python.core import PyFile + +from modjy_exceptions import * + +server_name = "modjy" +server_param_prefix = "%s.param" % server_name +j2ee_ns_prefix = "j2ee" + +class modjy_wsgi: + + # + # WSGI constants + # + + empty_pystring = "" + wsgi_version = (1,0) + + # + # Container-specific constants + # + + modjy_version = (0, 22, 3) + + def set_string_envvar(self, dict, name, value, default_value): + if value == default_value: + dict[name] = self.empty_pystring + else: + dict[name] = value + + def set_string_envvar_optional(self, dict, name, value, default_value): + if value != default_value: + dict[name] = str(value) + + def set_int_envvar(self, dict, name, value, default_value): + if value == default_value: + dict[name] = self.empty_pystring + else: + dict[name] = str(value) + + def set_container_specific_wsgi_vars(self, req, resp, dict, params): + dict["%s.version" % server_name] = self.modjy_version + for pname in params.keys(): + dict["%s.%s" % (server_param_prefix, pname)] = params[pname] + + def set_j2ee_specific_wsgi_vars(self, dict, j2ee_ns): + for p in j2ee_ns.keys(): + dict["%s.%s" % (j2ee_ns_prefix, p)] = j2ee_ns[p] + + def set_required_cgi_environ (self, req, resp, dict): + self.set_string_envvar(dict, "REQUEST_METHOD", req.getMethod(), None) + self.set_string_envvar(dict, "QUERY_STRING", req.getQueryString(), None) + self.set_string_envvar(dict, "CONTENT_TYPE", req.getContentType(), None) + self.set_int_envvar(dict, "CONTENT_LENGTH", req.getContentLength(), -1) + self.set_string_envvar(dict, "SERVER_NAME", req.getLocalName(), None) + self.set_int_envvar(dict, "SERVER_PORT", req.getLocalPort(), 0) + + def set_other_cgi_environ (self, req, resp, dict): + if req.isSecure(): + self.set_string_envvar(dict, "HTTPS", 'on', None) + else: + self.set_string_envvar(dict, "HTTPS", 'off', None) + self.set_string_envvar(dict, "SERVER_PROTOCOL", req.getProtocol(), None) + self.set_string_envvar(dict, "REMOTE_HOST", req.getRemoteHost(), None) + self.set_string_envvar(dict, "REMOTE_ADDR", req.getRemoteAddr(), None) + self.set_int_envvar(dict, "REMOTE_PORT", req.getRemotePort(), -1) + self.set_string_envvar_optional(dict, "AUTH_TYPE", req.getAuthType(), None) + self.set_string_envvar_optional(dict, "REMOTE_USER", req.getRemoteUser(), None) + + def set_http_header_environ(self, req, resp, dict): + for curr_header_name in req.getHeaderNames(): + values = None + for next_value in req.getHeaders(curr_header_name): + if values is None: + values = next_value + else: + if isinstance(values, types.ListType): + values.append(next_value) + else: + values = [values] + dict["HTTP_%s" % curr_header_name.replace('-', '_').upper()] = values + + def set_required_wsgi_vars(self, req, resp, dict): + dict["wsgi.version"] = self.wsgi_version + dict["wsgi.url_scheme"] = req.getScheme() + dict["wsgi.multithread"] = \ + int(dict["%s.cache_callables" % server_param_prefix]) \ + and \ + int(dict["%s.multithread" % server_param_prefix]) + dict["wsgi.multiprocess"] = self.wsgi_multiprocess = 0 + dict["wsgi.run_once"] = not(dict["%s.cache_callables" % server_param_prefix]) + + def set_wsgi_streams(self, req, resp, dict): + try: + dict["wsgi.input"] = PyFile(req.getInputStream()) + dict["wsgi.errors"] = PyFile(System.err) + except IOException, iox: + raise ModjyIOException(iox) + + def set_wsgi_classes(self, req, resp, dict): + # dict["wsgi.file_wrapper"] = modjy_file_wrapper + pass + + def set_user_specified_environment(self, req, resp, wsgi_environ, params): + if not params.has_key('initial_env') or not params['initial_env']: + return + user_env_string = params['initial_env'] + for l in user_env_string.split('\n'): + l = l.strip() + if l: + name, value = l.split(':', 1) + wsgi_environ[name.strip()] = value.strip() + + def set_wsgi_environment(self, req, resp, wsgi_environ, params, j2ee_ns): + self.set_container_specific_wsgi_vars(req, resp, wsgi_environ, params) + self.set_j2ee_specific_wsgi_vars(wsgi_environ, j2ee_ns) + self.set_required_cgi_environ(req, resp, wsgi_environ) + self.set_other_cgi_environ(req, resp, wsgi_environ) + self.set_http_header_environ(req, resp, wsgi_environ) + self.set_required_wsgi_vars(req, resp, wsgi_environ) + self.set_wsgi_streams(req, resp, wsgi_environ) + self.set_wsgi_classes(req, resp, wsgi_environ) + self.set_user_specified_environment(req, resp, wsgi_environ, params) Added: branches/modjy/src/com/xhaus/modjy/ModjyJServlet.java =================================================================== --- branches/modjy/src/com/xhaus/modjy/ModjyJServlet.java (rev 0) +++ branches/modjy/src/com/xhaus/modjy/ModjyJServlet.java 2008-12-12 15:46:58 UTC (rev 5751) @@ -0,0 +1,217 @@ +/*### +# +# Copyright 2004-2008 Alan Kennedy. +# +# You may contact the copyright holder at this uri: +# +# http://www.xhaus.com/contact/modjy +# +# The licence under which this code is released is the Apache License v2.0. +# +# The terms and conditions of this license are listed in a file contained +# in the distribution that also contained this file, under the name +# LICENSE.txt. +# +# You may also read a copy of the license at the following web address. +# +# http://www.xhaus.com/modjy/LICENSE.txt +# +###*/ + +package com.xhaus.modjy; + +import java.io.*; +import java.util.*; +import javax.servlet.*; +import javax.servlet.http.*; +import org.python.core.*; +import org.python.util.*; + +public class ModjyJServlet extends HttpServlet +{ + + protected final static String MODJY_PYTHON_CLASSNAME = "modjy_servlet"; + protected final static String LIB_PYTHON = "/WEB-INF/lib-python"; + protected final static String PTH_FILE_EXTENSION = ".pth"; + + protected PythonInterpreter interp; + protected HttpServlet modjyServlet; + + /** + * Read configuration + * 1. Both context and servlet parameters are included in the set, + * so that the definition of some parameters (e.g python.*) can be shared + * between multiple WSGI servlets. + * 2. servlet params take precedence over context parameters + */ + + protected Properties readConfiguration ( ) + { + Properties props = new Properties(); + // Context parameters + ServletContext context = getServletContext(); + Enumeration e = context.getInitParameterNames(); + while (e.hasMoreElements()) + { + String name = (String) e.nextElement(); + props.put(name, context.getInitParameter(name)); + } + // Servlet parameters override context parameters + e = getInitParameterNames(); + while (e.hasMoreElements()) + { + String name = (String) e.nextElement(); + props.put(name, getInitParameter(name)); + } + return props; + } + + /** + * Initialise the modjy servlet. + * 1. Read the configuration + * 2. Initialise the jython runtime + * 3. Setup, in relation to the J2EE servlet environment + * 4. Create the jython-implemented servlet + * 5. Initialise the jython-implemented servlet + */ + + public void init ( ) + throws ServletException + { + try + { + Properties props = readConfiguration(); + PythonInterpreter.initialize(System.getProperties(), props, new String[0]); + interp = new PythonInterpreter(null, new PySystemState()); + String modjyJarLocation = setupEnvironment(props, Py.getSystemState()); + try + { interp.exec("from modjy import "+MODJY_PYTHON_CLASSNAME); } + catch (PyException ix) + { throw new ServletException("Unable to import '"+MODJY_PYTHON_CLASSNAME+"' from "+modjyJarLocation+ + ": do you maybe need to set the 'modjy_jar.location' parameter?");} + PyObject pyServlet = ((PyClass)interp.get(MODJY_PYTHON_CLASSNAME)).__call__(); + Object temp = pyServlet.__tojava__(HttpServlet.class); + if (temp == Py.NoConversion) + throw new ServletException("Corrupted modjy file: cannot find definition of '"+MODJY_PYTHON_CLASSNAME+"' class"); + modjyServlet = (HttpServlet) temp; + modjyServlet.init(this); + } + catch (PyException pyx) + { + throw new ServletException("Exception creating modjy servlet: " + pyx.toString(), pyx); + } + } + + /** + * Actually service the incoming request. + * Simply delegate to the jython servlet. + * + * @param request - The incoming HttpServletRequest + * @param response - The outgoing HttpServletResponse + */ + + public void service ( HttpServletRequest req, HttpServletResponse resp ) + throws ServletException, IOException + { + modjyServlet.service(req, resp); + } + + /** + * Setup the modjy environment, i.e. + * 1. Find the location of the modjy.jar file and add it to sys.path + * 2. Process the WEB-INF/lib-python directory, if it exists + * + * @param props The properties from which config options are found + * @param systemState The PySystemState corresponding to the interpreter servicing requests + * @returns A String giving the path to the modjy.jar file (which is used only for error reporting) + */ + + protected String setupEnvironment(Properties props, PySystemState systemState) + { + String modjyJarLocation = locateModjyJar(props); + systemState.path.append(new PyString(modjyJarLocation)); + processPythonLib(systemState); + return modjyJarLocation; + } + + /** + * Find out the location of "modjy.jar", so that it can + * be added to the sys.path and thus imported + * + * @param The properties from which config options are found + * @returns A String giving the path to the modjy.jar file + */ + + protected String locateModjyJar ( Properties props ) + { + // Give priority to modjy_jar.location + if (props.get("modjy_jar.location") != null) + return (String)props.get("modjy_jar.location"); + // Then try to find it in WEB-INF/lib + String location = getServletContext().getRealPath("/WEB-INF/lib/modjy.jar"); + if (location != null) + { + File f = new File(location); + if (f.exists()) + return location; + } + // Try finding the archive that this class was loaded from + try + { return this.getClass().getProtectionDomain().getCodeSource().getLocation().getFile(); } + catch (Exception x) + { return null;} + } + + /** + * Do all processing in relation to the lib-python subdirectory of WEB-INF + * + * @param systemState - The PySystemState whose path should be updated + */ + + protected void processPythonLib(PySystemState systemState) + { + // Add the lib-python directory to sys.path + String pythonLibPath = getServletContext().getRealPath(LIB_PYTHON); + if (pythonLibPath == null) + return; + File pythonLib = new File(pythonLibPath); + if (!pythonLib.exists()) + return; + systemState.path.append(new PyString(pythonLibPath)); + // Now check for .pth files in lib-python and process each one + String[] libPythonContents = pythonLib.list(); + for (int ix = 0 ; ix < libPythonContents.length ; ix++) + if (libPythonContents[ix].endsWith(PTH_FILE_EXTENSION)) + processPthFile(systemState, pythonLibPath, libPythonContents[ix]); + } + + /** + * Process an individual file .pth file in the lib-python directory + * + * @param systemState - The PySystemState whose path should be updated + * @param pythonLibPath - The actual path to the lib-python directory + * @param pthFilename - The PySystemState whose path should be updated + */ + + protected void processPthFile(PySystemState systemState, String pythonLibPath, String pthFilename) + { + try + { + LineNumberReader lineReader = new LineNumberReader(new FileReader(new File(pythonLibPath, pthFilename))); + String line; + while ((line = lineReader.readLine()) != null) + { + line = line.trim(); + if (line.startsWith("#")) + continue; + File archiveFile = new File(pythonLibPath, line); + String archiveRealpath = archiveFile.getAbsolutePath(); + systemState.path.append(new PyString(archiveRealpath)); + } + } + catch (IOException iox) + { + System.err.println("IOException: " + iox.toString()); + } + } +} This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |