Skip to content

Commit

Permalink
Use "instance mode" of Velocity
Browse files Browse the repository at this point in the history
Avoids potential clash if another part of the app uses Velocity elsewhere
  • Loading branch information
mstahv committed Aug 7, 2023
1 parent d73cd87 commit e136ca9
Showing 1 changed file with 7 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import org.apache.velocity.Template;
import org.apache.velocity.VelocityContext;
import org.apache.velocity.app.Velocity;
import org.apache.velocity.app.VelocityEngine;

import java.io.Serializable;
import java.io.StringWriter;
Expand All @@ -30,11 +31,13 @@
*/
public abstract class AbstractVelocityJsComponent extends Component {

static VelocityEngine ve;
static {
Properties p = new Properties();
p.setProperty("resource.loader", "class");
p.setProperty("class.resource.loader.class", "org.apache.velocity.runtime.resource.loader.ClasspathResourceLoader");
Velocity.init( p );
p.setProperty("class.resource.loader.class", org.apache.velocity.runtime.resource.loader.ClasspathResourceLoader.class.getName());
ve = new VelocityEngine();
ve.init(p);
}

/**
Expand Down Expand Up @@ -67,7 +70,7 @@ protected PendingJavaScriptResult velocityJs(String jsVelocityTemplate, Velocity
specialParameters = Collections.emptyList();
}
StringWriter script = new StringWriter();
Velocity.evaluate(ctx, script, "velocityJsComponent", jsVelocityTemplate);
ve.evaluate(ctx, script, "velocityJsComponent", jsVelocityTemplate);
Serializable[] parameters = specialParameters.toArray(new Serializable[0]);
return getElement().executeJs(script.toString(), parameters);
}
Expand Down Expand Up @@ -102,7 +105,7 @@ public PendingJavaScriptResult jsTemplate(String templateName, Map<String, Objec
}
});

Template template = Velocity.getTemplate(templateName);
Template template = ve.getTemplate(templateName);
StringWriter sw = new StringWriter();
template.merge(ctx,
sw
Expand Down

0 comments on commit e136ca9

Please sign in to comment.