Search in sources :

Example 1 with SolrServerProperties

use of org.apache.stanbol.commons.solr.SolrServerAdapter.SolrServerProperties in project stanbol by apache.

the class ManagedSolrServerImpl method activate.

@Activate
protected void activate(ComponentContext context) throws ConfigurationException {
    log.info("Activate ManagedSolrServer:");
    //        this.context = context;
    BundleContext bc = context.getBundleContext();
    //first parse the configured Servername
    Object value = context.getProperties().get(PROPERTY_SERVER_NAME);
    if (value == null || value.toString().isEmpty()) {
        throw new ConfigurationException(PROPERTY_SERVER_NAME, "The Server Name is a required" + "Configuration and MUST NOT be NULL nor empty!");
    } else {
        serverName = value.toString();
        log.info(" > Name = {}", value.toString());
    }
    value = context.getProperties().get(MANAGED_SOLR_DIR_PROPERTY);
    if (value == null || value.toString().isEmpty()) {
        managedSolrDir = new File(FilenameUtils.separatorsToSystem(substituteProperty(DEFAULT_ROOT_PATH, bc)), serverName);
    } else {
        //note that property substitution is used on the parsed 
        //PROPERTY_SERVER_DIR value
        managedSolrDir = new File(FilenameUtils.separatorsToSystem(substituteProperty(value.toString(), bc)));
        if (!managedSolrDir.isAbsolute()) {
            managedSolrDir = new File(DEFAULT_ROOT_PATH, //make sure to convert '/' and '\' to the platform separator
            FilenameUtils.separatorsToSystem(value.toString()));
        }
    }
    log.info(" > managedDir = {}", managedSolrDir.getAbsolutePath());
    if (managedSolrDir.isFile()) {
        throw new ConfigurationException(PROPERTY_SERVER_DIR, String.format("The configured managed directory '%s'(dir: %s|name:%s) " + "exists but is no Directory!", managedSolrDir.getAbsolutePath(), value, serverName));
    }
    // check if the "solr.xml" file exists in the directory
    File solrConf = new File(managedSolrDir, "solr.xml");
    if (!solrConf.exists()) {
        log.info("   ... initialise managed directory '{}'", managedSolrDir);
        try {
            managedSolrDir = ConfigUtils.copyDefaultConfig(bc.getBundle(), managedSolrDir, false);
        } catch (IOException e) {
            throw new IllegalStateException(String.format("Unable to copy default configuration for the manages Solr Directory " + "to the configured path '%s'!", managedSolrDir.getAbsoluteFile()), e);
        }
    } else {
        log.info("   .... managed directory '{}' already present and initialised", managedSolrDir);
    }
    //init the SolrServerProperties and read the other parameters form the config
    SolrServerProperties serverProperties = new SolrServerProperties(managedSolrDir);
    serverProperties.setServerName(serverName);
    value = context.getProperties().get(PROPERTY_SERVER_RANKING);
    if (value instanceof Number) {
        serverProperties.setServerRanking(((Number) value).intValue());
    } else if (value != null && !value.toString().isEmpty()) {
        try {
            serverProperties.setServerRanking(Integer.parseInt(value.toString()));
            log.info(" > Ranking = {}", serverProperties.getServerRanking());
        } catch (NumberFormatException e) {
            throw new ConfigurationException(PROPERTY_SERVER_RANKING, "The configured Server Ranking '" + value + " can not be converted to an Integer!", e);
        }
    }
    //else not present or empty string -> do not set a ranking!
    value = context.getProperties().get(PROPERTY_SERVER_PUBLISH_REST);
    if (value == null || value instanceof Boolean) {
        serverProperties.setPublishREST((Boolean) value);
    } else {
        serverProperties.setPublishREST(Boolean.parseBoolean(value.toString()));
    }
    try {
        server = new SolrServerAdapter(context.getBundleContext(), serverProperties);
    } catch (SolrException e) {
        throw new ConfigurationException(PROPERTY_SERVER_DIR, "Unable to initialise " + "a SolrServer based on the Directory '" + serverProperties.getServerDir() + "'!", e);
    }
    //        dfpServiceRegistration = context.getBundleContext().registerService(
    //            DataFileProvider.class.getName(), 
    //            new ClassPathSolrIndexConfigProvider(
    //                context.getBundleContext().getBundle().getSymbolicName()), null);
    managedCores = new ManagedIndexMetadata(context);
    //After a restart of the CoreContainer we need to synchronise the state of
    //The cores with the state in the configs.
    //This may result in the activation of missing SolrCores as well as the
    //deactivation of unknown or inactive cores. It may also need to
    //change the state in the configuration in case a user has manually fixed
    //encountered problems while this service was deactivated.
    Collection<String> activeByMetadata = managedCores.getInState(ManagedIndexState.ACTIVE);
    Collection<String> activeOnSolrServer = new HashSet<String>(server.getCores());
    activeOnSolrServer.removeAll(activeByMetadata);
    activeByMetadata.removeAll(server.getCores());
    //(1) Try to activate missing cores on the CoreContainer
    if (!activeByMetadata.isEmpty()) {
        log.info("The following active managed Cores are not available on " + "the SolrServer: {}", activeByMetadata);
        for (String indexName : activeByMetadata) {
            IndexMetadata metadata = managedCores.getIndexMetadata(indexName);
            try {
                activateCore(metadata, server);
                log.info("  ... index {} successfully started!", indexName);
            } catch (IOException e) {
                metadata.setError(e);
                log.error("Unable to activate previously active SolrIndex '" + metadata.getIndexReference() + "'!", e);
            } catch (SAXException e) {
                metadata.setError(e);
                log.error("Unable to activate previously active SolrIndex '" + metadata.getIndexReference() + "'!", e);
            } catch (RuntimeException e) {
                metadata.setError(e);
                log.error("Unable to activate previously active SolrIndex '" + metadata.getIndexReference() + "'!", e);
            //} finally { The metadata are not modified anyway!
            //    managedCores.store(metadata);
            }
        }
    }
    //    based on the configuration
    if (!activeOnSolrServer.isEmpty()) {
        log.info("The following Cores active on the SolrServer are not " + "marked as active in the Metadata: {}", activeOnSolrServer);
        log.info("Based on the Metadata (UNKNOWN ... no Index for that name):");
        for (String indexName : activeOnSolrServer) {
            IndexMetadata metadata = managedCores.getIndexMetadata(indexName);
            ManagedIndexState state = metadata != null ? metadata.getState() : null;
            log.info("   - {} has state {}", indexName, state != null ? state : "UNKNOWN");
            if (metadata == null) {
                //unknown core ... deactivate
                deactivateCore(indexName, server);
                log.info("  ... deactiaved UNKOWN SolrCore {} on managed Solr Server {}", indexName, serverName);
            } else if (state == ManagedIndexState.INACTIVE) {
                ////the metadata way this core should be deactivated!
                deactivateCore(indexName, server);
                log.info("  ... deactiaved INACTIVE SolrCore {} on managed Solr Server {}", indexName, serverName);
            } else if (state == ManagedIndexState.ERROR) {
                //looks like that the error was resolved ...
                // ... maybe someone has manually edited some files and 
                //     restarted this server
                metadata.setState(ManagedIndexState.ACTIVE);
                managedCores.store(metadata);
                log.info("  ... successfully ACTIVATED SolrCore {} on managed Solr Server {}", indexName, serverName);
            } else if (state == ManagedIndexState.UNINITIALISED) {
                //looks like someone has copied the required files manually
                //to the solrServer ... update the metadata an activate
                ManagementUtils.updateMetadata(metadata, server.getCore(indexName));
                metadata.setState(ManagedIndexState.ACTIVE);
                managedCores.store(metadata);
                log.info("  ... successfully ACTIVATED SolrCore {} on managed Solr Server {}", indexName, serverName);
            }
        }
    }
    //now init uninitialised cores and dataFile tracking for those
    //(1) start the daemon that asyc updates cores on DataFileListener events
    updateDaemon = new IndexUpdateDaemon();
    //start the thread
    updateDaemon.start();
    //(2) init IndexArhive tracking
    indexArchiveTracker = new IndexArchiveTracker(dataFileTracker, dataFileProvider, managedCores, updateDaemon);
    log.info("   ... Managed SolrServer '{}' successfully initialised!", serverName);
}
Also used : SolrServerProperties(org.apache.stanbol.commons.solr.SolrServerAdapter.SolrServerProperties) IOException(java.io.IOException) SAXException(org.xml.sax.SAXException) ConfigurationException(org.osgi.service.cm.ConfigurationException) ParserConfigurationException(javax.xml.parsers.ParserConfigurationException) IndexMetadata(org.apache.stanbol.commons.solr.managed.IndexMetadata) ManagedIndexState(org.apache.stanbol.commons.solr.managed.ManagedIndexState) File(java.io.File) SolrServerAdapter(org.apache.stanbol.commons.solr.SolrServerAdapter) SolrException(org.apache.solr.common.SolrException) BundleContext(org.osgi.framework.BundleContext) HashSet(java.util.HashSet) Activate(org.apache.felix.scr.annotations.Activate)

Example 2 with SolrServerProperties

use of org.apache.stanbol.commons.solr.SolrServerAdapter.SolrServerProperties in project stanbol by apache.

the class ReferencedSolrServer method activate.

/*
     * NOTE: one could here also get the properties of the parsed 
     * ComponentContext and directly parse the values to the constructor of the
     * SolrServerAdapter. However here the configured values are all checkted
     * to generate meaningful error messages
     */
@Activate
protected void activate(ComponentContext context) throws ConfigurationException {
    log.info("Activate {}: ", getClass().getSimpleName());
    SolrServerProperties properties = null;
    Object value = context.getProperties().get(PROPERTY_SERVER_DIR);
    if (value == null || value.toString().isEmpty()) {
        throw new ConfigurationException(PROPERTY_SERVER_DIR, "The Server directory is a " + "required configuration and MUST NOT be NULL nor empty!");
    } else {
        File solrServerDir = new File(value.toString());
        if (solrServerDir.isDirectory()) {
            log.info(" > solrDir = {}", solrServerDir);
            properties = new SolrServerProperties(solrServerDir);
        } else {
            throw new ConfigurationException(PROPERTY_SERVER_DIR, "The parsed Solr Server directpry '" + value + "' does not exist or is not a directory!");
        }
    }
    value = context.getProperties().get(PROPERTY_SERVER_NAME);
    if (value == null || value.toString().isEmpty()) {
        throw new ConfigurationException(PROPERTY_SERVER_NAME, "The Server Name is a required" + "Configuration and MUST NOT be NULL nor empty!");
    } else {
        properties.setServerName(value.toString());
        log.info(" > Name = {}", value.toString());
    }
    value = context.getProperties().get(PROPERTY_SERVER_RANKING);
    if (value instanceof Number) {
        properties.setServerRanking(((Number) value).intValue());
    } else if (value != null && !value.toString().isEmpty()) {
        try {
            properties.setServerRanking(Integer.parseInt(value.toString()));
            log.info(" > Ranking = {}", properties.getServerRanking());
        } catch (NumberFormatException e) {
            throw new ConfigurationException(PROPERTY_SERVER_RANKING, "The configured Server Ranking '" + value + " can not be converted to an Integer!", e);
        }
    }
    //else not present or empty string -> do not set a ranking!
    value = properties.get(PROPERTY_SERVER_PUBLISH_REST);
    if (value == null || value instanceof Boolean) {
        properties.setPublishREST((Boolean) value);
    } else {
        properties.setPublishREST(Boolean.parseBoolean(value.toString()));
    }
    log.info(" > publisRest = {}", properties.isPublishREST());
    try {
        server = new SolrServerAdapter(context.getBundleContext(), properties);
    } catch (SolrException e) {
        throw new ConfigurationException(PROPERTY_SERVER_DIR, "Unable to initialise " + "a SolrServer based on the Directory '" + properties.getServerDir() + "'!", e);
    }
    log.info(" ... SolrServer successfully initialised!");
}
Also used : SolrServerProperties(org.apache.stanbol.commons.solr.SolrServerAdapter.SolrServerProperties) ConfigurationException(org.osgi.service.cm.ConfigurationException) ParserConfigurationException(javax.xml.parsers.ParserConfigurationException) File(java.io.File) SolrServerAdapter(org.apache.stanbol.commons.solr.SolrServerAdapter) SolrException(org.apache.solr.common.SolrException) Activate(org.apache.felix.scr.annotations.Activate)

Aggregations

File (java.io.File)2 ParserConfigurationException (javax.xml.parsers.ParserConfigurationException)2 Activate (org.apache.felix.scr.annotations.Activate)2 SolrException (org.apache.solr.common.SolrException)2 SolrServerAdapter (org.apache.stanbol.commons.solr.SolrServerAdapter)2 SolrServerProperties (org.apache.stanbol.commons.solr.SolrServerAdapter.SolrServerProperties)2 ConfigurationException (org.osgi.service.cm.ConfigurationException)2 IOException (java.io.IOException)1 HashSet (java.util.HashSet)1 IndexMetadata (org.apache.stanbol.commons.solr.managed.IndexMetadata)1 ManagedIndexState (org.apache.stanbol.commons.solr.managed.ManagedIndexState)1 BundleContext (org.osgi.framework.BundleContext)1 SAXException (org.xml.sax.SAXException)1