Skip to content

Commit

Permalink
[CAMEL-13076] ObjectHelper does no longer see META-INF/services from …
Browse files Browse the repository at this point in the history
…camel-core
  • Loading branch information
tdiesler committed Jan 18, 2019
1 parent b0c7db3 commit b129a4f
Showing 1 changed file with 91 additions and 66 deletions.
157 changes: 91 additions & 66 deletions camel-util/src/main/java/org/apache/camel/util/ObjectHelper.java
Expand Up @@ -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;
}
}

/**
Expand All @@ -535,77 +522,115 @@ 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;
}

/**
* 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;
}

/**
Expand Down

0 comments on commit b129a4f

Please sign in to comment.