Skip to content

Commit 3be8ba0

Browse files
committed
Simplified retrieval of services without types
1 parent 63bfe2d commit 3be8ba0

File tree

1 file changed

+31
-28
lines changed
  • in.bytehue.messaging.mqtt5.provider/src/main/java/in/bytehue/messaging/mqtt5/provider/helper

1 file changed

+31
-28
lines changed

in.bytehue.messaging.mqtt5.provider/src/main/java/in/bytehue/messaging/mqtt5/provider/helper/MessageHelper.java

+31-28
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@
3737
import java.nio.ByteBuffer;
3838
import java.util.Arrays;
3939
import java.util.Collection;
40+
import java.util.Comparator;
4041
import java.util.HashMap;
4142
import java.util.Map;
4243
import java.util.Optional;
@@ -83,41 +84,43 @@ private MessageHelper() {
8384
}
8485

8586
public static Object getServiceWithoutType(final String clazz, final String filter, final BundleContext context) {
86-
try {
87-
ServiceReference<?>[] references = context.getServiceReferences(clazz, filter);
88-
if (references == null || references.length == 0) {
89-
throw new RuntimeException("'" + clazz + "' service instance cannot be found");
90-
}
91-
92-
final ToLongFunction<ServiceReference<?>> srFunc =
93-
sr -> Optional.ofNullable(((ServiceReference<?>) sr).getProperty(SERVICE_RANKING))
94-
.filter(Number.class::isInstance)
95-
.map(Number.class::cast)
96-
.map(Number::longValue)
97-
.orElse(0L);
98-
99-
return Arrays.stream(references)
100-
.max(comparingLong(srFunc)) // Finds the reference with the highest ranking
101-
.map(context::getService)
102-
.orElseThrow(() -> new RuntimeException("'" + clazz + "' service instance cannot be found"));
103-
} catch (Exception e) {
104-
throw new RuntimeException("Service '" + clazz + "' cannot be retrieved", e);
105-
}
106-
}
87+
try {
88+
ServiceReference<?>[] references = context.getServiceReferences(clazz, filter);
89+
90+
if (references == null || references.length == 0) {
91+
throw new RuntimeException(String.format("'%s' service instance cannot be found", clazz));
92+
}
93+
94+
return Arrays.stream(references)
95+
.max(Comparator.comparingLong(getServiceRanking()))
96+
.map(context::getService)
97+
.orElseThrow(() -> new RuntimeException(String.format("'%s' service instance cannot be found", clazz)));
98+
99+
} catch (Exception e) {
100+
throw new RuntimeException(String.format("Service '%s' cannot be retrieved", clazz), e);
101+
}
102+
}
107103

108-
public static Optional<Object> getOptionalServiceWithoutType(final String clazz, String filter, final BundleContext context, final Logger logger) {
104+
public static Optional<Object> getOptionalServiceWithoutType(final String clazz, String filter, final BundleContext context, final Logger logger) {
109105
try {
110-
if (filter.trim().isEmpty()) {
111-
filter = null;
106+
if (filter == null || filter.trim().isEmpty()) {
107+
filter = null;
112108
}
113-
final Object service = getServiceWithoutType(clazz, filter, context);
114-
return Optional.ofNullable(service);
115-
} catch (final Exception e) {
116-
logger.warn("Service '{}' cannot be retrieved", clazz);
109+
return Optional.ofNullable(getServiceWithoutType(clazz, filter, context));
110+
} catch (Exception e) {
111+
logger.warn("Service '{}' cannot be retrieved", clazz, e);
117112
return Optional.empty();
118113
}
119114
}
120115

116+
private static ToLongFunction<ServiceReference<?>> getServiceRanking() {
117+
return sr -> Optional.ofNullable(sr.getProperty(SERVICE_RANKING))
118+
.filter(Number.class::isInstance)
119+
.map(Number.class::cast)
120+
.map(Number::longValue)
121+
.orElse(0L);
122+
}
123+
121124
public static <T> T getService(final Class<T> clazz, final String filter, final BundleContext context) {
122125
try {
123126
final Collection<ServiceReference<T>> references = context.getServiceReferences(clazz, filter);

0 commit comments

Comments
 (0)