From 8016b929f7c4ad5ce94456f889b5fa32c4a198f2 Mon Sep 17 00:00:00 2001 From: Milan M2 Date: Wed, 4 May 2022 08:10:43 +0200 Subject: [PATCH] For OSGi services lookup use only classes assignable from service class (#5051) --- .../jersey/internal/OsgiRegistry.java | 20 +++++++++++++------ 1 file changed, 14 insertions(+), 6 deletions(-) diff --git a/core-common/src/main/java/org/glassfish/jersey/internal/OsgiRegistry.java b/core-common/src/main/java/org/glassfish/jersey/internal/OsgiRegistry.java index ada9081668..b95a070444 100644 --- a/core-common/src/main/java/org/glassfish/jersey/internal/OsgiRegistry.java +++ b/core-common/src/main/java/org/glassfish/jersey/internal/OsgiRegistry.java @@ -1,5 +1,5 @@ /* - * Copyright (c) 2012, 2019 Oracle and/or its affiliates. All rights reserved. + * Copyright (c) 2012, 2022 Oracle and/or its affiliates. All rights reserved. * * This program and the accompanying materials are made available under the * terms of the Eclipse Public License v. 2.0, which is available at @@ -115,7 +115,7 @@ public Iterator createIterator( final ClassLoader loader, final boolean ignoreOnClassNotFound) { - final List> providerClasses = locateAllProviders(serviceName); + final List> providerClasses = locateAllProviders(serviceClass); if (!providerClasses.isEmpty()) { return new Iterator() { @@ -153,7 +153,7 @@ public void remove() { @Override public Iterator> createClassIterator( final Class service, final String serviceName, final ClassLoader loader, final boolean ignoreOnClassNotFound) { - final List> providerClasses = locateAllProviders(serviceName); + final List> providerClasses = locateAllProviders(service); if (!providerClasses.isEmpty()) { return new Iterator>() { @@ -552,14 +552,22 @@ private void register(final Bundle bundle) { } } - private List> locateAllProviders(final String serviceName) { + private List> locateAllProviders(final Class serviceClass) { lock.readLock().lock(); try { final List> result = new LinkedList>(); for (final Map>>> value : factories.values()) { - if (value.containsKey(serviceName)) { + if (value.containsKey(serviceClass.getName())) { try { - result.addAll(value.get(serviceName).call()); + for (final Class clazz : value.get(serviceClass.getName()).call()) { + if (serviceClass.isAssignableFrom(clazz)) { + result.add(clazz); + } else if (LOGGER.isLoggable(Level.FINER)) { + LOGGER.log(Level.FINER, + "Ignoring provider class " + clazz.getName() + " because it is not assignable to " + + " service class " + serviceClass.getName()); + } + } } catch (final Exception ex) { // ignore }