| Tor Norbye | 3a2425a | 2013-11-04 10:16:08 -0800 | [diff] [blame^] | 1 | ### |
| 2 | # |
| 3 | # Copyright Alan Kennedy. |
| 4 | # |
| 5 | # You may contact the copyright holder at this uri: |
| 6 | # |
| 7 | # http://www.xhaus.com/contact/modjy |
| 8 | # |
| 9 | # The licence under which this code is released is the Apache License v2.0. |
| 10 | # |
| 11 | # The terms and conditions of this license are listed in a file contained |
| 12 | # in the distribution that also contained this file, under the name |
| 13 | # LICENSE.txt. |
| 14 | # |
| 15 | # You may also read a copy of the license at the following web address. |
| 16 | # |
| 17 | # http://modjy.xhaus.com/LICENSE.txt |
| 18 | # |
| 19 | ### |
| 20 | |
| 21 | import jarray |
| 22 | import synchronize |
| 23 | import sys |
| 24 | import types |
| 25 | |
| 26 | sys.add_package("javax.servlet") |
| 27 | sys.add_package("javax.servlet.http") |
| 28 | sys.add_package("org.python.core") |
| 29 | |
| 30 | from modjy_exceptions import * |
| 31 | from modjy_log import * |
| 32 | from modjy_params import modjy_param_mgr, modjy_servlet_params |
| 33 | from modjy_wsgi import modjy_wsgi |
| 34 | from modjy_response import start_response_object |
| 35 | from modjy_impl import modjy_impl |
| 36 | from modjy_publish import modjy_publisher |
| 37 | |
| 38 | from javax.servlet.http import HttpServlet |
| 39 | |
| 40 | class modjy_servlet(HttpServlet, modjy_publisher, modjy_wsgi, modjy_impl): |
| 41 | |
| 42 | def __init__(self): |
| 43 | HttpServlet.__init__(self) |
| 44 | |
| 45 | def do_param(self, name, value): |
| 46 | if name[:3] == 'log': |
| 47 | getattr(self.log, "set_%s" % name)(value) |
| 48 | else: |
| 49 | self.params[name] = value |
| 50 | |
| 51 | def process_param_container(self, param_container): |
| 52 | param_enum = param_container.getInitParameterNames() |
| 53 | while param_enum.hasMoreElements(): |
| 54 | param_name = param_enum.nextElement() |
| 55 | self.do_param(param_name, param_container.getInitParameter(param_name)) |
| 56 | |
| 57 | def get_params(self): |
| 58 | self.process_param_container(self.servlet_context) |
| 59 | self.process_param_container(self.servlet) |
| 60 | |
| 61 | def init(self, delegator): |
| 62 | self.servlet = delegator |
| 63 | self.servlet_context = self.servlet.getServletContext() |
| 64 | self.servlet_config = self.servlet.getServletConfig() |
| 65 | self.log = modjy_logger(self.servlet_context) |
| 66 | self.params = modjy_param_mgr(modjy_servlet_params) |
| 67 | self.get_params() |
| 68 | self.init_impl() |
| 69 | self.init_publisher() |
| 70 | import modjy_exceptions |
| 71 | self.exc_handler = getattr(modjy_exceptions, '%s_handler' % self.params['exc_handler'])() |
| 72 | |
| 73 | def service (self, req, resp): |
| 74 | wsgi_environ = {} |
| 75 | try: |
| 76 | self.dispatch_to_application(req, resp, wsgi_environ) |
| 77 | except ModjyException, mx: |
| 78 | self.log.error("Exception servicing request: %s" % str(mx)) |
| 79 | typ, value, tb = sys.exc_info()[:] |
| 80 | self.exc_handler.handle(req, resp, wsgi_environ, mx, (typ, value, tb) ) |
| 81 | |
| 82 | def get_j2ee_ns(self, req, resp): |
| 83 | return { |
| 84 | 'servlet': self.servlet, |
| 85 | 'servlet_context': self.servlet_context, |
| 86 | 'servlet_config': self.servlet_config, |
| 87 | 'request': req, |
| 88 | 'response': resp, |
| 89 | } |
| 90 | |
| 91 | def dispatch_to_application(self, req, resp, environ): |
| 92 | app_callable = self.get_app_object(req, environ) |
| 93 | self.set_wsgi_environment(req, resp, environ, self.params, self.get_j2ee_ns(req, resp)) |
| 94 | response_callable = start_response_object(req, resp) |
| 95 | try: |
| 96 | app_return = self.call_application(app_callable, environ, response_callable) |
| 97 | if app_return is None: |
| 98 | raise ReturnNotIterable("Application returned None: must return an iterable") |
| 99 | self.deal_with_app_return(environ, response_callable, app_return) |
| 100 | except ModjyException, mx: |
| 101 | self.raise_exc(mx.__class__, str(mx)) |
| 102 | except Exception, x: |
| 103 | self.raise_exc(ApplicationException, str(x)) |
| 104 | |
| 105 | def call_application(self, app_callable, environ, response_callable): |
| 106 | if self.params['multithread']: |
| 107 | return app_callable.__call__(environ, response_callable) |
| 108 | else: |
| 109 | return synchronize.apply_synchronized( \ |
| 110 | app_callable, \ |
| 111 | app_callable, \ |
| 112 | (environ, response_callable)) |
| 113 | |
| 114 | def expand_relative_path(self, path): |
| 115 | if path.startswith("$"): |
| 116 | return self.servlet.getServletContext().getRealPath(path[1:]) |
| 117 | return path |
| 118 | |
| 119 | def raise_exc(self, exc_class, message): |
| 120 | self.log.error(message) |
| 121 | raise exc_class(message) |