From 3be8ba00926e3fca88b42cc7ebcfb44ffdf08f03 Mon Sep 17 00:00:00 2001 From: Amit Kumar Mondal Date: Sun, 21 Jul 2024 10:54:46 +0200 Subject: [PATCH] Simplified retrieval of services without types --- .../mqtt5/provider/helper/MessageHelper.java | 59 ++++++++++--------- 1 file changed, 31 insertions(+), 28 deletions(-) diff --git a/in.bytehue.messaging.mqtt5.provider/src/main/java/in/bytehue/messaging/mqtt5/provider/helper/MessageHelper.java b/in.bytehue.messaging.mqtt5.provider/src/main/java/in/bytehue/messaging/mqtt5/provider/helper/MessageHelper.java index 7e057c3..f4d19d2 100644 --- a/in.bytehue.messaging.mqtt5.provider/src/main/java/in/bytehue/messaging/mqtt5/provider/helper/MessageHelper.java +++ b/in.bytehue.messaging.mqtt5.provider/src/main/java/in/bytehue/messaging/mqtt5/provider/helper/MessageHelper.java @@ -37,6 +37,7 @@ import java.nio.ByteBuffer; import java.util.Arrays; import java.util.Collection; +import java.util.Comparator; import java.util.HashMap; import java.util.Map; import java.util.Optional; @@ -83,41 +84,43 @@ private MessageHelper() { } public static Object getServiceWithoutType(final String clazz, final String filter, final BundleContext context) { - try { - ServiceReference[] references = context.getServiceReferences(clazz, filter); - if (references == null || references.length == 0) { - throw new RuntimeException("'" + clazz + "' service instance cannot be found"); - } - - final ToLongFunction> srFunc = - sr -> Optional.ofNullable(((ServiceReference) sr).getProperty(SERVICE_RANKING)) - .filter(Number.class::isInstance) - .map(Number.class::cast) - .map(Number::longValue) - .orElse(0L); - - return Arrays.stream(references) - .max(comparingLong(srFunc)) // Finds the reference with the highest ranking - .map(context::getService) - .orElseThrow(() -> new RuntimeException("'" + clazz + "' service instance cannot be found")); - } catch (Exception e) { - throw new RuntimeException("Service '" + clazz + "' cannot be retrieved", e); - } - } + try { + ServiceReference[] references = context.getServiceReferences(clazz, filter); + + if (references == null || references.length == 0) { + throw new RuntimeException(String.format("'%s' service instance cannot be found", clazz)); + } + + return Arrays.stream(references) + .max(Comparator.comparingLong(getServiceRanking())) + .map(context::getService) + .orElseThrow(() -> new RuntimeException(String.format("'%s' service instance cannot be found", clazz))); + + } catch (Exception e) { + throw new RuntimeException(String.format("Service '%s' cannot be retrieved", clazz), e); + } + } - public static Optional getOptionalServiceWithoutType(final String clazz, String filter, final BundleContext context, final Logger logger) { + public static Optional getOptionalServiceWithoutType(final String clazz, String filter, final BundleContext context, final Logger logger) { try { - if (filter.trim().isEmpty()) { - filter = null; + if (filter == null || filter.trim().isEmpty()) { + filter = null; } - final Object service = getServiceWithoutType(clazz, filter, context); - return Optional.ofNullable(service); - } catch (final Exception e) { - logger.warn("Service '{}' cannot be retrieved", clazz); + return Optional.ofNullable(getServiceWithoutType(clazz, filter, context)); + } catch (Exception e) { + logger.warn("Service '{}' cannot be retrieved", clazz, e); return Optional.empty(); } } + private static ToLongFunction> getServiceRanking() { + return sr -> Optional.ofNullable(sr.getProperty(SERVICE_RANKING)) + .filter(Number.class::isInstance) + .map(Number.class::cast) + .map(Number::longValue) + .orElse(0L); + } + public static T getService(final Class clazz, final String filter, final BundleContext context) { try { final Collection> references = context.getServiceReferences(clazz, filter);