[opencms-dev] On how to integrate OpenCMS and velocity:

Alexander Kandzior alex at opencms.com
Sat Mar 30 22:36:24 CET 2002


Velocity integration will require more then the "simple" addition of a
new launcher. I already have looked into this, since it is close to my
JSP approach, and this is not the answer. Here's why:

1. The synchronized issue Werner has pointed out is not going to
disappear, since it is the _Velocity_ engine that requires this. Werners
implementation of the OpenCMSResourceLoader.class is used within
Velocity and the class is not initialized from OpenCms. Velocity can
with that class already access files in the OpenCms repository. The
synchronization is the hack to put the current cms Object into the
Velocity OpenCMSResourceLoader, which has a static cms Object. There is
not other simple way to pass the current CmsObject to the Velocity
ResourceLoader (at last not one I can think of right now). Adding a new
launcher will not solve this problem, since the ResourceLoader must
still be initialized.

2. Velocity is in many aspects like the XMLTemplates of OpenCms. A
Velocity template must be called from a servlet. Previously, the servlet
must set parameters in a context that will then be accessible in the
VTL. Same as with XMLTemplates. Advantage of the VTL is much easier
syntax and the ability to iterate over arrays, enumerations and such,
which is not possible in the XMLTemplates. So if you write a launcher
for VTL this must be able to also select a "default" velocity servlet
along with the template. So additionally to the launcher, one must also
provide something like a com.opencms.template.CmsRootTemplate.

BTW here's what I think would be the way to improve the situation:

For 1) I currently think of 3 Options:

Option 1: Mirror to "real" FS
In this scenario, every VTL file in the VFS is mirrored to the "real" FS
in some directory. Similar to my JSP approach. Velocity then read these
files using it's standard "file" ResourceLoader. Problem: Mirrored files
must be managed by OpenCms, and files in "real" FS will have no
permission system attached.

Option 2: Pass CmsObject to Velocity ResouceLoader
Lets assume it would be possible to pass an initialized CmsObject cms to
the Velocity ResouceLoader. How this would be done I was not able to
figure out yet, maybe it would require some extending of the internal
Velocity classes. The goal would be that the ResourceLoader would have
acceess to a CmsObject like in Werners implementation, however this
would not need to be synchornized. That way, each access to a VTL would
be done using the permissions of the current logged in user. However,
this would mean a lot of work in (probably extending Velocity) and also
would make template caching in velocity rather unusable, since this is
also not OpenCms permission based. You would end up with having to load
the template every time from the DB, which will impact performance. 

Option 3: Have a standard Velocity permission set
Here, every VTL in the OpenCms would be required to have some common
permission structure, which might be a CmsUser or a CmsGroup. Let's say
we have a standard user "velocity". Every VTL must be made accessible to
"velocity". A new Velocity ResourceLoader would simply always work as a
user "velocity", and the username / password could be set in the
Velocity property file. 

For the record, I currently would go with Option 3.

For 2) I have not thought about that in great detail, but I think there
could be something like a "CanonicalRoot" for the VTL, ie. a velocity
sevlet with some standard action. 


PS: Michael, what about my question regarding the wrapping of the
response output - stream? BTW, I have found out there is a
javax.servlet.http.HttpServletResponseWrapper already defined in the
Servlet 2.3 API for _exactly_ that purpose. That pretty much answers the
question, I would say. I should RTFM more often ;-)

-----Original Message-----
From: owner-opencms-dev at www.opencms.com
[mailto:owner-opencms-dev at www.opencms.com] On Behalf Of Michael Emmerich
Sent: Saturday, March 30, 2002 10:50 AM
To: opencms-dev at www.opencms.com
Subject: Re: [opencms-dev] On how to integrate OpenCMS and velocity:

Hello Werner,

after reading your ideas and example how to integrate Velocity into
OpenCms, it appears to me that there might be a simpler way to add
Velocity support into OpenCms. Please note that I have not tested your
implemetation on a running system so far, but I think i got your idea
right :)

Your idea is the following:
1) Write a OpenCms template that contains a <process> tag where the
generated Velocity output is generated.
2) Write a Template Class which will read the Velocity template,
generate the output from it and insert it into the <process> tag of the
OpenCms template.

So we need two template mechanisms to be started to generate the final
output of the page we want to see. Since the OpenCms template could
contain some HTML code as well, it might be nescessary to modify two
templates (the OpenCms one and the Velocity one) to modify the pages.
Not to forget that more templates involved alsways stand for more places
whare you can make errors :)

So my suggestion for a simpler integration would require a bit more
inital Java programming by adding a new resource type (Velocity
template) and a new launcher to the system.

When you request a resource form the OpenCms system, the resource is
first read form the OpenCms database. Depending on the type of this
resource (plain, xml, page, binary) a special launcher is started that
will initiate the production of the output of this resource. In the
simplest way, the resource content is taken and send back to the client
browser (this is done for text and binary resources, using the plain
launcher). For XML-Files and page resources, the OpenCms temlate
mechanism is started at that point, creating the HTML-output.

Now, the idea would be to write a Velocity launcher. Your resource
inside of OpenCms would only contain the Velocity template (like the
file you read from the getContent method in your example). The Velocity
launcher then would do the same as your getContent method now - get the
content of your Velocity Template and create the HTML output similar to

  parseResult = (String)
theParameters, cms);


The advantage of this implementation is that the OpenCms template
mechanism is not involved in creating the output anymore, and this would
have no negative effects on the system performance.

If you take a look at the exisiting launcher currently in the system,
its would be not that difficult to modifiy the current plain launcher to
directly access Velocity this way. 

The new resource type could be hooked in the system by writing a
resource type module. The other advantage woould be that the Velocity
templates would have the complete access control of resources in OpenCms
and would be part of the project mechanism of course.

Besides this, it would still be possbile to use your implementation to
mix the output of both worlds, Velocity and OpenCms templates.

Another intersting question is, how will it be possible that you can
access user and acces data of the OpenCms from within Velocity? Since
you have a complte user and right management in OpenCms already, it
would be good if this information could be used in the Velocity
templates as well.

So far my thoughts based on your ideas,

Regards and Happy Easter,

	Michael Emmerich

Home   : emmerich at german-navy.de
Work   : michael.emmerich at framfab.de
ICQ    : 18897063
WWW    : German Naval History http://www.german-navy.de

More information about the opencms-dev mailing list