Skip to content

Commit a50d91f

Browse files
authored
[client] support custom classloader (#607)
1 parent 2522b23 commit a50d91f

File tree

4 files changed

+26
-6
lines changed

4 files changed

+26
-6
lines changed

http-client/src/main/java/io/avaje/http/client/DHttpApi.java

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,11 +19,11 @@ final class DHttpApi {
1919
private final Map<Class<?>, HttpApiProvider<?>> providerMap = new HashMap<>();
2020

2121
DHttpApi() {
22-
init();
22+
this(Thread.currentThread().getContextClassLoader());
2323
}
2424

25-
void init() {
26-
for (final var apiProvider : ServiceLoader.load(GeneratedComponent.class)) {
25+
DHttpApi(ClassLoader classLoader) {
26+
for (final var apiProvider : ServiceLoader.load(GeneratedComponent.class, classLoader)) {
2727
apiProvider.register(providerMap);
2828
}
2929
log.log(DEBUG, "providers for {0}", providerMap.keySet());
@@ -46,4 +46,9 @@ <T> T provideFor(Class<T> type, HttpClient httpClient) {
4646
static <T> T get(Class<T> type, HttpClient httpClient) {
4747
return INSTANCE.provideFor(type, httpClient);
4848
}
49+
50+
static <T> T get(Class<T> clientInterface, HttpClient httpClient, ClassLoader classLoader) {
51+
return new DHttpApi(classLoader).provideFor(clientInterface, httpClient);
52+
}
53+
4954
}

http-client/src/main/java/io/avaje/http/client/DHttpClientContext.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,11 @@ public <T> T create(Class<T> clientInterface) {
7979
return DHttpApi.get(clientInterface, this);
8080
}
8181

82+
@Override
83+
public <T> T create(Class<T> clientInterface, ClassLoader classLoader) {
84+
return DHttpApi.get(clientInterface, this, classLoader);
85+
}
86+
8287
@Override
8388
public HttpClientRequest request() {
8489
return retryHandler == null

http-client/src/main/java/io/avaje/http/client/HttpClient.java

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,16 @@ static Builder builder() {
6262
*/
6363
<T> T create(Class<T> clientInterface);
6464

65+
/**
66+
* Return the http client API implementation using the given ClassLoader.
67+
*
68+
* @param clientInterface A <code>@Client</code> interface with annotated API methods.
69+
* @param classLoader The ClassLoader to use to service load the implementation
70+
* @param <T> The service type.
71+
* @return The http client API implementation.
72+
*/
73+
<T> T create(Class<T> clientInterface, ClassLoader classLoader);
74+
6575
/**
6676
* Create a new request.
6777
*/

http-client/src/test/java/io/avaje/http/client/DHttpApiTest.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,14 +18,14 @@ class DHttpApiTest {
1818
@Test
1919
void test_github_listRepos() {
2020

21-
final var clientContext = HttpClient.builder()
21+
final var client = HttpClient.builder()
2222
.baseUrl("https://api.github.com")
2323
.bodyAdapter(new JacksonBodyAdapter())
2424
.build();
2525

2626
DHttpApi httpApi = new DHttpApi();
2727
httpApi.addProvider(Simple.class, Simple$HttpClient::new);
28-
final Simple simple = httpApi.provideFor(Simple.class, clientContext);
28+
final Simple simple = httpApi.provideFor(Simple.class, client);
2929

3030
final List<Repo> repos = simple.listRepos("rbygrave", "junk");
3131
assertThat(repos).isNotEmpty();
@@ -44,7 +44,7 @@ void jsonb_github_listRepos() {
4444
.bodyAdapter(new JsonbBodyAdapter(jsonb))
4545
.build();
4646

47-
DHttpApi httpApi = new DHttpApi();
47+
DHttpApi httpApi = new DHttpApi(Thread.currentThread().getContextClassLoader());
4848
httpApi.addProvider(Simple.class, Simple$HttpClient::new);
4949
final Simple simple = httpApi.provideFor(Simple.class, client);
5050

0 commit comments

Comments
 (0)