performanceuser
performanceuser

Reputation: 2893

Velocity engine fails to load template from a remote shared folder

I have following code

File temlateFile = new File( "D:/config/emails/MailBody.vm" );
    temlateFile.exists();
    VelocityEngine velocityEngine = new VelocityEngine();
    velocityEngine.setProperty(RuntimeConstants.RESOURCE_LOADER, "file");
    velocityEngine.setProperty("file.resource.loader.class", FileResourceLoader.class.getName());
    velocityEngine.setProperty("file.resource.loader.path", temlateFile.getParentFile().getAbsolutePath());
    velocityEngine.init();
    template = velocityEngine.getTemplate( temlateFile.getName() );

This works because it is loading a file from local file system.

Once I change the first like to:

File temlateFile = new File( "//remote/config/emails/MailBody.vm" );

It doesn't work.

org.apache.velocity.exception.ResourceNotFoundException: Unable to find resource 'MailBody.vm'
    at org.apache.velocity.runtime.resource.ResourceManagerImpl.loadResource(ResourceManagerImpl.java:474)
    at org.apache.velocity.runtime.resource.ResourceManagerImpl.getResource(ResourceManagerImpl.java:352)
    at org.apache.velocity.runtime.RuntimeInstance.getTemplate(RuntimeInstance.java:1533)
    at org.apache.velocity.runtime.RuntimeInstance.getTemplate(RuntimeInstance.java:1514)
    at org.apache.velocity.app.VelocityEngine.getTemplate(VelocityEngine.java:373)
    at com.actuate.iserver.mail.VelocityContent.<init>(VelocityContent.java:33)
    at com.actuate.iserver.mail.VolumeCreationMail.<init>(VolumeCreationMail.java:40)
    at com.actuate.iserver.mail.VolumeCreationMail.main(VolumeCreationMail.java:67)

In both cases temlateFile.exists() always return true.

Any ideas?

Upvotes: 3

Views: 1441

Answers (2)

A Kunin
A Kunin

Reputation: 44452

The culprit - two backslashes in the beginning of a file path. They are not getting processed correctly by VelocityEngine. Workaround is to replace leading double backslash with double slash

velocityEngine.setProperty(RuntimeConstants.FILE_RESOURCE_LOADER_PATH, temlateFile.getParent().replace("\\\\", "//"));

or just replace all of them

velocityEngine.setProperty(RuntimeConstants.FILE_RESOURCE_LOADER_PATH, temlateFile.getParent().replace("\\", "/"));

Issue still exists in version 1.7

new File( "//remote/config/emails/MailBody.vm" ); - may not work because file.getParent() may convert path to have backslashes.

Upvotes: 0

performanceuser
performanceuser

Reputation: 2893

Found the problem. That looks like a velocity bug. So

File temlateFile = new File( "//remote/config/emails/MailBody.vm" ); works

File temlateFile = new File( "\\remote\config\emails\MailBody.vm" ); Doesn't work

Upvotes: 2

Related Questions