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.

git-svn-id: https://svn.apache.org/repos/asf/lucene/solr/trunk@709649 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
Erik Hatcher 2008-11-01 04:49:56 +00:00
parent 028e364415
commit d9407baa70
6 changed files with 44 additions and 19 deletions

View File

@ -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">

View File

@ -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>

View File

@ -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) {

View File

@ -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;
}
}

View File

@ -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;
}

View File

@ -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))