Search in sources :

Example 1 with DataFileProvider

use of org.apache.stanbol.commons.stanboltools.datafileprovider.DataFileProvider in project stanbol by apache.

the class StandaloneManagedSolrServer method updateIndex.

@Override
public IndexMetadata updateIndex(String name, String parsedResourceName, Properties properties) throws IOException {
    if (name == null || name.isEmpty()) {
        throw new IllegalArgumentException("The parsed index name MUST NOT be NULL nor empty!");
    }
    String resourceName;
    if (!ConfigUtils.isValidSolrIndexFileName(parsedResourceName)) {
        log.debug("add SolrIndexFileExtension to parsed indexArchive {}", parsedResourceName);
        resourceName = ConfigUtils.appandSolrIndexFileExtension(parsedResourceName, null);
    } else {
        resourceName = parsedResourceName;
    }
    Map<String, String> comments = new HashMap<String, String>();
    if (properties != null) {
        for (Entry<Object, Object> prop : properties.entrySet()) {
            comments.put(prop.getKey().toString(), prop.getValue().toString());
        }
    }
    InputStream is = null;
    for (Iterator<DataFileProvider> it = dataFileProviders.iterator(); is == null && it.hasNext(); ) {
        DataFileProvider dfp = it.next();
        try {
            is = dfp.getInputStream(null, resourceName, comments);
        } catch (IOException e) {
        // not found
        }
    }
    if (is != null || new File(managedSolrDir, parsedResourceName).isDirectory()) {
        ArchiveInputStream ais;
        try {
            ais = ManagementUtils.getArchiveInputStream(resourceName, is);
        } catch (ArchiveException e) {
            throw new IOException("Unable to open ArchiveInputStream for resource '" + resourceName + "'!", e);
        }
        IndexMetadata metadata = new IndexMetadata();
        if (properties != null) {
            metadata.putAll(properties);
        }
        metadata.setIndexName(name);
        metadata.setServerName(DEFAULT_SERVER_NAME);
        metadata.setSynchronized(false);
        metadata.setState(ManagedIndexState.ACTIVE);
        metadata.setArchive(resourceName);
        return updateCore(metadata, ais);
    } else {
        return null;
    }
}
Also used : HashMap(java.util.HashMap) ArchiveInputStream(org.apache.commons.compress.archivers.ArchiveInputStream) InputStream(java.io.InputStream) IOException(java.io.IOException) ArchiveException(org.apache.commons.compress.archivers.ArchiveException) ArchiveInputStream(org.apache.commons.compress.archivers.ArchiveInputStream) DataFileProvider(org.apache.stanbol.commons.stanboltools.datafileprovider.DataFileProvider) IndexMetadata(org.apache.stanbol.commons.solr.managed.IndexMetadata) File(java.io.File)

Example 2 with DataFileProvider

use of org.apache.stanbol.commons.stanboltools.datafileprovider.DataFileProvider in project stanbol by apache.

the class MainDataFileProvider method isAvailable.

@SuppressWarnings("unchecked")
@Override
public boolean isAvailable(String bundleSymbolicName, String filename, Map<String, String> comments) {
    String fileUrl = null;
    File dataFile = getDataFile(bundleSymbolicName, filename);
    // ordered by service ranking
    if (dataFile == null) {
        // Sort providers by service ranking
        final List<ServiceReference> refs = getSortedServiceRefs();
        Collections.sort(refs);
        for (ServiceReference ref : refs) {
            final Object o = providersTracker.getService(ref);
            if (o == this) {
                continue;
            }
            final DataFileProvider dfp = (DataFileProvider) o;
            try {
                if (dfp.isAvailable(bundleSymbolicName, filename, comments)) {
                    log.debug("{} does provide file {}", dfp, filename);
                    fileUrl = dfp.getClass().getName() + "://" + filename;
                    break;
                }
            } catch (RuntimeException e) {
                log.warn("Exception while checking availability of Datafile " + "'{}' on DataFileProvider {}", filename, dfp);
            }
        }
    } else {
        log.debug("{} does provide file {}", this, filename);
        fileUrl = dataFile.toURI().toASCIIString();
    }
    // Add event
    final DataFileProviderEvent event = new DataFileProviderEvent(bundleSymbolicName, filename, comments, fileUrl);
    synchronized (events) {
        if (events.size() >= maxEvents) {
            events.remove(0);
        }
        events.add(event);
    }
    return fileUrl != null;
}
Also used : DataFileProviderEvent(org.apache.stanbol.commons.stanboltools.datafileprovider.DataFileProviderEvent) DataFileProvider(org.apache.stanbol.commons.stanboltools.datafileprovider.DataFileProvider) File(java.io.File) ServiceReference(org.osgi.framework.ServiceReference)

Example 3 with DataFileProvider

use of org.apache.stanbol.commons.stanboltools.datafileprovider.DataFileProvider in project stanbol by apache.

the class MainDataFileProvider method getInputStream.

@SuppressWarnings("unchecked")
@Override
public InputStream getInputStream(String bundleSymbolicName, String filename, Map<String, String> comments) throws IOException {
    InputStream result = null;
    String fileUrl = null;
    final File dataFile = getDataFile(bundleSymbolicName, filename);
    // ordered by service ranking
    if (dataFile == null) {
        // Sort providers by service ranking
        final List<ServiceReference> refs = getSortedServiceRefs();
        for (ServiceReference ref : refs) {
            final Object o = providersTracker.getService(ref);
            if (o == this) {
                continue;
            }
            final DataFileProvider dfp = (DataFileProvider) o;
            try {
                result = dfp.getInputStream(bundleSymbolicName, filename, comments);
            } catch (Exception e) {
                // Exceptions thrown by an implementation should never
                // affect the MainDataFileProvider
                log.debug(String.format("Eception while searching DataFile %s by using provider %s (ignore)", filename, dfp), e);
            }
            if (result == null) {
                log.debug("{} does not provide file {}", dfp, filename);
            } else {
                fileUrl = dfp.getClass().getName() + "://" + filename;
                // break as soon as a resource was found
                break;
            }
        }
    } else {
        try {
            result = AccessController.doPrivileged(new PrivilegedExceptionAction<InputStream>() {

                @Override
                public InputStream run() throws IOException {
                    return new FileInputStream(dataFile);
                }
            });
        } catch (PrivilegedActionException pae) {
            Exception e = pae.getException();
            if (e instanceof IOException) {
                throw (IOException) e;
            } else {
                throw RuntimeException.class.cast(e);
            }
        }
        fileUrl = dataFile.toURI().toASCIIString();
    }
    // Add event
    final DataFileProviderEvent event = new DataFileProviderEvent(bundleSymbolicName, filename, comments, fileUrl);
    synchronized (events) {
        if (events.size() >= maxEvents) {
            events.remove(0);
        }
        events.add(event);
    }
    if (result == null) {
        throw new IOException("File not found: " + filename);
    }
    log.debug("Successfully loaded file {}", event);
    return result;
}
Also used : DataFileProviderEvent(org.apache.stanbol.commons.stanboltools.datafileprovider.DataFileProviderEvent) PrivilegedActionException(java.security.PrivilegedActionException) FileInputStream(java.io.FileInputStream) InputStream(java.io.InputStream) PrivilegedExceptionAction(java.security.PrivilegedExceptionAction) IOException(java.io.IOException) ConfigurationException(org.osgi.service.cm.ConfigurationException) PrivilegedActionException(java.security.PrivilegedActionException) IOException(java.io.IOException) FileInputStream(java.io.FileInputStream) ServiceReference(org.osgi.framework.ServiceReference) DataFileProvider(org.apache.stanbol.commons.stanboltools.datafileprovider.DataFileProvider) File(java.io.File)

Example 4 with DataFileProvider

use of org.apache.stanbol.commons.stanboltools.datafileprovider.DataFileProvider in project stanbol by apache.

the class TestNamedEntityExtractionEnhancementEngine method initDataFileProvicer.

@BeforeClass
public static void initDataFileProvicer() {
    DataFileProvider dataFileProvider = new ClasspathDataFileProvider(FAKE_BUNDLE_SYMBOLIC_NAME);
    openNLP = new OpenNLP(dataFileProvider);
}
Also used : OpenNLP(org.apache.stanbol.commons.opennlp.OpenNLP) DataFileProvider(org.apache.stanbol.commons.stanboltools.datafileprovider.DataFileProvider) BeforeClass(org.junit.BeforeClass)

Aggregations

DataFileProvider (org.apache.stanbol.commons.stanboltools.datafileprovider.DataFileProvider)4 File (java.io.File)3 IOException (java.io.IOException)2 InputStream (java.io.InputStream)2 DataFileProviderEvent (org.apache.stanbol.commons.stanboltools.datafileprovider.DataFileProviderEvent)2 ServiceReference (org.osgi.framework.ServiceReference)2 FileInputStream (java.io.FileInputStream)1 PrivilegedActionException (java.security.PrivilegedActionException)1 PrivilegedExceptionAction (java.security.PrivilegedExceptionAction)1 HashMap (java.util.HashMap)1 ArchiveException (org.apache.commons.compress.archivers.ArchiveException)1 ArchiveInputStream (org.apache.commons.compress.archivers.ArchiveInputStream)1 OpenNLP (org.apache.stanbol.commons.opennlp.OpenNLP)1 IndexMetadata (org.apache.stanbol.commons.solr.managed.IndexMetadata)1 BeforeClass (org.junit.BeforeClass)1 ConfigurationException (org.osgi.service.cm.ConfigurationException)1