Skip to content

[8.6] Cache the result of JsonProvider.provider() (#485) #486

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

Merged
merged 1 commit into from
Jan 16, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 14 additions & 2 deletions java-client/src/main/java/co/elastic/clients/json/JsonpUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -42,12 +42,24 @@

public class JsonpUtils {

private static JsonProvider systemJsonProvider = null;

/**
* Get a <code>JsonProvider</code> instance. This method first calls the standard `JsonProvider.provider()` that is based on
* the current thread's context classloader, and in case of failure tries to find a provider in other classloaders.
* the current thread's context classloader, and in case of failure tries to find a provider in other classloaders. The
* value is cached for subsequent calls.
*/
@AllowForbiddenApis("Implementation of the JsonProvider lookup")
public static JsonProvider provider() {
JsonProvider result = systemJsonProvider;
if (result == null) {
result = findProvider();
systemJsonProvider = result;
}
return result;
}

@AllowForbiddenApis("Implementation of the JsonProvider lookup")
static JsonProvider findProvider() {
RuntimeException exception;
try {
return JsonProvider.provider();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,16 @@ public Enumeration<URL> getResources(String name) {
}
}

@Test
@AllowForbiddenApis("Testing JsonpUtil.provider()")
public void testProviderCache() {
// A new provider at each call
assertNotSame(JsonpUtils.findProvider(), JsonpUtils.findProvider());

// Result is cached
assertSame(JsonpUtils.provider(), JsonpUtils.provider());
}

@Test
public void testObjectToString() {
// Test that we call toString() on application classes.
Expand Down