You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@logging.apache.org by ma...@apache.org on 2022/10/31 00:53:21 UTC
[logging-log4j2] 13/13: Catch ServiceConfigurationError in OSGi service loading
This is an automated email from the ASF dual-hosted git repository.
mattsicker pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/logging-log4j2.git
commit 2a03c3c6348698438293ad22177789d1997c2ba6
Author: Matt Sicker <ma...@apache.org>
AuthorDate: Sun Oct 30 19:51:53 2022 -0500
Catch ServiceConfigurationError in OSGi service loading
Signed-off-by: Matt Sicker <ma...@apache.org>
---
.../apache/logging/log4j/util3/ServiceRegistry.java | 18 ++++++++++++++++--
1 file changed, 16 insertions(+), 2 deletions(-)
diff --git a/log4j-api/src/main/java/org/apache/logging/log4j/util3/ServiceRegistry.java b/log4j-api/src/main/java/org/apache/logging/log4j/util3/ServiceRegistry.java
index f493c6110e..af57a6adc9 100644
--- a/log4j-api/src/main/java/org/apache/logging/log4j/util3/ServiceRegistry.java
+++ b/log4j-api/src/main/java/org/apache/logging/log4j/util3/ServiceRegistry.java
@@ -104,8 +104,22 @@ public class ServiceRegistry {
public <S> void loadServicesFromBundle(
final Class<S> serviceType, final long bundleId, final ClassLoader bundleClassLoader) {
final List<S> services = new ArrayList<>();
- for (final S service : ServiceLoader.load(serviceType, bundleClassLoader)) {
- services.add(service);
+ try {
+ final ServiceLoader<S> serviceLoader = ServiceLoader.load(serviceType, bundleClassLoader);
+ final Iterator<S> iterator = serviceLoader.iterator();
+ while (iterator.hasNext()) {
+ try {
+ services.add(iterator.next());
+ } catch (final ServiceConfigurationError sce) {
+ final String message = String.format("Unable to load %s service in bundle id %d",
+ serviceType.getName(), bundleId);
+ LowLevelLogUtil.logException(message, sce);
+ }
+ }
+ } catch (final ServiceConfigurationError e) {
+ final String message = String.format("Unable to load any %s services in bundle id %d",
+ serviceType.getName(), bundleId);
+ LowLevelLogUtil.logException(message, e);
}
registerBundleServices(serviceType, bundleId, services);
}