Skip to content

Instantly share code, notes, and snippets.

@saikocat
Created October 29, 2017 11:36
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save saikocat/bd4e4f2be1cd895984f09b05de50dd92 to your computer and use it in GitHub Desktop.
Save saikocat/bd4e4f2be1cd895984f09b05de50dd92 to your computer and use it in GitHub Desktop.
// Guava 14.0 - bundled with Spark
// https://github.com/google/guava/blob/v14.0/guava/src/com/google/common/io/Resources.java#L191
/**
* Returns a {@code URL} pointing to {@code resourceName} if the resource is
* found in the class path. {@code Resources.class.getClassLoader()} is used
* to locate the resource.
*/
public static URL getResource(String resourceName) {
URL url = Resources.class.getClassLoader().getResource(resourceName);
checkArgument(url != null, "resource %s not found.", resourceName);
return url;
}
// --------------------------------------------------------------------------------------------------------------------------
// Guava 18.0 - dependency for embedded-redis
// https://github.com/google/guava/blob/v18.0/guava/src/com/google/common/io/Resources.java
/**
* Returns a {@code URL} pointing to {@code resourceName} if the resource is
* found using the {@linkplain Thread#getContextClassLoader() context class
* loader}. In simple environments, the context class loader will find
* resources from the class path. In environments where different threads can
* have different class loaders, for example app servers, the context class
* loader will typically have been set to an appropriate loader for the
* current thread.
*
* <p>In the unusual case where the context class loader is null, the class
* loader that loaded this class ({@code Resources}) will be used instead.
*
*/
public static URL getResource(String resourceName) {
ClassLoader loader = MoreObjects.firstNonNull(
Thread.currentThread().getContextClassLoader(),
Resources.class.getClassLoader());
URL url = loader.getResource(resourceName);
checkArgument(url != null, "resource %s not found.", resourceName);
return url;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment