Search in sources :

Example 1 with ManagedSolrServer

use of org.apache.stanbol.commons.solr.managed.ManagedSolrServer in project stanbol by apache.

the class SolrIndexInstaller method activate.

@Activate
protected void activate(ComponentContext context) {
    bc = context.getBundleContext();
    serverTracker = new ServiceTracker(bc, ManagedSolrServer.class.getName(), new ServiceTrackerCustomizer() {

        /**
                 * The servers managed by this instance
                 */
        private SortedMap<ServiceReference, ManagedSolrServer> servers = new TreeMap<ServiceReference, ManagedSolrServer>();

        @Override
        public void removedService(ServiceReference reference, Object service) {
            lock.writeLock().lock();
            try {
                servers.remove(reference);
                updateRegistration(servers);
            } finally {
                lock.writeLock().unlock();
            }
        }

        @Override
        public void modifiedService(ServiceReference reference, Object service) {
            lock.writeLock().lock();
            try {
                servers.put(reference, (ManagedSolrServer) service);
                updateRegistration(servers);
            } finally {
                lock.writeLock().unlock();
            }
        }

        @Override
        public Object addingService(ServiceReference reference) {
            ManagedSolrServer server = (ManagedSolrServer) bc.getService(reference);
            if (server != null) {
                lock.writeLock().lock();
                try {
                    servers.put(reference, server);
                    updateRegistration(servers);
                } finally {
                    lock.writeLock().unlock();
                }
            }
            return server;
        }
    });
    serverTracker.open();
}
Also used : ServiceTracker(org.osgi.util.tracker.ServiceTracker) ServiceTrackerCustomizer(org.osgi.util.tracker.ServiceTrackerCustomizer) SortedMap(java.util.SortedMap) ManagedSolrServer(org.apache.stanbol.commons.solr.managed.ManagedSolrServer) ServiceReference(org.osgi.framework.ServiceReference) Activate(org.apache.felix.scr.annotations.Activate)

Example 2 with ManagedSolrServer

use of org.apache.stanbol.commons.solr.managed.ManagedSolrServer in project stanbol by apache.

the class SolrYardComponent method initManagedSolrIndex.

/**
     * initialise ManagedSolrIndex and that starts tracking for the {@link SolrCore}
     * @param managedServer the managedServer to init the SolrCore (if necessary)
     * @param config the {@link SolrYardConfig}
     * @throws IllegalStateException if the initialization fails
     */
private void initManagedSolrIndex(final ManagedSolrServer managedServer, final SolrYardConfig config) {
    if (managedServer == null || config == null) {
        //component was not yet activated ... will be called again
        return;
    }
    IndexReference solrIndexRef = IndexReference.parse(config.getSolrServerLocation());
    if (config.isAllowInitialisation()) {
        //are we allowed to create the SolrServer
        //get the name of the config to be used (default: default.solrindex.zip")
        String configName = config.getIndexConfigurationName();
        IndexMetadata metadata = managedServer.getIndexMetadata(solrIndexRef.getIndex());
        if (metadata == null) {
            //create a new index
            log.info(" ... creating Managed SolrIndex {} (configName: {}) on Server {}", new Object[] { solrIndexRef.getIndex(), configName, managedServer.getServerName() });
            try {
                metadata = managedServer.createSolrIndex(solrIndexRef.getIndex(), configName, null);
            } catch (IOException e) {
                throw new IllegalStateException("Unable to create Managed SolrIndex " + solrIndexRef.getIndex() + " (configName: " + configName + ") on Server " + managedServer.getServerName() + "!", e);
            }
        } else if (metadata.getState() != ManagedIndexState.ACTIVE) {
            log.info(" ... activating Managed SolrIndex {} on Server {} (current state: {})", new Object[] { solrIndexRef.getIndex(), managedServer.getServerName(), metadata.getState() });
            try {
                managedServer.activateIndex(metadata.getIndexName());
            } catch (IOException e) {
                throw new IllegalStateException("Unable to activate Managed SolrIndex " + solrIndexRef.getIndex() + " (configName: " + configName + ") on Server " + managedServer.getServerName() + "!", e);
            } catch (SAXException e) {
                throw new IllegalStateException("Unable to activate Managed SolrIndex " + solrIndexRef.getIndex() + " (configName: " + configName + ") on Server " + managedServer.getServerName() + "!", e);
            }
        }
        //else already active nothing todo
        solrIndexRef = metadata.getIndexReference();
    }
    //else the SolrServer will be supplied (e.g. created by installing a full index)
    try {
        registeredServerTracker = new RegisteredSolrServerTracker(context, solrIndexRef, null) {

            @Override
            public void removedService(ServiceReference reference, Object service) {
                updateSolrYardRegistration(registeredServerTracker.getService(), config);
                super.removedService(reference, service);
            }

            @Override
            public void modifiedService(ServiceReference reference, Object service) {
                updateSolrYardRegistration(registeredServerTracker.getService(), config);
                super.modifiedService(reference, service);
            }

            @Override
            public SolrServer addingService(ServiceReference reference) {
                SolrServer server = super.addingService(reference);
                if (solrServer != null) {
                    log.warn("Multiple SolrServer for IndexLocation {} available!", config.getSolrServerLocation());
                } else {
                    updateSolrYardRegistration(server, config);
                }
                return server;
            }
        };
        log.info(" ... start tracking for SolrCore based on {}", solrIndexRef);
        //start tracking
        registeredServerTracker.open();
    } catch (InvalidSyntaxException e) {
        throw new IllegalStateException("Unable to track Managed SolrIndex " + solrIndexRef.getIndex() + "on Server " + managedServer.getServerName() + "!", e);
    }
}
Also used : RegisteredSolrServerTracker(org.apache.stanbol.commons.solr.RegisteredSolrServerTracker) InvalidSyntaxException(org.osgi.framework.InvalidSyntaxException) IOException(java.io.IOException) IndexMetadata(org.apache.stanbol.commons.solr.managed.IndexMetadata) IndexReference(org.apache.stanbol.commons.solr.IndexReference) ManagedSolrServer(org.apache.stanbol.commons.solr.managed.ManagedSolrServer) HttpSolrServer(org.apache.solr.client.solrj.impl.HttpSolrServer) SolrServer(org.apache.solr.client.solrj.SolrServer) SAXException(org.xml.sax.SAXException) ServiceReference(org.osgi.framework.ServiceReference)

Example 3 with ManagedSolrServer

use of org.apache.stanbol.commons.solr.managed.ManagedSolrServer in project stanbol by apache.

the class IndexInstallTask method execute.

@Override
public void execute(InstallationContext ctx) {
    String indexName = (String) getResource().getAttribute(INDEX_NAME);
    if (indexName == null) {
        log.error("Unable to remove Managed Index because the required Property '{}'" + "used to define the name of the Index is missing", INDEX_NAME);
        setFinishedState(ResourceState.IGNORED);
    } else {
        String serverName = (String) getResource().getAttribute(ManagedIndexConstants.SERVER_NAME);
        ManagedSolrServer server = managedServers.get(serverName);
        if (server == null) {
            log.warn("Unable to install Managed Solr Index {} because the {} " + "Server {} is currently not active!", new Object[] { indexName, serverName == null ? "default" : "", serverName != null ? serverName : "" });
            //needs still to be installed
            setFinishedState(ResourceState.IGNORED);
        } else {
            //we have an index name and a server to in stall it ... 
            //  ... let's do the work
            String archiveFormat = (String) getResource().getAttribute(PROPERTY_ARCHIVE_FORMAT);
            InputStream is = null;
            try {
                is = getResource().getInputStream();
                if ("properties".equals(archiveFormat)) {
                    InputStreamReader reader = new InputStreamReader(is, "UTF-8");
                    Properties props = new Properties();
                    try {
                        props.load(reader);
                    } finally {
                        IOUtils.closeQuietly(reader);
                    }
                    //TODO install to the right server!
                    String indexPath = props.getProperty(INDEX_ARCHIVES);
                    if (indexPath == null) {
                        indexPath = indexName + '.' + ConfigUtils.SOLR_INDEX_ARCHIVE_EXTENSION;
                        log.info("Property \"" + INDEX_ARCHIVES + "\" not present within the SolrIndex references file. Will use the default name \"" + indexPath + "\"");
                    }
                    server.updateIndex(indexName, indexPath, props);
                    setFinishedState(ResourceState.INSTALLED);
                } else {
                    ArchiveInputStream ais = null;
                    try {
                        ais = ConfigUtils.getArchiveInputStream(archiveFormat, is);
                        server.updateIndex(indexName, ais);
                        // we are done ... set the state to installed!
                        setFinishedState(ResourceState.INSTALLED);
                    } finally {
                        IOUtils.closeQuietly(ais);
                    }
                }
            // now we can copy the core!
            } catch (Exception e) {
                String message = String.format("Unable to install SolrIndexArchive for index name '%s'!" + " (resource=%s, arviceFormat=%s)", indexName, getResource().getURL(), archiveFormat);
                log.error(message, e);
                ctx.log("%s! Reason: %s", message, e.getMessage());
                setFinishedState(ResourceState.IGNORED);
            } finally {
                IOUtils.closeQuietly(is);
            }
        }
    }
}
Also used : ArchiveInputStream(org.apache.commons.compress.archivers.ArchiveInputStream) InputStreamReader(java.io.InputStreamReader) ArchiveInputStream(org.apache.commons.compress.archivers.ArchiveInputStream) InputStream(java.io.InputStream) Properties(java.util.Properties) ManagedSolrServer(org.apache.stanbol.commons.solr.managed.ManagedSolrServer)

Example 4 with ManagedSolrServer

use of org.apache.stanbol.commons.solr.managed.ManagedSolrServer in project stanbol by apache.

the class IndexRemoveTask method execute.

@Override
public void execute(InstallationContext ctx) {
    String indexName = (String) getResource().getAttribute(INDEX_NAME);
    if (indexName == null) {
        log.error("Unable to remove Managed Index because the required Property '{}'" + "used to define the name of the Index is missing", INDEX_NAME);
        setFinishedState(ResourceState.IGNORED);
    } else {
        String serverName = (String) getResource().getAttribute(ManagedIndexConstants.SERVER_NAME);
        ManagedSolrServer server = managedServers.get(serverName);
        if (server == null) {
            log.warn("Unable to remove Managed Solr Index {} because the {} " + "Server {} is currently not active!", new Object[] { indexName, serverName == null ? "default" : "", serverName != null ? serverName : "" });
            setFinishedState(ResourceState.IGNORED);
        } else {
            server.removeIndex(indexName, true);
            setFinishedState(ResourceState.UNINSTALLED);
        }
    }
}
Also used : ManagedSolrServer(org.apache.stanbol.commons.solr.managed.ManagedSolrServer)

Example 5 with ManagedSolrServer

use of org.apache.stanbol.commons.solr.managed.ManagedSolrServer in project stanbol by apache.

the class SolrIndexInstaller method createActiveServersMap.

/**
     * Assumed to be called while a writeLock is active on {@link #lock}
     * @return
     */
private Map<String, ManagedSolrServer> createActiveServersMap(SortedMap<ServiceReference, ManagedSolrServer> servers) {
    Map<String, ManagedSolrServer> map;
    if (servers.isEmpty()) {
        map = Collections.emptyMap();
    } else {
        map = new HashMap<String, ManagedSolrServer>();
        //this is a sorted iteration over the ManagedSolrServers
        //so the first will be the default server
        boolean first = true;
        for (ManagedSolrServer server : servers.values()) {
            map.put(server.getServerName(), server);
            if (first) {
                //put the default server under null
                map.put(null, server);
                first = false;
            }
        }
    }
    return map;
}
Also used : ManagedSolrServer(org.apache.stanbol.commons.solr.managed.ManagedSolrServer)

Aggregations

ManagedSolrServer (org.apache.stanbol.commons.solr.managed.ManagedSolrServer)6 SolrServer (org.apache.solr.client.solrj.SolrServer)2 IndexReference (org.apache.stanbol.commons.solr.IndexReference)2 ServiceReference (org.osgi.framework.ServiceReference)2 IOException (java.io.IOException)1 InputStream (java.io.InputStream)1 InputStreamReader (java.io.InputStreamReader)1 Properties (java.util.Properties)1 SortedMap (java.util.SortedMap)1 ArchiveInputStream (org.apache.commons.compress.archivers.ArchiveInputStream)1 Activate (org.apache.felix.scr.annotations.Activate)1 HttpSolrServer (org.apache.solr.client.solrj.impl.HttpSolrServer)1 NamespacePrefixService (org.apache.stanbol.commons.namespaceprefix.NamespacePrefixService)1 RegisteredSolrServerTracker (org.apache.stanbol.commons.solr.RegisteredSolrServerTracker)1 IndexMetadata (org.apache.stanbol.commons.solr.managed.IndexMetadata)1 StandaloneManagedSolrServer (org.apache.stanbol.commons.solr.managed.standalone.StandaloneManagedSolrServer)1 SolrYard (org.apache.stanbol.entityhub.yard.solr.impl.SolrYard)1 SolrYardConfig (org.apache.stanbol.entityhub.yard.solr.impl.SolrYardConfig)1 BeforeClass (org.junit.BeforeClass)1 InvalidSyntaxException (org.osgi.framework.InvalidSyntaxException)1