Check whether a velocity template exists before using it.

If we don't do this then an exception will be thrown and caught by
code below. This works too, but when Velocity throws an exception it
logs ERROR saying that template is absent. It confuses as you might
think that something bad happend, but the Gerrit is fine - it uses
a default template.

Change-Id: I2b444f73b026a477f2c8e438071447a05f816cfd
This commit is contained in:
Anatol Pomazau
2010-08-25 16:47:56 -07:00
parent 7acb50388e
commit f7e03ee787

View File

@@ -344,30 +344,24 @@ public abstract class OutgoingEmail {
velocityContext.put("StringUtils", StringUtils.class);
}
protected String velocify(String tpl) throws EmailException {
protected String velocify(String template) throws EmailException {
try {
StringWriter w = new StringWriter();
Velocity.evaluate(velocityContext, w, "OutgoingEmail", tpl);
Velocity.evaluate(velocityContext, w, "OutgoingEmail", template);
return w.toString();
} catch(Exception e) {
throw new EmailException("Velocity template "+ tpl.toString(), e);
throw new EmailException("Velocity template " + template, e);
}
}
protected String velocifyFile(String name) throws EmailException {
if (!Velocity.resourceExists(name)) {
name = "com/google/gerrit/server/mail/" + name;
}
try {
StringWriter w = new StringWriter();
Velocity.mergeTemplate(name, velocityContext, w);
Velocity.mergeTemplate(name, "UTF-8", velocityContext, w);
return w.toString();
} catch(ResourceNotFoundException e) {
try {
StringWriter w = new StringWriter();
String pkg = "com/google/gerrit/server/mail/";
Velocity.mergeTemplate(pkg + name, velocityContext, w);
return w.toString();
} catch(Exception e2) {
throw new EmailException("Velocity WAR template" + name + ".\n", e2);
}
} catch(Exception e) {
throw new EmailException("Velocity template " + name + ".\n", e);
}