File tree 2 files changed +24
-2
lines changed
main/java/co/elastic/clients/json
test/java/co/elastic/clients/json
2 files changed +24
-2
lines changed Original file line number Diff line number Diff line change 42
42
43
43
public class JsonpUtils {
44
44
45
+ private static JsonProvider systemJsonProvider = null ;
46
+
45
47
/**
46
48
* Get a <code>JsonProvider</code> instance. This method first calls the standard `JsonProvider.provider()` that is based on
47
- * the current thread's context classloader, and in case of failure tries to find a provider in other classloaders.
49
+ * the current thread's context classloader, and in case of failure tries to find a provider in other classloaders. The
50
+ * value is cached for subsequent calls.
48
51
*/
49
- @ AllowForbiddenApis ("Implementation of the JsonProvider lookup" )
50
52
public static JsonProvider provider () {
53
+ JsonProvider result = systemJsonProvider ;
54
+ if (result == null ) {
55
+ result = findProvider ();
56
+ systemJsonProvider = result ;
57
+ }
58
+ return result ;
59
+ }
60
+
61
+ @ AllowForbiddenApis ("Implementation of the JsonProvider lookup" )
62
+ static JsonProvider findProvider () {
51
63
RuntimeException exception ;
52
64
try {
53
65
return JsonProvider .provider ();
Original file line number Diff line number Diff line change @@ -69,6 +69,16 @@ public Enumeration<URL> getResources(String name) {
69
69
}
70
70
}
71
71
72
+ @ Test
73
+ @ AllowForbiddenApis ("Testing JsonpUtil.provider()" )
74
+ public void testProviderCache () {
75
+ // A new provider at each call
76
+ assertNotSame (JsonpUtils .findProvider (), JsonpUtils .findProvider ());
77
+
78
+ // Result is cached
79
+ assertSame (JsonpUtils .provider (), JsonpUtils .provider ());
80
+ }
81
+
72
82
@ Test
73
83
public void testObjectToString () {
74
84
// Test that we call toString() on application classes.
You can’t perform that action at this time.
0 commit comments