You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@lucene.apache.org by eh...@apache.org on 2015/09/02 02:16:27 UTC
svn commit: r1700692 - in /lucene/dev/branches/branch_5x/solr: ./
contrib/velocity/src/java/org/apache/solr/response/
contrib/velocity/src/test-files/velocity/solr/collection1/conf/
contrib/velocity/src/test-files/velocity/solr/collection1/conf/velocit...
Author: ehatcher
Date: Wed Sep 2 00:16:27 2015
New Revision: 1700692
URL: http://svn.apache.org/r1700692
Log:
SOLR-7915: Provide pluggable context tool support for VelocityResponseWriter (merged from trunk r1700691)
Added:
lucene/dev/branches/branch_5x/solr/contrib/velocity/src/test-files/velocity/solr/collection1/conf/velocity/custom_tool.vm
- copied unchanged from r1700691, lucene/dev/trunk/solr/contrib/velocity/src/test-files/velocity/solr/collection1/conf/velocity/custom_tool.vm
lucene/dev/branches/branch_5x/solr/contrib/velocity/src/test/org/apache/solr/velocity/MockTool.java
- copied unchanged from r1700691, lucene/dev/trunk/solr/contrib/velocity/src/test/org/apache/solr/velocity/MockTool.java
Modified:
lucene/dev/branches/branch_5x/solr/CHANGES.txt (contents, props changed)
lucene/dev/branches/branch_5x/solr/contrib/velocity/src/java/org/apache/solr/response/VelocityResponseWriter.java
lucene/dev/branches/branch_5x/solr/contrib/velocity/src/test-files/velocity/solr/collection1/conf/solrconfig.xml
lucene/dev/branches/branch_5x/solr/contrib/velocity/src/test/org/apache/solr/velocity/VelocityResponseWriterTest.java
Modified: lucene/dev/branches/branch_5x/solr/CHANGES.txt
URL: http://svn.apache.org/viewvc/lucene/dev/branches/branch_5x/solr/CHANGES.txt?rev=1700692&r1=1700691&r2=1700692&view=diff
==============================================================================
--- lucene/dev/branches/branch_5x/solr/CHANGES.txt (original)
+++ lucene/dev/branches/branch_5x/solr/CHANGES.txt Wed Sep 2 00:16:27 2015
@@ -56,6 +56,8 @@ New Features
* SOLR-4316: Add a collections dropdown to angular admin UI (Upayavira, Shalin Shekhar Mangar)
+* SOLR-7915: Provide pluggable context tool support for VelocityResponseWriter (Erik Hatcher)
+
Bug Fixes
----------------------
Modified: lucene/dev/branches/branch_5x/solr/contrib/velocity/src/java/org/apache/solr/response/VelocityResponseWriter.java
URL: http://svn.apache.org/viewvc/lucene/dev/branches/branch_5x/solr/contrib/velocity/src/java/org/apache/solr/response/VelocityResponseWriter.java?rev=1700692&r1=1700691&r2=1700692&view=diff
==============================================================================
--- lucene/dev/branches/branch_5x/solr/contrib/velocity/src/java/org/apache/solr/response/VelocityResponseWriter.java (original)
+++ lucene/dev/branches/branch_5x/solr/contrib/velocity/src/java/org/apache/solr/response/VelocityResponseWriter.java Wed Sep 2 00:16:27 2015
@@ -25,7 +25,9 @@ import java.io.StringWriter;
import java.io.Writer;
import java.nio.charset.StandardCharsets;
import java.util.ArrayList;
+import java.util.HashMap;
import java.util.Locale;
+import java.util.Map;
import java.util.Properties;
import java.util.ResourceBundle;
@@ -83,6 +85,7 @@ public class VelocityResponseWriter impl
private static final Logger log = LoggerFactory.getLogger(VelocityResponseWriter.class);
private static final SolrVelocityLogger velocityLogger = new SolrVelocityLogger(log);
private Properties velocityInitProps = new Properties();
+ private Map<String,String> customTools = new HashMap<String,String>();
@Override
public void init(NamedList args) {
@@ -111,6 +114,14 @@ public class VelocityResponseWriter impl
solrResourceLoaderEnabled = (null == srle ? true : srle);
initPropertiesFileName = (String) args.get(PROPERTIES_FILE);
+
+ NamedList tools = (NamedList)args.get("tools");
+ if (tools != null) {
+ for(Object t : tools) {
+ Map.Entry tool = (Map.Entry)t;
+ customTools.put(tool.getKey().toString(), tool.getValue().toString());
+ }
+ }
}
@Override
@@ -182,9 +193,8 @@ public class VelocityResponseWriter impl
private VelocityContext createContext(SolrQueryRequest request, SolrQueryResponse response) {
VelocityContext context = new VelocityContext();
- context.put("request", request);
-
// Register useful Velocity "tools"
+ context.put("log", log); // TODO: add test; TODO: should this be overridable with a custom "log" named tool?
context.put("esc", new EscapeTool());
context.put("date", new ComparisonDateTool());
context.put("list", new ListTool());
@@ -196,6 +206,25 @@ public class VelocityResponseWriter impl
request.getCore().getSolrConfig().getResourceLoader().getClassLoader(),
request.getParams().get(LOCALE)));
+/*
+ // Custom tools, specified in config as:
+ <queryResponseWriter name="velocityWithCustomTools" class="solr.VelocityResponseWriter">
+ <lst name="tools">
+ <str name="mytool">com.example.solr.velocity.MyTool</str>
+ </lst>
+ </queryResponseWriter>
+
+
+*/
+ // Custom tools can override any of the built-in tools provided above, by registering one with the same name
+ for(String name : customTools.keySet()) {
+ context.put(name, SolrCore.createInstance(customTools.get(name), Object.class, "VrW custom tool", request.getCore(), request.getCore().getResourceLoader()));
+ }
+
+ // custom tools _cannot_ override context objects added below, like $request and $response
+ // TODO: at least log a warning when one of the *fixed* tools classes in name with a custom one, currently silently ignored
+
+
// Turn the SolrQueryResponse into a SolrResponse.
// QueryResponse has lots of conveniences suitable for a view
// Problem is, which SolrResponse class to use?
@@ -219,6 +248,8 @@ public class VelocityResponseWriter impl
rsp = new SolrResponseBase();
rsp.setResponse(parsedResponse);
}
+
+ context.put("request", request);
context.put("response", rsp);
return context;
Modified: lucene/dev/branches/branch_5x/solr/contrib/velocity/src/test-files/velocity/solr/collection1/conf/solrconfig.xml
URL: http://svn.apache.org/viewvc/lucene/dev/branches/branch_5x/solr/contrib/velocity/src/test-files/velocity/solr/collection1/conf/solrconfig.xml?rev=1700692&r1=1700691&r2=1700692&view=diff
==============================================================================
--- lucene/dev/branches/branch_5x/solr/contrib/velocity/src/test-files/velocity/solr/collection1/conf/solrconfig.xml (original)
+++ lucene/dev/branches/branch_5x/solr/contrib/velocity/src/test-files/velocity/solr/collection1/conf/solrconfig.xml Wed Sep 2 00:16:27 2015
@@ -35,4 +35,20 @@
<queryResponseWriter name="velocityWithInitProps" class="solr.VelocityResponseWriter">
<str name="init.properties.file">velocity-init.properties</str>
</queryResponseWriter>
+
+ <queryResponseWriter name="velocityWithCustomTools" class="solr.VelocityResponseWriter">
+ <!-- Enable params resource loader to make tests leaner, no external template needed -->
+ <bool name="params.resource.loader.enabled">true</bool>
+
+ <lst name="tools">
+ <!-- how someone would typically add a custom tool, with a custom, non-clashing name -->
+ <str name="mytool">org.apache.solr.velocity.MockTool</str>
+
+ <!-- override the $log context object -->
+ <str name="log">org.apache.solr.velocity.MockTool</str>
+
+ <!-- Try to override response, but ignored -->
+ <str name="response">org.apache.solr.velocity.MockTool</str>
+ </lst>
+ </queryResponseWriter>
</config>
Modified: lucene/dev/branches/branch_5x/solr/contrib/velocity/src/test/org/apache/solr/velocity/VelocityResponseWriterTest.java
URL: http://svn.apache.org/viewvc/lucene/dev/branches/branch_5x/solr/contrib/velocity/src/test/org/apache/solr/velocity/VelocityResponseWriterTest.java?rev=1700692&r1=1700691&r2=1700692&view=diff
==============================================================================
--- lucene/dev/branches/branch_5x/solr/contrib/velocity/src/test/org/apache/solr/velocity/VelocityResponseWriterTest.java (original)
+++ lucene/dev/branches/branch_5x/solr/contrib/velocity/src/test/org/apache/solr/velocity/VelocityResponseWriterTest.java Wed Sep 2 00:16:27 2015
@@ -120,6 +120,29 @@ public class VelocityResponseWriterTest
}
@Test
+ public void testCustomTools() throws Exception {
+ assertEquals("", h.query(req("q","*:*", "wt","velocity",VelocityResponseWriter.TEMPLATE,"custom_tool")));
+ assertEquals("** LATERALUS **", h.query(req("q","*:*", "wt","velocityWithCustomTools",VelocityResponseWriter.TEMPLATE,"t",
+ SolrParamResourceLoader.TEMPLATE_PARAM_PREFIX+"t", "$mytool.star(\"LATERALUS\")")));
+
+ // Does $log get overridden?
+ assertEquals("** log overridden **", h.query(req("q","*:*", "wt","velocityWithCustomTools",VelocityResponseWriter.TEMPLATE,"t",
+ SolrParamResourceLoader.TEMPLATE_PARAM_PREFIX+"t", "$log.star(\"log overridden\")")));
+
+ // Does $response get overridden? actual blank response because of the bang on $! reference that silences bogus $-references
+ assertEquals("", h.query(req("q","*:*", "wt","velocityWithCustomTools",VelocityResponseWriter.TEMPLATE,"t",
+ SolrParamResourceLoader.TEMPLATE_PARAM_PREFIX+"t", "$!response.star(\"response overridden??\")")));
+
+ // Custom tools can also have a SolrCore-arg constructor because they are instantiated with SolrCore.createInstance
+ // TODO: do we really need to support this? no great loss, as a custom tool could take a SolrCore object as a parameter to
+ // TODO: any method, so one could do $mytool.my_method($request.core)
+ // I'm currently inclined to make this feature undocumented/unsupported, as we may want to instantiate classes
+ // in a different manner that only supports no-arg constructors, commented (passing) test case out
+// assertEquals("collection1", h.query(req("q","*:*", "wt","velocityWithCustomTools",VelocityResponseWriter.TEMPLATE,"t",
+// SolrParamResourceLoader.TEMPLATE_PARAM_PREFIX+"t", "$mytool.core.name")));
+ }
+
+ @Test
public void testLocaleFeature() throws Exception {
assertEquals("Color", h.query(req("q", "*:*", "wt", "velocity", VelocityResponseWriter.TEMPLATE, "locale",
VelocityResponseWriter.LOCALE,"en_US")));