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