This is an automated email from the ASF dual-hosted git repository.

ggregory pushed a commit to branch 2.x
in repository https://gitbox.apache.org/repos/asf/logging-log4j2.git


The following commit(s) were added to refs/heads/2.x by this push:
     new 8e4f34a596 Log a status error event instead of an NPE
8e4f34a596 is described below

commit 8e4f34a596d6ab460b6a4774878906ad5b5d6bb8
Author: Gary Gregory <[email protected]>
AuthorDate: Fri Mar 10 10:20:13 2023 -0500

    Log a status error event instead of an NPE
    
    See org.apache.logging.log4j.util.OsgiServiceLocator.loadServices(Class,
    Lookup, boolean) when a bundle has no valid BundleContext for a service
    type.
---
 .../logging/log4j/util/OsgiServiceLocator.java     | 15 ++++++++----
 src/changelog/.2.x.x/OsgiServiceLocator_npe.xml    | 28 ++++++++++++++++++++++
 2 files changed, 39 insertions(+), 4 deletions(-)

diff --git 
a/log4j-api/src/main/java/org/apache/logging/log4j/util/OsgiServiceLocator.java 
b/log4j-api/src/main/java/org/apache/logging/log4j/util/OsgiServiceLocator.java
index 9b3663d2f7..b5ffb41dbd 100644
--- 
a/log4j-api/src/main/java/org/apache/logging/log4j/util/OsgiServiceLocator.java
+++ 
b/log4j-api/src/main/java/org/apache/logging/log4j/util/OsgiServiceLocator.java
@@ -17,6 +17,7 @@
 package org.apache.logging.log4j.util;
 
 import java.lang.invoke.MethodHandles.Lookup;
+import java.util.Objects;
 import java.util.stream.Stream;
 
 import org.apache.logging.log4j.status.StatusLogger;
@@ -54,13 +55,19 @@ public class OsgiServiceLocator {
     }
 
     public static <T> Stream<T> loadServices(final Class<T> serviceType, final 
Lookup lookup, final boolean verbose) {
-        final Bundle bundle = FrameworkUtil.getBundle(lookup.lookupClass());
+        final Class<?> lookupClass = Objects.requireNonNull(lookup, 
"lookup").lookupClass();
+        final Bundle bundle = 
FrameworkUtil.getBundle(Objects.requireNonNull(lookupClass, "lookupClass"));
         if (bundle != null) {
             final BundleContext ctx = bundle.getBundleContext();
+            if (ctx == null) {
+                if (verbose) {
+                    StatusLogger.getLogger().error(
+                            "Unable to load OSGI services: The bundle has no 
valid BundleContext for serviceType = {}, lookup = {}, lookupClass = {}, bundle 
= {}",
+                            serviceType, lookup, lookupClass, bundle);
+                }
+            }
             try {
-                return ctx.getServiceReferences(serviceType, null)
-                        .stream()
-                        .map(ctx::getService);
+                return ctx.getServiceReferences(serviceType, 
null).stream().map(ctx::getService);
             } catch (Throwable e) {
                 if (verbose) {
                     StatusLogger.getLogger().error("Unable to load OSGI 
services for service {}", serviceType, e);
diff --git a/src/changelog/.2.x.x/OsgiServiceLocator_npe.xml 
b/src/changelog/.2.x.x/OsgiServiceLocator_npe.xml
new file mode 100644
index 0000000000..866397539b
--- /dev/null
+++ b/src/changelog/.2.x.x/OsgiServiceLocator_npe.xml
@@ -0,0 +1,28 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<!--
+   Licensed to the Apache Software Foundation (ASF) under one or more
+   contributor license agreements.  See the NOTICE file distributed with
+   this work for additional information regarding copyright ownership.
+   The ASF licenses this file to You under the Apache License, Version 2.0
+   (the "License"); you may not use this file except in compliance with
+   the License.  You may obtain a copy of the License at
+
+       http://www.apache.org/licenses/LICENSE-2.0
+
+   Unless required by applicable law or agreed to in writing, software
+   distributed under the License is distributed on an "AS IS" BASIS,
+   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+   See the License for the specific language governing permissions and
+   limitations under the License.
+-->
+<entry xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance";
+       xmlns="http://logging.apache.org/log4j/changelog";
+       xsi:schemaLocation="http://logging.apache.org/log4j/changelog 
https://logging.apache.org/log4j/changelog-0.1.0.xsd";
+       type="changed">
+  <author id="ggregory"/>
+  <description format="asciidoc">
+    Log a status error event instead of an NPE in 
+    org.apache.logging.log4j.util.OsgiServiceLocator.loadServices(Class, 
Lookup, boolean)
+    when a bundle has no valid BundleContext for a service type.
+  </description>
+</entry>

Reply via email to