/** * Creates and configures the velocity engine. * * @param devMode * @return */ private VelocityEngine configureVelocityEngine(final boolean devMode) { VelocityEngine engine = new VelocityEngine(); engine.setProperty("resource.loader", "classpath, jar"); engine.setProperty("classpath.resource.loader.class", ClasspathResourceLoader.class.getName()); engine.setProperty("classpath.resource.loader.cache", !devMode); engine.setProperty("classpath.resource.loader.modificationCheckInterval", 5L); engine.setProperty("jar.resource.loader.class", JarResourceLoader.class.getName()); engine.setProperty("jar.resource.loader.cache", !devMode); engine.setProperty("resource.manager.logwhenfound", false); engine.setProperty("input.encoding", "UTF-8"); engine.setProperty("output.encoding", "UTF-8"); engine.setProperty("directive.set.null.allowed", true); engine.setProperty("resource.manager.logwhenfound", false); engine.setProperty("velocimacro.permissions.allow.inline", true); engine.setProperty("velocimacro.library.autoreload", devMode); engine.setProperty("velocimacro.library", "/azkaban/webapp/servlet/velocity/macros.vm"); engine.setProperty("velocimacro.permissions.allow.inline.to.replace.global", true); engine.setProperty("velocimacro.arguments.strict", true); engine.setProperty("runtime.log.invalid.references", devMode); engine.setProperty("runtime.log.logsystem.class", Log4JLogChute.class); engine.setProperty("runtime.log.logsystem.log4j.logger", Logger.getLogger("org.apache.velocity.Logger")); engine.setProperty("parser.pool.size", 3); return engine; }
@Override protected void postProcessVelocityEngine(VelocityEngine velocityEngine) { super.postProcessVelocityEngine(velocityEngine); velocityEngine.setProperty(RuntimeConstants.RESOURCE_LOADER, "webapp,file,class,url,jar,spring,springMacro"); velocityEngine.setProperty("webapp.resource.loader.class", WebappResourceLoader.class.getName()); velocityEngine.setProperty("file.resource.loader.class", FileResourceLoader.class.getName()); velocityEngine.setProperty("class.resource.loader.class", ClasspathResourceLoader.class.getName()); velocityEngine.setProperty("url.resource.loader.class", URLResourceLoader.class.getName()); velocityEngine.setProperty("jar.resource.loader.class", JarResourceLoader.class.getName()); velocityEngine.setProperty("resource.manager.cache.class", ResourceCacheImpl.class.getName()); velocityEngine.setProperty("resource.manager.cache.size", 2048); velocityEngine.setProperty("resource.manager.class", ResourceManagerImpl.class.getName()); velocityEngine.setProperty(RuntimeConstants.COUNTER_INITIAL_VALUE, 1); velocityEngine.setProperty(RuntimeConstants.INPUT_ENCODING, inputEncoding); velocityEngine.setProperty(RuntimeConstants.OUTPUT_ENCODING, outputEncoding); velocityEngine.setProperty("contentType", contentType); // org.apache.velocity.runtime.log.AvalonLogChute // org.apache.velocity.runtime.log.Log4JLogChute // org.apache.velocity.runtime.log.CommonsLogLogChute // org.apache.velocity.runtime.log.ServletLogChute // org.apache.velocity.runtime.log.JdkLogChute velocityEngine.setProperty(RuntimeConstants.RUNTIME_LOG_LOGSYSTEM, new Log4JLogChute()); velocityEngine.setProperty(RuntimeConstants.FILE_RESOURCE_LOADER_CACHE, true); }
/** * Creates and configures the velocity engine. * * @param devMode * @return */ private VelocityEngine configureVelocityEngine(final boolean devMode) { VelocityEngine engine = new VelocityEngine(); engine.setProperty("resource.loader", "classpath, jar"); engine.setProperty("classpath.resource.loader.class", ClasspathResourceLoader.class.getName()); engine.setProperty("classpath.resource.loader.cache", !devMode); engine.setProperty("classpath.resource.loader.modificationCheckInterval", 5L); engine.setProperty("jar.resource.loader.class", JarResourceLoader.class.getName()); engine.setProperty("jar.resource.loader.cache", !devMode); engine.setProperty("resource.manager.logwhenfound", false); engine.setProperty("input.encoding", "UTF-8"); engine.setProperty("output.encoding", "UTF-8"); engine.setProperty("directive.set.null.allowed", true); engine.setProperty("resource.manager.logwhenfound", false); engine.setProperty("velocimacro.permissions.allow.inline", true); engine.setProperty("velocimacro.library.autoreload", devMode); engine.setProperty("velocimacro.library", "/azkaban/webapp/servlet/velocity/macros.vm"); engine.setProperty( "velocimacro.permissions.allow.inline.to.replace.global", true); engine.setProperty("velocimacro.arguments.strict", true); engine.setProperty("runtime.log.invalid.references", devMode); engine.setProperty("runtime.log.logsystem.class", Log4JLogChute.class); engine.setProperty("runtime.log.logsystem.log4j.logger", Logger.getLogger("org.apache.velocity.Logger")); engine.setProperty("parser.pool.size", 3); return engine; }
@Override public RuntimeInstance get() { String pkg = "org.apache.velocity.runtime.resource.loader"; Properties p = new Properties(); p.setProperty(RuntimeConstants.VM_PERM_INLINE_LOCAL, "true"); p.setProperty(RuntimeConstants.RUNTIME_LOG_LOGSYSTEM_CLASS, Slf4jLogChute.class.getName()); p.setProperty(RuntimeConstants.RUNTIME_REFERENCES_STRICT, "true"); p.setProperty("runtime.log.logsystem.log4j.category", "velocity"); p.setProperty(VELOCITY_RESOURCE_LOADER, "file, class, jar"); p.setProperty(VELOCITY_FILE_RESOURCE_LOADER_CLASS, pkg + ".FileResourceLoader"); p.setProperty( VELOCITY_FILE_RESOURCE_LOADER_PATH, site.static_dir.getParent().toAbsolutePath().toString()); p.setProperty(VELOCITY_CLASS_RESOURCE_LOADER_CLASS, ClasspathResourceLoader.class.getName()); p.setProperty(VELOCITY_JAR_RESOURCE_LOADER_CLASS, JarResourceLoader.class.getName()); p.setProperty(VELOCITY_JAR_RESOURCE_LOADER_PATH, detectPluginJar()); RuntimeInstance ri = new RuntimeInstance(); try { ri.init(p); } catch (Exception err) { throw new ProvisionException("Cannot configure Velocity templates", err); } return ri; }
/** * Returns a new {@link Page} using the given .vm template file path. The template file * path must be the resource path for the .vm file in the JAR since the VelocityEngine * is configured to load .vm files from JAR resources. */ public static Page newPage(String template) { VelocityEngine engine = new VelocityEngine(); engine.setProperty("resource.loader", "classpath, jar"); engine.setProperty("classpath.resource.loader.class", ClasspathResourceLoader.class.getName()); engine.setProperty("jar.resource.loader.class", JarResourceLoader.class.getName()); engine.setProperty("input.encoding", "UTF-8"); engine.setProperty("output.encoding", "UTF-8"); engine.setProperty("directive.set.null.allowed", true); engine.setProperty("parser.pool.size", 3); engine.setProperty("runtime.references.strict", true); return new Page(engine, template); }
/** * Initialize Velocity engine. Support the template files in either jar file or file system directory. */ public VelocityTemplatingEngine() { final URL templateDirUrl = getClass().getClassLoader().getResource(VELOCITY_TEMPLATE_DIR); if (templateDirUrl == null) { throw new Rest4JInternalException("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 Rest4JInternalException(e); } }