Search in sources :

Example 1 with DataFileProviderEvent

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>");
}
Also used : DataFileProviderEvent(org.apache.stanbol.commons.stanboltools.datafileprovider.DataFileProviderEvent) SimpleDateFormat(java.text.SimpleDateFormat) Map(java.util.Map) PrintWriter(java.io.PrintWriter)

Example 2 with DataFileProviderEvent

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;
}
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 DataFileProviderEvent

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

Aggregations

DataFileProviderEvent (org.apache.stanbol.commons.stanboltools.datafileprovider.DataFileProviderEvent)3 File (java.io.File)2 DataFileProvider (org.apache.stanbol.commons.stanboltools.datafileprovider.DataFileProvider)2 ServiceReference (org.osgi.framework.ServiceReference)2 FileInputStream (java.io.FileInputStream)1 IOException (java.io.IOException)1 InputStream (java.io.InputStream)1 PrintWriter (java.io.PrintWriter)1 PrivilegedActionException (java.security.PrivilegedActionException)1 PrivilegedExceptionAction (java.security.PrivilegedExceptionAction)1 SimpleDateFormat (java.text.SimpleDateFormat)1 Map (java.util.Map)1 ConfigurationException (org.osgi.service.cm.ConfigurationException)1