public VelocityTemplatingEngine()
{
final URL templateDirUrl = getClass().getClassLoader().getResource(VELOCITY_TEMPLATE_DIR);
if (templateDirUrl == null)
{
throw new RestLiInternalException("Unable to find the Velocity template resources");
}
StringBuilder configName;
if ("jar".equals(templateDirUrl.getProtocol()))
{
_velocity = new VelocityEngine();
// config Velocity to use the jar resource loader
// more detail in Velocity user manual
_velocity.setProperty(VelocityEngine.RESOURCE_LOADER, "jar");
configName = new StringBuilder("jar.").append(VelocityEngine.RESOURCE_LOADER).append(".class");
_velocity.setProperty(configName.toString(), JarResourceLoader.class.getName());
configName = new StringBuilder("jar.").append(VelocityEngine.RESOURCE_LOADER).append(".path");
// fix for Velocity 1.5: jar URL needs to be ended with "!/"
final String normalizedUrl = templateDirUrl.toString().substring(0, templateDirUrl.toString().length() - VELOCITY_TEMPLATE_DIR.length());
_velocity.setProperty(configName.toString(), normalizedUrl);
}
else if ("file".equals(templateDirUrl.getProtocol()))
{
_velocity = new VelocityEngine();
final String resourceDirPath = new File(templateDirUrl.getPath()).getParent();
configName = new StringBuilder("file.").append(VelocityEngine.RESOURCE_LOADER).append(".path");
_velocity.setProperty(configName.toString(), resourceDirPath);
}
else
{
throw new IllegalArgumentException("Unsupported template path scheme");
}
_velocity.setProperty(RuntimeConstants.RUNTIME_LOG_LOGSYSTEM_CLASS, Log4JLogChute.class.getName());
_velocity.setProperty(Log4JLogChute.RUNTIME_LOG_LOG4J_LOGGER, getClass().getName());
try
{
_velocity.init();
}
catch (Exception e)
{
throw new RestLiInternalException(e);
}
}