jodd.util.ResourcesUtil Maven / Gradle / Ivy
// Copyright (c) 2003-present, Jodd Team (http://jodd.org)
// All rights reserved.
//
// Redistribution and use in source and binary forms, with or without
// modification, are permitted provided that the following conditions are met:
//
// 1. Redistributions of source code must retain the above copyright notice,
// this list of conditions and the following disclaimer.
//
// 2. Redistributions in binary form must reproduce the above copyright
// notice, this list of conditions and the following disclaimer in the
// documentation and/or other materials provided with the distribution.
//
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
// AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
// IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
// ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
// LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
// CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
// SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
// INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
// CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
// ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
// POSSIBILITY OF SUCH DAMAGE.
package jodd.util;
import jodd.io.IOUtil;
import java.io.IOException;
import java.io.InputStream;
import java.net.URL;
import java.net.URLConnection;
public class ResourcesUtil {
// ---------------------------------------------------------------- get resource
/**
* Retrieves given resource as URL.
* @see #getResourceUrl(String, ClassLoader)
*/
public static URL getResourceUrl(final String resourceName) {
return getResourceUrl(resourceName, null);
}
/**
* Retrieves given resource as URL. Resource is always absolute and may
* starts with a slash character.
*
* Resource will be loaded using class loaders in the following order:
*
* - {@link Thread#getContextClassLoader() Thread.currentThread().getContextClassLoader()}
* - {@link Class#getClassLoader() ClassLoaderUtil.class.getClassLoader()}
* - if
callingClass
is provided: {@link Class#getClassLoader() callingClass.getClassLoader()}
*
*/
public static URL getResourceUrl(String resourceName, final ClassLoader classLoader) {
if (resourceName.startsWith("/")) {
resourceName = resourceName.substring(1);
}
URL resourceUrl;
// try #1 - using provided class loader
if (classLoader != null) {
resourceUrl = classLoader.getResource(resourceName);
if (resourceUrl != null) {
return resourceUrl;
}
}
// try #2 - using thread class loader
final ClassLoader currentThreadClassLoader = Thread.currentThread().getContextClassLoader();
if ((currentThreadClassLoader != null) && (currentThreadClassLoader != classLoader)) {
resourceUrl = currentThreadClassLoader.getResource(resourceName);
if (resourceUrl != null) {
return resourceUrl;
}
}
// try #3 - using caller classloader, similar as Class.forName()
final Class callerClass = ClassUtil.getCallerClass(2);
final ClassLoader callerClassLoader = callerClass.getClassLoader();
if ((callerClassLoader != classLoader) && (callerClassLoader != currentThreadClassLoader)) {
resourceUrl = callerClassLoader.getResource(resourceName);
if (resourceUrl != null) {
return resourceUrl;
}
}
return null;
}
// ---------------------------------------------------------------- get resource string
public static String getResourceAsString(final String resourceName) throws IOException {
final InputStream inputStream = getResourceAsStream(resourceName);
try {
final char[] data = IOUtil.readChars(inputStream);
return new String(data);
}
finally {
IOUtil.close(inputStream);
}
}
// ---------------------------------------------------------------- get resource stream
/**
* Opens a resource of the specified name for reading.
* @see #getResourceAsStream(String, ClassLoader)
*/
public static InputStream getResourceAsStream(final String resourceName) throws IOException {
return getResourceAsStream(resourceName, null);
}
/**
* Opens a resource of the specified name for reading.
* @see #getResourceUrl(String, ClassLoader)
*/
public static InputStream getResourceAsStream(final String resourceName, final ClassLoader callingClass) throws IOException {
final URL url = getResourceUrl(resourceName, callingClass);
if (url != null) {
return url.openStream();
}
return null;
}
/**
* Opens a resource of the specified name for reading. Controls caching,
* that is important when the same jar is reloaded using custom classloader.
*/
public static InputStream getResourceAsStream(final String resourceName, final ClassLoader callingClass, final boolean useCache) throws IOException {
final URL url = getResourceUrl(resourceName, callingClass);
if (url != null) {
final URLConnection urlConnection = url.openConnection();
urlConnection.setUseCaches(useCache);
return urlConnection.getInputStream();
}
return null;
}
}