Skip to content

Commit

Permalink
Simplified retrieval of services without types
Browse files Browse the repository at this point in the history
  • Loading branch information
amitjoy committed Sep 10, 2024
1 parent 63bfe2d commit 3be8ba0
Showing 1 changed file with 31 additions and 28 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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<ServiceReference<?>> 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<Object> getOptionalServiceWithoutType(final String clazz, String filter, final BundleContext context, final Logger logger) {
public static Optional<Object> 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<ServiceReference<?>> 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> T getService(final Class<T> clazz, final String filter, final BundleContext context) {
try {
final Collection<ServiceReference<T>> references = context.getServiceReferences(clazz, filter);
Expand Down

0 comments on commit 3be8ba0

Please sign in to comment.