use of org.apache.stanbol.commons.stanboltools.datafileprovider.DataFileProviderEvent in project stanbol by apache.
the class WebConsolePlugin method doGet.
@Override
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
final PrintWriter pw = response.getWriter();
pw.println("<p class='statline ui-state-highlight'>");
pw.println("Displaying the last " + dataFileProviderLog.size() + " DataFileProvider events");
String dfPath = "<PATH NOT FOUND??>";
if (dataFileProvider instanceof MainDataFileProvider) {
dfPath = ((MainDataFileProvider) dataFileProvider).getDataFilesFolder().getAbsolutePath();
}
pw.println("<br/>");
pw.println("Data files found in the " + dfPath + " folder have precedence");
pw.println("<br/>");
pw.println("The main DataFileProvider is " + dataFileProvider.getClass().getName());
pw.println("</p>");
pw.println("<table class='nicetable'>");
final String[] labels = { "timestamp", "bundle/filename", "actual location/download info" };
pw.println("<thead><tr>");
for (String label : labels) {
cell("th", pw, null, label);
}
pw.println("</tr></thead><tbody>");
final SimpleDateFormat fmt = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss");
for (DataFileProviderEvent e : dataFileProviderLog) {
pw.println("<tr>");
cell(pw, null, fmt.format(e.getTimestamp()));
cell(pw, null, e.getBundleSymbolicName(), "b", e.getFilename());
final StringBuilder sb = new StringBuilder();
for (Map.Entry<?, ?> comment : e.getComments().entrySet()) {
if (sb.length() > 0) {
sb.append("<br/>");
}
sb.append(comment.getKey());
sb.append(": ");
sb.append(comment.getValue());
}
cell(pw, null, e.getActualFileLocation(), "i", sb.toString());
pw.println("</tr>");
}
pw.println("</tbody></table>");
}
use of org.apache.stanbol.commons.stanboltools.datafileprovider.DataFileProviderEvent 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.DataFileProviderEvent 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;
}
Aggregations