Issue1207

classification
Title: Deploying a Django-Jython app to Tomcat generates java.lang.ClassCastException
Type: behaviour Severity: normal
Components: Any Versions: 2.5b0
Milestone:
process
Status: closed Resolution: out of date
Dependencies: Superseder:
Assigned To: amak Nosy List: JohnSJohnM, amak, pjenvey
Priority: Keywords:

Created on 2008-12-17.11:00:17 by JohnSJohnM, last changed 2009-01-08.13:04:50 by JohnSJohnM.

Messages
msg3942 (view) Author: JohnSJohnM (JohnSJohnM) Date: 2008-12-17.11:00:16
I am using Jython SVN revision 5773, Django (both 1.0.2 and 1.1pre-
alpha) and django-jython trunk. Applications run ok using the Django 
built in webserver and also builds a valid war file.

Upon deployment to an application server (tested on both Tomcat 6.0.18 
and Glassfish 2, using JDK 1.6.0_05) the deployed application generates 
the following Java exception:

exception 
javax.servlet.ServletException: Servlet.init() for servlet modjy threw 
exception
	org.apache.catalina.valves.ErrorReportValve.invoke
(ErrorReportValve.java:102)
	org.apache.catalina.connector.CoyoteAdapter.service
(CoyoteAdapter.java:286)
	org.apache.coyote.http11.Http11Processor.process
(Http11Processor.java:845)
	org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.
process(Http11Protocol.java:583)
	org.apache.tomcat.util.net.JIoEndpoint$Worker.run
(JIoEndpoint.java:447)
	java.lang.Thread.run(Thread.java:619)


root cause
java.lang.ClassCastException: org.python.core.PyType cannot be cast to 
org.python.core.PyClass
	com.xhaus.modjy.ModjyJServlet.init(ModjyJServlet.java:82)
	javax.servlet.GenericServlet.init(GenericServlet.java:212)
	org.apache.catalina.valves.ErrorReportValve.invoke
(ErrorReportValve.java:102)
	org.apache.catalina.connector.CoyoteAdapter.service
(CoyoteAdapter.java:286)
	org.apache.coyote.http11.Http11Processor.process
(Http11Processor.java:845)
	org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.
process(Http11Protocol.java:583)
	org.apache.tomcat.util.net.JIoEndpoint$Worker.run
(JIoEndpoint.java:447)
	java.lang.Thread.run(Thread.java:619)

This occurs whether using an existing Django application with database 
connectsion etc, or whether a fresh application with no db's and just 
the django default 'getting started' page. I haven't been able to test 
any other Python code as a deployed web application.
msg3957 (view) Author: Philip Jenvey (pjenvey) Date: 2008-12-17.20:41:29
Yep, I just ran into this yesterday myself. newstyle merge broke this

modjy also has a couple other problems I noticed (like still using 
PyFile(InputStream))
msg3959 (view) Author: JohnSJohnM (JohnSJohnM) Date: 2008-12-18.08:40:50
I did a bit of further work and managed to get Django *mostly* working; 
ie I can now see Django error pages and the built-in admin login prompt.

In ModJyServlet.java I had to change the lines

PyObject pyServlet = ((PyClass)interp.get
(MODJY_PYTHON_CLASSNAME)).__call__();

to

PyObject pyServlet = ((PyObject)interp.get
(MODJY_PYTHON_CLASSNAME)).__call__();

This solved the ClassCastException. I then had the problem with PyFile, 
as you mention; thankfully I found a blog 
(http://jblebrun.blogspot.com/2008/10/django-on-glassfish-via-
jython.html) from someone who had encountered the same problem. So in 
modjy_wsgi.py the corresponding lines using PyFile thus becomes:

dict["wsgi.input"]  = FileUtil.wrap(req.getInputStream())
dict["wsgi.errors"] =  FileUtil.wrap(System.err)

I also had to follow that guys suggestion on turning unicode back into 
strings. That finally got me to a state where I can now see Django 
pages and error messages. 

The last remaining problem seems to be that I get a Python error when 
logging in to the Django /admin pages:

Traceback:
File "/root/tomcat/webapps/ROOT/WEB-INF/lib-
python/django/django/core/handlers/base.py" in get_response
  86.                 response = callback(request, *callback_args, 
**callback_kwargs)
File "/root/tomcat/webapps/ROOT/WEB-INF/lib-
python/django/django/contrib/admin/sites.py" in root
  141.             return self.login(request)
File "/root/tomcat/webapps/ROOT/WEB-INF/lib-
python/django/django/views/decorators/cache.py" in _wrapped_view_func
  44.         response = view_func(request, *args, **kwargs)
File "/root/tomcat/webapps/ROOT/WEB-INF/lib-
python/django/django/contrib/admin/sites.py" in login
  224.         if not request.POST.has_key(LOGIN_FORM_KEY):
File "/root/tomcat/webapps/ROOT/WEB-INF/lib-
python/django/django/core/handlers/wsgi.py" in _get_post
  169.             self._load_post_and_files()
File "/root/tomcat/webapps/ROOT/WEB-INF/lib-
python/django/django/core/handlers/wsgi.py" in _load_post_and_files
  149.                 self._post, self._files = http.QueryDict
(self.raw_post_data, encoding=self._encoding), 
datastructures.MultiValueDict()
File "/root/tomcat/webapps/ROOT/WEB-INF/lib-
python/django/django/core/handlers/wsgi.py" in _get_raw_post_data
  202.                 safe_copyfileobj(self.environ['wsgi.input'], buf,
File "/root/tomcat/webapps/ROOT/WEB-INF/lib-
python/django/django/core/handlers/wsgi.py" in safe_copyfileobj
  69.         buf = fsrc.read(min(length, size))

Exception Type: AttributeError at /admin/
Exception Value: 'str' object has no attribute 'read'

It looks pretty close to working now.
msg3964 (view) Author: Alan Kennedy (amak) Date: 2008-12-18.20:35:23
I fixed this problem in modjy yesterday; the fixed version wll be
released in next version of jython 2.5.

In the meantime, please download the latest modjy 0.25 from SVN

https://jython.svn.sourceforge.net/svnroot/jython/trunk/jython/extlibs/modjy_0_25_0.zip

Please report back if that solves your problem.
msg3974 (view) Author: Alan Kennedy (amak) Date: 2008-12-24.13:16:41
Since we haven't heard any feedback from the OP, I will soon close this
bug as "out-of-date".
msg4003 (view) Author: Alan Kennedy (amak) Date: 2009-01-06.17:54:17
No report back from the OP.

This bug was directly addressed by a new release, so closing as
"out-of-date".
msg4011 (view) Author: JohnSJohnM (JohnSJohnM) Date: 2009-01-08.13:04:49
Apologies for not posting back sooner.

I've checked out r5877 (along with Django r9718 and Django-jython 
trunk) and the cast issue does seem to be resolved, however I am now 
seeing two exceptions that I did not see before. 
The first exception is:

java.lang.NoClassDefFoundError: org/antlr/runtime/CharStream

If I explicitely include a downloaded version of antlr.jar when 
building the war then this error dissapears.

However the next exception seems to refer to a built in Jython class. 
When I enable the same debug code in ModjyJServlet.java as before to 
print out the actual exception when a servlet exception is thrown I see 
the following:

javax.servlet.ServletException: Servlet exception starts here --
java.lang.NoClassDefFoundError: org/python/compiler/Module
	at org.python.core.Py.compile_flags(Py.java:1651)
	at org.python.core.Py.compile_flags(Py.java:1663)
	at org.python.core.Py.compile_flags(Py.java:1693)
	at org.python.util.PythonInterpreter.exec
(PythonInterpreter.java:124)
	at com.xhaus.modjy.ModjyJServlet.init(ModjyJServlet.java:88)
	at javax.servlet.GenericServlet.init(GenericServlet.java:212)
	at org.apache.catalina.core.StandardWrapper.loadServlet
(StandardWrapper.java:1172)
	at org.apache.catalina.core.StandardWrapper.allocate
(StandardWrapper.java:808)
	at org.apache.catalina.core.StandardWrapperValve.invoke
(StandardWrapperValve.java:129)
	at org.apache.catalina.core.StandardContextValve.invoke
(StandardContextValve.java:191)
	at org.apache.catalina.core.StandardHostValve.invoke
(StandardHostValve.java:128)
	at org.apache.catalina.valves.ErrorReportValve.invoke
(ErrorReportValve.java:102)
	at org.apache.catalina.core.StandardEngineValve.invoke
(StandardEngineValve.java:109)
	at org.apache.catalina.connector.CoyoteAdapter.service
(CoyoteAdapter.java:286)
	at org.apache.coyote.http11.Http11Processor.process
(Http11Processor.java:845)
	at org.apache.coyote.http11.Http11Protocol
$Http11ConnectionHandler.process(Http11Protocol.java:583)
	at org.apache.tomcat.util.net.JIoEndpoint$Worker.run
(JIoEndpoint.java:447)
	at java.lang.Thread.run(Thread.java:619)

java.lang.NoClassDefFoundError: java.lang.NoClassDefFoundError: org/
python/compiler/Module

Caused by: java.lang.NoClassDefFoundError: org/python/compiler/Module
	at org.python.core.Py.compile_flags(Py.java:1651)
	at org.python.core.Py.compile_flags(Py.java:1663)
	at org.python.core.Py.compile_flags(Py.java:1693)
	at org.python.util.PythonInterpreter.exec
(PythonInterpreter.java:124)
	at com.xhaus.modjy.ModjyJServlet.init(ModjyJServlet.java:88)
	at javax.servlet.GenericServlet.init(GenericServlet.java:212)
	at org.apache.catalina.core.StandardWrapper.loadServlet
(StandardWrapper.java:1172)
	at org.apache.catalina.core.StandardWrapper.allocate
(StandardWrapper.java:808)
	at org.apache.catalina.core.StandardWrapperValve.invoke
(StandardWrapperValve.java:129)
	at org.apache.catalina.core.StandardContextValve.invoke
(StandardContextValve.java:191)
	at org.apache.catalina.core.StandardHostValve.invoke
(StandardHostValve.java:128)
	at org.apache.catalina.valves.ErrorReportValve.invoke
(ErrorReportValve.java:102)
	at org.apache.catalina.core.StandardEngineValve.invoke
(StandardEngineValve.java:109)
	at org.apache.catalina.connector.CoyoteAdapter.service
(CoyoteAdapter.java:286)
	at org.apache.coyote.http11.Http11Processor.process
(Http11Processor.java:845)
	at org.apache.coyote.http11.Http11Protocol
$Http11ConnectionHandler.process(Http11Protocol.java:583)
	at org.apache.tomcat.util.net.JIoEndpoint$Worker.run
(JIoEndpoint.java:447)
	at java.lang.Thread.run(Thread.java:619)
-- Unable to import 'modjy_servlet' from /root/tomcat/webapps/
LSE_PublicSite/WEB-INF/lib/modjy.jar: do you maybe need to set the 
'modjy_jar.location' parameter?
	com.xhaus.modjy.ModjyJServlet.init(ModjyJServlet.java:94)
	javax.servlet.GenericServlet.init(GenericServlet.java:212)
	org.apache.catalina.valves.ErrorReportValve.invoke
(ErrorReportValve.java:102)
	org.apache.catalina.connector.CoyoteAdapter.service
(CoyoteAdapter.java:286)
	org.apache.coyote.http11.Http11Processor.process
(Http11Processor.java:845)
	org.apache.coyote.http11.Http11Protocol
$Http11ConnectionHandler.process(Http11Protocol.java:583)
	org.apache.tomcat.util.net.JIoEndpoint$Worker.run
(JIoEndpoint.java:447)
	java.lang.Thread.run(Thread.java:619)
History
Date User Action Args
2009-01-08 13:04:50JohnSJohnMsetmessages: + msg4011
2009-01-06 17:54:17amaksetstatus: open -> closed
resolution: out of date
messages: + msg4003
2008-12-24 13:16:42amaksetmessages: + msg3974
2008-12-18 20:36:11amaksetassignee: amak
2008-12-18 20:35:23amaksetnosy: + amak
messages: + msg3964
2008-12-18 08:40:51JohnSJohnMsetmessages: + msg3959
2008-12-17 20:41:29pjenveysetnosy: + pjenvey
messages: + msg3957
2008-12-17 11:00:17JohnSJohnMcreate