Search in sources :

Example 1 with VelocityEngine

use of org.apache.velocity.app.VelocityEngine in project jmxtrans by jmxtrans.

the class VelocityWriter method getVelocityEngine.

/**
	 * Sets velocity up to load resources from a list of paths.
	 */
protected VelocityEngine getVelocityEngine(List<String> paths) {
    VelocityEngine ve = new VelocityEngine();
    ve.setProperty(RuntimeConstants.RESOURCE_LOADER, "file");
    ve.setProperty("cp.resource.loader.class", "org.apache.velocity.runtime.resource.loader.FileResourceLoader");
    ve.setProperty("cp.resource.loader.cache", "true");
    ve.setProperty("cp.resource.loader.path", StringUtils.join(paths, ","));
    ve.setProperty("cp.resource.loader.modificationCheckInterval ", "10");
    ve.setProperty("input.encoding", "UTF-8");
    ve.setProperty("output.encoding", "UTF-8");
    ve.setProperty("runtime.log", "");
    return ve;
}
Also used : VelocityEngine(org.apache.velocity.app.VelocityEngine)

Example 2 with VelocityEngine

use of org.apache.velocity.app.VelocityEngine in project stashbot by palantir.

the class VelocityManagerTest method testVelocityTools.

@Test
public void testVelocityTools() {
    VelocityEngine ve = vm.getVelocityEngine();
    VelocityContext vc = vm.getVelocityContext();
    // contains escape tool
    Assert.assertTrue(vc.containsKey("esc"));
    // escape tool works
    Template test = ve.getTemplate("test-template.vm");
    StringWriter testWriter = new StringWriter();
    test.merge(vc, testWriter);
    String result = testWriter.toString();
    Assert.assertEquals("&amp;\n", result);
}
Also used : VelocityEngine(org.apache.velocity.app.VelocityEngine) StringWriter(java.io.StringWriter) VelocityContext(org.apache.velocity.VelocityContext) Template(org.apache.velocity.Template) Test(org.junit.Test)

Example 3 with VelocityEngine

use of org.apache.velocity.app.VelocityEngine in project camel by apache.

the class VelocityEndpoint method getVelocityEngine.

private synchronized VelocityEngine getVelocityEngine() throws Exception {
    if (velocityEngine == null) {
        velocityEngine = new VelocityEngine();
        // set the class resolver as a property so we can access it from CamelVelocityClasspathResourceLoader
        velocityEngine.addProperty("CamelClassResolver", getCamelContext().getClassResolver());
        // set default properties
        Properties properties = new Properties();
        properties.setProperty(RuntimeConstants.FILE_RESOURCE_LOADER_CACHE, isLoaderCache() ? "true" : "false");
        properties.setProperty(RuntimeConstants.RESOURCE_LOADER, "file, class");
        properties.setProperty("class.resource.loader.description", "Camel Velocity Classpath Resource Loader");
        properties.setProperty("class.resource.loader.class", CamelVelocityClasspathResourceLoader.class.getName());
        properties.setProperty(RuntimeConstants.RUNTIME_LOG_LOGSYSTEM_CLASS, CommonsLogLogChute.class.getName());
        properties.setProperty(CommonsLogLogChute.LOGCHUTE_COMMONS_LOG_NAME, VelocityEndpoint.class.getName());
        // load the velocity properties from property file which may overrides the default ones
        if (ObjectHelper.isNotEmpty(getPropertiesFile())) {
            InputStream reader = ResourceHelper.resolveMandatoryResourceAsInputStream(getCamelContext(), getPropertiesFile());
            try {
                properties.load(reader);
                log.info("Loaded the velocity configuration file " + getPropertiesFile());
            } finally {
                IOHelper.close(reader, getPropertiesFile(), log);
            }
        }
        log.debug("Initializing VelocityEngine with properties {}", properties);
        // help the velocityEngine to load the CamelVelocityClasspathResourceLoader
        ClassLoader old = Thread.currentThread().getContextClassLoader();
        try {
            ClassLoader delegate = new CamelVelocityDelegateClassLoader(old);
            Thread.currentThread().setContextClassLoader(delegate);
            velocityEngine.init(properties);
        } finally {
            Thread.currentThread().setContextClassLoader(old);
        }
    }
    return velocityEngine;
}
Also used : VelocityEngine(org.apache.velocity.app.VelocityEngine) CommonsLogLogChute(org.apache.velocity.runtime.log.CommonsLogLogChute) InputStream(java.io.InputStream) Properties(java.util.Properties)

Example 4 with VelocityEngine

use of org.apache.velocity.app.VelocityEngine in project camel by apache.

the class AbstractGeneratorMojo method getEngine.

protected static VelocityEngine getEngine() {
    if (engine == null) {
        // initialize velocity to load resources from class loader and use Log4J
        Properties velocityProperties = new Properties();
        velocityProperties.setProperty(RuntimeConstants.RESOURCE_LOADER, "cloader");
        velocityProperties.setProperty("cloader.resource.loader.class", ClasspathResourceLoader.class.getName());
        velocityProperties.setProperty(RuntimeConstants.RUNTIME_LOG_LOGSYSTEM_CLASS, Log4JLogChute.class.getName());
        final Logger velocityLogger = LoggerFactory.getLogger("org.apache.camel.maven.Velocity");
        velocityProperties.setProperty(Log4JLogChute.RUNTIME_LOG_LOG4J_LOGGER, velocityLogger.getName());
        engine = new VelocityEngine(velocityProperties);
        engine.init();
    }
    return engine;
}
Also used : VelocityEngine(org.apache.velocity.app.VelocityEngine) Log4JLogChute(org.apache.velocity.runtime.log.Log4JLogChute) ClasspathResourceLoader(org.apache.velocity.runtime.resource.loader.ClasspathResourceLoader) Properties(java.util.Properties) Logger(org.slf4j.Logger)

Example 5 with VelocityEngine

use of org.apache.velocity.app.VelocityEngine in project camel by apache.

the class CamelSalesforceMojo method createVelocityEngine.

static VelocityEngine createVelocityEngine() {
    // initialize velocity to load resources from class loader and use Log4J
    final Properties velocityProperties = new Properties();
    velocityProperties.setProperty(RuntimeConstants.RESOURCE_LOADER, "cloader");
    velocityProperties.setProperty("cloader.resource.loader.class", ClasspathResourceLoader.class.getName());
    velocityProperties.setProperty(RuntimeConstants.RUNTIME_LOG_LOGSYSTEM_CLASS, Log4JLogChute.class.getName());
    velocityProperties.setProperty(RuntimeConstants.RUNTIME_LOG_LOGSYSTEM + ".log4j.logger", LOG.getName());
    final VelocityEngine engine = new VelocityEngine(velocityProperties);
    return engine;
}
Also used : VelocityEngine(org.apache.velocity.app.VelocityEngine) Log4JLogChute(org.apache.velocity.runtime.log.Log4JLogChute) ClasspathResourceLoader(org.apache.velocity.runtime.resource.loader.ClasspathResourceLoader) Properties(java.util.Properties)

Aggregations

VelocityEngine (org.apache.velocity.app.VelocityEngine)29 VelocityContext (org.apache.velocity.VelocityContext)13 StringWriter (java.io.StringWriter)10 Properties (java.util.Properties)9 Template (org.apache.velocity.Template)9 IOException (java.io.IOException)7 ClasspathResourceLoader (org.apache.velocity.runtime.resource.loader.ClasspathResourceLoader)6 Writer (java.io.Writer)5 File (java.io.File)4 ResourceNotFoundException (org.apache.velocity.exception.ResourceNotFoundException)4 CopyrightManager (com.intellij.copyright.CopyrightManager)3 InputStreamReader (java.io.InputStreamReader)3 Reader (java.io.Reader)3 MojoExecutionException (org.apache.maven.plugin.MojoExecutionException)3 ParseErrorException (org.apache.velocity.exception.ParseErrorException)3 FileNotFoundException (java.io.FileNotFoundException)2 InputStream (java.io.InputStream)2 OutputStreamWriter (java.io.OutputStreamWriter)2 Map (java.util.Map)2 ExtendedProperties (org.apache.commons.collections.ExtendedProperties)2