Apache Solr open-source search software
SOLR-260 -- adding a standard PluginLoader framework. NOTE, this commit includes parts of files from (the soon to be commited) highlight plugins SOLR-225
Ryan McKinley95b41b373f75
diff --git a/CHANGES.txt b/CHANGES.txtindex d47f32dd17b..d0e9181b57c 100644--- a/CHANGES.txt+++ b/CHANGES.txt@@ -83,6 +83,10 @@ New Featuresparsed and before they are committed to the index. This is a good placefor custom document manipulation or document based authorization. (ryan)+13. SOLR-260: Converting to a standard PluginLoader framework. This reworks+ RequestHandlers, FieldTypes, and QueryResponseWriters to share the same+ base code for loading and initalizing plugins. (ryan)+Changes in runtime behaviorOptimizationsdiff --git a/src/java/org/apache/solr/core/RequestHandlers.java b/src/java/org/apache/solr/core/RequestHandlers.javaindex e298439666c..2eb4df0ddc5 100644--- a/src/java/org/apache/solr/core/RequestHandlers.java+++ b/src/java/org/apache/solr/core/RequestHandlers.java@@ -20,7 +20,6 @@ package org.apache.solr.core;import java.net.URL;import java.util.Collections;import java.util.HashMap;-import java.util.LinkedHashMap;import java.util.Map;import java.util.logging.Logger;@@ -34,8 +33,9 @@ import org.apache.solr.handler.StandardRequestHandler;import org.apache.solr.request.SolrQueryRequest;import org.apache.solr.request.SolrQueryResponse;import org.apache.solr.request.SolrRequestHandler;-import org.w3c.dom.Node;+import org.apache.solr.util.plugin.AbstractPluginLoader;import org.w3c.dom.NodeList;+import org.w3c.dom.Node;/*** @author yonik@@ -46,7 +46,7 @@ final class RequestHandlers {public static final String DEFAULT_HANDLER_NAME="standard";// Use a synchronized map - since the handlers can be changed at runtime,- // the map implementaion should be thread safe+ // the map implementation should be thread safeprivate final Map<String, SolrRequestHandler> handlers = Collections.synchronizedMap(new HashMap<String,SolrRequestHandler>() );@@ -75,8 +75,8 @@ final class RequestHandlers {}/**- * Handlers must be initalized before calling this function. As soon as this is- * called, the handler can immediatly accept requests.+ * Handlers must be initialized before calling this function. As soon as this is+ * called, the handler can immediately accept requests.** This call is thread safe.*@@ -97,7 +97,7 @@ final class RequestHandlers {}/**- * Returns an unmodifieable Map containing the registered handlers+ * Returns an unmodifiable Map containing the registered handlers*/public Map<String,SolrRequestHandler> getRequestHandlers() {return Collections.unmodifiableMap( handlers );@@ -110,96 +110,73 @@ final class RequestHandlers {* This function should <b>only</b> be called from the SolrCore constructor. It is* not intended as a public API.*- * While the normal runtime registration contract is that handlers MUST be initalizad+ * While the normal runtime registration contract is that handlers MUST be initialized* before they are registered, this function does not do that exactly.*- * This funciton registers all handlers first and then calls init() for each one.+ * This function registers all handlers first and then calls init() for each one.** This is OK because this function is only called at startup and there is no chance that- * a handler could be asked to handle a request before it is initalized.+ * a handler could be asked to handle a request before it is initialized.** The advantage to this approach is that handlers can know what path they are registered- * to and what other handlers are avaliable at startup.+ * to and what other handlers are available at startup.*- * Handlers will be registered and initalized in the order they appear in solrconfig.xml+ * Handlers will be registered and initialized in the order they appear in solrconfig.xml*/- @SuppressWarnings("unchecked")void initHandlersFromConfig( Config config ){- NodeList nodes = (NodeList)config.evaluate("requestHandler", XPathConstants.NODESET);-- if (nodes !=null ) {- // make sure it only once/handler and that that handlers get initalized in the- // order they were defined- Map<String,NamedList<Object>> names = new LinkedHashMap<String,NamedList<Object>>();- for (int i=0; i<nodes.getLength(); i++) {- Node node = nodes.item(i);-- // In a production environment, we can tolerate an error in some request handlers,- // still load the others, and have a working system.- try {- String name = DOMUtil.getAttr(node,"name","requestHandler config");- String className = DOMUtil.getAttr(node,"class","requestHandler config");- String startup = DOMUtil.getAttr(node,"startup", null );- NamedList<Object> args = DOMUtil.childNodesToNamedList(node);-- // Perhaps lazy load the request handler with a wrapper- SolrRequestHandler handler = null;+ final RequestHandlers handlers = this;+ AbstractPluginLoader<SolrRequestHandler> loader =+ new AbstractPluginLoader<SolrRequestHandler>( "[solrconfig.xml] requestHandler", true )+ {+ @Override+ protected SolrRequestHandler create( String name, String className, Map<String,String> params, Node node ) throws Exception+ {+ String startup = params.get( "startup" );+ if( startup != null ) {if( "lazy".equals( startup ) ) {- log.info("adding lazy requestHandler: " + name + "=" + className);- handler = new LazyRequestHandlerWrapper( className, args );+ log.info("adding lazy requestHandler: " + className );+ NamedList args = DOMUtil.childNodesToNamedList(node);+ return new LazyRequestHandlerWrapper( className, args );}else {- Class<? extends SolrRequestHandler> clazz = Config.findClass( className, new String[]{} );- log.info("adding requestHandler: " + name + "=" + className);- handler = clazz.newInstance();- }-- SolrRequestHandler old = register( name, handler );- if( old != null ) {- String msg = "multiple handlers registered on the same path! ignoring: "+old;- Throwable t = new SolrException( SolrException.ErrorCode.SERVER_ERROR, msg );- SolrConfig.severeErrors.add( t );- SolrException.logOnce(log,null,t);+ throw new Exception( "Unknown startup value: '"+startup+"' for: "+className );}- names.put( name, args );- }- catch (Exception e) {- SolrConfig.severeErrors.add( e );- SolrException.logOnce(log,null,e);}+ return super.create( name, className, params, node );}-- // Call init() on each handler after they have all been registered- for( Map.Entry<String, NamedList<Object>> reg : names.entrySet() ) {- try {- handlers.get( reg.getKey() ).init( reg.getValue() );- }- catch( Exception e ) {- SolrConfig.severeErrors.add( e );- SolrException.logOnce(log,null,e);- }++ @Override+ protected SolrRequestHandler register(String name, SolrRequestHandler plugin) throws Exception {+ return handlers.register( name, plugin );}- }++ @Override+ protected void init(SolrRequestHandler plugin, Map<String, String> params, Node node ) throws Exception {+ plugin.init( DOMUtil.childNodesToNamedList(node) );+ }+ };- //- // Get the default handler and add it in the map under null and empty- // to act as the default.- //- SolrRequestHandler handler = get(RequestHandlers.DEFAULT_HANDLER_NAME);- if (handler == null) {- handler = new StandardRequestHandler();- register(RequestHandlers.DEFAULT_HANDLER_NAME, handler);+ NodeList nodes = (NodeList)config.evaluate("requestHandler", XPathConstants.NODESET);++ // Load the handlers and get the default one+ SolrRequestHandler defaultHandler = loader.load( nodes );+ if( defaultHandler == null ) {+ defaultHandler = get(RequestHandlers.DEFAULT_HANDLER_NAME);+ if( defaultHandler == null ) {+ defaultHandler = new StandardRequestHandler();+ register(RequestHandlers.DEFAULT_HANDLER_NAME, defaultHandler);+ }}- register(null, handler);- register("", handler);+ register(null, defaultHandler);+ register("", defaultHandler);}/*** The <code>LazyRequestHandlerWrapper</core> wraps any {@link SolrRequestHandler}.* Rather then instanciate and initalize the handler on startup, this wrapper waits- * untill it is actually called. This should only be used for handlers that are+ * until it is actually called. This should only be used for handlers that are* unlikely to be used in the normal lifecycle.** You can enable lazy loading in solrconfig.xml using:@@ -227,7 +204,7 @@ final class RequestHandlers {{_className = className;_args = args;- _handler = null; // don't initalize+ _handler = null; // don't initialize}/**@@ -238,7 +215,7 @@ final class RequestHandlers {}/**- * Wait for the first request before initalizing the wrapped handler+ * Wait for the first request before initializing the wrapped handler*/public void handleRequest(SolrQueryRequest req, SolrQueryResponse rsp) {getWrappedHandler().handleRequest( req, rsp );@@ -319,7 +296,7 @@ final class RequestHandlers {return _handler.getStatistics();}NamedList<String> lst = new SimpleOrderedMap<String>();- lst.add("note", "not initaized yet" );+ lst.add("note", "not initialized yet" );return lst;}}@@ -329,3 +306,5 @@ final class RequestHandlers {++diff --git a/src/java/org/apache/solr/core/SolrCore.java b/src/java/org/apache/solr/core/SolrCore.javaindex 0b1404a8a31..39a83e18f95 100644--- a/src/java/org/apache/solr/core/SolrCore.java+++ b/src/java/org/apache/solr/core/SolrCore.java@@ -42,6 +42,7 @@ import org.apache.solr.common.params.SolrParams.EchoParamStyle;import org.apache.solr.common.util.DOMUtil;import org.apache.solr.common.util.NamedList;import org.apache.solr.common.util.SimpleOrderedMap;+import org.apache.solr.highlight.SolrHighlighter;import org.apache.solr.request.JSONResponseWriter;import org.apache.solr.request.PythonResponseWriter;import org.apache.solr.request.QueryResponseWriter;@@ -57,7 +58,7 @@ import org.apache.solr.update.SolrIndexConfig;import org.apache.solr.update.SolrIndexWriter;import org.apache.solr.update.UpdateHandler;import org.apache.solr.util.RefCounted;-import org.w3c.dom.Element;+import org.apache.solr.util.plugin.NamedListPluginLoader;import org.w3c.dom.Node;import org.w3c.dom.NodeList;@@ -79,7 +80,8 @@ public final class SolrCore {private final UpdateHandler updateHandler;private static final long startTime = System.currentTimeMillis();private final RequestHandlers reqHandlers = new RequestHandlers();-+ private final SolrHighlighter highlighter;+public long getStartTime() { return startTime; }public static SolrIndexConfig mainIndexConfig = new SolrIndexConfig("mainIndex");@@ -211,6 +213,10 @@ public final class SolrCore {reqHandlers.initHandlersFromConfig( SolrConfig.config );+ // TODO? could select the highlighter implementation+ highlighter = new SolrHighlighter();+ highlighter.initalize( SolrConfig.config );+try {// Open the searcher *before* the handler so we don't end up opening// one in the middle.@@ -269,6 +275,13 @@ public final class SolrCore {return reqHandlers.getRequestHandlers();}+ /**+ * Get the SolrHighlighter+ */+ public SolrHighlighter getHighlighter() {+ return highlighter;+ }+/*** Registers a handler at the specified location. If one exists there, it will be replaced.* To remove a handler, register <code>null</code> at its path@@ -718,31 +731,18 @@ public final class SolrCore {private void initWriters() {String xpath = "queryResponseWriter";NodeList nodes = (NodeList) SolrConfig.config.evaluate(xpath, XPathConstants.NODESET);- int length = nodes.getLength();- for (int i=0; i<length; i++) {- Element elm = (Element) nodes.item(i);-- try {- String name = DOMUtil.getAttr(elm,"name", xpath+" config");- String className = DOMUtil.getAttr(elm,"class", xpath+" config");- log.info("adding queryResponseWriter "+name+"="+className);-- QueryResponseWriter writer = (QueryResponseWriter) Config.newInstance(className);- writer.init(DOMUtil.childNodesToNamedList(elm));- responseWriters.put(name, writer);- } catch (Exception ex) {- SolrConfig.severeErrors.add( ex );- SolrException.logOnce(log,null, ex);- // if a writer can't be created, skip it and continue- }- }-++ NamedListPluginLoader<QueryResponseWriter> loader =+ new NamedListPluginLoader<QueryResponseWriter>( "[solrconfig.xml] "+xpath, responseWriters );++ defaultResponseWriter = loader.load( nodes );+// configure the default response writer; this one should never be null- if (responseWriters.containsKey("standard")) {- defaultResponseWriter = responseWriters.get("standard");- }if (defaultResponseWriter == null) {- defaultResponseWriter = new XMLResponseWriter();+ defaultResponseWriter = responseWriters.get("standard");+ if( defaultResponseWriter == null ) {+ defaultResponseWriter = new XMLResponseWriter();+ }}// make JSON response writers available by defaultdiff --git a/src/java/org/apache/solr/core/SolrInfoMBean.java b/src/java/org/apache/solr/core/SolrInfoMBean.javaindex b6a806f0211..6a398e5c014 100644--- a/src/java/org/apache/solr/core/SolrInfoMBean.java+++ b/src/java/org/apache/solr/core/SolrInfoMBean.java@@ -20,7 +20,6 @@ package org.apache.solr.core;import java.net.URL;import org.apache.solr.common.util.NamedList;-import org.apache.solr.util.*;/*** MBean interface for getting various ui friendly strings and URLs@@ -32,7 +31,7 @@ import org.apache.solr.util.*;*/public interface SolrInfoMBean {- public enum Category { CORE, QUERYHANDLER, UPDATEHANDLER, CACHE, OTHER };+ public enum Category { CORE, QUERYHANDLER, UPDATEHANDLER, CACHE, HIGHLIGHTING, OTHER };/*** Simple common usage name, e.g. BasicQueryHandler,diff --git a/src/java/org/apache/solr/handler/DisMaxRequestHandler.java b/src/java/org/apache/solr/handler/DisMaxRequestHandler.javaindex fc93a08aee9..e9e3cd53962 100644--- a/src/java/org/apache/solr/handler/DisMaxRequestHandler.java+++ b/src/java/org/apache/solr/handler/DisMaxRequestHandler.java@@ -38,6 +38,7 @@ import org.apache.solr.common.params.DisMaxParams;import org.apache.solr.common.params.SolrParams;import org.apache.solr.common.util.NamedList;import org.apache.solr.core.SolrCore;+import org.apache.solr.highlight.SolrHighlighter;import org.apache.solr.request.SimpleFacets;import org.apache.solr.request.SolrQueryRequest;import org.apache.solr.request.SolrQueryResponse;@@ -356,20 +357,19 @@ public class DisMaxRequestHandler extends RequestHandlerBase {}} catch (Exception e) {- SolrException.logOnce(SolrCore.log,- "Exception during debug", e);+ SolrException.logOnce(SolrCore.log, "Exception during debug", e);rsp.add("exception_during_debug", SolrException.toStr(e));}/* * * Highlighting/Summarizing * * */- if(HighlightingUtils.isHighlightingEnabled(req) && parsedUserQuery != null) {+ SolrHighlighter highlighter = req.getCore().getHighlighter();+ if(highlighter.isHighlightingEnabled( params ) && parsedUserQuery != null) {String[] highFields = queryFields.keySet().toArray(new String[0]);- NamedList sumData =- HighlightingUtils.doHighlighting(- results.docList,- parsedUserQuery.rewrite(req.getSearcher().getReader()),- req,- highFields);+ NamedList sumData = highlighter.doHighlighting(+ results.docList,+ parsedUserQuery.rewrite(req.getSearcher().getReader()),+ req,+ highFields);if(sumData != null)rsp.add("highlighting", sumData);}diff --git a/src/java/org/apache/solr/handler/StandardRequestHandler.java b/src/java/org/apache/solr/handler/StandardRequestHandler.javaindex 548833ca261..8f0701ac8c5 100644--- a/src/java/org/apache/solr/handler/StandardRequestHandler.java+++ b/src/java/org/apache/solr/handler/StandardRequestHandler.java@@ -36,6 +36,7 @@ import org.apache.solr.common.util.MoreLikeThisParams;import org.apache.solr.common.util.NamedList;import org.apache.solr.common.util.StrUtils;import org.apache.solr.core.SolrCore;+import org.apache.solr.highlight.SolrHighlighter;import static org.apache.solr.common.params.SolrParams.*;@@ -161,7 +162,9 @@ public class StandardRequestHandler extends RequestHandlerBase {rsp.add("exception_during_debug", SolrException.toStr(e));}- NamedList sumData = HighlightingUtils.doHighlighting(++ SolrHighlighter highlighter = req.getCore().getHighlighter();+ NamedList sumData = highlighter.doHighlighting(results.docList, query.rewrite(req.getSearcher().getReader()), req, new String[]{defaultField});if(sumData != null)rsp.add("highlighting", sumData);diff --git a/src/java/org/apache/solr/request/QueryResponseWriter.java b/src/java/org/apache/solr/request/QueryResponseWriter.javaindex ccd103925ed..b4b97f811d3 100644--- a/src/java/org/apache/solr/request/QueryResponseWriter.java+++ b/src/java/org/apache/solr/request/QueryResponseWriter.java@@ -21,6 +21,7 @@ import java.io.Writer;import java.io.IOException;import org.apache.solr.common.util.NamedList;+import org.apache.solr.util.plugin.NamedListInitializedPlugin;/*** Implementations of <code>QueryResponseWriter</code> are used to format responses to query requests.@@ -41,7 +42,7 @@ import org.apache.solr.common.util.NamedList;* @author yonik* @version $Id$*/-public interface QueryResponseWriter {+public interface QueryResponseWriter extends NamedListInitializedPlugin {public static String CONTENT_TYPE_XML_UTF8="text/xml; charset=UTF-8";public static String CONTENT_TYPE_TEXT_UTF8="text/plain; charset=UTF-8";public static String CONTENT_TYPE_TEXT_ASCII="text/plain; charset=US-ASCII";@@ -68,7 +69,7 @@ public interface QueryResponseWriter {* <p>* QueryResponseWriter's must implement this method to return a valid* HTTP Content-Type header for the request, that will logically- * corrispond with the output produced by the write method.+ * correspond with the output produced by the write method.* </p>* @return a Content-Type string, which may not be null.*/@@ -82,3 +83,5 @@ public interface QueryResponseWriter {public void init(NamedList args);}++diff --git a/src/java/org/apache/solr/schema/IndexSchema.java b/src/java/org/apache/solr/schema/IndexSchema.javaindex 85cd51df36f..48d760ed3ba 100644--- a/src/java/org/apache/solr/schema/IndexSchema.java+++ b/src/java/org/apache/solr/schema/IndexSchema.java@@ -31,6 +31,7 @@ import org.apache.solr.analysis.TokenFilterFactory;import org.apache.solr.analysis.TokenizerChain;import org.apache.solr.analysis.TokenizerFactory;import org.apache.solr.search.SolrQueryParser;+import org.apache.solr.util.plugin.AbstractPluginLoader;import org.w3c.dom.Document;import org.w3c.dom.NamedNodeMap;import org.w3c.dom.Node;@@ -295,7 +296,7 @@ public final class IndexSchema {Config config = new Config("schema", getInputStream(), "/schema/");Document document = config.getDocument();- XPath xpath = config.getXPath();+ final XPath xpath = config.getXPath();Node nd = (Node) xpath.evaluate("/schema/@name", document, XPathConstants.NODE);if (nd==null) {@@ -307,50 +308,51 @@ public final class IndexSchema {version = config.getFloat("/schema/@version", 1.0f);- String expression = "/schema/types/fieldtype | /schema/types/fieldType";- NodeList nodes = (NodeList) xpath.evaluate(expression, document, XPathConstants.NODESET);--- for (int i=0; i<nodes.getLength(); i++) {- Node node = nodes.item(i);- NamedNodeMap attrs = node.getAttributes();-- String name = DOMUtil.getAttr(attrs,"name","fieldtype error");- log.finest("reading fieldtype "+name);- String clsName = DOMUtil.getAttr(attrs,"class", "fieldtype error");- FieldType ft = (FieldType)Config.newInstance(clsName);- ft.setTypeName(name);-- expression = "./analyzer[@type='query']";- Node anode = (Node)xpath.evaluate(expression, node, XPathConstants.NODE);- Analyzer queryAnalyzer = readAnalyzer(anode);-- // An analyzer without a type specified, or with type="index"- expression = "./analyzer[not(@type)] | ./analyzer[@type='index']";- anode = (Node)xpath.evaluate(expression, node, XPathConstants.NODE);- Analyzer analyzer = readAnalyzer(anode);-- if (queryAnalyzer==null) queryAnalyzer=analyzer;- if (analyzer==null) analyzer=queryAnalyzer;- if (analyzer!=null) {- ft.setAnalyzer(analyzer);- ft.setQueryAnalyzer(queryAnalyzer);+ final IndexSchema schema = this;+ AbstractPluginLoader<FieldType> loader = new AbstractPluginLoader<FieldType>( "[schema.xml] fieldType" ) {++ @Override+ protected FieldType create( String name, String className, Map<String,String> params, Node node ) throws Exception+ {+ FieldType ft = (FieldType)Config.newInstance(className);+ ft.setTypeName(name);++ String expression = "./analyzer[@type='query']";+ Node anode = (Node)xpath.evaluate(expression, node, XPathConstants.NODE);+ Analyzer queryAnalyzer = readAnalyzer(anode);++ // An analyzer without a type specified, or with type="index"+ expression = "./analyzer[not(@type)] | ./analyzer[@type='index']";+ anode = (Node)xpath.evaluate(expression, node, XPathConstants.NODE);+ Analyzer analyzer = readAnalyzer(anode);++ if (queryAnalyzer==null) queryAnalyzer=analyzer;+ if (analyzer==null) analyzer=queryAnalyzer;+ if (analyzer!=null) {+ ft.setAnalyzer(analyzer);+ ft.setQueryAnalyzer(queryAnalyzer);+ }+ return ft;+ }++ @Override+ protected void init(FieldType plugin, Map<String, String> params, Node node) throws Exception {+ plugin.setArgs(schema, params );}-- ft.setArgs(this, DOMUtil.toMapExcept(attrs,"name","class"));- FieldType old = fieldTypes.put(ft.typeName,ft);- if( old != null ) {- String msg = "[schema.xml] Duplicate fieldType definition for '"- + ft.typeName + "' ignoring: "+old.toString();-- Throwable t = new SolrException( SolrException.ErrorCode.SERVER_ERROR, msg );- SolrException.logOnce(log,null,t);- SolrConfig.severeErrors.add( t );+ @Override+ protected FieldType register(String name, FieldType plugin) throws Exception {+ log.finest("fieldtype defined: " + plugin );+ return fieldTypes.put( name, plugin );}- log.finest("fieldtype defined: " + ft);- }+ };++ String expression = "/schema/types/fieldtype | /schema/types/fieldType";+ NodeList nodes = (NodeList) xpath.evaluate(expression, document, XPathConstants.NODESET);+ loader.load( nodes );++// Hang on to the fields that say if they are required -- this lets us set a reasonable default for the unique keyMap<String,Boolean> explicitRequiredProp = new HashMap<String, Boolean>();diff --git a/src/java/org/apache/solr/util/SolrPluginUtils.java b/src/java/org/apache/solr/util/SolrPluginUtils.javaindex 784a3c88c7e..d25a3f142d7 100644--- a/src/java/org/apache/solr/util/SolrPluginUtils.java+++ b/src/java/org/apache/solr/util/SolrPluginUtils.java@@ -17,7 +17,6 @@package org.apache.solr.util;-import org.apache.lucene.analysis.Analyzer;import org.apache.lucene.document.Document;import org.apache.lucene.queryParser.ParseException;import org.apache.lucene.queryParser.QueryParser;@@ -25,13 +24,13 @@ import org.apache.lucene.search.*;import org.apache.lucene.search.BooleanClause.Occur;import org.apache.solr.common.SolrException;import org.apache.solr.common.params.AppendedSolrParams;-import org.apache.solr.common.params.CommonParams;import org.apache.solr.common.params.DefaultSolrParams;import org.apache.solr.common.params.SolrParams;import org.apache.solr.common.util.NamedList;import org.apache.solr.common.util.SimpleOrderedMap;import org.apache.solr.common.util.StrUtils;import org.apache.solr.core.SolrCore;+import org.apache.solr.highlight.SolrHighlighter;import org.apache.solr.request.SolrQueryRequest;import org.apache.solr.request.SolrQueryResponse;import org.apache.solr.schema.IndexSchema;@@ -39,7 +38,6 @@ import org.apache.solr.schema.SchemaField;import org.apache.solr.search.*;import java.io.IOException;-import java.io.StringReader;import java.util.*;import java.util.logging.Level;import java.util.regex.Pattern;@@ -240,8 +238,9 @@ public class SolrPluginUtils {// copy return fields listfieldFilter = new HashSet<String>(returnFields);// add highlight fields- if(HighlightingUtils.isHighlightingEnabled(req)) {- for(String field: HighlightingUtils.getHighlightFields(query, req, null))+ SolrHighlighter highligher = req.getCore().getHighlighter();+ if(highligher.isHighlightingEnabled(req.getParams())) {+ for(String field: highligher.getHighlightFields(query, req, null))fieldFilter.add(field);}// fetch unique key if one exists.@@ -871,3 +870,5 @@ public class SolrPluginUtils {++diff --git a/src/java/org/apache/solr/util/plugin/AbstractPluginLoader.java b/src/java/org/apache/solr/util/plugin/AbstractPluginLoader.javanew file mode 100644index 00000000000..b9a66a65051--- /dev/null+++ b/src/java/org/apache/solr/util/plugin/AbstractPluginLoader.java@@ -0,0 +1,177 @@+/**+ * Licensed to the Apache Software Foundation (ASF) under one or more+ * contributor license agreements. See the NOTICE file distributed with+ * this work for additional information regarding copyright ownership.+ * The ASF licenses this file to You under the Apache License, Version 2.0+ * (the "License"); you may not use this file except in compliance with+ * the License. You may obtain a copy of the License at+ *+ * http://www.apache.org/licenses/LICENSE-2.0+ *+ * Unless required by applicable law or agreed to in writing, software+ * distributed under the License is distributed on an "AS IS" BASIS,+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.+ * See the License for the specific language governing permissions and+ * limitations under the License.+ */++package org.apache.solr.util.plugin;++import java.util.ArrayList;+import java.util.List;+import java.util.Map;+import java.util.logging.Logger;++import org.apache.solr.common.SolrException;+import org.apache.solr.common.util.DOMUtil;+import org.apache.solr.core.Config;+import org.apache.solr.core.SolrConfig;+import org.w3c.dom.Node;+import org.w3c.dom.NodeList;++/**+ * An abstract super class that manages standard solr-style plugin configuration.+ *+ * @author ryan+ * @version $Id$+ * @since solr 1.3+ */+public abstract class AbstractPluginLoader<T>+{+ public static Logger log = Logger.getLogger(AbstractPluginLoader.class.getName());++ private final String type;+ private final boolean preRegister;++ /**+ * @param type is the 'type' name included in error messages.+ * @param preRegister, if true, this will first register all Plugins, then it will initialize them.+ */+ public AbstractPluginLoader( String type, boolean preRegister )+ {+ this.type = type;+ this.preRegister = preRegister;+ }++ public AbstractPluginLoader( String type )+ {+ this( type, false );+ }++ /**+ * Where to look for classes+ */+ protected String[] getDefaultPackages()+ {+ return new String[]{};+ }++ /**+ * @param name - The registered name+ * @param className - class name for requested plugin+ * @param params - the parameters specified in the XML+ * @param node - the XML node defining this plugin+ */+ @SuppressWarnings("unchecked")+ protected T create( String name, String className, Map<String,String> params, Node node ) throws Exception+ {+ return (T) Config.newInstance( className, getDefaultPackages() );+ }++ /**+ * Register a plugin with a given name.+ * @return The plugin previously registered to this name, or null+ */+ abstract protected T register( String name, T plugin ) throws Exception;++ /**+ * Initialize the plugin+ */+ abstract protected void init( T plugin, Map<String,String> params, Node node ) throws Exception;++ /**+ * Given a NodeList from XML, this will+ */+ public T load( NodeList nodes )+ {+ List<PluginInitInfo> info = new ArrayList<PluginInitInfo>();+ T defaultPlugin = null;++ if (nodes !=null ) {+ for (int i=0; i<nodes.getLength(); i++) {+ Node node = nodes.item(i);++ // In a production environment, we can tolerate an error in some request handlers,+ // still load the others, and have a working system.+ try {+ String name = DOMUtil.getAttr(node,"name", type);+ String className = DOMUtil.getAttr(node,"class", type);+ String defaultStr = DOMUtil.getAttr(node,"default", null );++ Map<String,String> params = DOMUtil.toMapExcept( node.getAttributes(),+ "name","class" );++ T plugin = create(name, className, params, node );+ log.info("created "+name+": " + plugin.getClass().getName() );++ // Either initialize now or wait till everything has been registered+ if( preRegister ) {+ info.add( new PluginInitInfo( plugin, params, node ) );+ }+ else {+ init( plugin, params, node );+ }++ T old = register( name, plugin );+ if( old != null ) {+ throw new SolrException( SolrException.ErrorCode.SERVER_ERROR,+ "Multiple "+type+" registered to the same name: "+name+" ignoring: "+old );+ }++ if( defaultStr != null && Boolean.parseBoolean( defaultStr ) ) {+ if( defaultPlugin != null ) {+ throw new SolrException( SolrException.ErrorCode.SERVER_ERROR,+ "Multiple default "+type+" plugins: "+defaultPlugin + " AND " + name );+ }+ defaultPlugin = plugin;+ }+ }+ catch (Exception e) {+ SolrConfig.severeErrors.add( e );+ SolrException.logOnce(log,null,e);+ }+ }+ }++ // If everything needs to be registered *first*, this will initialize later+ for( PluginInitInfo pinfo : info ) {+ try {+ init( pinfo.plugin, pinfo.params, pinfo.node );+ }+ catch( Exception ex ) {+ SolrConfig.severeErrors.add( ex );+ SolrException.logOnce(log,null,ex);+ }+ }+ return defaultPlugin;+ }+++ /**+ * Internal class to hold onto initialization info so that it can be initialized+ * after it is registered.+ */+ private class PluginInitInfo+ {+ final T plugin;+ final Map<String,String> params;+ final Node node;++ PluginInitInfo( T plugin, Map<String,String> params, Node node )+ {+ this.plugin = plugin;+ this.params = params;+ this.node = node;+ }+ }+}diff --git a/src/java/org/apache/solr/util/plugin/MapInitializedPlugin.java b/src/java/org/apache/solr/util/plugin/MapInitializedPlugin.javanew file mode 100644index 00000000000..6539456f383--- /dev/null+++ b/src/java/org/apache/solr/util/plugin/MapInitializedPlugin.java@@ -0,0 +1,31 @@+/**+ * Licensed to the Apache Software Foundation (ASF) under one or more+ * contributor license agreements. See the NOTICE file distributed with+ * this work for additional information regarding copyright ownership.+ * The ASF licenses this file to You under the Apache License, Version 2.0+ * (the "License"); you may not use this file except in compliance with+ * the License. You may obtain a copy of the License at+ *+ * http://www.apache.org/licenses/LICENSE-2.0+ *+ * Unless required by applicable law or agreed to in writing, software+ * distributed under the License is distributed on an "AS IS" BASIS,+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.+ * See the License for the specific language governing permissions and+ * limitations under the License.+ */++package org.apache.solr.util.plugin;++import java.util.Map;++/**+ * A plugin that can be initialized with a Map<String,String>+ *+ * @author ryan+ * @version $Id$+ * @since solr 1.3+ */+public interface MapInitializedPlugin {+ void init( Map<String,String> args );+}diff --git a/src/java/org/apache/solr/util/plugin/MapPluginLoader.java b/src/java/org/apache/solr/util/plugin/MapPluginLoader.javanew file mode 100644index 00000000000..2e2e0e90ee3--- /dev/null+++ b/src/java/org/apache/solr/util/plugin/MapPluginLoader.java@@ -0,0 +1,52 @@+/**+ * Licensed to the Apache Software Foundation (ASF) under one or more+ * contributor license agreements. See the NOTICE file distributed with+ * this work for additional information regarding copyright ownership.+ * The ASF licenses this file to You under the Apache License, Version 2.0+ * (the "License"); you may not use this file except in compliance with+ * the License. You may obtain a copy of the License at+ *+ * http://www.apache.org/licenses/LICENSE-2.0+ *+ * Unless required by applicable law or agreed to in writing, software+ * distributed under the License is distributed on an "AS IS" BASIS,+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.+ * See the License for the specific language governing permissions and+ * limitations under the License.+ */++package org.apache.solr.util.plugin;++import java.util.Map;++import org.w3c.dom.Node;++/**+ *+ * @author ryan+ * @version $Id$+ * @since solr 1.3+ */+public class MapPluginLoader<T extends MapInitializedPlugin> extends AbstractPluginLoader<T>+{+ private final Map<String,T> registry;++ public MapPluginLoader( String name, Map<String,T> map )+ {+ super( name );+ registry = map;+ }++ @Override+ protected void init(T plugin, Map<String, String> params, Node node) throws Exception {+ plugin.init( params );+ }++ @Override+ protected T register(String name, T plugin) throws Exception {+ if( registry != null ) {+ return registry.put( name, plugin );+ }+ return null;+ }+}diff --git a/src/java/org/apache/solr/util/plugin/NamedListInitializedPlugin.java b/src/java/org/apache/solr/util/plugin/NamedListInitializedPlugin.javanew file mode 100644index 00000000000..268576ef563--- /dev/null+++ b/src/java/org/apache/solr/util/plugin/NamedListInitializedPlugin.java@@ -0,0 +1,31 @@+/**+ * Licensed to the Apache Software Foundation (ASF) under one or more+ * contributor license agreements. See the NOTICE file distributed with+ * this work for additional information regarding copyright ownership.+ * The ASF licenses this file to You under the Apache License, Version 2.0+ * (the "License"); you may not use this file except in compliance with+ * the License. You may obtain a copy of the License at+ *+ * http://www.apache.org/licenses/LICENSE-2.0+ *+ * Unless required by applicable law or agreed to in writing, software+ * distributed under the License is distributed on an "AS IS" BASIS,+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.+ * See the License for the specific language governing permissions and+ * limitations under the License.+ */++package org.apache.solr.util.plugin;++import org.apache.solr.common.util.NamedList;++/**+ * A plugin that can be initialized with a NamedList+ *+ * @author ryan+ * @version $Id$+ * @since solr 1.3+ */+public interface NamedListInitializedPlugin {+ void init( NamedList args );+}diff --git a/src/java/org/apache/solr/util/plugin/NamedListPluginLoader.java b/src/java/org/apache/solr/util/plugin/NamedListPluginLoader.javanew file mode 100644index 00000000000..da2a2570e79--- /dev/null+++ b/src/java/org/apache/solr/util/plugin/NamedListPluginLoader.java@@ -0,0 +1,52 @@+/**+ * Licensed to the Apache Software Foundation (ASF) under one or more+ * contributor license agreements. See the NOTICE file distributed with+ * this work for additional information regarding copyright ownership.+ * The ASF licenses this file to You under the Apache License, Version 2.0+ * (the "License"); you may not use this file except in compliance with+ * the License. You may obtain a copy of the License at+ *+ * http://www.apache.org/licenses/LICENSE-2.0+ *+ * Unless required by applicable law or agreed to in writing, software+ * distributed under the License is distributed on an "AS IS" BASIS,+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.+ * See the License for the specific language governing permissions and+ * limitations under the License.+ */++package org.apache.solr.util.plugin;++import java.util.Map;++import org.apache.solr.common.util.DOMUtil;+import org.w3c.dom.Node;++/**+ * @author ryan+ * @version $Id$+ * @since solr 1.3+ */+public class NamedListPluginLoader<T extends NamedListInitializedPlugin> extends AbstractPluginLoader<T>+{+ private final Map<String,T> registry;++ public NamedListPluginLoader( String name, Map<String,T> map )+ {+ super( name );+ registry = map;+ }++ @Override+ protected void init(T plugin, Map<String, String> params, Node node) throws Exception {+ plugin.init( DOMUtil.childNodesToNamedList(node) );+ }++ @Override+ protected T register(String name, T plugin) throws Exception {+ if( registry != null ) {+ return registry.put( name, plugin );+ }+ return null;+ }+}