<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<HTML><HEAD>
<META http-equiv=Content-Type content="text/html; charset=iso-8859-1">
<META content="MSHTML 6.00.2600.0" name=GENERATOR>
<STYLE></STYLE>
</HEAD>
<BODY bgColor=#ffffff>
<DIV><FONT face=Arial size=2>Hi people,</FONT></DIV>
<DIV><FONT face=Arial size=2></FONT> </DIV>
<DIV><FONT face=Arial size=2>Here's a little contribution from me for those who
are interested.</FONT></DIV>
<DIV><FONT face=Arial size=2></FONT> </DIV>
<DIV><FONT face=Arial size=2>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.
</FONT></DIV>
<DIV><FONT face=Arial size=2></FONT> </DIV>
<DIV><FONT face=Arial size=2>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.
</FONT></DIV>
<DIV><FONT face=Arial size=2></FONT> </DIV>
<DIV><FONT face=Arial size=2>
<DIV><FONT face=Arial size=2>Example python code</FONT></DIV>
<DIV><FONT face=Arial size=2></FONT><FONT face=Arial size=2></FONT> </DIV>
<DIV><FONT face=Arial size=2>o = cms.getCmsObject()<BR>ctx =
o.getRequestContext()<BR>user = ctx.currentUser()</FONT></DIV>
<DIV><FONT face=Arial size=2>print user</FONT></DIV>
<DIV><FONT face=Arial size=2>from java.util import Date<BR>d =
Date(user.getLastlogin())<BR>print "last login", d<BR></FONT></DIV>
<DIV><FONT face=Arial size=2></FONT> </DIV>
<DIV><FONT face=Arial size=2>The output</FONT></DIV>
<DIV><FONT face=Arial size=2></FONT> </DIV>
<DIV><FONT face=Arial size=2><!--StartFragment -->[User]:will , Id=21 , flags=0
, type=1 :Will the web user<BR>last login Thu Jul 17 11:30:34 PDT
2003</FONT></DIV></FONT></DIV>
<DIV><FONT face=Arial size=2></FONT> </DIV>
<DIV><FONT face=Arial size=2></FONT> </DIV>
<DIV><FONT face=Arial size=2>Jython is available from <A
href="http://www.jython.org/">http://www.jython.org/</A></FONT></DIV>
<DIV><FONT face=Arial size=2></FONT> </DIV>
<DIV><FONT face=Arial size=2>1. Install Jython.</FONT></DIV>
<DIV><FONT face=Arial size=2>2. Put a copy of jython.jar in your
opencms\WEB-INF\lib directory.</FONT></DIV>
<DIV><FONT face=Arial size=2>3. Add -Dpython.home=C:\jython21 to the java
startup of opencms. (Using the path where your jython is
installed).</FONT></DIV>
<DIV><FONT face=Arial size=2>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.</FONT></DIV>
<DIV><FONT face=Arial size=2></FONT> </DIV>
<DIV><FONT face=Arial size=2>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.</FONT></DIV>
<DIV><FONT face=Arial size=2></FONT> </DIV>
<DIV><FONT face=Arial size=2>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.</FONT></DIV>
<DIV><FONT face=Arial size=2></FONT> </DIV>
<DIV><FONT face=Arial size=2><%@ page import="org.python.util.*,
com.opencms.flex.jsp.*" %></FONT></DIV>
<DIV><FONT face=Arial size=2></FONT> </DIV>
<DIV><FONT face=Arial
size=2><html><BR><body><BR><h1>Python</h2><BR><form
action="py" method=post></FONT></DIV>
<DIV><FONT face=Arial size=2></FONT> </DIV>
<DIV><FONT face=Arial size=2><table width="100%"
border="0"><BR><tr><BR><td>Script</td><BR></tr><BR><tr><BR><td"><BR><input
type="text" name="script" size=80 value="<%= request.getParameter("script")
!= null ? request.getParameter("script") :
""%>"><BR></td><BR></tr><BR><tr><BR><td>Code</td><BR></tr><BR><tr><BR><td
width="100%"><BR><textarea name="code" rows=12 cols=80><BR><%=
request.getParameter("code") != null ? request.getParameter("code") :
""%><BR></textarea
><BR></td><BR></tr><BR><tr><BR><td><BR><input
type="submit"
value="Submit"><BR></td><BR></tr><BR></table><BR></form></FONT></DIV>
<DIV> </DIV>
<DIV><FONT face=Arial size=2><%<BR> CmsJspActionElement cms = new
CmsJspActionElement(pageContext, request, response);<BR>
org.python.util.PythonInterpreter py = new
org.python.util.PythonInterpreter();<BR> py.set("cms", cms);<BR>
py.set("request", request);<BR> py.set("response", response);<BR>
py.set("pageContext", pageContext);<BR>
py.setOut(pageContext.getOut());<BR> String script =
request.getParameter("script");<BR> if (script != null &&
!script.equals(""))<BR> {<BR>
pageContext.getOut().print("<h3>" + script +
"</h3>");<BR>
pageContext.getOut().print("<pre>");<BR>
py.execfile(script);<BR>
pageContext.getOut().print("</pre>");</FONT></DIV>
<DIV> </DIV>
<DIV><FONT face=Arial size=2> }<BR> else<BR>
{<BR> String code =
request.getParameter("code");<BR> if (code != null
&& !code.equals(""))<BR>
{<BR>
pageContext.getOut().print("<pre>");<BR>
py.exec(code);<BR>
pageContext.getOut().print("</pre>");<BR>
}<BR>
}<BR>%><BR><h2>done</h2><BR></body><BR></html></FONT><FONT
face=Arial size=2></DIV></FONT></BODY></HTML>