Java Code Examples for org.apache.velocity.runtime.RuntimeInstance#init()

The following examples show how to use org.apache.velocity.runtime.RuntimeInstance#init() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: ConversionHandlerTestCase.java    From velocity-engine with Apache License 2.0 6 votes vote down vote up
public void testCustomConversionHandlerInstance()
{
    RuntimeInstance ve = new RuntimeInstance();
    ve.setProperty( Velocity.VM_PERM_INLINE_LOCAL, Boolean.TRUE);
    ve.setProperty(Velocity.RUNTIME_LOG_INSTANCE, log);
    ve.setProperty(RuntimeConstants.RESOURCE_LOADERS, "file");
    ve.setProperty(RuntimeConstants.FILE_RESOURCE_LOADER_PATH, TEST_COMPARE_DIR + "/conversion");
    ve.setProperty(RuntimeConstants.CONVERSION_HANDLER_INSTANCE, new MyCustomConverter());
    ve.init();
    Uberspect uberspect = ve.getUberspect();
    assertTrue(uberspect instanceof UberspectImpl);
    UberspectImpl ui = (UberspectImpl)uberspect;
    TypeConversionHandler ch = ui.getConversionHandler();
    assertTrue(ch != null);
    assertTrue(ch instanceof MyCustomConverter);
    VelocityContext context = new VelocityContext();
    context.put("obj", new Obj());
    Writer writer = new StringWriter();
    ve.evaluate(context, writer, "test", "$obj.objectString(1.0)");
    assertEquals("String ok: foo", writer.toString());
}
 
Example 2
Source File: VelocityScriptEngine.java    From velocity-engine with Apache License 2.0 5 votes vote down vote up
private void initVelocityEngine(ScriptContext ctx)
{
    if (ctx == null)
    {
        ctx = getContext();
    }
    if (velocityEngine == null)
    {
        synchronized (this)
        {
            if (velocityEngine != null) return;

            Properties props = getVelocityProperties(ctx);
            RuntimeInstance tmpEngine = new RuntimeInstance();
            try
            {
                if (props != null)
                {
                    tmpEngine.init(props);
                }
                else
                {
                    tmpEngine.init();
                }
            }
            catch (RuntimeException rexp)
            {
                throw rexp;
            }
            catch (Exception exp)
            {
                throw new RuntimeException(exp);
            }
            velocityEngine = tmpEngine;
        }
    }
}
 
Example 3
Source File: DataSourceResourceLoaderTestCase.java    From velocity-engine with Apache License 2.0 5 votes vote down vote up
public void setUp()
        throws Exception
{

    assureResultsDirectoryExists(RESULTS_DIR);

    DataSource ds1 = new TestDataSource(TEST_JDBC_DRIVER_CLASS, TEST_JDBC_URI, TEST_JDBC_LOGIN, TEST_JDBC_PASSWORD);
    DataSourceResourceLoader rl1 = new DataSourceResourceLoader();
    rl1.setDataSource(ds1);

    DataSource ds2 = new TestDataSource(TEST_JDBC_DRIVER_CLASS, TEST_JDBC_URI, TEST_JDBC_LOGIN, TEST_JDBC_PASSWORD);
    DataSourceResourceLoader rl2 = new DataSourceResourceLoader();
    rl2.setDataSource(ds2);

    ExtProperties props = new ExtProperties();
    props.addProperty( "resource.loader", "ds" );
    props.setProperty( "ds.resource.loader.instance", rl1);
    props.setProperty( "ds.resource.loader.resource.table",           "velocity_template_varchar");
    props.setProperty( "ds.resource.loader.resource.keycolumn",       "vt_id");
    props.setProperty( "ds.resource.loader.resource.templatecolumn",  "vt_def");
    props.setProperty( "ds.resource.loader.resource.timestampcolumn", "vt_timestamp");
    props.setProperty(Velocity.RUNTIME_LOG_INSTANCE, new TestLogger(false, false));

    varcharTemplatesEngine = new RuntimeInstance();
    varcharTemplatesEngine.setConfiguration(props);
    varcharTemplatesEngine.init();

    ExtProperties props2 = (ExtProperties)props.clone();
    props2.setProperty( "ds.resource.loader.instance", rl2);
    props2.setProperty( "ds.resource.loader.resource.table",           "velocity_template_clob");
    clobTemplatesEngine = new RuntimeInstance();
    clobTemplatesEngine.setConfiguration(props2);
    clobTemplatesEngine.init();
}
 
Example 4
Source File: UberspectorTestCase.java    From velocity-engine with Apache License 2.0 4 votes vote down vote up
public void setUp()
        throws Exception
{
    ri = new RuntimeInstance();
    ri.init();
}