1
0
Fork 0
mirror of https://github.com/graphhopper/jsprit.git synced 2020-01-24 07:45:05 +01:00

Merge pull request #94 from pierredavidbelanger/resource-loading-fix

fix in Resource loading utils class to avoid using the filesystem
This commit is contained in:
jsprit 2014-04-16 18:26:59 +02:00
commit b54daca531

View file

@ -35,32 +35,41 @@ public class Resource {
private static Logger log = Logger.getLogger(Resource.class); private static Logger log = Logger.getLogger(Resource.class);
public final static URL getAsURL(final String filename) { public final static URL getAsURL(final String filename) {
URL url = Resource.class.getClassLoader().getResource(filename);
if (url != null) {
return url;
}
log.info("Resource " + filename + " is unreachable by the current class loader, try the filesystem");
File file = new File(filename); File file = new File(filename);
if (file.exists()) { if (!file.exists()) {
try { log.warn("Resource " + filename + " do not exists on the filesystem");
return file.toURI().toURL(); return null;
} catch (MalformedURLException e) {
log.warn("Even resource exists, could not return its URL.", e);
}
} }
URL url = Resource.class.getResource("/" + filename); try {
if (url == null) { return file.toURI().toURL();
log.warn("Could not find resource '" + filename + "'!"); } catch (MalformedURLException e) {
log.warn("Resource " + filename + " exists on the filesystem, but its URL is invalid: " + e.getMessage());
return null;
} }
return url;
} }
public final static InputStream getAsInputStream(final String filename) { public final static InputStream getAsInputStream(final String filename) {
InputStream stream = Resource.class.getClassLoader().getResourceAsStream(filename);
if (stream != null) {
return stream;
}
log.info("Resource " + filename + " is unreachable by the current class loader, try the filesystem");
File file = new File(filename);
if (!file.exists()) {
log.warn("Resource " + filename + " do not exists on the filesystem");
return null;
}
try { try {
return new FileInputStream("/" + filename); return new FileInputStream(file);
} catch (FileNotFoundException e) { } catch (FileNotFoundException e) {
log.info("Could not find '" + filename + "'!."); log.warn("Resource " + filename + " exists on the filesystem, but its URL is invalid: " + e.getMessage());
return null;
} }
InputStream stream = Resource.class.getResourceAsStream("/" + filename);
if (stream == null) {
log.warn("Could not find resource '" + filename + "'!");
}
return stream;
} }
} }