diff --git a/camel-util/src/main/java/org/apache/camel/util/ObjectHelper.java b/camel-util/src/main/java/org/apache/camel/util/ObjectHelper.java index f9acd9409c1df..58798d374ec34 100644 --- a/camel-util/src/main/java/org/apache/camel/util/ObjectHelper.java +++ b/camel-util/src/main/java/org/apache/camel/util/ObjectHelper.java @@ -485,34 +485,21 @@ public static InputStream loadResourceAsStream(String name) { } /** - * Attempts to load the given resource as a stream using the thread context - * class loader or the class loader used to load this class + * Attempts to load the given resource as a stream using + * first the given class loader, then the thread context + * class loader and finally the class loader used to load this class * * @param name the name of the resource to load * @param loader optional classloader to attempt first * @return the stream or null if it could not be loaded */ public static InputStream loadResourceAsStream(String name, ClassLoader loader) { - InputStream in = null; - - String resolvedName = resolveUriPath(name); - if (loader != null) { - in = loader.getResourceAsStream(resolvedName); - } - if (in == null) { - ClassLoader contextClassLoader = Thread.currentThread().getContextClassLoader(); - if (contextClassLoader != null) { - in = contextClassLoader.getResourceAsStream(resolvedName); - } - } - if (in == null) { - in = ObjectHelper.class.getClassLoader().getResourceAsStream(resolvedName); - } - if (in == null) { - in = ObjectHelper.class.getResourceAsStream(resolvedName); - } - - return in; + try { + URL res = loadResourceAsURL(name); + return res != null ? res.openStream() : null; + } catch (IOException e) { + return null; + } } /** @@ -535,25 +522,44 @@ public static URL loadResourceAsURL(String name) { * @return the stream or null if it could not be loaded */ public static URL loadResourceAsURL(String name, ClassLoader loader) { + URL url = null; - String resolvedName = resolveUriPath(name); + + // #1 First, try the given class loader + if (loader != null) { - url = loader.getResource(resolvedName); - } - if (url == null) { - ClassLoader contextClassLoader = Thread.currentThread().getContextClassLoader(); - if (contextClassLoader != null) { - url = contextClassLoader.getResource(resolvedName); - } - } - if (url == null) { - url = ObjectHelper.class.getClassLoader().getResource(resolvedName); + url = loader.getResource(resolvedName); + if (url != null) return url; } - if (url == null) { - url = ObjectHelper.class.getResource(resolvedName); + + // #2 Next, is the TCCL + + ClassLoader tccl = Thread.currentThread().getContextClassLoader(); + if (tccl != null) { + + url = tccl.getResource(resolvedName); + if (url != null) return url; + + // #3 The TCCL may be able to see camel-core, but not META-INF resources + + try { + + Class clazz = tccl.loadClass("org.apache.camel.impl.DefaultCamelContext"); + url = clazz.getClassLoader().getResource(resolvedName); + if (url != null) return url; + + } catch (ClassNotFoundException e) { + // ignore + } } - + + // #4 Last, for the unlikely case that stuff can be loaded from camel-util + + url = ObjectHelper.class.getClassLoader().getResource(resolvedName); + if (url != null) return url; + + url = ObjectHelper.class.getResource(resolvedName); return url; } @@ -561,51 +567,70 @@ public static URL loadResourceAsURL(String name, ClassLoader loader) { * Attempts to load the given resources from the given package name using the thread context * class loader or the class loader used to load this class * - * @param packageName the name of the package to load its resources + * @param uri the name of the package to load its resources * @return the URLs for the resources or null if it could not be loaded */ - public static Enumeration loadResourcesAsURL(String packageName) { - return loadResourcesAsURL(packageName, null); + public static Enumeration loadResourcesAsURL(String uri) { + return loadResourcesAsURL(uri, null); } /** * Attempts to load the given resources from the given package name using the thread context * class loader or the class loader used to load this class * - * @param packageName the name of the package to load its resources + * @param uri the name of the package to load its resources * @param loader optional classloader to attempt first * @return the URLs for the resources or null if it could not be loaded */ - public static Enumeration loadResourcesAsURL(String packageName, ClassLoader loader) { - Enumeration url = null; + public static Enumeration loadResourcesAsURL(String uri, ClassLoader loader) { + + Enumeration res = null; + // #1 First, try the given class loader + if (loader != null) { - try { - url = loader.getResources(packageName); - } catch (IOException e) { - // ignore - } - } - - if (url == null) { - ClassLoader contextClassLoader = Thread.currentThread().getContextClassLoader(); - if (contextClassLoader != null) { - try { - url = contextClassLoader.getResources(packageName); - } catch (IOException e) { - // ignore - } - } + try { + res = loader.getResources(uri); + if (res != null) return res; + } catch (IOException e) { + // ignore + } } - if (url == null) { - try { - url = ObjectHelper.class.getClassLoader().getResources(packageName); - } catch (IOException e) { - // ignore - } + + // #2 Next, is the TCCL + + ClassLoader tccl = Thread.currentThread().getContextClassLoader(); + if (tccl != null) { + + try { + res = tccl.getResources(uri); + if (res != null) return res; + } catch (IOException e1) { + // ignore + } + + // #3 The TCCL may be able to see camel-core, but not META-INF resources + + try { + + Class clazz = tccl.loadClass("org.apache.camel.impl.DefaultCamelContext"); + res = clazz.getClassLoader().getResources(uri); + if (res != null) return res; + + } catch (ClassNotFoundException | IOException e) { + // ignore + } } - - return url; + + // #4 Last, for the unlikely case that stuff can be loaded from camel-util + + try { + res = ObjectHelper.class.getClassLoader().getResources(uri); + } catch (IOException e) { + // ignore + } + + return res; } /**