Skip to content

Commit

Permalink
Add a fallback for JsonValidationProvider#provider()
Browse files Browse the repository at this point in the history
See Issue #40
  • Loading branch information
leadpony committed Dec 8, 2019
1 parent 2b1bbff commit 4f62548
Showing 1 changed file with 16 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -42,13 +42,14 @@ public abstract class JsonValidationProvider {
* @throws JsonException if there is no provider found.
*/
public static JsonValidationProvider provider() {
ServiceLoader<JsonValidationProvider> loader = ServiceLoader.load(JsonValidationProvider.class);
Iterator<JsonValidationProvider> it = loader.iterator();
if (it.hasNext()) {
return it.next();
} else {
throw new JsonException("JSON validation provider was not found.");
JsonValidationProvider provider = loadProvider(Thread.currentThread().getContextClassLoader());
if (provider == null) {
provider = loadProvider(JsonValidationProvider.class.getClassLoader());
if (provider == null) {
throw new JsonException("JSON validation provider was not found.");
}
}
return provider;
}

/**
Expand All @@ -64,4 +65,13 @@ protected JsonValidationProvider() {
* @throws JsonException if an error is encountered while creating the instance.
*/
public abstract JsonValidationService createService();

private static JsonValidationProvider loadProvider(ClassLoader classLoader) {
ServiceLoader<JsonValidationProvider> loader = ServiceLoader.load(JsonValidationProvider.class, classLoader);
Iterator<JsonValidationProvider> it = loader.iterator();
if (it.hasNext()) {
return it.next();
}
return null;
}
}

0 comments on commit 4f62548

Please sign in to comment.