[opencms-dev] J-Python integration

Scott Newham scott at wranglers.com.au
Thu Jul 17 04:52:02 CEST 2003


Hi people,

Here's a little contribution from me for those who are interested.

I'm implementing a site with OpenCMS for the first time. I've spent a couple weeks trolling the internals to get an understanding for integrating external functionality into it. As part of my investigations I've integrated Python scripting into a JSP so that I can interactively call OpenCMS internals to learn how they work and automate some development tasks. 

Jython is an implementation of Python on a JVM. The Python code compiles down to Java byte code and allows a virtually seamless mix of Java and Python. You can subclass Java classes in Python and lots more. I find I always use it when doing Java work because it allows rapid investigation without needing a compile/debug cycle. I'm finding this of greater benefit in OpenCMS because of the latency in editing and publishing JSPs. 

Example python code

o = cms.getCmsObject()
ctx = o.getRequestContext()
user = ctx.currentUser()
print user
from java.util import Date
d = Date(user.getLastlogin())
print "last login", d


The output

[User]:will , Id=21 , flags=0 , type=1 :Will the web user
last login Thu Jul 17 11:30:34 PDT 2003


Jython is available from http://www.jython.org/

1. Install Jython.
2. Put a copy of jython.jar in your opencms\WEB-INF\lib directory.
3. Add  -Dpython.home=C:\jython21 to the java startup of opencms. (Using the path where your jython is installed).
4. Add the following JSP. I call it "py". It's very basic - I just knocked it up to get it working. It could be improved vastly but I really just wanted it to work so I could get on with other stuff.

Note it will run an external script or you can type code into the text area. The variable "cms" (and others) are put into your Python scope to use. "cms" is the a CmsJspActionElement which should get you on your way. Stdout in python is redirected to the JSP output.

TODO: Handle python exceptions properly. If an exception is raised it will propogate back to the jsp renderer and display a java stack trace which isn't very helpful.

<%@ page import="org.python.util.*, com.opencms.flex.jsp.*" %>

<html>
<body>
<h1>Python</h2>
<form action="py" method=post>

<table width="100%" border="0">
<tr>
<td>Script</td>
</tr>
<tr>
<td">
<input type="text" name="script" size=80 value="<%= request.getParameter("script") != null ? request.getParameter("script") : ""%>">
</td>
</tr>
<tr>
<td>Code</td>
</tr>
<tr>
<td width="100%">
<textarea name="code" rows=12 cols=80>
<%= request.getParameter("code") != null ? request.getParameter("code") : ""%>
</textarea >
</td>
</tr>
<tr>
<td>
<input type="submit" value="Submit">
</td>
</tr>
</table>
</form>

<%
  CmsJspActionElement cms = new CmsJspActionElement(pageContext, request, response);
  org.python.util.PythonInterpreter py = new org.python.util.PythonInterpreter();
  py.set("cms", cms);
  py.set("request", request);
  py.set("response", response);
  py.set("pageContext", pageContext);
  py.setOut(pageContext.getOut());
  String script = request.getParameter("script");
  if (script != null && !script.equals(""))
  {
      pageContext.getOut().print("<h3>" + script + "</h3>");
      pageContext.getOut().print("<pre>");
      py.execfile(script);
      pageContext.getOut().print("</pre>");

  }
  else
  {
      String code = request.getParameter("code");
      if (code != null && !code.equals(""))
      {
          pageContext.getOut().print("<pre>");
          py.exec(code);
          pageContext.getOut().print("</pre>");
      }
  }
%>
<h2>done</h2>
</body>
</html>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://webmail.opencms.org/pipermail/opencms-dev/attachments/20030717/16505d62/attachment.htm>


More information about the opencms-dev mailing list