Skip to content

Commit 5280a5c

Browse files
ueiselesrnagar
andauthored
azure-core: use classloader that loaded providers class to load individual provider classes (Azure#20760)
* azure-core: use classloader that loaded providers class to load individual provider classes * fix HttpClientProviders: use HttpClientProviders classloader instead of HttpClientProvider Co-authored-by: Srikanta <51379715+srnagar@users.noreply.github.com> Co-authored-by: Srikanta <51379715+srnagar@users.noreply.github.com>
1 parent 62127ba commit 5280a5c

File tree

5 files changed

+38
-6
lines changed

5 files changed

+38
-6
lines changed

sdk/core/azure-core/src/main/java/com/azure/core/http/policy/HttpPolicyProviders.java

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,15 @@ public final class HttpPolicyProviders {
1717
private static final List<AfterRetryPolicyProvider> AFTER_PROVIDER = new ArrayList<>();
1818

1919
static {
20-
ServiceLoader.load(BeforeRetryPolicyProvider.class).forEach(BEFORE_PROVIDER::add);
21-
ServiceLoader.load(AfterRetryPolicyProvider.class).forEach(AFTER_PROVIDER::add);
20+
// Use as classloader to load provider-configuration files and provider classes the classloader
21+
// that loaded this class. In most cases this will be the System classloader.
22+
// But this choice here provides additional flexibility in managed environments that control
23+
// classloading differently (OSGi, Spring and others) and don't/ depend on the
24+
// System classloader to load BeforeRetryPolicyProvider and AfterRetryPolicyProvider classes.
25+
ServiceLoader.load(BeforeRetryPolicyProvider.class, HttpPolicyProviders.class.getClassLoader())
26+
.forEach(BEFORE_PROVIDER::add);
27+
ServiceLoader.load(AfterRetryPolicyProvider.class, HttpPolicyProviders.class.getClassLoader())
28+
.forEach(AFTER_PROVIDER::add);
2229
}
2330

2431
private HttpPolicyProviders() {

sdk/core/azure-core/src/main/java/com/azure/core/implementation/http/HttpClientProviders.java

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,14 @@ public final class HttpClientProviders {
2323
private static HttpClientProvider defaultProvider;
2424

2525
static {
26-
ServiceLoader<HttpClientProvider> serviceLoader = ServiceLoader.load(HttpClientProvider.class);
26+
// Use as classloader to load provider-configuration files and provider classes the classloader
27+
// that loaded this class. In most cases this will be the System classloader.
28+
// But this choice here provides additional flexibility in managed environments that control
29+
// classloading differently (OSGi, Spring and others) and don't/ depend on the
30+
// System classloader to load HttpClientProvider classes.
31+
ServiceLoader<HttpClientProvider> serviceLoader = ServiceLoader.load(
32+
HttpClientProvider.class,
33+
HttpClientProviders.class.getClassLoader());
2734
// Use the first provider found in the service loader iterator.
2835
Iterator<HttpClientProvider> it = serviceLoader.iterator();
2936
if (it.hasNext()) {

sdk/core/azure-core/src/main/java/com/azure/core/util/serializer/JsonSerializerProviders.java

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,14 @@ private static synchronized void loadDefaultSerializer() {
6565
}
6666

6767
attemptedLoad = true;
68-
Iterator<JsonSerializerProvider> iterator = ServiceLoader.load(JsonSerializerProvider.class).iterator();
68+
// Use as classloader to load provider-configuration files and provider classes the classloader
69+
// that loaded this class. In most cases this will be the System classloader.
70+
// But this choice here provides additional flexibility in managed environments that control
71+
// classloading differently (OSGi, Spring and others) and don't/ depend on the
72+
// System classloader to load JsonSerializerProviders classes.
73+
Iterator<JsonSerializerProvider> iterator =
74+
ServiceLoader.load(JsonSerializerProvider.class, JsonSerializerProviders.class.getClassLoader())
75+
.iterator();
6976
if (iterator.hasNext()) {
7077
jsonSerializerProvider = iterator.next();
7178
}

sdk/core/azure-core/src/main/java/com/azure/core/util/serializer/MemberNameConverterProviders.java

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,8 +38,14 @@ private static synchronized void loadFromClasspath() {
3838
}
3939

4040
attemptedLoad = true;
41+
// Use as classloader to load provider-configuration files and provider classes the classloader
42+
// that loaded this class. In most cases this will be the System classloader.
43+
// But this choice here provides additional flexibility in managed environments that control
44+
// classloading differently (OSGi, Spring and others) and don't/ depend on the
45+
// System classloader to load MemberNameConverterProviders classes.
4146
Iterator<MemberNameConverterProvider> iterator =
42-
ServiceLoader.load(MemberNameConverterProvider.class).iterator();
47+
ServiceLoader.load(MemberNameConverterProvider.class, MemberNameConverterProviders.class.getClassLoader())
48+
.iterator();
4349
if (iterator.hasNext()) {
4450
defaultProvider = iterator.next();
4551
} else {

sdk/core/azure-core/src/main/java/com/azure/core/util/tracing/TracerProxy.java

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,12 @@ public final class TracerProxy {
1818
private static Tracer tracer;
1919

2020
static {
21-
ServiceLoader<Tracer> serviceLoader = ServiceLoader.load(Tracer.class);
21+
// Use as classloader to load provider-configuration files and provider classes the classloader
22+
// that loaded this class. In most cases this will be the System classloader.
23+
// But this choice here provides additional flexibility in managed environments that control
24+
// classloading differently (OSGi, Spring and others) and don't/ depend on the
25+
// System classloader to load Tracer classes.
26+
ServiceLoader<Tracer> serviceLoader = ServiceLoader.load(Tracer.class, TracerProxy.class.getClassLoader());
2227
Iterator<?> iterator = serviceLoader.iterator();
2328
if (iterator.hasNext()) {
2429
tracer = serviceLoader.iterator().next();

0 commit comments

Comments
 (0)