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 ){
Map<SolrConfig.PluginInfo,SolrRequestHandler> handlers = new HashMap<SolrConfig.PluginInfo,SolrRequestHandler>();
for (SolrConfig.PluginInfo info : config.reqHandlerInfo) {
for (SolrConfig.PluginInfo info : config.getReqHandlerInfo()) {
try {
SolrRequestHandler requestHandler;
if( info.startup != null ) {

View File

@ -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<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>();
NodeList nodes = (NodeList) evaluate("requestHandler", XPathConstants.NODESET);
NodeList nodes = (NodeList) evaluate(tag, XPathConstants.NODESET);
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) ;
}
@ -208,7 +225,16 @@ public class SolrConfig extends Config {
// default & main index configurations
public final SolrIndexConfig defaultIndexConfig;
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 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<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));
public PluginInfo(Node node, String err, List<String> 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<String> required, String err) {
return DOMUtil.getAttr(node, name, required.contains(name) ? err : null);
}
private String getVal(Node node, String name, List<String> 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<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.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<SolrEventListener> parseListener(String path) {
private List<SolrEventListener> parseListener(List<SolrConfig.PluginInfo> path) {
List<SolrEventListener> lst = new ArrayList<SolrEventListener>();
log.info( logid+"Searching for listeners: " +path);
NodeList nodes = (NodeList)solrConfig.evaluate(path, XPathConstants.NODESET);
if (nodes!=null) {
for (int i=0; i<nodes.getLength(); i++) {
Node node = nodes.item(i);
String className = DOMUtil.getAttr(node,"class");
SolrEventListener listener = createEventListener(className);
listener.init(DOMUtil.childNodesToNamedList(node));
lst.add(listener);
log.info( logid+"Added SolrEventListener: " + listener);
}
for (SolrConfig.PluginInfo info : path) {
SolrEventListener listener = createEventListener(info.className);
listener.init(info.initArgs);
lst.add(listener);
log.info(logid + "Added SolrEventListener: " + listener);
}
return lst;
}
@ -280,8 +277,8 @@ public final class SolrCore implements SolrInfoMBean {
List<SolrEventListener> firstSearcherListeners;
List<SolrEventListener> 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<String, DirectoryFactory> registry = new HashMap<String, DirectoryFactory>();
NamedListPluginLoader<DirectoryFactory> indexReaderFactoryLoader = new NamedListPluginLoader<DirectoryFactory>(
"[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 extends Object> T createInstance(String className, Class<T> 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<String, SearchComponent> loadSearchComponents( SolrConfig config )
private Map<String, SearchComponent> loadSearchComponents( SolrConfig config )
{
Map<String, SearchComponent> components = new HashMap<String, SearchComponent>();
String xpath = "searchComponent";
NamedListPluginLoader<SearchComponent> loader = new NamedListPluginLoader<SearchComponent>( xpath, components );
loader.load( config.getResourceLoader(), (NodeList)config.evaluate( xpath, XPathConstants.NODESET ) );
final Map<String,Class<? extends SearchComponent>> standardcomponents
= new HashMap<String, Class<? extends SearchComponent>>();
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<QueryResponseWriter> loader =
new NamedListPluginLoader<QueryResponseWriter>( "[solrconfig.xml] "+xpath, responseWriters );
defaultResponseWriter = loader.load( solrConfig.getResourceLoader(), nodes );
defaultResponseWriter = initPlugins(solrConfig.getRespWriterInfo(), responseWriters, QueryResponseWriter.class);
for (Map.Entry<String, QueryResponseWriter> 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<QParserPlugin> loader =
new NamedListPluginLoader<QParserPlugin>( "[solrconfig.xml] "+xpath, qParserPlugins);
loader.load( solrConfig.getResourceLoader(), nodes );
initPlugins(solrConfig.getQueryParserInfo(),qParserPlugins,QParserPlugin.class);
// default parsers
for (int i=0; i<QParserPlugin.standardPlugins.length; i+=2) {
try {
@ -1489,14 +1471,7 @@ public final class SolrCore implements SolrInfoMBean {
/** Configure the ValueSource (function) plugins */
private void initValueSourceParsers() {
String xpath = "valueSourceParser";
NodeList nodes = (NodeList) solrConfig.evaluate(xpath, XPathConstants.NODESET);
NamedListPluginLoader<ValueSourceParser> loader =
new NamedListPluginLoader<ValueSourceParser>( "[solrconfig.xml] "+xpath, valueSourceParsers);
loader.load( solrConfig.getResourceLoader(), nodes );
initPlugins(solrConfig.getValueSourceParserInfo(),valueSourceParsers,ValueSourceParser.class);
// default value source parsers
for (Map.Entry<String, ValueSourceParser> entry : ValueSourceParser.standardValueSourceParsers.entrySet()) {
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) {
return valueSourceParsers.get(parserName);
}