Apache Lucene open-source search software
SOLR-1198. Moving requesthandler parsing to SolrConfig
Noble Paule1a0a5bc4eb7
diff --git a/src/java/org/apache/solr/core/RequestHandlers.java b/src/java/org/apache/solr/core/RequestHandlers.javaindex 78c5209f65b..5903e7beffc 100644--- a/src/java/org/apache/solr/core/RequestHandlers.java+++ b/src/java/org/apache/solr/core/RequestHandlers.java@@ -18,9 +18,7 @@package org.apache.solr.core;import java.net.URL;-import java.util.Collections;-import java.util.HashMap;-import java.util.Map;+import java.util.*;import java.util.concurrent.ConcurrentHashMap;import org.slf4j.Logger;@@ -38,7 +36,6 @@ import org.apache.solr.request.SolrQueryRequest;import org.apache.solr.request.SolrQueryResponse;import org.apache.solr.request.SolrRequestHandler;import org.apache.solr.util.plugin.AbstractPluginLoader;-import org.apache.solr.util.plugin.ResourceLoaderAware;import org.apache.solr.util.plugin.SolrCoreAware;import org.w3c.dom.NodeList;import org.w3c.dom.Node;@@ -118,11 +115,11 @@ 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 initialized+ * While the normal runtime registration contract is that handlers MUST be initialized* before they are registered, this function does not do that exactly.- *- * This function 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 initialized.*@@ -131,52 +128,44 @@ final class RequestHandlers {** Handlers will be registered and initialized in the order they appear in solrconfig.xml*/- void initHandlersFromConfig( final Config config )- {- final RequestHandlers handlers = this;- AbstractPluginLoader<SolrRequestHandler> loader =- new AbstractPluginLoader<SolrRequestHandler>( "[solrconfig.xml] requestHandler", true, true )- {- @Override- protected SolrRequestHandler create( ResourceLoader config, String name, String className, Node node ) throws Exception- {- String startup = DOMUtil.getAttr( node, "startup" );- if( startup != null ) {- if( "lazy".equals( startup ) ) {- log.info("adding lazy requestHandler: " + className );- NamedList args = DOMUtil.childNodesToNamedList(node);- return new LazyRequestHandlerWrapper( core, className, args );- }- else {- throw new Exception( "Unknown startup value: '"+startup+"' for: "+className );++ void initHandlersFromConfig(SolrConfig config ){+ Map<SolrConfig.PluginInfo,SolrRequestHandler> handlers = new HashMap<SolrConfig.PluginInfo,SolrRequestHandler>();+ for (SolrConfig.PluginInfo info : config.reqHandlerInfo) {+ try {+ SolrRequestHandler requestHandler;+ if( info.startup != null ) {+ if( "lazy".equals(info.startup ) ) {+ log.info("adding lazy requestHandler: " + info.className);+ requestHandler = new LazyRequestHandlerWrapper( core, info.className, info.initArgs );+ } else {+ throw new Exception( "Unknown startup value: '"+info.startup+"' for: "+info.className );}+ } else {+ requestHandler = (SolrRequestHandler) config.getResourceLoader().newInstance(info.className);}- return super.create( config, name, className, node );- }-- @Override- protected SolrRequestHandler register(String name, SolrRequestHandler plugin) throws Exception {- return handlers.register( name, plugin );- }-- @Override- protected void init(SolrRequestHandler plugin, Node node ) throws Exception {- plugin.init( DOMUtil.childNodesToNamedList(node) );- }- };-- NodeList nodes = (NodeList)config.evaluate("requestHandler", XPathConstants.NODESET);-- // Load the handlers and get the default one- SolrRequestHandler defaultHandler = loader.load( config.getResourceLoader(), nodes );- if( defaultHandler == null ) {- defaultHandler = get(RequestHandlers.DEFAULT_HANDLER_NAME);- if( defaultHandler == null ) {- defaultHandler = new StandardRequestHandler();- register(RequestHandlers.DEFAULT_HANDLER_NAME, defaultHandler);+ handlers.put(info,requestHandler);+ requestHandler.init(info.initArgs);+ SolrRequestHandler old = register(info.name, requestHandler);+ if(old != null) {+ log.warn("Multiple requestHandler registered to the same name: " + info.name + " ignoring: " + old.getClass().getName());+ }+ if(info.isDefault){+ old = register("",requestHandler);+ if(old != null)+ log.warn("Multiple default requestHandler registered" + " ignoring: " + old.getClass().getName());+ }+ log.info("created "+info.name+": " + info.className);+ } catch (Exception e) {+ SolrConfig.severeErrors.add( e );+ SolrException.logOnce(log,null,e);}}- register("", defaultHandler);+ for (Map.Entry<SolrConfig.PluginInfo,SolrRequestHandler> entry : handlers.entrySet()) {+ entry.getValue().init(entry.getKey().initArgs);+ }++ if(get("") == null) register("", get(DEFAULT_HANDLER_NAME));}diff --git a/src/java/org/apache/solr/core/SolrConfig.java b/src/java/org/apache/solr/core/SolrConfig.javaindex 452c65dc684..4cc397b9679 100644--- a/src/java/org/apache/solr/core/SolrConfig.java+++ b/src/java/org/apache/solr/core/SolrConfig.java@@ -18,6 +18,7 @@package org.apache.solr.core;import org.apache.solr.common.util.NamedList;+import org.apache.solr.common.util.DOMUtil;import org.apache.solr.handler.PingRequestHandler;import org.apache.solr.request.LocalSolrQueryRequest;import org.apache.solr.request.SolrQueryRequest;@@ -31,9 +32,11 @@ import org.slf4j.Logger;import org.slf4j.LoggerFactory;import org.w3c.dom.Node;+import org.w3c.dom.NodeList;import org.xml.sax.SAXException;import javax.xml.parsers.ParserConfigurationException;+import javax.xml.xpath.XPathConstants;import java.util.*;import java.util.regex.Pattern;@@ -164,14 +167,23 @@ public class SolrConfig extends Config {jmxConfig = new JmxConfiguration(false, null, null);}maxWarmingSearchers = getInt("query/maxWarmingSearchers",Integer.MAX_VALUE);+ reqHandlerInfo = loadRequestHandlerInfo();-Config.log.info("Loaded SolrConfig: " + name);// TODO -- at solr 2.0. this should go awayconfig = this;}+ private List<PluginInfo> loadRequestHandlerInfo() {+ ArrayList<PluginInfo> result = new ArrayList<PluginInfo>();+ NodeList nodes = (NodeList) evaluate("requestHandler", XPathConstants.NODESET);+ for (int i=0; i<nodes.getLength(); i++) {+ result.add(new PluginInfo(nodes.item(i) ,"[solrconfig.xml] requestHandler","name","class"));+ }+ return Collections.unmodifiableList(result) ;+ }+/* The set of materialized parameters: */public final int booleanQueryMaxClauseCount;// SolrIndexSearcher - nutch optimizer@@ -196,11 +208,12 @@ public class SolrConfig extends Config {// default & main index configurationspublic final SolrIndexConfig defaultIndexConfig;public final SolrIndexConfig mainIndexConfig;+ public final List<PluginInfo> reqHandlerInfo;public final int maxWarmingSearchers;public final boolean unlockOnStartup;public final boolean useColdSearcher;-+//JMX configurationpublic final JmxConfiguration jmxConfig;@@ -332,4 +345,36 @@ public class SolrConfig extends Config {public Long getMaxAge() { return maxAge; }public LastModFrom getLastModFrom() { return lastModFrom; }}++ public static class PluginInfo {+ final String startup, name, className, event;+ final boolean isDefault;+ final NamedList initArgs;++ public PluginInfo(String startup, String name, String className,+ String event, boolean isdefault, NamedList initArgs) {+ this.startup = startup;+ this.name = name;+ this.className = className;+ this.event = event;+ isDefault = isdefault;+ this.initArgs = initArgs;+ }+++ public PluginInfo(Node node, String err, String... requiredFields) {+ List<String> l = requiredFields == null? Collections.EMPTY_LIST: Arrays.asList(requiredFields);+ startup = getVal( node, "startup",l,err);+ name = getVal(node, "name", l,err);+ className = getVal(node, "class",l,err);+ event = getVal(node, "event",l,err);+ isDefault = Boolean.parseBoolean(getVal(node,"default",l,err));+ initArgs = DOMUtil.childNodesToNamedList(node);+ }++ private String getVal(Node node, String name, List<String> required, String err) {+ return DOMUtil.getAttr(node, name, required.contains(name) ? err : null);+ }++ }}