Apache Lucene open-source search software
Wire in a SolrResourceLoader bridge for Velocity templates, allowing a JAR of templates to live in conf/lib. Refactored the footer.vm template to use this mechanism as an example.
Erik Hatcherd9407baa7008
diff --git a/contrib/velocity/build.xml b/contrib/velocity/build.xmlindex 57c7301ab4b..5f453de335f 100644--- a/contrib/velocity/build.xml+++ b/contrib/velocity/build.xml@@ -61,7 +61,9 @@<target name="build" depends="compile"><solr-jar destfile="src/main/solr/lib/${fullnamever}.jar" basedir="target/classes"- manifest="${common.dir}/${dest}/META-INF/MANIFEST.MF" />+ manifest="${common.dir}/${dest}/META-INF/MANIFEST.MF">+ <fileset dir="src/main/java" excludes="**/*.java"/>+ </solr-jar></target><target name="compileTests" depends="compile">diff --git a/contrib/velocity/src/main/solr/conf/velocity/footer.vm b/contrib/velocity/src/main/java/footer.vmsimilarity index 51%rename from contrib/velocity/src/main/solr/conf/velocity/footer.vmrename to contrib/velocity/src/main/java/footer.vmindex 639a2667834..d4b8b444908 100644--- a/contrib/velocity/src/main/solr/conf/velocity/footer.vm+++ b/contrib/velocity/src/main/java/footer.vm@@ -6,5 +6,4 @@</pre>#end-Generated using <a href="http://wiki.apache.org/solr/VelocityResponseWriter">VelocityResponseWriter</a>-+Generated by <a href="http://wiki.apache.org/solr/VelocityResponseWriter">VelocityResponseWriter</a>\ No newline at end of filediff --git a/contrib/velocity/src/main/java/org/apache/solr/request/SolrParamResourceLoader.java b/contrib/velocity/src/main/java/org/apache/solr/request/SolrParamResourceLoader.javaindex ecac054f5dc..8d3d491f24d 100644--- a/contrib/velocity/src/main/java/org/apache/solr/request/SolrParamResourceLoader.java+++ b/contrib/velocity/src/main/java/org/apache/solr/request/SolrParamResourceLoader.java@@ -30,6 +30,7 @@ import java.util.Iterator;public class SolrParamResourceLoader extends ResourceLoader {private HashMap<String,String> templates = new HashMap();public SolrParamResourceLoader(SolrQueryRequest request) {+ super();// TODO: Consider using content streams, but need a template name associated with each stream// for now, a custom param convention of template.<name>=<template body> is a nice example@@ -44,7 +45,6 @@ public class SolrParamResourceLoader extends ResourceLoader {templates.put(name.substring(9) + ".vm",params.get(name));}}-}public void init(ExtendedProperties extendedProperties) {diff --git a/contrib/velocity/src/main/java/org/apache/solr/request/SolrVelocityResourceLoader.java b/contrib/velocity/src/main/java/org/apache/solr/request/SolrVelocityResourceLoader.javanew file mode 100644index 00000000000..b485213adfc--- /dev/null+++ b/contrib/velocity/src/main/java/org/apache/solr/request/SolrVelocityResourceLoader.java@@ -0,0 +1,34 @@+package org.apache.solr.request;++import org.apache.velocity.runtime.resource.loader.ResourceLoader;+import org.apache.velocity.runtime.resource.Resource;+import org.apache.velocity.exception.ResourceNotFoundException;+import org.apache.commons.collections.ExtendedProperties;+import org.apache.solr.core.SolrResourceLoader;++import java.io.InputStream;++// TODO: the name of this class seems ridiculous+public class SolrVelocityResourceLoader extends ResourceLoader {+ private SolrResourceLoader loader;++ public SolrVelocityResourceLoader(SolrResourceLoader loader) {+ super();+ this.loader = loader;+ }++ public void init(ExtendedProperties extendedProperties) {+ }++ public InputStream getResourceStream(String template_name) throws ResourceNotFoundException {+ return loader.openResource(template_name);+ }++ public boolean isSourceModified(Resource resource) {+ return false;+ }++ public long getLastModified(Resource resource) {+ return 0;+ }+}diff --git a/contrib/velocity/src/main/java/org/apache/solr/request/VelocityResponseWriter.java b/contrib/velocity/src/main/java/org/apache/solr/request/VelocityResponseWriter.javaindex 1b50c744869..ae4d309f022 100644--- a/contrib/velocity/src/main/java/org/apache/solr/request/VelocityResponseWriter.java+++ b/contrib/velocity/src/main/java/org/apache/solr/request/VelocityResponseWriter.java@@ -49,8 +49,10 @@ public class VelocityResponseWriter implements QueryResponseWriter {baseDir = new File(template_root);}engine.setProperty(VelocityEngine.FILE_RESOURCE_LOADER_PATH, baseDir.getAbsolutePath());- engine.setProperty("params.resource.loader.instance",new SolrParamResourceLoader(request));- engine.setProperty(VelocityEngine.RESOURCE_LOADER, "params,file");+ engine.setProperty("params.resource.loader.instance", new SolrParamResourceLoader(request));+ engine.setProperty("solr.resource.loader.instance",+ new SolrVelocityResourceLoader(request.getCore().getSolrConfig().getResourceLoader()));+ engine.setProperty(VelocityEngine.RESOURCE_LOADER, "params,file,solr");return engine;}diff --git a/contrib/velocity/src/main/solr/conf/velocity/browse.vm b/contrib/velocity/src/main/solr/conf/velocity/browse.vmindex 7cffe9600d0..9c1b4e11489 100644--- a/contrib/velocity/src/main/solr/conf/velocity/browse.vm+++ b/contrib/velocity/src/main/solr/conf/velocity/browse.vm@@ -44,19 +44,7 @@<div class="pagination"><span><span class="results-found">$page.results_found</span> results found in ${response.responseHeader.QTime} ms</span></div>-#if (${nlResponse.suggestions.size()} > 0)-<div class="spelling">- <span>Did you mean?</span>- #foreach ($suggestion in ${nlResponse.suggestions})- <a href="${request.requestURL}?q=${suggestion}">${suggestion}</a>#if ($velocityCount < $nlResponse.suggestions.size()),#end- #end-</div>-#end-#if (${nlResponse.autoSpell} == true)-<div class="autoSpell">There were no results for your original query, so we automatically searched a few spelling- variants: ${nlResponse.autoSpellQuery}-</div>-#end+<div class="results">#foreach($id in $doclist.iterator())#set($doc = $searcher.doc($id,$response.returnFields))