Search in sources :

Example 1 with ResourceNotFoundException

use of eu.esdihumboldt.util.resource.ResourceNotFoundException in project hale by halestudio.

the class BundleResolver method resolve.

/**
 * @see ResourceResolver#resolve(URI)
 */
@Override
public InputSupplier<? extends InputStream> resolve(URI uri) throws ResourceNotFoundException {
    if (bundle != null) {
        // OSGi
        final URL entry = bundle.getEntry(uri.getPath());
        if (entry == null) {
            throw new ResourceNotFoundException("Resource with path " + uri.getPath() + " not contained in bundle " + bundle.getSymbolicName());
        }
        preventDirectoryMatch(uri, entry);
        return new InputSupplier<InputStream>() {

            @Override
            public InputStream getInput() throws IOException {
                return entry.openStream();
            }
        };
    } else {
        // no OSGi
        // ClassLoader.getSystemClassLoader();
        final ClassLoader loader = getClass().getClassLoader();
        String pathCandidate = uri.getPath();
        final String path = (pathCandidate != null && pathCandidate.startsWith("/")) ? (pathCandidate.substring(1)) : (pathCandidate);
        Enumeration<URL> resources;
        try {
            resources = loader.getResources(path);
        } catch (IOException e) {
            log.error("Error accessing classpath resource", e);
            throw new ResourceNotFoundException(e);
        }
        if (resources.hasMoreElements()) {
            URL entry = resources.nextElement();
            // XXX not sure if this is needed here
            preventDirectoryMatch(uri, entry);
            return new InputSupplier<InputStream>() {

                @Override
                public InputStream getInput() throws IOException {
                    return loader.getResourceAsStream(path);
                }
            };
        } else {
            throw new ResourceNotFoundException();
        }
    }
}
Also used : IOException(java.io.IOException) ResourceNotFoundException(eu.esdihumboldt.util.resource.ResourceNotFoundException) URL(java.net.URL) InputSupplier(eu.esdihumboldt.util.io.InputSupplier)

Aggregations

InputSupplier (eu.esdihumboldt.util.io.InputSupplier)1 ResourceNotFoundException (eu.esdihumboldt.util.resource.ResourceNotFoundException)1 IOException (java.io.IOException)1 URL (java.net.URL)1