1
15
16 package software.amazon.awssdk.core.internal.http.loader;
17
18 import java.util.Optional;
19 import software.amazon.awssdk.annotations.SdkInternalApi;
20 import software.amazon.awssdk.core.SdkSystemSetting;
21 import software.amazon.awssdk.core.exception.SdkClientException;
22 import software.amazon.awssdk.http.SdkHttpService;
23 import software.amazon.awssdk.http.async.SdkAsyncHttpService;
24 import software.amazon.awssdk.utils.SystemSetting;
25
26
30 @SdkInternalApi
31 final class SystemPropertyHttpServiceProvider<T> implements SdkHttpServiceProvider<T> {
32
33 private final SystemSetting implSetting;
34 private final Class<T> serviceClass;
35
36
40 private SystemPropertyHttpServiceProvider(SystemSetting implSetting, Class<T> serviceClass) {
41 this.implSetting = implSetting;
42 this.serviceClass = serviceClass;
43 }
44
45 @Override
46 public Optional<T> loadService() {
47 return implSetting
48 .getStringValue()
49 .map(this::createServiceFromProperty);
50 }
51
52 private T createServiceFromProperty(String httpImplFqcn) {
53 try {
54 return serviceClass.cast(Class.forName(httpImplFqcn).newInstance());
55 } catch (Exception e) {
56 throw SdkClientException.builder()
57 .message(String.format("Unable to load the HTTP factory implementation from the "
58 + "%s system property. Ensure the class '%s' is present on the classpath" +
59 "and has a no-arg constructor",
60 SdkSystemSetting.SYNC_HTTP_SERVICE_IMPL.property(), httpImplFqcn))
61 .cause(e)
62 .build();
63 }
64 }
65
66
69 static SystemPropertyHttpServiceProvider<SdkHttpService> syncProvider() {
70 return new SystemPropertyHttpServiceProvider<>(SdkSystemSetting.SYNC_HTTP_SERVICE_IMPL, SdkHttpService.class);
71 }
72
73
76 static SystemPropertyHttpServiceProvider<SdkAsyncHttpService> asyncProvider() {
77 return new SystemPropertyHttpServiceProvider<>(SdkSystemSetting.ASYNC_HTTP_SERVICE_IMPL, SdkAsyncHttpService.class);
78 }
79 }
80