From ebc1d1e7eee428652565634be1823fee74a4924f Mon Sep 17 00:00:00 2001 From: Noble Paul Date: Mon, 8 Jun 2009 07:55:57 +0000 Subject: [PATCH] SOLR-1198 moved valueSourceParser, listeners, deletionPolicy,directoryFactory,queryParser,responseWriter to solconfig git-svn-id: https://svn.apache.org/repos/asf/lucene/solr/trunk@782552 13f79535-47bb-0310-9956-ffa450edef68 --- .../org/apache/solr/core/RequestHandlers.java | 2 +- src/java/org/apache/solr/core/SolrConfig.java | 89 +++++++++--- src/java/org/apache/solr/core/SolrCore.java | 135 ++++++++---------- 3 files changed, 134 insertions(+), 92 deletions(-) diff --git a/src/java/org/apache/solr/core/RequestHandlers.java b/src/java/org/apache/solr/core/RequestHandlers.java index 5903e7beffc..0ca104c8de4 100644 --- a/src/java/org/apache/solr/core/RequestHandlers.java +++ b/src/java/org/apache/solr/core/RequestHandlers.java @@ -131,7 +131,7 @@ final class RequestHandlers { void initHandlersFromConfig(SolrConfig config ){ Map handlers = new HashMap(); - for (SolrConfig.PluginInfo info : config.reqHandlerInfo) { + for (SolrConfig.PluginInfo info : config.getReqHandlerInfo()) { try { SolrRequestHandler requestHandler; if( info.startup != null ) { diff --git a/src/java/org/apache/solr/core/SolrConfig.java b/src/java/org/apache/solr/core/SolrConfig.java index 4cc397b9679..79810146781 100644 --- a/src/java/org/apache/solr/core/SolrConfig.java +++ b/src/java/org/apache/solr/core/SolrConfig.java @@ -167,7 +167,8 @@ public class SolrConfig extends Config { jmxConfig = new JmxConfiguration(false, null, null); } maxWarmingSearchers = getInt("query/maxWarmingSearchers",Integer.MAX_VALUE); - reqHandlerInfo = loadRequestHandlerInfo(); + + loadPluginInfo(); Config.log.info("Loaded SolrConfig: " + name); @@ -175,11 +176,27 @@ public class SolrConfig extends Config { config = this; } - private List loadRequestHandlerInfo() { + protected void loadPluginInfo() { + List reqFields = Arrays.asList("name","class"); + reqHandlerInfo = loadPluginInfo("requestHandler",reqFields); + respWriterInfo = loadPluginInfo("queryResponseWriter",reqFields); + valueSourceParserInfo = loadPluginInfo("valueSourceParser",reqFields); + queryParserInfo = loadPluginInfo("queryParser",reqFields); + searchComponentInfo = loadPluginInfo("searchComponent",reqFields); + List plugins = loadPluginInfo("directoryFactory",reqFields); + directoryfactoryInfo = plugins.isEmpty() ? null:plugins.get(0); + reqFields = Arrays.asList("class"); + plugins = loadPluginInfo("mainIndex/deletionPolicy",reqFields); + deletionPolicyInfo = plugins.isEmpty() ? null : plugins.get(0); + firstSearcherListenerInfo = loadPluginInfo("//listener[@event='firstSearcher']",reqFields); + newSearcherListenerInfo = loadPluginInfo("//listener[@event='newSearcher']",reqFields); + } + + private List loadPluginInfo(String tag, List reqFields) { ArrayList result = new ArrayList(); - NodeList nodes = (NodeList) evaluate("requestHandler", XPathConstants.NODESET); + NodeList nodes = (NodeList) evaluate(tag, XPathConstants.NODESET); for (int i=0; i reqHandlerInfo; + + protected List reqHandlerInfo; + protected List queryParserInfo; + protected List respWriterInfo; + protected List valueSourceParserInfo; + protected List searchComponentInfo; + protected List firstSearcherListenerInfo; + protected PluginInfo deletionPolicyInfo; + protected List newSearcherListenerInfo; + protected PluginInfo directoryfactoryInfo; public final int maxWarmingSearchers; public final boolean unlockOnStartup; @@ -347,34 +373,59 @@ public class SolrConfig extends Config { } public static class PluginInfo { - final String startup, name, className, event; + final String startup, name, className; final boolean isDefault; final NamedList initArgs; public PluginInfo(String startup, String name, String className, - String event, boolean isdefault, NamedList initArgs) { + boolean isdefault, NamedList initArgs) { this.startup = startup; this.name = name; this.className = className; - this.event = event; - isDefault = isdefault; + this.isDefault = isdefault; this.initArgs = initArgs; } - public PluginInfo(Node node, String err, String... requiredFields) { - List 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)); + public PluginInfo(Node node, String err, List reqFields) { + startup = getVal( node, "startup",reqFields,err); + name = getVal(node, "name", reqFields,err); + className = getVal(node, "class",reqFields,err); + isDefault = Boolean.parseBoolean(getVal(node,"default",reqFields,err)); initArgs = DOMUtil.childNodesToNamedList(node); } - private String getVal(Node node, String name, List required, String err) { - return DOMUtil.getAttr(node, name, required.contains(name) ? err : null); - } + private String getVal(Node node, String name, List required, String err) { + return DOMUtil.getAttr(node, name, required.contains(name) ? err : null); + } + @Override + public String toString() { + StringBuilder sb = new StringBuilder("{"); + if(name != null) sb.append("name = "+name +","); + if(className != null) sb.append("class = "+className +","); + if(isDefault) sb.append("default = "+isDefault +","); + if(initArgs.size() >0) sb.append("args = "+initArgs); + sb.append("}"); + return sb.toString(); + } } + + public List getReqHandlerInfo() { return reqHandlerInfo; } + + public List getQueryParserInfo() { return queryParserInfo; } + + public List getRespWriterInfo() { return respWriterInfo; } + + public List getValueSourceParserInfo() { return valueSourceParserInfo; } + + public List getSearchComponentInfo() { return searchComponentInfo; } + + public List getFirstSearcherListenerInfo() { return firstSearcherListenerInfo; } + + public List getNewSearcherListenerInfo() { return newSearcherListenerInfo; } + + public PluginInfo getDirectoryfactoryInfo() { return directoryfactoryInfo; } + + public PluginInfo getDeletionPolicyInfo() { return deletionPolicyInfo; } } diff --git a/src/java/org/apache/solr/core/SolrCore.java b/src/java/org/apache/solr/core/SolrCore.java index 9255fb6c882..d0f2d7c4661 100644 --- a/src/java/org/apache/solr/core/SolrCore.java +++ b/src/java/org/apache/solr/core/SolrCore.java @@ -70,6 +70,7 @@ import java.util.concurrent.atomic.AtomicInteger; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import java.net.URL; +import java.lang.reflect.Constructor; /** @@ -248,31 +249,27 @@ public final class SolrCore implements SolrInfoMBean { return infoRegistry; } - private void initDeletionPolicy() { - String className = solrConfig.get("mainIndex/deletionPolicy/@class", SolrDeletionPolicy.class.getName()); - IndexDeletionPolicy delPolicy = createInstance(className, IndexDeletionPolicy.class, "Deletion Policy for SOLR"); + private void initDeletionPolicy() { + SolrConfig.PluginInfo info = solrConfig.getDeletionPolicyInfo(); + IndexDeletionPolicy delPolicy = null; + if(info != null){ + delPolicy = createInstance(info.className,IndexDeletionPolicy.class,"Deletion Policy for SOLR"); + if (delPolicy instanceof NamedListInitializedPlugin) { + ((NamedListInitializedPlugin) delPolicy).init(info.initArgs); + } + } else { + delPolicy = new SolrDeletionPolicy(); + } + solrDelPolicy = new IndexDeletionPolicyWrapper(delPolicy); + } - Node node = (Node) solrConfig.evaluate("mainIndex/deletionPolicy", XPathConstants.NODE); - if (node != null) { - if (delPolicy instanceof NamedListInitializedPlugin) - ((NamedListInitializedPlugin) delPolicy).init(DOMUtil.childNodesToNamedList(node)); - } - solrDelPolicy = new IndexDeletionPolicyWrapper(delPolicy); - } - - public List parseListener(String path) { + private List parseListener(List path) { List lst = new ArrayList(); - log.info( logid+"Searching for listeners: " +path); - NodeList nodes = (NodeList)solrConfig.evaluate(path, XPathConstants.NODESET); - if (nodes!=null) { - for (int i=0; i firstSearcherListeners; List newSearcherListeners; private void parseListeners() { - firstSearcherListeners = parseListener("//listener[@event=\"firstSearcher\"]"); - newSearcherListeners = parseListener("//listener[@event=\"newSearcher\"]"); + firstSearcherListeners = parseListener(solrConfig.getFirstSearcherListenerInfo()); + newSearcherListeners = parseListener(solrConfig.getNewSearcherListenerInfo()); } /** @@ -330,21 +327,16 @@ public final class SolrCore implements SolrInfoMBean { return new SolrIndexSearcher(this, schema, name, directoryFactory.open(getIndexDir()), readOnly, false); } - private void initDirectoryFactory() { - String xpath = "directoryFactory"; - Node node = (Node) solrConfig.evaluate(xpath, XPathConstants.NODE); - DirectoryFactory dirFactory; - if (node != null) { - Map registry = new HashMap(); - NamedListPluginLoader indexReaderFactoryLoader = new NamedListPluginLoader( - "[solrconfig.xml] " + xpath, registry); - dirFactory = indexReaderFactoryLoader.loadSingle(solrConfig - .getResourceLoader(), node); + private void initDirectoryFactory() { + DirectoryFactory dirFactory; + SolrConfig.PluginInfo info = solrConfig.getDirectoryfactoryInfo(); + if (info != null) { + dirFactory = (DirectoryFactory) getResourceLoader().newInstance(info.className); + dirFactory.init(info.initArgs); } else { dirFactory = new StandardDirectoryFactory(); } - // And set it directoryFactory = dirFactory; } @@ -402,17 +394,20 @@ public final class SolrCore implements SolrInfoMBean { private T createInstance(String className, Class cast, String msg) { Class clazz = null; if (msg == null) msg = "SolrCore Object"; - try { - try { - clazz = solrConfig.getResourceLoader().findClass(className); + try { + clazz = getResourceLoader().findClass(className); if (cast != null && !cast.isAssignableFrom(clazz)) throw new SolrException(SolrException.ErrorCode.SERVER_ERROR,"Error Instantiating "+msg+", "+className+ " is not a " +cast.getName()); - - java.lang.reflect.Constructor cons = clazz.getConstructor(new Class[]{SolrCore.class}); - return (T) cons.newInstance(new Object[]{this}); - } catch(NoSuchMethodException xnomethod) { - return (T) clazz.newInstance(); - } + //most of the classes do not have constructors whiuch take in SolrCore. It is recommended to obtain SolrCore by implementing SolrCoreAare. + // So invariably always it will cause a NoSuchMethodException. So iterate though the list of available constructors + Constructor[] cons = clazz.getConstructors(); + for (Constructor con : cons) { + Class[] types = con.getParameterTypes(); + if(types.length == 1 && types[0] == SolrCore.class){ + return (T)con.newInstance(this); + } + } + return (T) clazz.newInstance();//use the empty constructor } catch (SolrException e) { throw e; } catch (Exception e) { @@ -830,14 +825,14 @@ public final class SolrCore implements SolrInfoMBean { /** * Register the default search components */ - private static Map loadSearchComponents( SolrConfig config ) + private Map loadSearchComponents( SolrConfig config ) { Map components = new HashMap(); - + String xpath = "searchComponent"; NamedListPluginLoader loader = new NamedListPluginLoader( xpath, components ); loader.load( config.getResourceLoader(), (NodeList)config.evaluate( xpath, XPathConstants.NODESET ) ); - + final Map> standardcomponents = new HashMap>(); standardcomponents.put( QueryComponent.COMPONENT_NAME, QueryComponent.class ); @@ -1415,13 +1410,7 @@ public final class SolrCore implements SolrInfoMBean { /** Configure the query response writers. There will always be a default writer; additional * writers may also be configured. */ private void initWriters() { - String xpath = "queryResponseWriter"; - NodeList nodes = (NodeList) solrConfig.evaluate(xpath, XPathConstants.NODESET); - - NamedListPluginLoader loader = - new NamedListPluginLoader( "[solrconfig.xml] "+xpath, responseWriters ); - - defaultResponseWriter = loader.load( solrConfig.getResourceLoader(), nodes ); + defaultResponseWriter = initPlugins(solrConfig.getRespWriterInfo(), responseWriters, QueryResponseWriter.class); for (Map.Entry entry : DEFAULT_RESPONSE_WRITERS.entrySet()) { if(responseWriters.get(entry.getKey()) == null) responseWriters.put(entry.getKey(), entry.getValue()); } @@ -1455,14 +1444,7 @@ public final class SolrCore implements SolrInfoMBean { /** Configure the query parsers. */ private void initQParsers() { - String xpath = "queryParser"; - NodeList nodes = (NodeList) solrConfig.evaluate(xpath, XPathConstants.NODESET); - - NamedListPluginLoader loader = - new NamedListPluginLoader( "[solrconfig.xml] "+xpath, qParserPlugins); - - loader.load( solrConfig.getResourceLoader(), nodes ); - + initPlugins(solrConfig.getQueryParserInfo(),qParserPlugins,QParserPlugin.class); // default parsers for (int i=0; i loader = - new NamedListPluginLoader( "[solrconfig.xml] "+xpath, valueSourceParsers); - - loader.load( solrConfig.getResourceLoader(), nodes ); - + initPlugins(solrConfig.getValueSourceParserInfo(),valueSourceParsers,ValueSourceParser.class); // default value source parsers for (Map.Entry entry : ValueSourceParser.standardValueSourceParsers.entrySet()) { try { @@ -1511,7 +1486,23 @@ public final class SolrCore implements SolrInfoMBean { } } } - + + private T initPlugins(List pluginInfos , Map registry, Class type){ + T def = null; + for (SolrConfig.PluginInfo info : pluginInfos) { + T o = createInstance(info.className,type, type.getSimpleName()); + if (o instanceof NamedListInitializedPlugin) { + ((NamedListInitializedPlugin) o).init(info.initArgs); + } + registry.put(info.name, o); + if(info.isDefault){ + def = o; + } + + } + return def; + } + public ValueSourceParser getValueSourceParser(String parserName) { return valueSourceParsers.get(parserName); }