Skip to content

Commit

Permalink
Log a status error event instead of an NPE
Browse files Browse the repository at this point in the history
See org.apache.logging.log4j.util.OsgiServiceLocator.loadServices(Class,
Lookup, boolean) when a bundle has no valid BundleContext for a service
type.
  • Loading branch information
garydgregory committed Mar 10, 2023
1 parent e03266d commit 8e4f34a
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -54,13 +55,19 @@ public static <T> Stream<T> loadServices(final Class<T> serviceType, final Looku
}

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);
Expand Down
28 changes: 28 additions & 0 deletions src/changelog/.2.x.x/OsgiServiceLocator_npe.xml
Original file line number Diff line number Diff line change
@@ -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>

0 comments on commit 8e4f34a

Please sign in to comment.