001: /**
002: * Licensed to the Apache Software Foundation (ASF) under one or more
003: * contributor license agreements. See the NOTICE file distributed with
004: * this work for additional information regarding copyright ownership.
005: * The ASF licenses this file to You under the Apache License, Version 2.0
006: * (the "License"); you may not use this file except in compliance with
007: * the License. You may obtain a copy of the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: */package org.apache.solr.core;
017:
018: import org.apache.solr.request.LocalSolrQueryRequest;
019: import org.apache.solr.request.SolrQueryRequest;
020: import org.apache.solr.util.NamedList;
021:
022: import org.xml.sax.SAXException;
023:
024: import javax.xml.parsers.ParserConfigurationException;
025:
026: import java.util.Collection;
027: import java.util.HashSet;
028: import java.util.StringTokenizer;
029: import java.io.IOException;
030: import java.io.InputStream;
031:
032: /**
033: * Provides a static reference to a Config object modeling the main
034: * configuration data for a a Solr instance -- typically found in
035: * "solrconfig.xml".
036: *
037: * @author yonik
038: * @version $Id: SolrConfig.java 535431 2007-05-05 01:04:08Z ryan $
039: */
040: public class SolrConfig {
041:
042: public static final String DEFAULT_CONF_FILE = "solrconfig.xml";
043:
044: /**
045: * Singleton containing all configuration.
046: */
047: public static Config config;
048:
049: /**
050: * Singleton keeping track of configuration errors
051: */
052: public static final Collection<Throwable> severeErrors = new HashSet<Throwable>();
053:
054: /**
055: * (Re)loads the static configuration information from the specified file.
056: *
057: * <p>
058: * This method is called implicitly on ClassLoad, but it may be
059: * called explicitly to change the Configuration used for the purpose
060: * of testing - in which case it should be called prior to initializing
061: * a SolrCore.
062: * </p>
063: *
064: * <p>
065: * This method should <b>only</b> be called for testing purposes.
066: * Because it modifies a singleton, it is not suitable for running
067: * multi-threaded tests.
068: * </p>
069: *
070: * @param file file name to load
071: * @see Config#openResource
072: */
073: public static synchronized void initConfig(String file)
074: throws ParserConfigurationException, IOException,
075: SAXException {
076:
077: InputStream is = Config.openResource(file);
078: config = new Config(file, is, "/config/");
079: is.close();
080: Config.log.info("Loaded SolrConfig: " + file);
081: }
082:
083: static {
084: try {
085: initConfig(DEFAULT_CONF_FILE);
086: } catch (Exception ee) {
087: severeErrors.add(ee);
088: throw new RuntimeException("Error in " + DEFAULT_CONF_FILE,
089: ee);
090: }
091: }
092:
093: /**
094: * Returns a Request object based on the admin/pingQuery section
095: * of the Solr config file.
096: */
097: public static SolrQueryRequest getPingQueryRequest(SolrCore core) {
098:
099: // TODO: check for nested tags and parse as a named list instead
100: String urlSnippet = config.get("admin/pingQuery", "").trim();
101:
102: StringTokenizer qtokens = new StringTokenizer(urlSnippet, "&");
103: String tok;
104: NamedList params = new NamedList();
105: while (qtokens.hasMoreTokens()) {
106: tok = qtokens.nextToken();
107: String[] split = tok.split("=", 2);
108: params.add(split[0], split[1]);
109: }
110: return new LocalSolrQueryRequest(core, params);
111: }
112: }
|