Search in sources :

Example 16 with CoreDescriptor

use of org.apache.solr.core.CoreDescriptor in project stanbol by apache.

the class RegisteredSolrServerTracker method addingService.

@Override
public SolrServer addingService(ServiceReference reference) {
    log.info(" ... in addingService for {} (ref: {})", this.reference, reference);
    String coreName;
    CoreContainer server;
    Object service = super.addingService(reference);
    if (service == null) {
        log.warn("addingService({}) returned null -> unable to create " + "EmbeddedSolrServer for IndexReference {}", reference, this.reference);
    }
    if (trackingSolrCore) {
        SolrCore core;
        //might not be the same as for this classloader
        if (service instanceof SolrCore) {
            core = (SolrCore) service;
        } else {
            log.warn("SolrCore fitting to IndexReference {} is incompatible to the" + "classloader of bundle [{}]{} - Service [{}] ignored!", new Object[] { reference, context.getBundle().getBundleId(), context.getBundle().getSymbolicName() }, reference.getProperty(Constants.SERVICE_ID));
            context.ungetService(reference);
            return null;
        }
        coreName = core.getName();
        CoreDescriptor descriptor = core.getCoreDescriptor();
        if (descriptor == null) {
            //core not registered with a container!
            context.ungetService(reference);
            //ignore
            return null;
        } else {
            server = descriptor.getCoreContainer();
        }
    } else {
        if (service instanceof CoreContainer) {
            server = (CoreContainer) service;
        } else {
            log.warn("Solr CoreContainer fitting to IndexReference {} is incompatible to the" + "classloader of bundle [{}]{} - Service [{}] ignored!", new Object[] { reference, context.getBundle().getBundleId(), context.getBundle().getSymbolicName() }, reference.getProperty(Constants.SERVICE_ID));
            context.ungetService(reference);
            return null;
        }
        coreName = this.coreName;
    }
    return new EmbeddedSolrServer(server, coreName);
}
Also used : CoreContainer(org.apache.solr.core.CoreContainer) SolrCore(org.apache.solr.core.SolrCore) CoreDescriptor(org.apache.solr.core.CoreDescriptor) EmbeddedSolrServer(org.apache.solr.client.solrj.embedded.EmbeddedSolrServer)

Example 17 with CoreDescriptor

use of org.apache.solr.core.CoreDescriptor in project stanbol by apache.

the class SolrServerAdapter method registerCore.

/**
     * Registers a SolrCore based on the parsed configuration. If a SolrCore
     * with the same name as provided by the configuration is already present
     * it will be replace by this one.
     * @param parsedCoreConfig The configuration.
     * @return The ServiceReference to the SolrCore.
     * @throws SolrException If the core could not be registered
     */
public ServiceReference registerCore(SolrCoreProperties parsedCoreConfig) {
    SolrCoreProperties coreConfig = parsedCoreConfig.clone();
    String coreName = coreConfig.getCoreName();
    log.info("Register Core {} to SolrServerAdapter (coreContainer: {})", coreName, serverProperties.getServerName());
    if (coreName == null) {
        coreName = server.getDefaultCoreName();
    }
    File coreDir = coreConfig.getCoreDir();
    if (coreDir == null) {
        coreDir = new File(serverProperties.getServerDir(), coreName);
    }
    //SolrCore old = null;
    ClassLoader classLoader = updateContextClassLoader();
    SolrCore core;
    try {
        //NOTE: this code depends on the fact that the create method of the
        //      CoreContainer is overridden by the SolrServerAdapter
        //      to use the OSGI specific SolrResourceLoader!
        core = server.create(new CoreDescriptor(server, coreName, coreDir.getPath()));
        //CloseHook is now appied by the overridden registerCore(..) method
        //of the wrapped CoreContainer!
        //core.addCloseHook(closeHook);
        // parse ture as third argument to avoid closing the current core for now
        //old = 
        server.register(coreName, core, true);
    } finally {
        Thread.currentThread().setContextClassLoader(classLoader);
    }
    //NOTE: core registration is now done as part of the registration of the
    //      SolrCore to the CoreContainer
    //        ServiceReference coreRef = registerCoreService(coreName,core);
    //        if(old != null){
    //            //cleanup the old core
    //            cleanupSolrCore(old);
    //        }
    //        // persist the new core to have it available on the next start
    //        //server.persist();
    //        //update the OSGI service is now done by the overridden CoreContainer#create(..)
    //        //method
    //        updateServerRegistration();
    //so just get the ServiceReference for the ServiceRegistration
    CoreRegistration reg = registrations.get(coreName);
    if (reg == null) {
        throw new IllegalStateException("No OSGI ServiceRegistration present after " + "adding SolrCore '" + coreName + "' to SolrCore!");
    } else {
        return reg.getServiceReference();
    }
}
Also used : SolrCore(org.apache.solr.core.SolrCore) CoreDescriptor(org.apache.solr.core.CoreDescriptor) File(java.io.File)

Example 18 with CoreDescriptor

use of org.apache.solr.core.CoreDescriptor in project stanbol by apache.

the class StandaloneManagedSolrServer method registerCore.

/**
     * registers a {@link SolrCore} to the {@link #server} managed by this
     * instance. Will replace an already existing {@link SolrCore} with the 
     * same name
     * @param coreName the name of the {@link SolrCore} to register
     * @param coreDir the directory for the Core. If <code>null</code> is parsed
     * {@link #managedSolrDir}/coreName is used as default.
     */
private void registerCore(String coreName, File coreDir) {
    if (coreName == null) {
        coreName = server.getDefaultCoreName();
    }
    if (coreDir == null) {
        //use the coreName as default
        coreDir = new File(managedSolrDir, coreName);
    }
    if (!coreDir.isDirectory()) {
        throw new IllegalArgumentException("The Core Directory '" + coreDir + " for the Core '" + coreName + "' does not exist or is not an directory");
    }
    SolrCore core;
    CoreDescriptor coreDescriptor = new CoreDescriptor(server, coreName, coreDir.getAbsolutePath());
    core = server.create(coreDescriptor);
    //this will also replace an existing core with the same name
    server.register(coreName, core, false);
    //store the new/updated SolrCore in the solr.xml
    server.persist();
}
Also used : SolrCore(org.apache.solr.core.SolrCore) CoreDescriptor(org.apache.solr.core.CoreDescriptor) File(java.io.File)

Example 19 with CoreDescriptor

use of org.apache.solr.core.CoreDescriptor in project stanbol by apache.

the class SolrYardIndexingDestination method initialise.

@Override
public void initialise() {
    log.info("initialise {}", getClass().getSimpleName());
    //The constructors and the setConfiguration(..) only validate the parsed
    //parameters and initialise the member variables. This method performs 
    //the the actual initialisation of the SolrYard!
    //copy a custom configuration (if present)
    EmbeddedSolrServer server;
    IndexReference solrServerRef = IndexReference.parse(solrYardConfig.getSolrServerLocation());
    if (solrIndexConfig != null) {
        //copy the configuration
        try {
            log.info(" ... copy Solr Configuration form {} to {}", solrIndexConfig, solrIndexLocation);
            FileUtils.copyDirectory(solrIndexConfig, solrIndexLocation);
        } catch (IOException e) {
            throw new IllegalStateException(String.format("Unable to copy the Solr index configuration from %s to %s!", solrIndexConfig, solrIndexLocation), e);
        }
        solrYardConfig.setAllowInitialisation(Boolean.FALSE);
        server = StandaloneEmbeddedSolrServerProvider.getInstance().getSolrServer(solrServerRef, solrServerRef.getIndex());
        this.core = server.getCoreContainer().getCore(solrServerRef.getIndex());
    } else {
        //allow the default initialisation
        solrYardConfig.setAllowInitialisation(Boolean.TRUE);
        StandaloneEmbeddedSolrServerProvider.getInstance();
        server = StandaloneEmbeddedSolrServerProvider.getInstance().getSolrServer(solrServerRef, solrYardConfig.getIndexConfigurationName());
        if (server != null) {
            log.info("   ... initialised SolrCore with default configuration");
            this.core = server.getCoreContainer().getCore(solrServerRef.getIndex());
        } else if (solrServerRef.isPath() && new File(solrServerRef.getIndex()).isAbsolute()) {
            //the parsed absolute path is not within the managed SolrServer
            //so we need to create some CoreContainer and init/register
            //the core at the parsed location
            StandaloneManagedSolrServer s;
            if (solrServerRef.getServer() == null) {
                s = StandaloneManagedSolrServer.getManagedServer();
            } else {
                s = StandaloneManagedSolrServer.getManagedServer(solrServerRef.getServer());
            }
            CoreContainer cc = s.getCoreContainer();
            CoreDescriptor cd = new CoreDescriptor(cc, "dummy", solrServerRef.getIndex());
            this.core = cc.create(cd);
            cc.register(core, false);
            server = new EmbeddedSolrServer(cc, "dummy");
            log.info("   ... initialised existing SolrCore at {}", solrServerRef.getIndex());
        } else {
            throw new IllegalStateException("Unable to initialise SolrCore " + solrServerRef);
        }
    }
    log.info("   ... create SolrYard");
    this.solrYard = new SolrYard(server, solrYardConfig, namespacePrefixService);
}
Also used : CoreContainer(org.apache.solr.core.CoreContainer) CoreDescriptor(org.apache.solr.core.CoreDescriptor) StandaloneManagedSolrServer(org.apache.stanbol.commons.solr.managed.standalone.StandaloneManagedSolrServer) IOException(java.io.IOException) EmbeddedSolrServer(org.apache.solr.client.solrj.embedded.EmbeddedSolrServer) IndexReference(org.apache.stanbol.commons.solr.IndexReference) File(java.io.File) SolrYard(org.apache.stanbol.entityhub.yard.solr.impl.SolrYard)

Example 20 with CoreDescriptor

use of org.apache.solr.core.CoreDescriptor 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)

Aggregations

CoreDescriptor (org.apache.solr.core.CoreDescriptor)21 CoreContainer (org.apache.solr.core.CoreContainer)11 SolrCore (org.apache.solr.core.SolrCore)11 File (java.io.File)7 IOException (java.io.IOException)6 EmbeddedSolrServer (org.apache.solr.client.solrj.embedded.EmbeddedSolrServer)5 SolrException (org.apache.solr.common.SolrException)4 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)3 NamedList (org.apache.solr.common.util.NamedList)3 SolrResourceLoader (org.apache.solr.core.SolrResourceLoader)3 Path (java.nio.file.Path)2 HashMap (java.util.HashMap)2 List (java.util.List)2 CountDownLatch (java.util.concurrent.CountDownLatch)2 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)2 ParserConfigurationException (javax.xml.parsers.ParserConfigurationException)2 CloudDescriptor (org.apache.solr.cloud.CloudDescriptor)2 ZkController (org.apache.solr.cloud.ZkController)2 ClusterState (org.apache.solr.common.cloud.ClusterState)2 Slice (org.apache.solr.common.cloud.Slice)2