[英]Velocity in OSGi: how to load templates from classpath
我正在开发一个带速度模板引擎的OSGi应用程序。 它适用于通过文件加载器加载我的模板,但现在我必须在我的jar中实现这个模板并将其作为资源加载。
我怎样才能使它发挥作用?
我的代码:
ve = new VelocityEngine();
ve.setProperty(RuntimeConstants.RESOURCE_LOADER, "classpath");
ve.setProperty("classpath.resource.loader.class",
ClasspathResourceLoader.class.getName());
ve.setProperty("classpath.resource.loader.path", "/velocitytemplates");
ve.init();
ve.getTemplate("foo.vm");
这将抛出一个例外
无法找到资源'index.vm'
引起:
org.apache.velocity.exception.ResourceNotFoundException:无法找到资源'index.vm'
可悲的是Velocity并不是OSGi友好的。 因此,您无法使用内置的ClasspathResourceLoader,也很难添加自定义开发的ResourceLoader。
我建议您以任何常规方式将模板作为Reader使用,并选择以下选项之一:
如果您不必经常合并模板,则可以使用第一个选项,因此性能不是关键要求。
下面是第二个选项的示例,其中可以通过调用其上的合并函数来重用创建的模板对象(期望您已经有一个读取器到您的vm文件或资源):
RuntimeInstance runtimeInstance = new RuntimeInstance();
runtimeInstance.init();
SimpleNode simpleNode = runtimeInstance.parse(reader, "nameOfYourTemplateResource");
Template template = new Template();
simpleNode.init(new InternalContextAdapterImpl(new VelocityContext()), runtimeInstance);
template.setData(simpleNode);
template.merge(...);
...
要获得OSGi中vm文件的读者,您应该选择一个肯定与vm资源在同一个包中的类,并调用SameBundleClass.class.getResourceAsStream ...您可以使用InputStreamReader将您的流转换为writer。
请注意,该示例错过了一些try-catch-finally块。
要验证两件事
确保通过MANIFEST.MF设置OSGi包的类路径以包含一个点:
Bundle-ClassPath: .
点表示在类加载层次结构中包含bundle的根,您的文件夹“velocitytemplates”可能驻留在该层次结构中。
并且你需要将Velocity jar文件放在模板文件所在的同一个包中,否则你会得到类加载问题,因为Velocity将驻留在另一个包中,因此根本不会看到“velocitytemplates”类路径。
ClasspathResourceLoader不支持设置“路径”,因为它根据定义使用Classpath,因此要么将“velocitytemplates”添加到OSGi包中的Classpath(MANIFESt.MF),要么使用完整路径引用速度模板,即“velocitytemplates / index” .vm”
2天后,我和一位同事发现了Velocity Engine默认的解决方案:file.resource.loader.class = org.apache.velocity.runtime.resource.loader.FileResourceLoader
像这样创建自己的类Resource loader
public static final class PdfResourceLoader extends ResourceLoader
@Override
public void init(ExtendedProperties configuration)
{
}
@Override
public InputStream getResourceStream(String source) throws ResourceNotFoundException
{
return getClass().getResourceAsStream(source);
}
@Override
public boolean isSourceModified(Resource resource)
{
return false;
}
@Override
public long getLastModified(Resource resource)
{
return 0;
}
}
设置新的上下文类加载器
Thread.currentThread().setContextClassLoader(PdfResourceLoader.class.getClassLoader());
VelocityEngine ve = new VelocityEngine();
在速度引擎内更改了默认属性
ve.setProperty("resource.loader", "pdf");
ve.setProperty("pdf.resource.loader.class",
PdfResourceLoader.class.getName());
ve.init();
示例名称路径模板
String pathTemplate = "/templates/yourTemplateName.html";
Template t = ve.getTemplate(pathTemplate, "UTF-8");
就是这样
我遇到了类加载器模板的类似问题,我想指定一个不同的根。 我通过继承ClasspathResourceLoader来解决它。
package my.package;
import java.io.InputStream;
import org.apache.commons.collections.ExtendedProperties;
import org.apache.velocity.exception.ResourceNotFoundException;
import org.apache.velocity.runtime.resource.loader.ClasspathResourceLoader;
public class PrefixedClasspathResourceLoader
extends ClasspathResourceLoader
{
/** Prefix to be added to any names */
private String prefix = "";
@Override
public void init(ExtendedProperties configuration) {
prefix = configuration.getString("prefix","");
}
@Override
public InputStream getResourceStream(String name)
throws ResourceNotFoundException
{
return super.getResourceStream(prefix+name);
}
}
设置以下属性
resource.loader=myloader
myloader.resource.loader.class=my.package.PrefixedClasspathResourceLoader
myloader.resource.loader.prefix=/velocitytemplates/
这样,如果你有一个名为“index.vm”的模板,velocity将使用类加载器来查找名为“/velocitytemplates/index.vm”的资源。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.