Quantcast

Using Freemarker from Mule transformer

classic Classic list List threaded Threaded
2 messages Options
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Using Freemarker from Mule transformer

athieme

I am having problems using a templating engine like Freemarker or Velocity from within a Mule deployed app. I've had similar ClassNotFoundExceptions using both Freemarker and Velocity. I suspect there's some magic Mule or Maven dependency problem, but I'm not sure what. As an aside, I first tried using Velocity and had a ClassNotFoundException (org.apache.velocity.exception.VelocityException), too. And, I followed the steps outlined in David Dossot's Mule In Action example (http://code.google.com/p/muleinaction/source/browse/branches/mule-3/chapter05/velocity-transformer/?r=483).

Using Freemarker as the example, when I attempt to deploy my application to Mule, I get the following Exception.

Caused by: java.lang.ClassNotFoundException: freemarker.cache.TemplateLoader
at java.net.URLClassLoader$1.run(URLClassLoader.java:202)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(URLClassLoader.java:190)
at java.lang.ClassLoader.loadClass(ClassLoader.java:306)
at java.lang.ClassLoader.loadClass(ClassLoader.java:247)
... 63 more

During the deployment of the app, I see the freemarker-2.3.19.jar loaded. See below.

INFO  2012-05-25 22:50:00,890 [Mule.app.deployer.monitor.1.thread.1] org.mule.module.launcher.MuleApplicationClassLoader: [dartmouth-server] Loading the following jars:
=============================
file:/Users/athieme/mule-standalone-3.2.1/apps/dartmouth-server/lib/aopalliance-1.0.jar
file:/Users/athieme/mule-standalone-3.2.1/apps/dartmouth-server/lib/cglib-nodep-2.1_3.jar
file:/Users/athieme/mule-standalone-3.2.1/apps/dartmouth-server/lib/commons-logging-1.1.1.jar
file:/Users/athieme/mule-standalone-3.2.1/apps/dartmouth-server/lib/dartmouth-common.jar
file:/Users/athieme/mule-standalone-3.2.1/apps/dartmouth-server/lib/freemarker-2.3.19.jar
file:/Users/athieme/mule-standalone-3.2.1/apps/dartmouth-server/lib/guava-r09.jar
file:/Users/athieme/mule-standalone-3.2.1/apps/dartmouth-server/lib/jaxb-api-2.2.5.jar
file:/Users/athieme/mule-standalone-3.2.1/apps/dartmouth-server/lib/jaxb2-basics-0.6.0.jar
file:/Users/athieme/mule-standalone-3.2.1/apps/dartmouth-server/lib/jaxb2-basics-runtime-0.6.0.jar
file:/Users/athieme/mule-standalone-3.2.1/apps/dartmouth-server/lib/jaxb2-basics-tools-0.6.0.jar
file:/Users/athieme/mule-standalone-3.2.1/apps/dartmouth-server/lib/jedis-2.0.0.jar
file:/Users/athieme/mule-standalone-3.2.1/apps/dartmouth-server/lib/spring-asm-3.1.1.RELEASE.jar
file:/Users/athieme/mule-standalone-3.2.1/apps/dartmouth-server/lib/spring-context-support-3.1.1.RELEASE.jar
file:/Users/athieme/mule-standalone-3.2.1/apps/dartmouth-server/lib/spring-core-3.1.1.RELEASE.jar
file:/Users/athieme/mule-standalone-3.2.1/apps/dartmouth-server/lib/spring-data-redis-1.0.0.RC1.jar
file:/Users/athieme/mule-standalone-3.2.1/apps/dartmouth-server/lib/spring-tx-3.1.0.RC1.jar
=============================

It's freemarker-2.3.19.jar that contains freemarker.cache.TemplateLoader. See below.

11:28 PM dartmouth> jar tvf ~/mule/apps/dartmouth-server/lib/freemarker-2.3.19.jar |grep "freemarker.cache.TemplateLoader"
   445 Thu Mar 01 01:10:00 EST 2012 freemarker/cache/TemplateLoader.class
11:28 PM dartmouth> 

In my Mule/Spring configuration, I have the following.

<spring:bean id="freemarkerConfig" class="org.springframework.ui.freemarker.FreeMarkerConfigurationFactoryBean">
      <spring:property name="templateLoaderPath" value="classpath:com/athieme/"/>
    </spring:bean>

As a separate test, I created a very small, non-Mule Maven project that includes the required dependancies to inject the Freemarker Configuration object. Here's that pom.xml. When I run code from this project, Freemarker is called correctly.

<?xml version="1.0" encoding="UTF-8"?>
    <modelVersion>4.0.0</modelVersion>
    <groupId>com.athieme</groupId>
    <artifactId>simple-spring-maven</artifactId>
    <version>1.0.0-SNAPSHOT</version>
    <packaging>jar</packaging>
    <properties>
        <org.springframework>3.1.1.RELEASE</org.springframework>
    </properties>
    <dependencies>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-core</artifactId>
            <version>${org.springframework}</version>
       </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-context</artifactId>
            <version>${org.springframework}</version>
       </dependency>
       <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-context-support</artifactId>
            <version>${org.springframework}</version>
        </dependency>
       <dependency>
           <groupId>org.freemarker</groupId>
           <artifactId>freemarker</artifactId>
           <version>2.3.19</version>
        </dependency>
    </dependencies>
</project>

Here's (basically) the Java code to call Freemarker; although, Mule never gets this far because the Mule/Spring config never loads completely.

final StringWriter result = new StringWriter();
final Template template = configuration.getTemplate("password.ftl");
final Message message = new Message();
java.util.Map map = new java.util.HashMap();
map.put("message", message);
template.process(map, result);
String str = result.toString();
System.out.println(str);


Alex Thieme
508-361-2788



Alex Thieme
508-361-2788

Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: Using Freemarker from Mule transformer

athieme
This question was answered (offline) by David Dossot (see below). Copying the freemarker jar into mule/lib/user folder resolved the class loading problem.

On May 28, 2012, at 12:29 PM, David Dossot wrote:

Two things that came to mind:
Cheers,
D.

Loading...