Skip to content

Commit 8e4f34a

Browse files
committed
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.
1 parent e03266d commit 8e4f34a

File tree

2 files changed

+39
-4
lines changed

2 files changed

+39
-4
lines changed

log4j-api/src/main/java/org/apache/logging/log4j/util/OsgiServiceLocator.java

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
package org.apache.logging.log4j.util;
1818

1919
import java.lang.invoke.MethodHandles.Lookup;
20+
import java.util.Objects;
2021
import java.util.stream.Stream;
2122

2223
import org.apache.logging.log4j.status.StatusLogger;
@@ -54,13 +55,19 @@ public static <T> Stream<T> loadServices(final Class<T> serviceType, final Looku
5455
}
5556

5657
public static <T> Stream<T> loadServices(final Class<T> serviceType, final Lookup lookup, final boolean verbose) {
57-
final Bundle bundle = FrameworkUtil.getBundle(lookup.lookupClass());
58+
final Class<?> lookupClass = Objects.requireNonNull(lookup, "lookup").lookupClass();
59+
final Bundle bundle = FrameworkUtil.getBundle(Objects.requireNonNull(lookupClass, "lookupClass"));
5860
if (bundle != null) {
5961
final BundleContext ctx = bundle.getBundleContext();
62+
if (ctx == null) {
63+
if (verbose) {
64+
StatusLogger.getLogger().error(
65+
"Unable to load OSGI services: The bundle has no valid BundleContext for serviceType = {}, lookup = {}, lookupClass = {}, bundle = {}",
66+
serviceType, lookup, lookupClass, bundle);
67+
}
68+
}
6069
try {
61-
return ctx.getServiceReferences(serviceType, null)
62-
.stream()
63-
.map(ctx::getService);
70+
return ctx.getServiceReferences(serviceType, null).stream().map(ctx::getService);
6471
} catch (Throwable e) {
6572
if (verbose) {
6673
StatusLogger.getLogger().error("Unable to load OSGI services for service {}", serviceType, e);
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<!--
3+
Licensed to the Apache Software Foundation (ASF) under one or more
4+
contributor license agreements. See the NOTICE file distributed with
5+
this work for additional information regarding copyright ownership.
6+
The ASF licenses this file to You under the Apache License, Version 2.0
7+
(the "License"); you may not use this file except in compliance with
8+
the License. You may obtain a copy of the License at
9+
10+
http://www.apache.org/licenses/LICENSE-2.0
11+
12+
Unless required by applicable law or agreed to in writing, software
13+
distributed under the License is distributed on an "AS IS" BASIS,
14+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15+
See the License for the specific language governing permissions and
16+
limitations under the License.
17+
-->
18+
<entry xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
19+
xmlns="http://logging.apache.org/log4j/changelog"
20+
xsi:schemaLocation="http://logging.apache.org/log4j/changelog https://logging.apache.org/log4j/changelog-0.1.0.xsd"
21+
type="changed">
22+
<author id="ggregory"/>
23+
<description format="asciidoc">
24+
Log a status error event instead of an NPE in
25+
org.apache.logging.log4j.util.OsgiServiceLocator.loadServices(Class, Lookup, boolean)
26+
when a bundle has no valid BundleContext for a service type.
27+
</description>
28+
</entry>

0 commit comments

Comments
 (0)