Search in sources :

Example 11 with SolrResourceLoader

use of org.apache.solr.core.SolrResourceLoader in project lucene-solr by apache.

the class ManagedIndexSchemaFactory method warnIfNonManagedSchemaExists.

/**
   * Return whether a non-managed schema exists, either in local storage or on ZooKeeper. 
   */
private void warnIfNonManagedSchemaExists() {
    if (!resourceName.equals(managedSchemaResourceName)) {
        boolean exists = false;
        SolrResourceLoader loader = config.getResourceLoader();
        if (loader instanceof ZkSolrResourceLoader) {
            ZkSolrResourceLoader zkLoader = (ZkSolrResourceLoader) loader;
            String nonManagedSchemaPath = zkLoader.getConfigSetZkPath() + "/" + resourceName;
            try {
                exists = zkLoader.getZkController().pathExists(nonManagedSchemaPath);
            } catch (InterruptedException e) {
                // Restore the interrupted status
                Thread.currentThread().interrupt();
                // Log as warning and suppress the exception 
                log.warn("", e);
            } catch (KeeperException e) {
                // log as warning and suppress the exception
                log.warn("Error checking for the existence of the non-managed schema " + resourceName, e);
            }
        } else {
            // Config is not in ZooKeeper
            InputStream nonManagedSchemaInputStream = null;
            try {
                nonManagedSchemaInputStream = loader.openSchema(resourceName);
                if (null != nonManagedSchemaInputStream) {
                    exists = true;
                }
            } catch (IOException e) {
            // This is expected when the non-managed schema does not exist
            } finally {
                IOUtils.closeQuietly(nonManagedSchemaInputStream);
            }
        }
        if (exists) {
            log.warn("The schema has been upgraded to managed, but the non-managed schema " + resourceName + " is still loadable.  PLEASE REMOVE THIS FILE.");
        }
    }
}
Also used : SolrResourceLoader(org.apache.solr.core.SolrResourceLoader) ZkSolrResourceLoader(org.apache.solr.cloud.ZkSolrResourceLoader) ByteArrayInputStream(java.io.ByteArrayInputStream) InputStream(java.io.InputStream) ZkSolrResourceLoader(org.apache.solr.cloud.ZkSolrResourceLoader) IOException(java.io.IOException) KeeperException(org.apache.zookeeper.KeeperException)

Example 12 with SolrResourceLoader

use of org.apache.solr.core.SolrResourceLoader in project lucene-solr by apache.

the class IndexSchemaFactory method create.

/** Returns an index schema created from a local resource */
public IndexSchema create(String resourceName, SolrConfig config) {
    SolrResourceLoader loader = config.getResourceLoader();
    InputStream schemaInputStream = null;
    if (null == resourceName) {
        resourceName = IndexSchema.DEFAULT_SCHEMA_FILE;
    }
    try {
        schemaInputStream = loader.openSchema(resourceName);
    } catch (Exception e) {
        final String msg = "Error loading schema resource " + resourceName;
        log.error(msg, e);
        throw new SolrException(ErrorCode.SERVER_ERROR, msg, e);
    }
    InputSource inputSource = new InputSource(schemaInputStream);
    inputSource.setSystemId(SystemIdResolver.createSystemIdFromResourceName(resourceName));
    IndexSchema schema = new IndexSchema(config, resourceName, inputSource);
    return schema;
}
Also used : SolrResourceLoader(org.apache.solr.core.SolrResourceLoader) InputSource(org.xml.sax.InputSource) InputStream(java.io.InputStream) SolrException(org.apache.solr.common.SolrException) SolrException(org.apache.solr.common.SolrException)

Example 13 with SolrResourceLoader

use of org.apache.solr.core.SolrResourceLoader in project lucene-solr by apache.

the class FieldTypePluginLoader method readAnalyzer.

//
// <analyzer><tokenizer class="...."/><tokenizer class="...." arg="....">
//
//
private Analyzer readAnalyzer(Node node) throws XPathExpressionException {
    final SolrResourceLoader loader = schema.getResourceLoader();
    if (node == null)
        return null;
    NamedNodeMap attrs = node.getAttributes();
    String analyzerName = DOMUtil.getAttr(attrs, "class");
    // check for all of these up front, so we can error if used in 
    // conjunction with an explicit analyzer class.
    NodeList charFilterNodes = (NodeList) xpath.evaluate("./charFilter", node, XPathConstants.NODESET);
    NodeList tokenizerNodes = (NodeList) xpath.evaluate("./tokenizer", node, XPathConstants.NODESET);
    NodeList tokenFilterNodes = (NodeList) xpath.evaluate("./filter", node, XPathConstants.NODESET);
    if (analyzerName != null) {
        // own custom nodes (ie: <description> or something like that)
        if (0 != charFilterNodes.getLength() || 0 != tokenizerNodes.getLength() || 0 != tokenFilterNodes.getLength()) {
            throw new SolrException(SolrException.ErrorCode.SERVER_ERROR, "Configuration Error: Analyzer class='" + analyzerName + "' can not be combined with nested analysis factories");
        }
        try {
            // No need to be core-aware as Analyzers are not in the core-aware list
            final Class<? extends Analyzer> clazz = loader.findClass(analyzerName, Analyzer.class);
            Analyzer analyzer = clazz.newInstance();
            final String matchVersionStr = DOMUtil.getAttr(attrs, LUCENE_MATCH_VERSION_PARAM);
            final Version luceneMatchVersion = (matchVersionStr == null) ? schema.getDefaultLuceneMatchVersion() : Config.parseLuceneVersionString(matchVersionStr);
            if (luceneMatchVersion == null) {
                throw new SolrException(SolrException.ErrorCode.SERVER_ERROR, "Configuration Error: Analyzer '" + clazz.getName() + "' needs a 'luceneMatchVersion' parameter");
            }
            analyzer.setVersion(luceneMatchVersion);
            return analyzer;
        } catch (Exception e) {
            log.error("Cannot load analyzer: " + analyzerName, e);
            throw new SolrException(SolrException.ErrorCode.SERVER_ERROR, "Cannot load analyzer: " + analyzerName, e);
        }
    }
    // Load the CharFilters
    final ArrayList<CharFilterFactory> charFilters = new ArrayList<>();
    AbstractPluginLoader<CharFilterFactory> charFilterLoader = new AbstractPluginLoader<CharFilterFactory>("[schema.xml] analyzer/charFilter", CharFilterFactory.class, false, false) {

        @Override
        protected CharFilterFactory create(SolrResourceLoader loader, String name, String className, Node node) throws Exception {
            final Map<String, String> params = DOMUtil.toMap(node.getAttributes());
            String configuredVersion = params.remove(LUCENE_MATCH_VERSION_PARAM);
            params.put(LUCENE_MATCH_VERSION_PARAM, parseConfiguredVersion(configuredVersion, CharFilterFactory.class.getSimpleName()).toString());
            CharFilterFactory factory = loader.newInstance(className, CharFilterFactory.class, getDefaultPackages(), new Class[] { Map.class }, new Object[] { params });
            factory.setExplicitLuceneMatchVersion(null != configuredVersion);
            return factory;
        }

        @Override
        protected void init(CharFilterFactory plugin, Node node) throws Exception {
            if (plugin != null) {
                charFilters.add(plugin);
            }
        }

        @Override
        protected CharFilterFactory register(String name, CharFilterFactory plugin) {
            // used for map registration
            return null;
        }
    };
    charFilterLoader.load(loader, charFilterNodes);
    // Load the Tokenizer
    // Although an analyzer only allows a single Tokenizer, we load a list to make sure
    // the configuration is ok
    final ArrayList<TokenizerFactory> tokenizers = new ArrayList<>(1);
    AbstractPluginLoader<TokenizerFactory> tokenizerLoader = new AbstractPluginLoader<TokenizerFactory>("[schema.xml] analyzer/tokenizer", TokenizerFactory.class, false, false) {

        @Override
        protected TokenizerFactory create(SolrResourceLoader loader, String name, String className, Node node) throws Exception {
            final Map<String, String> params = DOMUtil.toMap(node.getAttributes());
            String configuredVersion = params.remove(LUCENE_MATCH_VERSION_PARAM);
            params.put(LUCENE_MATCH_VERSION_PARAM, parseConfiguredVersion(configuredVersion, TokenizerFactory.class.getSimpleName()).toString());
            TokenizerFactory factory = loader.newInstance(className, TokenizerFactory.class, getDefaultPackages(), new Class[] { Map.class }, new Object[] { params });
            factory.setExplicitLuceneMatchVersion(null != configuredVersion);
            return factory;
        }

        @Override
        protected void init(TokenizerFactory plugin, Node node) throws Exception {
            if (!tokenizers.isEmpty()) {
                throw new SolrException(SolrException.ErrorCode.SERVER_ERROR, "The schema defines multiple tokenizers for: " + node);
            }
            tokenizers.add(plugin);
        }

        @Override
        protected TokenizerFactory register(String name, TokenizerFactory plugin) {
            // used for map registration
            return null;
        }
    };
    tokenizerLoader.load(loader, tokenizerNodes);
    // Make sure something was loaded
    if (tokenizers.isEmpty()) {
        throw new SolrException(SolrException.ErrorCode.SERVER_ERROR, "analyzer without class or tokenizer");
    }
    // Load the Filters
    final ArrayList<TokenFilterFactory> filters = new ArrayList<>();
    AbstractPluginLoader<TokenFilterFactory> filterLoader = new AbstractPluginLoader<TokenFilterFactory>("[schema.xml] analyzer/filter", TokenFilterFactory.class, false, false) {

        @Override
        protected TokenFilterFactory create(SolrResourceLoader loader, String name, String className, Node node) throws Exception {
            final Map<String, String> params = DOMUtil.toMap(node.getAttributes());
            String configuredVersion = params.remove(LUCENE_MATCH_VERSION_PARAM);
            params.put(LUCENE_MATCH_VERSION_PARAM, parseConfiguredVersion(configuredVersion, TokenFilterFactory.class.getSimpleName()).toString());
            TokenFilterFactory factory = loader.newInstance(className, TokenFilterFactory.class, getDefaultPackages(), new Class[] { Map.class }, new Object[] { params });
            factory.setExplicitLuceneMatchVersion(null != configuredVersion);
            return factory;
        }

        @Override
        protected void init(TokenFilterFactory plugin, Node node) throws Exception {
            if (plugin != null) {
                filters.add(plugin);
            }
        }

        @Override
        protected TokenFilterFactory register(String name, TokenFilterFactory plugin) throws Exception {
            // used for map registration
            return null;
        }
    };
    filterLoader.load(loader, tokenFilterNodes);
    return new TokenizerChain(charFilters.toArray(new CharFilterFactory[charFilters.size()]), tokenizers.get(0), filters.toArray(new TokenFilterFactory[filters.size()]));
}
Also used : AbstractPluginLoader(org.apache.solr.util.plugin.AbstractPluginLoader) NamedNodeMap(org.w3c.dom.NamedNodeMap) TokenizerFactory(org.apache.lucene.analysis.util.TokenizerFactory) KeywordTokenizerFactory(org.apache.lucene.analysis.core.KeywordTokenizerFactory) NodeList(org.w3c.dom.NodeList) CharFilterFactory(org.apache.lucene.analysis.util.CharFilterFactory) Node(org.w3c.dom.Node) ArrayList(java.util.ArrayList) KeywordAnalyzer(org.apache.lucene.analysis.core.KeywordAnalyzer) Analyzer(org.apache.lucene.analysis.Analyzer) XPathExpressionException(javax.xml.xpath.XPathExpressionException) SolrException(org.apache.solr.common.SolrException) TokenFilterFactory(org.apache.lucene.analysis.util.TokenFilterFactory) SolrResourceLoader(org.apache.solr.core.SolrResourceLoader) TokenizerChain(org.apache.solr.analysis.TokenizerChain) Version(org.apache.lucene.util.Version) SolrException(org.apache.solr.common.SolrException)

Example 14 with SolrResourceLoader

use of org.apache.solr.core.SolrResourceLoader in project lucene-solr by apache.

the class SolrDispatchFilter method loadNodeConfig.

/**
   * Get the NodeConfig whether stored on disk, in ZooKeeper, etc.
   * This may also be used by custom filters to load relevant configuration.
   * @return the NodeConfig
   */
public static NodeConfig loadNodeConfig(Path solrHome, Properties nodeProperties) {
    SolrResourceLoader loader = new SolrResourceLoader(solrHome, null, nodeProperties);
    if (!StringUtils.isEmpty(System.getProperty("solr.solrxml.location"))) {
        log.warn("Solr property solr.solrxml.location is no longer supported. " + "Will automatically load solr.xml from ZooKeeper if it exists");
    }
    String zkHost = System.getProperty("zkHost");
    if (!StringUtils.isEmpty(zkHost)) {
        try (SolrZkClient zkClient = new SolrZkClient(zkHost, 30000)) {
            if (zkClient.exists("/solr.xml", true)) {
                log.info("solr.xml found in ZooKeeper. Loading...");
                byte[] data = zkClient.getData("/solr.xml", null, null, true);
                return SolrXmlConfig.fromInputStream(loader, new ByteArrayInputStream(data));
            }
        } catch (Exception e) {
            throw new SolrException(ErrorCode.SERVER_ERROR, "Error occurred while loading solr.xml from zookeeper", e);
        }
        log.info("Loading solr.xml from SolrHome (not found in ZooKeeper)");
    }
    return SolrXmlConfig.fromSolrHome(loader, loader.getInstancePath());
}
Also used : SolrResourceLoader(org.apache.solr.core.SolrResourceLoader) ByteArrayInputStream(java.io.ByteArrayInputStream) SolrZkClient(org.apache.solr.common.cloud.SolrZkClient) ServletException(javax.servlet.ServletException) SolrException(org.apache.solr.common.SolrException) IOException(java.io.IOException) UnavailableException(javax.servlet.UnavailableException) SolrException(org.apache.solr.common.SolrException)

Example 15 with SolrResourceLoader

use of org.apache.solr.core.SolrResourceLoader in project lucene-solr by apache.

the class SolrTestCaseJ4 method createCoreContainer.

public static CoreContainer createCoreContainer(String coreName, String dataDir, String solrConfig, String schema) {
    NodeConfig nodeConfig = TestHarness.buildTestNodeConfig(new SolrResourceLoader(TEST_PATH()));
    CoresLocator locator = new TestHarness.TestCoresLocator(coreName, dataDir, solrConfig, schema);
    CoreContainer cc = createCoreContainer(nodeConfig, locator);
    h.coreName = coreName;
    return cc;
}
Also used : SolrResourceLoader(org.apache.solr.core.SolrResourceLoader) CoreContainer(org.apache.solr.core.CoreContainer) CoresLocator(org.apache.solr.core.CoresLocator) NodeConfig(org.apache.solr.core.NodeConfig)

Aggregations

SolrResourceLoader (org.apache.solr.core.SolrResourceLoader)54 Test (org.junit.Test)18 File (java.io.File)11 InputStream (java.io.InputStream)11 NamedList (org.apache.solr.common.util.NamedList)10 SolrException (org.apache.solr.common.SolrException)9 NodeConfig (org.apache.solr.core.NodeConfig)9 Path (java.nio.file.Path)8 CoreContainer (org.apache.solr.core.CoreContainer)8 InputStreamReader (java.io.InputStreamReader)7 Reader (java.io.Reader)7 Map (java.util.Map)7 IOException (java.io.IOException)6 HashMap (java.util.HashMap)5 LinkedHashMap (java.util.LinkedHashMap)5 XMLResponseParser (org.apache.solr.client.solrj.impl.XMLResponseParser)5 SolrCore (org.apache.solr.core.SolrCore)5 ArrayList (java.util.ArrayList)4 ZkSolrResourceLoader (org.apache.solr.cloud.ZkSolrResourceLoader)4 SolrConfig (org.apache.solr.core.SolrConfig)4