Skip to content

Commit

Permalink
Allow for multiple JSON providers
Browse files Browse the repository at this point in the history
Signed-off-by: Jorge Bescos Gascon <[email protected]>
  • Loading branch information
jbescos committed Jun 8, 2022
1 parent 7f7ba7e commit fbc40b5
Showing 1 changed file with 34 additions and 2 deletions.
36 changes: 34 additions & 2 deletions api/src/main/java/jakarta/json/spi/JsonProvider.java
Original file line number Diff line number Diff line change
Expand Up @@ -98,15 +98,47 @@ protected JsonProvider() {
*
* @see ServiceLoader
* @return a JSON provider
*
*/
public static JsonProvider provider() {
return provider(null);
}

/**
* Creates a JSON provider object.
*
* Implementation discovery consists of following steps:
* <ol>
* <li>If the system property {@value #JSONP_PROVIDER_FACTORY} exists,
* then its value is assumed to be the provider factory class.
* This phase of the look up enables per-JVM override of the JsonProvider implementation.</li>
* <li>The provider is loaded using the {@link ServiceLoader#load(Class)} method. When 'providerClassName'
* is not null, it will return the instance having the same class qualified name if exists. This
* is useful when more than one JsonProvider is loaded by the {@link ServiceLoader#load(Class)}.</li>
* <li>If all the steps above fail, then the rest of the look up is unspecified. That said,
* the recommended behavior is to simply look for some hard-coded platform default Jakarta
* JSON Processing implementation. This phase of the look up is so that a platform can have
* its own Jakarta JSON Processing implementation as the last resort.</li>
* </ol>
* Users are recommended to cache the result of this method.
*
* @see ServiceLoader
* @param providerClassName The name of the class to be found from the {@link ServiceLoader#load(Class)}.
* @return a JSON provider
*
* @since 2.1.1
*/
public static JsonProvider provider(String providerClassName) {
if (LazyFactoryLoader.JSON_PROVIDER != null) {
return newInstance(LazyFactoryLoader.JSON_PROVIDER);
}
ServiceLoader<JsonProvider> loader = ServiceLoader.load(JsonProvider.class);
Iterator<JsonProvider> it = loader.iterator();
if (it.hasNext()) {
return it.next();
while (it.hasNext()) {
JsonProvider provider = it.next();
if (providerClassName == null || provider.getClass().getName().equals(providerClassName)) {
return provider;
}
}

// handling OSGi (specific default)
Expand Down

0 comments on commit fbc40b5

Please sign in to comment.