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.search;
017:
018: import org.w3c.dom.Node;
019: import org.w3c.dom.NodeList;
020:
021: import java.util.Map;
022:
023: import org.apache.solr.util.DOMUtil;
024: import org.apache.solr.core.SolrException;
025: import org.apache.solr.core.SolrConfig;
026: import org.apache.solr.core.Config;
027:
028: import javax.xml.xpath.XPathConstants;
029:
030: /**
031: * Contains the knowledge of how cache config is
032: * stored in the solrconfig.xml file, and implements a
033: * factory to create caches.
034: *
035: * @author yonik
036: * @version $Id: CacheConfig.java 472574 2006-11-08 18:25:52Z yonik $
037: */
038: class CacheConfig {
039: private String nodeName;
040: private Map args;
041:
042: private String cacheImpl;
043: private Class clazz;
044:
045: private Object[] persistence = new Object[1];
046:
047: private String regenImpl;
048: private CacheRegenerator regenerator;
049:
050: public CacheRegenerator getRegenerator() {
051: return regenerator;
052: }
053:
054: public void setRegenerator(CacheRegenerator regenerator) {
055: this .regenerator = regenerator;
056: }
057:
058: public static CacheConfig[] getMultipleConfigs(String configPath) {
059: NodeList nodes = (NodeList) SolrConfig.config.evaluate(
060: configPath, XPathConstants.NODESET);
061: if (nodes == null || nodes.getLength() == 0)
062: return null;
063: CacheConfig[] configs = new CacheConfig[nodes.getLength()];
064: for (int i = 0; i < nodes.getLength(); i++) {
065: configs[i] = getConfig(nodes.item(i));
066: }
067: return configs;
068: }
069:
070: public static CacheConfig getConfig(String xpath) {
071: Node node = (Node) SolrConfig.config.getNode(xpath, false);
072: return getConfig(node);
073: }
074:
075: public static CacheConfig getConfig(Node node) {
076: if (node == null)
077: return null;
078: CacheConfig config = new CacheConfig();
079: config.nodeName = node.getNodeName();
080: config.args = DOMUtil.toMap(node.getAttributes());
081: String nameAttr = (String) config.args.get("name"); // OPTIONAL
082: if (nameAttr == null) {
083: config.args.put("name", config.nodeName);
084: }
085:
086: config.cacheImpl = (String) config.args.get("class");
087: config.regenImpl = (String) config.args.get("regenerator");
088: config.clazz = Config.findClass(config.cacheImpl);
089: if (config.regenImpl != null) {
090: config.regenerator = (CacheRegenerator) Config
091: .newInstance(config.regenImpl);
092: }
093:
094: return config;
095: }
096:
097: public SolrCache newInstance() {
098: try {
099: SolrCache cache = (SolrCache) clazz.newInstance();
100: persistence[0] = cache.init(args, persistence[0],
101: regenerator);
102: return cache;
103: } catch (Exception e) {
104: SolrException.log(SolrCache.log,
105: "Error instantiating cache", e);
106: // we can carry on without a cache... but should we?
107: // in some cases (like an OOM) we probably should try to continue.
108: return null;
109: }
110: }
111:
112: }
|