Friday, September 7, 2012

How to use Velocity in xPages

We are using velocity in our project for build some HTML fro some templates.

There was a problem with using it in the Java libraries. Look at this post to find problem description and solution.

Now we want to use velocity in the xPage context. There is also a little trouble:

at java.lang.SecurityManager.checkPermission(SecurityManager.java:544)
at java.lang.Thread.getContextClassLoader(Thread.java:456)
at org.apache.velocity.util.ClassUtils.getClass(ClassUtils.java:68)


This problem could be resolved  by adding next string in to the java.policy:

permission java.lang.RuntimePermission "getClassLoader";

We could not edit java.policy unfortunately. So we resolve it in the next way.

1. Download velocity source

2. Find place of error (at org.apache.velocity.util.ClassUtils.getClass(ClassUtils.java:68)):
ClassLoader loader = Thread.currentThread().getContextClassLoader();

3. Replace this string with the xPage classloader initialization:
ClassLoader cl = com.ibm.domino.xsp.module.nsf.NotesContext.getCurrent().getModule().getModuleClassLoader();

4. Add appropriate library for compile velocity source with this new class.  You will need
Domino\osgi\shared\eclipse\plugins\com.ibm.xsp.domino_8.5.3.20110915-2025\lwpd.xsp.domino.jar
or similar

5. Compile new velocity jar from modified source.

Well done! You could upload velocity jar in to the WebContent\WEB-INF\lib and use it in the xPage context.

No comments: