views:

787

answers:

3

i am trying to use velocity framework on google app engine. i wrote a small program with a main method and tried running it locally. i get the following exception :

Exception in thread "main" org.apache.velocity.exception.VelocityException: Failed to initialize an instance of org.apache.velocity.runtime.log.ServletLogChute with the current runtime configuration.
 at org.apache.velocity.runtime.log.LogManager.createLogChute(LogManager.java:206)
 at org.apache.velocity.runtime.log.LogManager.updateLog(LogManager.java:255)
 at org.apache.velocity.runtime.RuntimeInstance.initializeLog(RuntimeInstance.java:795)
 at org.apache.velocity.runtime.RuntimeInstance.init(RuntimeInstance.java:250)
 at org.apache.velocity.app.VelocityEngine.init(VelocityEngine.java:107)
 at Main.main(Main.java:10)
Caused by: java.lang.UnsupportedOperationException: Could not retrieve ServletContext from application attributes
 at org.apache.velocity.runtime.log.ServletLogChute.init(ServletLogChute.java:73)
 at org.apache.velocity.runtime.log.LogManager.createLogChute(LogManager.java:157)
 ... 5 more

Here is my program:

import java.io.StringWriter;
import org.apache.velocity.app.VelocityEngine;
import org.apache.velocity.Template;
import org.apache.velocity.VelocityContext;

public class Main {
 public static void main(String[] args) throws Exception{
        /*  first, get and initialize an engine  */
        VelocityEngine ve = new VelocityEngine();
        ve.init();
        /*  next, get the Template  */
        Template t = ve.getTemplate( "helloworld.vm" );
        /*  create a context and add data */
        VelocityContext context = new VelocityContext();
        context.put("name", "World");
        /* now render the template into a StringWriter */
        StringWriter writer = new StringWriter();
        t.merge( context, writer );
        /* show the World */
        System.out.println( writer.toString() );    
 }
}

the same program runs perfectly fine on a normal eclipse project. what could be the problem?

+1  A: 

This may not be the end of the world and story, but there's a list of GAE compatible software:

http://groups.google.com/group/google-appengine-java/web/will-it-play-in-app-engine

and I didn't find Velocity in it. It's possible people just forgot to test and include it in the list, but it's also possible Velocity brings some API with it that doesn't play nicely with GAE.

Carl Smotricz
thanks carl..they had just missed it...its working perfectly fine on app engine
Aadith
Good. Did you have to resolve the logging problem I read about, or did you just manage to get it working as it was?
Carl Smotricz
Indeed, I would be interested to know what the solution was for reference.
Will Prescott
+3  A: 

Seems to only be the ServletLogChute class that requires the ServletContext, Velocity itself can work entirely standalone from a Servlet environment.

Since you obviously don't have a servelt log, try adding the following before you call ve.init():

ve.setProperty("runtime.log.logsystem.class", "org.apache.velocity.runtime.log.NullLogChute");

...or check here if you have specific logging requirements.

Will Prescott
+1  A: 

Velocity can be definitively made to run on GAE/J.

Apache Click Framework that is using Velocity as it's template engine, works without a problem on GAE/J.

It needs of course a different configuration than usual since GAE/J is a constraint environment, but nevertheless, it works.

A. Ionescu