Skip to content

Commit df40e20

Browse files
committed
simplify the provider lookup, add logging
Signed-off-by: Lukas Jungmann <[email protected]>
1 parent fda87c3 commit df40e20

File tree

2 files changed

+46
-51
lines changed

2 files changed

+46
-51
lines changed

api/src/main/java/jakarta/json/spi/JsonProvider.java

Lines changed: 45 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -23,11 +23,15 @@
2323
import java.lang.reflect.Method;
2424
import java.math.BigDecimal;
2525
import java.math.BigInteger;
26+
import java.security.AccessController;
27+
import java.security.PrivilegedAction;
2628
import java.util.Collection;
2729
import java.util.Iterator;
2830
import java.util.Map;
2931
import java.util.Optional;
3032
import java.util.ServiceLoader;
33+
import java.util.logging.Level;
34+
import java.util.logging.Logger;
3135

3236
import jakarta.json.JsonArray;
3337
import jakarta.json.JsonArrayBuilder;
@@ -74,6 +78,9 @@ public abstract class JsonProvider {
7478
private static final String DEFAULT_PROVIDER
7579
= "org.eclipse.parsson.JsonProviderImpl";
7680

81+
/** A logger */
82+
private static final Logger LOG = Logger.getLogger(JsonProvider.class.getName());
83+
7784
/**
7885
* Default constructor.
7986
*/
@@ -100,48 +107,62 @@ protected JsonProvider() {
100107
* @return a JSON provider
101108
*/
102109
public static JsonProvider provider() {
103-
if (LazyFactoryLoader.JSON_PROVIDER != null) {
104-
return newInstance(LazyFactoryLoader.JSON_PROVIDER);
110+
LOG.log(Level.FINE, "Checking system property {0}", JSONP_PROVIDER_FACTORY);
111+
final String factoryClassName = System.getSecurityManager() != null
112+
? AccessController.doPrivileged((PrivilegedAction<String>) () -> System.getProperty(JSONP_PROVIDER_FACTORY))
113+
: System.getProperty(JSONP_PROVIDER_FACTORY);
114+
if (factoryClassName != null) {
115+
JsonProvider provider = newInstance(factoryClassName);
116+
LOG.log(Level.FINE, "System property used; returning object [{0}]",
117+
provider.getClass().getName());
118+
return provider;
105119
}
120+
121+
LOG.log(Level.FINE, "Checking ServiceLoader");
106122
ServiceLoader<JsonProvider> loader = ServiceLoader.load(JsonProvider.class);
107123
Iterator<JsonProvider> it = loader.iterator();
108124
if (it.hasNext()) {
109-
return it.next();
125+
JsonProvider provider = it.next();
126+
LOG.log(Level.FINE, "ServiceLoader loading Facility used; returning object [{0}]",
127+
provider.getClass().getName());
128+
return provider;
110129
}
111130

112131
// handling OSGi (specific default)
113132
if (isOsgi()) {
114-
JsonProvider result = lookupUsingOSGiServiceLoader(JsonProvider.class);
115-
if (result != null) {
116-
return result;
133+
LOG.log(Level.FINE, "Checking OSGi");
134+
JsonProvider provider = lookupUsingOSGiServiceLoader(JsonProvider.class);
135+
if (provider != null) {
136+
LOG.log(Level.FINE, "OSGi loading facility used; returning object [{0}].",
137+
provider.getClass().getName());
138+
return provider;
117139
}
118140
}
119141

120-
try {
121-
Class<?> clazz = Class.forName(DEFAULT_PROVIDER);
122-
return (JsonProvider) clazz.getConstructor().newInstance();
123-
} catch (ClassNotFoundException x) {
124-
throw new JsonException(
125-
"Provider " + DEFAULT_PROVIDER + " not found", x);
126-
} catch (Exception x) {
127-
throw new JsonException(
128-
"Provider " + DEFAULT_PROVIDER + " could not be instantiated: " + x,
129-
x);
130-
}
142+
// else no provider found
143+
LOG.fine("Trying to create the platform default provider");
144+
return newInstance(DEFAULT_PROVIDER);
131145
}
132146

133147
/**
134148
* Creates a new instance from the specified class
135-
* @param clazz class to instance
149+
* @param className class to instantiate
136150
* @return the JsonProvider instance
137151
* @throws IllegalArgumentException for reflection issues
138152
*/
139-
private static JsonProvider newInstance(Class<? extends JsonProvider> clazz) {
140-
checkPackageAccess(clazz.getName());
153+
private static JsonProvider newInstance(String className) {
141154
try {
155+
checkPackageAccess(className);
156+
@SuppressWarnings({"unchecked"})
157+
Class<JsonProvider> clazz = (Class<JsonProvider>) Class.forName(className);
142158
return clazz.getConstructor().newInstance();
143-
} catch (ReflectiveOperationException e) {
144-
throw new IllegalArgumentException("Unable to create " + clazz.getName(), e);
159+
} catch (ClassNotFoundException x) {
160+
throw new JsonException(
161+
"Provider " + DEFAULT_PROVIDER + " not found", x);
162+
} catch (Exception x) {
163+
throw new JsonException(
164+
"Provider " + DEFAULT_PROVIDER + " could not be instantiated: " + x,
165+
x);
145166
}
146167
}
147168

@@ -617,37 +638,10 @@ private static <T> T lookupUsingOSGiServiceLoader(Class<? extends T> serviceClas
617638
@SuppressWarnings({"unchecked"})
618639
Iterator<? extends T> iter = ((Iterable<? extends T>) m.invoke(null, (Object[]) args)).iterator();
619640
return iter.hasNext() ? iter.next() : null;
620-
} catch (Exception ignored) {
621-
// log and continue
641+
} catch (Exception ex) {
642+
LOG.log(Level.FINE, "Unable to find from OSGi: [" + serviceClass.getName() + "]", ex);
622643
return null;
623644
}
624645
}
625646

626-
/**
627-
* Lazy loads the class specified in System property with the key JSONP_PROVIDER_FACTORY.
628-
* If no property is set, the value of {@link #JSON_PROVIDER} will be null.
629-
* In case of errors an IllegalStateException is thrown.
630-
*
631-
*/
632-
@SuppressWarnings("unchecked")
633-
private static class LazyFactoryLoader {
634-
635-
/**
636-
* JSON provider class
637-
*/
638-
private static final Class<? extends JsonProvider> JSON_PROVIDER;
639-
640-
static {
641-
String className = System.getProperty(JSONP_PROVIDER_FACTORY);
642-
if (className != null) {
643-
try {
644-
JSON_PROVIDER = (Class<? extends JsonProvider>) Class.forName(className);
645-
} catch (ReflectiveOperationException e) {
646-
throw new IllegalStateException("Unable to create " + className, e);
647-
}
648-
} else {
649-
JSON_PROVIDER = null;
650-
}
651-
}
652-
}
653647
}

api/src/main/java/module-info.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
* Jakarta JSON Processing API.
1919
*/
2020
module jakarta.json {
21+
requires java.logging;
2122
exports jakarta.json;
2223
exports jakarta.json.spi;
2324
exports jakarta.json.stream;

0 commit comments

Comments
 (0)