Skip to content

Commit 87fe163

Browse files
committed
Update and ClassLoaderHelper#classLoader to load class using the class loader of that class and fallback to context class loader and system class loader. See #56
Address comments and make classLoader fallback to contextClassLoader
1 parent 474d34b commit 87fe163

File tree

6 files changed

+46
-17
lines changed

6 files changed

+46
-17
lines changed
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
{
2+
"category": "AWS SDK for Java v2",
3+
"type": "bugfix",
4+
"description": "Use the class loader that loaded the SDK to load the HTTP implementations. See [#56](https://github.com/aws/aws-sdk-java-v2/issues/56)"
5+
}

core/sdk-core/src/main/java/software/amazon/awssdk/core/interceptor/ClasspathInterceptorChainFactory.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -136,7 +136,7 @@ private ExecutionInterceptor createExecutionInterceptor(String interceptorClassN
136136
}
137137

138138
private ClassLoader classLoader() {
139-
return Validate.notNull(ClassLoaderHelper.classLoader(),
140-
"Failed to load the classloader for the current thread or the system.");
139+
return Validate.notNull(ClassLoaderHelper.classLoader(getClass()),
140+
"Failed to load the classloader of this class or the system.");
141141
}
142142
}

core/sdk-core/src/main/java/software/amazon/awssdk/core/internal/http/loader/SdkServiceLoader.java

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,16 +18,17 @@
1818
import java.util.Iterator;
1919
import java.util.ServiceLoader;
2020
import software.amazon.awssdk.annotations.SdkInternalApi;
21+
import software.amazon.awssdk.core.internal.util.ClassLoaderHelper;
2122

2223
/**
23-
* Thin layer over {@link ServiceLoader} to allow mocking in tests.
24+
* Thin layer over {@link ServiceLoader}.
2425
*/
2526
@SdkInternalApi
2627
class SdkServiceLoader {
2728

2829
public static final SdkServiceLoader INSTANCE = new SdkServiceLoader();
2930

3031
<T> Iterator<T> loadServices(Class<T> clzz) {
31-
return ServiceLoader.load(clzz).iterator();
32+
return ServiceLoader.load(clzz, ClassLoaderHelper.classLoader(SdkServiceLoader.class)).iterator();
3233
}
3334
}

core/sdk-core/src/main/java/software/amazon/awssdk/core/internal/util/ClassLoaderHelper.java

Lines changed: 34 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -24,23 +24,25 @@ private ClassLoaderHelper() {
2424
}
2525

2626
private static Class<?> loadClassViaClasses(String fqcn, Class<?>[] classes) {
27-
if (classes != null) {
28-
for (Class<?> c : classes) {
29-
ClassLoader loader = c.getClassLoader();
30-
if (loader != null) {
31-
try {
32-
return loader.loadClass(fqcn);
33-
} catch (ClassNotFoundException e) {
34-
// move on to try the next class loader
35-
}
27+
if (classes == null) {
28+
return null;
29+
}
30+
31+
for (Class<?> clzz: classes) {
32+
ClassLoader loader = clzz.getClassLoader();
33+
if (loader != null) {
34+
try {
35+
return loader.loadClass(fqcn);
36+
} catch (ClassNotFoundException e) {
37+
// move on to try the next class loader
3638
}
3739
}
3840
}
3941
return null;
4042
}
4143

4244
private static Class<?> loadClassViaContext(String fqcn) {
43-
ClassLoader loader = classLoader();
45+
ClassLoader loader = contextClassLoader();
4446
try {
4547
return loader == null ? null : loader.loadClass(fqcn);
4648
} catch (ClassNotFoundException e) {
@@ -114,11 +116,32 @@ public static Class<?> loadClass(String fqcn, boolean classesFirst,
114116
* Attempt to get the current thread's class loader and fallback to the system classloader if null
115117
* @return a {@link ClassLoader} or null if none found
116118
*/
117-
public static ClassLoader classLoader() {
119+
private static ClassLoader contextClassLoader() {
118120
ClassLoader threadClassLoader = Thread.currentThread().getContextClassLoader();
119121
if (threadClassLoader != null) {
120122
return threadClassLoader;
121123
}
122124
return ClassLoader.getSystemClassLoader();
123125
}
126+
127+
/**
128+
* Attempt to get class loader that loads the classes and fallback to the thread context classloader if null.
129+
*
130+
* @param classes the classes
131+
* @return a {@link ClassLoader} or null if none found
132+
*/
133+
public static ClassLoader classLoader(Class<?>... classes) {
134+
if (classes != null) {
135+
for (Class clzz : classes) {
136+
ClassLoader classLoader = clzz.getClassLoader();
137+
138+
if (classLoader != null) {
139+
return classLoader;
140+
}
141+
}
142+
}
143+
144+
return contextClassLoader();
145+
}
146+
124147
}

core/sdk-core/src/main/java/software/amazon/awssdk/core/internal/util/Mimetype.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,7 @@ public final class Mimetype {
7171

7272
private static final String MIME_TYPE_PATH = "software/amazon/awssdk/core/util/mime.types";
7373

74-
private static final ClassLoader CLASS_LOADER = ClassLoaderHelper.classLoader();
74+
private static final ClassLoader CLASS_LOADER = ClassLoaderHelper.classLoader(Mimetype.class);
7575

7676
private static volatile Mimetype mimetype;
7777

services/sts/src/main/java/software/amazon/awssdk/services/sts/auth/StsAssumeRoleWithSamlCredentialsProvider.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -100,7 +100,7 @@ public Builder refreshRequest(AssumeRoleWithSamlRequest assumeRoleWithSamlReques
100100
}
101101

102102
/**
103-
* Similar to {@link #refreshRequest(AssumeRoleRequest)}, but takes a {@link Supplier} to supply the request to
103+
* Similar to {@link #refreshRequest(AssumeRoleWithSamlRequest)}, but takes a {@link Supplier} to supply the request to
104104
* STS.
105105
*
106106
* @param assumeRoleWithSamlRequestSupplier A supplier

0 commit comments

Comments
 (0)