Search in sources :

Example 16 with EmbeddedSolrServer

use of org.apache.solr.client.solrj.embedded.EmbeddedSolrServer in project ddf by codice.

the class EmbeddedSolrFactory method getEmbeddedSolrServer.

/**
     * Creates a new {@link EmbeddedSolrServer} using the Solr core and configuration file names,
     * schema and configuration file proxy provided.
     *
     * @param coreName             name of the Solr core
     * @param solrConfigXml        name of the Solr configuration file. Defaults to
     *                             {@value HttpSolrClientFactory#DEFAULT_SOLRCONFIG_XML} if
     *                             {@code null}.
     * @param schemaXml            file name of the Solr core schema. Defaults to
     *                             {@value HttpSolrClientFactory#DEFAULT_SCHEMA_XML} if
     *                             {@code null}.
     * @param givenConfigFileProxy {@link ConfigurationFileProxy} instance to use. If {@code null},
     *                             a new {@link ConfigurationFileProxy} will be used.
     * @return a new {@link EmbeddedSolrServer} instance
     */
public static EmbeddedSolrServer getEmbeddedSolrServer(String coreName, @Nullable String solrConfigXml, @Nullable String schemaXml, @Nullable ConfigurationFileProxy givenConfigFileProxy) {
    LOGGER.debug("Retrieving embedded solr with the following properties: [{},{},{}]", solrConfigXml, schemaXml, givenConfigFileProxy);
    String solrConfigFileName = HttpSolrClientFactory.DEFAULT_SOLRCONFIG_XML;
    String schemaFileName = HttpSolrClientFactory.DEFAULT_SCHEMA_XML;
    if (isNotBlank(solrConfigXml)) {
        solrConfigFileName = solrConfigXml;
    }
    if (isNotBlank(schemaXml)) {
        schemaFileName = schemaXml;
    }
    ConfigurationFileProxy configProxy = givenConfigFileProxy;
    if (givenConfigFileProxy == null) {
        configProxy = new ConfigurationFileProxy(ConfigurationStore.getInstance());
    }
    configProxy.writeSolrConfiguration(coreName);
    File solrConfigFile = getConfigFile(solrConfigFileName, configProxy, coreName);
    File solrSchemaFile = getConfigFile(schemaFileName, configProxy, coreName);
    if (solrSchemaFile == null) {
        solrSchemaFile = getConfigFile("managed-schema", configProxy, coreName);
        if (solrSchemaFile == null) {
            throw new IllegalArgumentException("Unable to find Solr schema file.");
        }
    }
    File solrConfigHome = new File(solrConfigFile.getParent());
    ClassLoader tccl = Thread.currentThread().getContextClassLoader();
    try {
        Thread.currentThread().setContextClassLoader(EmbeddedSolrFactory.class.getClassLoader());
        // NamedSPILoader uses the thread context classloader to lookup
        // codecs, posting formats, and analyzers
        SolrConfig solrConfig = new SolrConfig(Paths.get(solrConfigHome.getParent()), solrConfigFileName, new InputSource(FileUtils.openInputStream(solrConfigFile)));
        IndexSchema indexSchema = new IndexSchema(solrConfig, schemaFileName, new InputSource(FileUtils.openInputStream(solrSchemaFile)));
        SolrResourceLoader loader = new SolrResourceLoader(Paths.get(solrConfigHome.getAbsolutePath()));
        SolrCoreContainer container = new SolrCoreContainer(loader);
        String dataDirPath = null;
        if (!ConfigurationStore.getInstance().isInMemory()) {
            File dataDir = configProxy.getDataDirectory();
            if (dataDir != null) {
                dataDirPath = Paths.get(dataDir.getAbsolutePath(), coreName, "data").toString();
                LOGGER.debug("Using data directory [{}]", dataDirPath);
            }
        } else {
            PluginInfo info = solrConfig.getPluginInfo(DirectoryFactory.class.getName());
            if (info != null && !"solr.RAMDirectoryFactory".equals(info.className)) {
                LOGGER.debug("Using in-memory configuration without RAMDirectoryFactory.");
            }
        }
        CoreDescriptor coreDescriptor = new CoreDescriptor(container, coreName, solrConfig.getResourceLoader().getInstancePath());
        SolrCore core = new SolrCore(coreName, dataDirPath, solrConfig, indexSchema, null, coreDescriptor, null, null, null);
        container.register(coreName, core, false, true);
        return new EmbeddedSolrServer(container, coreName);
    } catch (ParserConfigurationException | IOException | SAXException e) {
        throw new IllegalArgumentException("Unable to parse Solr configuration file: " + solrConfigFileName, e);
    } finally {
        Thread.currentThread().setContextClassLoader(tccl);
    }
}
Also used : SolrConfig(org.apache.solr.core.SolrConfig) InputSource(org.xml.sax.InputSource) CoreDescriptor(org.apache.solr.core.CoreDescriptor) SolrCore(org.apache.solr.core.SolrCore) IOException(java.io.IOException) SAXException(org.xml.sax.SAXException) SolrResourceLoader(org.apache.solr.core.SolrResourceLoader) DirectoryFactory(org.apache.solr.core.DirectoryFactory) IndexSchema(org.apache.solr.schema.IndexSchema) PluginInfo(org.apache.solr.core.PluginInfo) ParserConfigurationException(javax.xml.parsers.ParserConfigurationException) File(java.io.File) EmbeddedSolrServer(org.apache.solr.client.solrj.embedded.EmbeddedSolrServer)

Example 17 with EmbeddedSolrServer

use of org.apache.solr.client.solrj.embedded.EmbeddedSolrServer in project ddf by codice.

the class FilteringSolrIndex method createSolrServer.

private static EmbeddedSolrServer createSolrServer(String coreName, ConfigurationFileProxy configProxy) {
    File configFile = getConfigFile(IMMEMORY_SOLRCONFIG_XML, configProxy, coreName);
    if (configFile == null) {
        throw new IllegalArgumentException("Unable to find Solr configuration file");
    }
    File schemaFile = getConfigFile(DEFAULT_SCHEMA_XML, configProxy, coreName);
    if (schemaFile == null) {
        throw new IllegalArgumentException("Unable to find Solr schema file");
    }
    File solrConfigHome = new File(configFile.getParent());
    ClassLoader tccl = Thread.currentThread().getContextClassLoader();
    try {
        Thread.currentThread().setContextClassLoader(EmbeddedSolrFactory.class.getClassLoader());
        SolrConfig solrConfig = new SolrConfig(Paths.get(solrConfigHome.getParent()), IMMEMORY_SOLRCONFIG_XML, new InputSource(FileUtils.openInputStream(configFile)));
        if (indexSchema == null) {
            indexSchema = new IndexSchema(solrConfig, DEFAULT_SCHEMA_XML, new InputSource(FileUtils.openInputStream(schemaFile)));
        }
        SolrResourceLoader loader = new SolrResourceLoader(Paths.get(solrConfigHome.getAbsolutePath()));
        SolrCoreContainer container = new SolrCoreContainer(loader);
        CoreDescriptor coreDescriptor = new CoreDescriptor(container, coreName, solrConfig.getResourceLoader().getInstancePath());
        SolrCore core = new SolrCore(coreName, null, solrConfig, indexSchema, null, coreDescriptor, null, null, null);
        container.register(coreName, core, false, true);
        return new EmbeddedSolrServer(container, coreName);
    } catch (ParserConfigurationException | SAXException | IOException e) {
        throw new IllegalArgumentException("Unable to parse Solr configuration file", e);
    } finally {
        Thread.currentThread().setContextClassLoader(tccl);
    }
}
Also used : SolrConfig(org.apache.solr.core.SolrConfig) InputSource(org.xml.sax.InputSource) CoreDescriptor(org.apache.solr.core.CoreDescriptor) SolrCore(org.apache.solr.core.SolrCore) IOException(java.io.IOException) EmbeddedSolrFactory(org.codice.solr.factory.impl.EmbeddedSolrFactory) SAXException(org.xml.sax.SAXException) SolrResourceLoader(org.apache.solr.core.SolrResourceLoader) IndexSchema(org.apache.solr.schema.IndexSchema) ParserConfigurationException(javax.xml.parsers.ParserConfigurationException) SolrCoreContainer(org.codice.solr.factory.impl.SolrCoreContainer) EmbeddedSolrFactory.getConfigFile(org.codice.solr.factory.impl.EmbeddedSolrFactory.getConfigFile) File(java.io.File) EmbeddedSolrServer(org.apache.solr.client.solrj.embedded.EmbeddedSolrServer)

Aggregations

EmbeddedSolrServer (org.apache.solr.client.solrj.embedded.EmbeddedSolrServer)17 File (java.io.File)5 IOException (java.io.IOException)5 CoreContainer (org.apache.solr.core.CoreContainer)5 CoreDescriptor (org.apache.solr.core.CoreDescriptor)5 SolrCore (org.apache.solr.core.SolrCore)5 Test (org.junit.Test)4 HttpSolrClient (org.apache.solr.client.solrj.impl.HttpSolrClient)3 QueryResponse (org.apache.solr.client.solrj.response.QueryResponse)3 SolrInputDocument (org.apache.solr.common.SolrInputDocument)3 SolrResourceLoader (org.apache.solr.core.SolrResourceLoader)3 IndexSchema (org.apache.solr.schema.IndexSchema)3 FileOutputStream (java.io.FileOutputStream)2 InputStream (java.io.InputStream)2 ParserConfigurationException (javax.xml.parsers.ParserConfigurationException)2 EmbeddedSolrServerConfiguration (org.apache.jackrabbit.oak.plugins.index.solr.configuration.EmbeddedSolrServerConfiguration)2 SolrClient (org.apache.solr.client.solrj.SolrClient)2 SolrQuery (org.apache.solr.client.solrj.SolrQuery)2 SolrServer (org.apache.solr.client.solrj.SolrServer)2 ErrorTrackingConcurrentUpdateSolrClient (org.apache.solr.client.solrj.embedded.SolrExampleStreamingTest.ErrorTrackingConcurrentUpdateSolrClient)2