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;
}
}
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;
}
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;
}
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);
}
Aggregations