Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[CAMEL-13076] ObjectHelper does no longer see META-INF/services from … #2715

Merged
merged 1 commit into from Jan 18, 2019
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I looked into the JDK source and its loadResourceAsStream does the same here to load via URL first and then open its stream. So we can safely do this also in Camel.

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