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
This commit is contained in:
Noble Paul 2009-06-08 07:55:57 +00:00
parent 2eef2dfdc5
commit ebc1d1e7ee
3 changed files with 134 additions and 92 deletions

View File

@ -131,7 +131,7 @@ final class RequestHandlers {
void initHandlersFromConfig(SolrConfig config ){ void initHandlersFromConfig(SolrConfig config ){
Map<SolrConfig.PluginInfo,SolrRequestHandler> handlers = new HashMap<SolrConfig.PluginInfo,SolrRequestHandler>(); Map<SolrConfig.PluginInfo,SolrRequestHandler> handlers = new HashMap<SolrConfig.PluginInfo,SolrRequestHandler>();
for (SolrConfig.PluginInfo info : config.reqHandlerInfo) { for (SolrConfig.PluginInfo info : config.getReqHandlerInfo()) {
try { try {
SolrRequestHandler requestHandler; SolrRequestHandler requestHandler;
if( info.startup != null ) { if( info.startup != null ) {

View File

@ -167,7 +167,8 @@ public class SolrConfig extends Config {
jmxConfig = new JmxConfiguration(false, null, null); jmxConfig = new JmxConfiguration(false, null, null);
} }
maxWarmingSearchers = getInt("query/maxWarmingSearchers",Integer.MAX_VALUE); maxWarmingSearchers = getInt("query/maxWarmingSearchers",Integer.MAX_VALUE);
reqHandlerInfo = loadRequestHandlerInfo();
loadPluginInfo();
Config.log.info("Loaded SolrConfig: " + name); Config.log.info("Loaded SolrConfig: " + name);
@ -175,11 +176,27 @@ public class SolrConfig extends Config {
config = this; config = this;
} }
private List<PluginInfo> loadRequestHandlerInfo() { protected void loadPluginInfo() {
List<String> 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<PluginInfo> 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<PluginInfo> loadPluginInfo(String tag, List<String> reqFields) {
ArrayList<PluginInfo> result = new ArrayList<PluginInfo>(); ArrayList<PluginInfo> result = new ArrayList<PluginInfo>();
NodeList nodes = (NodeList) evaluate("requestHandler", XPathConstants.NODESET); NodeList nodes = (NodeList) evaluate(tag, XPathConstants.NODESET);
for (int i=0; i<nodes.getLength(); i++) { for (int i=0; i<nodes.getLength(); i++) {
result.add(new PluginInfo(nodes.item(i) ,"[solrconfig.xml] requestHandler","name","class")); result.add(new PluginInfo(nodes.item(i) ,"[solrconfig.xml] "+tag,reqFields));
} }
return Collections.unmodifiableList(result) ; return Collections.unmodifiableList(result) ;
} }
@ -208,7 +225,16 @@ public class SolrConfig extends Config {
// default & main index configurations // default & main index configurations
public final SolrIndexConfig defaultIndexConfig; public final SolrIndexConfig defaultIndexConfig;
public final SolrIndexConfig mainIndexConfig; public final SolrIndexConfig mainIndexConfig;
public final List<PluginInfo> reqHandlerInfo;
protected List<PluginInfo> reqHandlerInfo;
protected List<PluginInfo> queryParserInfo;
protected List<PluginInfo> respWriterInfo;
protected List<PluginInfo> valueSourceParserInfo;
protected List<PluginInfo> searchComponentInfo;
protected List<PluginInfo> firstSearcherListenerInfo;
protected PluginInfo deletionPolicyInfo;
protected List<PluginInfo> newSearcherListenerInfo;
protected PluginInfo directoryfactoryInfo;
public final int maxWarmingSearchers; public final int maxWarmingSearchers;
public final boolean unlockOnStartup; public final boolean unlockOnStartup;
@ -347,34 +373,59 @@ public class SolrConfig extends Config {
} }
public static class PluginInfo { public static class PluginInfo {
final String startup, name, className, event; final String startup, name, className;
final boolean isDefault; final boolean isDefault;
final NamedList initArgs; final NamedList initArgs;
public PluginInfo(String startup, String name, String className, public PluginInfo(String startup, String name, String className,
String event, boolean isdefault, NamedList initArgs) { boolean isdefault, NamedList initArgs) {
this.startup = startup; this.startup = startup;
this.name = name; this.name = name;
this.className = className; this.className = className;
this.event = event; this.isDefault = isdefault;
isDefault = isdefault;
this.initArgs = initArgs; this.initArgs = initArgs;
} }
public PluginInfo(Node node, String err, String... requiredFields) { public PluginInfo(Node node, String err, List<String> reqFields) {
List<String> l = requiredFields == null? Collections.EMPTY_LIST: Arrays.asList(requiredFields); startup = getVal( node, "startup",reqFields,err);
startup = getVal( node, "startup",l,err); name = getVal(node, "name", reqFields,err);
name = getVal(node, "name", l,err); className = getVal(node, "class",reqFields,err);
className = getVal(node, "class",l,err); isDefault = Boolean.parseBoolean(getVal(node,"default",reqFields,err));
event = getVal(node, "event",l,err);
isDefault = Boolean.parseBoolean(getVal(node,"default",l,err));
initArgs = DOMUtil.childNodesToNamedList(node); initArgs = DOMUtil.childNodesToNamedList(node);
} }
private String getVal(Node node, String name, List<String> required, String err) { private String getVal(Node node, String name, List<String> required, String err) {
return DOMUtil.getAttr(node, name, required.contains(name) ? err : null); 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<PluginInfo> getReqHandlerInfo() { return reqHandlerInfo; }
public List<PluginInfo> getQueryParserInfo() { return queryParserInfo; }
public List<PluginInfo> getRespWriterInfo() { return respWriterInfo; }
public List<PluginInfo> getValueSourceParserInfo() { return valueSourceParserInfo; }
public List<PluginInfo> getSearchComponentInfo() { return searchComponentInfo; }
public List<PluginInfo> getFirstSearcherListenerInfo() { return firstSearcherListenerInfo; }
public List<PluginInfo> getNewSearcherListenerInfo() { return newSearcherListenerInfo; }
public PluginInfo getDirectoryfactoryInfo() { return directoryfactoryInfo; }
public PluginInfo getDeletionPolicyInfo() { return deletionPolicyInfo; }
} }

View File

@ -70,6 +70,7 @@ import java.util.concurrent.atomic.AtomicInteger;
import org.slf4j.Logger; import org.slf4j.Logger;
import org.slf4j.LoggerFactory; import org.slf4j.LoggerFactory;
import java.net.URL; import java.net.URL;
import java.lang.reflect.Constructor;
/** /**
@ -248,31 +249,27 @@ public final class SolrCore implements SolrInfoMBean {
return infoRegistry; return infoRegistry;
} }
private void initDeletionPolicy() { private void initDeletionPolicy() {
String className = solrConfig.get("mainIndex/deletionPolicy/@class", SolrDeletionPolicy.class.getName()); SolrConfig.PluginInfo info = solrConfig.getDeletionPolicyInfo();
IndexDeletionPolicy delPolicy = createInstance(className, IndexDeletionPolicy.class, "Deletion Policy for SOLR"); 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); private List<SolrEventListener> parseListener(List<SolrConfig.PluginInfo> path) {
if (node != null) {
if (delPolicy instanceof NamedListInitializedPlugin)
((NamedListInitializedPlugin) delPolicy).init(DOMUtil.childNodesToNamedList(node));
}
solrDelPolicy = new IndexDeletionPolicyWrapper(delPolicy);
}
public List<SolrEventListener> parseListener(String path) {
List<SolrEventListener> lst = new ArrayList<SolrEventListener>(); List<SolrEventListener> lst = new ArrayList<SolrEventListener>();
log.info( logid+"Searching for listeners: " +path); for (SolrConfig.PluginInfo info : path) {
NodeList nodes = (NodeList)solrConfig.evaluate(path, XPathConstants.NODESET); SolrEventListener listener = createEventListener(info.className);
if (nodes!=null) { listener.init(info.initArgs);
for (int i=0; i<nodes.getLength(); i++) { lst.add(listener);
Node node = nodes.item(i); log.info(logid + "Added SolrEventListener: " + listener);
String className = DOMUtil.getAttr(node,"class");
SolrEventListener listener = createEventListener(className);
listener.init(DOMUtil.childNodesToNamedList(node));
lst.add(listener);
log.info( logid+"Added SolrEventListener: " + listener);
}
} }
return lst; return lst;
} }
@ -280,8 +277,8 @@ public final class SolrCore implements SolrInfoMBean {
List<SolrEventListener> firstSearcherListeners; List<SolrEventListener> firstSearcherListeners;
List<SolrEventListener> newSearcherListeners; List<SolrEventListener> newSearcherListeners;
private void parseListeners() { private void parseListeners() {
firstSearcherListeners = parseListener("//listener[@event=\"firstSearcher\"]"); firstSearcherListeners = parseListener(solrConfig.getFirstSearcherListenerInfo());
newSearcherListeners = parseListener("//listener[@event=\"newSearcher\"]"); 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); 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<String, DirectoryFactory> registry = new HashMap<String, DirectoryFactory>();
NamedListPluginLoader<DirectoryFactory> indexReaderFactoryLoader = new NamedListPluginLoader<DirectoryFactory>(
"[solrconfig.xml] " + xpath, registry);
dirFactory = indexReaderFactoryLoader.loadSingle(solrConfig private void initDirectoryFactory() {
.getResourceLoader(), node); DirectoryFactory dirFactory;
SolrConfig.PluginInfo info = solrConfig.getDirectoryfactoryInfo();
if (info != null) {
dirFactory = (DirectoryFactory) getResourceLoader().newInstance(info.className);
dirFactory.init(info.initArgs);
} else { } else {
dirFactory = new StandardDirectoryFactory(); dirFactory = new StandardDirectoryFactory();
} }
// And set it // And set it
directoryFactory = dirFactory; directoryFactory = dirFactory;
} }
@ -402,17 +394,20 @@ public final class SolrCore implements SolrInfoMBean {
private <T extends Object> T createInstance(String className, Class<T> cast, String msg) { private <T extends Object> T createInstance(String className, Class<T> cast, String msg) {
Class clazz = null; Class clazz = null;
if (msg == null) msg = "SolrCore Object"; if (msg == null) msg = "SolrCore Object";
try { try {
try { clazz = getResourceLoader().findClass(className);
clazz = solrConfig.getResourceLoader().findClass(className);
if (cast != null && !cast.isAssignableFrom(clazz)) if (cast != null && !cast.isAssignableFrom(clazz))
throw new SolrException(SolrException.ErrorCode.SERVER_ERROR,"Error Instantiating "+msg+", "+className+ " is not a " +cast.getName()); throw new SolrException(SolrException.ErrorCode.SERVER_ERROR,"Error Instantiating "+msg+", "+className+ " is not a " +cast.getName());
//most of the classes do not have constructors whiuch take in SolrCore. It is recommended to obtain SolrCore by implementing SolrCoreAare.
java.lang.reflect.Constructor cons = clazz.getConstructor(new Class[]{SolrCore.class}); // So invariably always it will cause a NoSuchMethodException. So iterate though the list of available constructors
return (T) cons.newInstance(new Object[]{this}); Constructor[] cons = clazz.getConstructors();
} catch(NoSuchMethodException xnomethod) { for (Constructor con : cons) {
return (T) clazz.newInstance(); 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) { } catch (SolrException e) {
throw e; throw e;
} catch (Exception e) { } catch (Exception e) {
@ -830,14 +825,14 @@ public final class SolrCore implements SolrInfoMBean {
/** /**
* Register the default search components * Register the default search components
*/ */
private static Map<String, SearchComponent> loadSearchComponents( SolrConfig config ) private Map<String, SearchComponent> loadSearchComponents( SolrConfig config )
{ {
Map<String, SearchComponent> components = new HashMap<String, SearchComponent>(); Map<String, SearchComponent> components = new HashMap<String, SearchComponent>();
String xpath = "searchComponent"; String xpath = "searchComponent";
NamedListPluginLoader<SearchComponent> loader = new NamedListPluginLoader<SearchComponent>( xpath, components ); NamedListPluginLoader<SearchComponent> loader = new NamedListPluginLoader<SearchComponent>( xpath, components );
loader.load( config.getResourceLoader(), (NodeList)config.evaluate( xpath, XPathConstants.NODESET ) ); loader.load( config.getResourceLoader(), (NodeList)config.evaluate( xpath, XPathConstants.NODESET ) );
final Map<String,Class<? extends SearchComponent>> standardcomponents final Map<String,Class<? extends SearchComponent>> standardcomponents
= new HashMap<String, Class<? extends SearchComponent>>(); = new HashMap<String, Class<? extends SearchComponent>>();
standardcomponents.put( QueryComponent.COMPONENT_NAME, QueryComponent.class ); 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 /** Configure the query response writers. There will always be a default writer; additional
* writers may also be configured. */ * writers may also be configured. */
private void initWriters() { private void initWriters() {
String xpath = "queryResponseWriter"; defaultResponseWriter = initPlugins(solrConfig.getRespWriterInfo(), responseWriters, QueryResponseWriter.class);
NodeList nodes = (NodeList) solrConfig.evaluate(xpath, XPathConstants.NODESET);
NamedListPluginLoader<QueryResponseWriter> loader =
new NamedListPluginLoader<QueryResponseWriter>( "[solrconfig.xml] "+xpath, responseWriters );
defaultResponseWriter = loader.load( solrConfig.getResourceLoader(), nodes );
for (Map.Entry<String, QueryResponseWriter> entry : DEFAULT_RESPONSE_WRITERS.entrySet()) { for (Map.Entry<String, QueryResponseWriter> entry : DEFAULT_RESPONSE_WRITERS.entrySet()) {
if(responseWriters.get(entry.getKey()) == null) responseWriters.put(entry.getKey(), entry.getValue()); 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. */ /** Configure the query parsers. */
private void initQParsers() { private void initQParsers() {
String xpath = "queryParser"; initPlugins(solrConfig.getQueryParserInfo(),qParserPlugins,QParserPlugin.class);
NodeList nodes = (NodeList) solrConfig.evaluate(xpath, XPathConstants.NODESET);
NamedListPluginLoader<QParserPlugin> loader =
new NamedListPluginLoader<QParserPlugin>( "[solrconfig.xml] "+xpath, qParserPlugins);
loader.load( solrConfig.getResourceLoader(), nodes );
// default parsers // default parsers
for (int i=0; i<QParserPlugin.standardPlugins.length; i+=2) { for (int i=0; i<QParserPlugin.standardPlugins.length; i+=2) {
try { try {
@ -1489,14 +1471,7 @@ public final class SolrCore implements SolrInfoMBean {
/** Configure the ValueSource (function) plugins */ /** Configure the ValueSource (function) plugins */
private void initValueSourceParsers() { private void initValueSourceParsers() {
String xpath = "valueSourceParser"; initPlugins(solrConfig.getValueSourceParserInfo(),valueSourceParsers,ValueSourceParser.class);
NodeList nodes = (NodeList) solrConfig.evaluate(xpath, XPathConstants.NODESET);
NamedListPluginLoader<ValueSourceParser> loader =
new NamedListPluginLoader<ValueSourceParser>( "[solrconfig.xml] "+xpath, valueSourceParsers);
loader.load( solrConfig.getResourceLoader(), nodes );
// default value source parsers // default value source parsers
for (Map.Entry<String, ValueSourceParser> entry : ValueSourceParser.standardValueSourceParsers.entrySet()) { for (Map.Entry<String, ValueSourceParser> entry : ValueSourceParser.standardValueSourceParsers.entrySet()) {
try { try {
@ -1511,7 +1486,23 @@ public final class SolrCore implements SolrInfoMBean {
} }
} }
} }
private <T> T initPlugins(List<SolrConfig.PluginInfo> pluginInfos , Map<String ,T> registry, Class<T> 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) { public ValueSourceParser getValueSourceParser(String parserName) {
return valueSourceParsers.get(parserName); return valueSourceParsers.get(parserName);
} }