This is an automated email from the ASF dual-hosted git repository.

davsclaus pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/camel.git


The following commit(s) were added to refs/heads/master by this push:
     new e220844  [CAMEL-13076] ObjectHelper does no longer see 
META-INF/services from camel-core (#2715)
e220844 is described below

commit e220844b37e8f7fbeb34122cf79eca2c781f5401
Author: Thomas Diesler <thomas.dies...@jboss.com>
AuthorDate: Fri Jan 18 10:36:39 2019 +0100

    [CAMEL-13076] ObjectHelper does no longer see META-INF/services from 
camel-core (#2715)
---
 .../java/org/apache/camel/util/ObjectHelper.java   | 157 ++++++++++++---------
 1 file changed, 91 insertions(+), 66 deletions(-)

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 f9acd94..58798d3 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 final class ObjectHelper {
     }
 
     /**
-     * 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 final class ObjectHelper {
      * @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 final class ObjectHelper {
      * 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<URL> loadResourcesAsURL(String packageName) {
-        return loadResourcesAsURL(packageName, null);
+    public static Enumeration<URL> 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<URL> loadResourcesAsURL(String packageName, 
ClassLoader loader) {
-        Enumeration<URL> url = null;
+    public static Enumeration<URL> loadResourcesAsURL(String uri, ClassLoader 
loader) {
+       
+        Enumeration<URL> 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;
     }
 
     /**

Reply via email to