Support Velocity 1.5

This is a tiny backport to enable use of Velocity 1.5 rather than
1.6.4. The change is necessary to run under some servlet containers
that are incorrectly loading the wrong Velocity binary into the
application's classpath.

Change-Id: Ibbb83354cb04994ec347bb0b23c0cdfcf9b03d28
This commit is contained in:
Shawn O. Pearce
2011-10-10 12:44:27 -07:00
parent f16431b7b4
commit 493a7acaa8

View File

@@ -22,11 +22,14 @@ import com.google.gerrit.server.mail.EmailHeader.AddressList;
import org.apache.commons.lang.StringUtils;
import org.apache.velocity.Template;
import org.apache.velocity.VelocityContext;
import org.apache.velocity.context.InternalContextAdapterImpl;
import org.apache.velocity.runtime.RuntimeInstance;
import org.apache.velocity.runtime.parser.node.SimpleNode;
import org.eclipse.jgit.util.SystemReader;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.io.StringReader;
import java.io.StringWriter;
import java.net.MalformedURLException;
import java.net.URL;
@@ -346,9 +349,19 @@ public abstract class OutgoingEmail {
protected String velocify(String template) throws EmailException {
try {
StringWriter w = new StringWriter();
args.velocityRuntime.evaluate(velocityContext, w, "OutgoingEmail", template);
return w.toString();
RuntimeInstance runtime = args.velocityRuntime;
String templateName = "OutgoingEmail";
SimpleNode tree = runtime.parse(new StringReader(template), templateName);
InternalContextAdapterImpl ica = new InternalContextAdapterImpl(velocityContext);
ica.pushCurrentTemplateName(templateName);
try {
tree.init(ica, runtime);
StringWriter w = new StringWriter();
tree.render(ica, w);
return w.toString();
} finally {
ica.popCurrentTemplateName();
}
} catch (Exception e) {
throw new EmailException("Cannot format velocity template: " + template, e);
}