[Json Foster]
>I'm currently trying to graft Jython into the Cocoon2 web publishing
>framework using IBM's Bean Scripting Framework. So far it has been a
>straightforward process, so kudos to the Jython team.
>
>If you're interested, check out xml.apache.org/cocoon and take a look at
>ScriptGenerator.
>
>Everything works great so long as all of the python code is within a single
>script. As previous posts have mentioned, as soon as I try to import from
>another file jython complains about not being able to find the resource in
>question.
>
>The solution presented earlier (in the context of the JythonServlet?),
>namely to use sys.add_package(), sounds like a good solution if you have
>control of the servlet. Unfortunately given the number of toolkits and
>classloaders I'm working with I don't have this option.
>
>I was wondering whether or not it would be possible to do something similar
>to sys.add_package() from within a python script? I tried this (naively)
>and was told that "sys" wasn't a known name.
Calling add_package from a python script is possible, but you will have
to import the "sys" module first:
import sys
sys.add_package('javax.xml.parsers')
from javax import xml
>My idea is that I know, as
>developer, that a class with a certain name is available somehow.
>
>I'm not sure that this solution would work, given that Tomcat and Cocoon2
>for sure have their own classloaders and the BSF might as well, but it's the
>only one I can think of.
It should work assuming that the classloader(s) set up by tomcat/cocoon2
would allow loading of the classes. If it doesn't work try to see of the
classes are available:
import java
cl = java.lang.Class.forName("javax.xml.parsers.SAXParserFactory")
If that fails to load the class you are after, no amount of
sys.add_package() will help.
>I *really* don't want to add things to my system
>classpath if I don't have to.
>
>As an aside, since I've been out of Java programming for so long, can I set
>that various jython parameters programmatically as opposed to having to use
>"-D" on the command line?
You can set some or all of the registry properties by explicit calling
PythonInterpreter.initialize() before calling any other methods in the
jython API. Examples of calling initialize() are normally available in
the FAQ (which happens to be offline at the moment).
regards,
finn
|