Search in sources :

Example 46 with InvalidSyntaxException

use of org.osgi.framework.InvalidSyntaxException 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 47 with InvalidSyntaxException

use of org.osgi.framework.InvalidSyntaxException in project ddf by codice.

the class SourceConfigurationHandler method regenerateOneSource.

@Override
public void regenerateOneSource(String registryId) throws FederationAdminException {
    try {
        List<Metacard> metacards = federationAdminService.getRegistryMetacardsByRegistryIds(Collections.singletonList(registryId));
        if (metacards.size() != 1) {
            throw new FederationAdminException("Error looking up metacard to regenerate sources. registry-id=" + registryId);
        }
        deleteRegistryConfigurations(metacards.get(0));
        updateRegistryConfigurations(metacards.get(0), true);
    } catch (IOException | ParserException | InvalidSyntaxException e) {
        throw new FederationAdminException("Error regenerating sources for registry entry " + registryId, e);
    }
}
Also used : FederationAdminException(org.codice.ddf.registry.federationadmin.service.internal.FederationAdminException) ParserException(org.codice.ddf.parser.ParserException) Metacard(ddf.catalog.data.Metacard) InvalidSyntaxException(org.osgi.framework.InvalidSyntaxException) IOException(java.io.IOException)

Example 48 with InvalidSyntaxException

use of org.osgi.framework.InvalidSyntaxException in project ddf by codice.

the class RESTEndpoint method generateMetacard.

private Metacard generateMetacard(MimeType mimeType, String id, InputStream message, String transformerId) throws MetacardCreationException {
    Metacard generatedMetacard = null;
    List<InputTransformer> listOfCandidates = mimeTypeToTransformerMapper.findMatches(InputTransformer.class, mimeType);
    List<String> stackTraceList = new ArrayList<>();
    LOGGER.trace("Entering generateMetacard.");
    LOGGER.debug("List of matches for mimeType [{}]: {}", mimeType, listOfCandidates);
    try (TemporaryFileBackedOutputStream fileBackedOutputStream = new TemporaryFileBackedOutputStream()) {
        try {
            if (null != message) {
                IOUtils.copy(message, fileBackedOutputStream);
            } else {
                throw new MetacardCreationException("Could not copy bytes of content message.  Message was NULL.");
            }
        } catch (IOException e) {
            throw new MetacardCreationException("Could not copy bytes of content message.", e);
        }
        Iterator<InputTransformer> it = listOfCandidates.iterator();
        if (StringUtils.isNotEmpty(transformerId)) {
            BundleContext bundleContext = getBundleContext();
            Collection<ServiceReference<InputTransformer>> serviceReferences = bundleContext.getServiceReferences(InputTransformer.class, "(id=" + transformerId + ")");
            it = serviceReferences.stream().map(bundleContext::getService).iterator();
        }
        while (it.hasNext()) {
            InputTransformer transformer = it.next();
            try (InputStream inputStreamMessageCopy = fileBackedOutputStream.asByteSource().openStream()) {
                generatedMetacard = transformer.transform(inputStreamMessageCopy);
            } catch (CatalogTransformerException | IOException e) {
                List<String> stackTraces = Arrays.asList(ExceptionUtils.getRootCauseStackTrace(e));
                stackTraceList.add(String.format("Transformer [%s] could not create metacard.", transformer));
                stackTraceList.addAll(stackTraces);
                LOGGER.debug("Transformer [{}] could not create metacard.", transformer, e);
            }
            if (generatedMetacard != null) {
                break;
            }
        }
        if (generatedMetacard == null) {
            throw new MetacardCreationException(String.format("Could not create metacard with mimeType %s : %s", mimeType, StringUtils.join(stackTraceList, "\n")));
        }
        if (id != null) {
            generatedMetacard.setAttribute(new AttributeImpl(Metacard.ID, id));
        } else {
            LOGGER.debug("Metacard had a null id");
        }
    } catch (IOException e) {
        throw new MetacardCreationException("Could not create metacard.", e);
    } catch (InvalidSyntaxException e) {
        throw new MetacardCreationException("Could not determine transformer", e);
    }
    return generatedMetacard;
}
Also used : MetacardCreationException(ddf.catalog.data.MetacardCreationException) TemporaryFileBackedOutputStream(org.codice.ddf.platform.util.TemporaryFileBackedOutputStream) ByteArrayInputStream(java.io.ByteArrayInputStream) InputStream(java.io.InputStream) AttributeImpl(ddf.catalog.data.impl.AttributeImpl) ArrayList(java.util.ArrayList) CatalogTransformerException(ddf.catalog.transform.CatalogTransformerException) IOException(java.io.IOException) InputTransformer(ddf.catalog.transform.InputTransformer) ServiceReference(org.osgi.framework.ServiceReference) Metacard(ddf.catalog.data.Metacard) ArrayList(java.util.ArrayList) List(java.util.List) InvalidSyntaxException(org.osgi.framework.InvalidSyntaxException) BundleContext(org.osgi.framework.BundleContext)

Example 49 with InvalidSyntaxException

use of org.osgi.framework.InvalidSyntaxException in project ddf by codice.

the class TestCswSourceBase method setUp.

@Before
public void setUp() {
    ServiceReference ref = mock(ServiceReference.class);
    ServiceReference[] serviceRefs = new ServiceReference[] { ref };
    try {
        when(mockContext.getServiceReferences(eq(MetadataTransformer.class.getName()), anyString())).thenReturn(serviceRefs);
        when(mockContext.getServiceReferences(eq(STSClientConfiguration.class.getName()), anyString())).thenReturn(null);
    } catch (InvalidSyntaxException e) {
        LOGGER.error(e.getMessage(), e);
    }
    MetadataTransformer transformer = mock(MetadataTransformer.class);
    // Just return same Metacard that was passed in
    when(mockContext.getService(any(ServiceReference.class))).thenReturn(transformer);
    try {
        when(transformer.transform(any(Metacard.class))).thenAnswer(invocation -> invocation.getArguments()[0]);
    } catch (CatalogTransformerException e) {
        LOGGER.error(e.getMessage(), e);
    }
    when(mockAvailabilityTask.isAvailable()).thenReturn(true);
}
Also used : Metacard(ddf.catalog.data.Metacard) MetadataTransformer(org.codice.ddf.spatial.ogc.catalog.MetadataTransformer) InvalidSyntaxException(org.osgi.framework.InvalidSyntaxException) CatalogTransformerException(ddf.catalog.transform.CatalogTransformerException) ServiceReference(org.osgi.framework.ServiceReference) Before(org.junit.Before)

Example 50 with InvalidSyntaxException

use of org.osgi.framework.InvalidSyntaxException in project ddf by codice.

the class DumpCommand method getTransformers.

private List<MetacardTransformer> getTransformers() {
    ServiceReference[] refs = null;
    try {
        refs = bundleContext.getAllServiceReferences(MetacardTransformer.class.getName(), "(|" + "(" + Constants.SERVICE_ID + "=" + transformerId + ")" + ")");
    } catch (InvalidSyntaxException e) {
        console.printf("Fail to get MetacardTransformer references due to %s", e.getMessage());
    }
    if (refs == null || refs.length == 0) {
        return null;
    }
    List<MetacardTransformer> metacardTransformerList = new ArrayList<>();
    for (ServiceReference ref : refs) {
        metacardTransformerList.add((MetacardTransformer) bundleContext.getService(ref));
    }
    return metacardTransformerList;
}
Also used : MetacardTransformer(ddf.catalog.transform.MetacardTransformer) ArrayList(java.util.ArrayList) InvalidSyntaxException(org.osgi.framework.InvalidSyntaxException) ServiceReference(org.osgi.framework.ServiceReference)

Aggregations

InvalidSyntaxException (org.osgi.framework.InvalidSyntaxException)124 ServiceReference (org.osgi.framework.ServiceReference)59 Filter (org.osgi.framework.Filter)29 ArrayList (java.util.ArrayList)27 IOException (java.io.IOException)23 BundleContext (org.osgi.framework.BundleContext)21 HashMap (java.util.HashMap)17 ServiceTracker (org.osgi.util.tracker.ServiceTracker)14 BundleException (org.osgi.framework.BundleException)12 Configuration (org.osgi.service.cm.Configuration)12 Map (java.util.Map)11 Dictionary (java.util.Dictionary)9 Hashtable (java.util.Hashtable)9 Test (org.junit.Test)9 List (java.util.List)8 File (java.io.File)7 ConcurrentHashMap (java.util.concurrent.ConcurrentHashMap)7 URL (java.net.URL)6 LinkedHashMap (java.util.LinkedHashMap)6 FilterImpl (org.eclipse.osgi.internal.framework.FilterImpl)6