|
37 | 37 | import java.nio.ByteBuffer;
|
38 | 38 | import java.util.Arrays;
|
39 | 39 | import java.util.Collection;
|
| 40 | +import java.util.Comparator; |
40 | 41 | import java.util.HashMap;
|
41 | 42 | import java.util.Map;
|
42 | 43 | import java.util.Optional;
|
@@ -83,41 +84,43 @@ private MessageHelper() {
|
83 | 84 | }
|
84 | 85 |
|
85 | 86 | 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 | + } |
107 | 103 |
|
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) { |
109 | 105 | try {
|
110 |
| - if (filter.trim().isEmpty()) { |
111 |
| - filter = null; |
| 106 | + if (filter == null || filter.trim().isEmpty()) { |
| 107 | + filter = null; |
112 | 108 | }
|
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); |
117 | 112 | return Optional.empty();
|
118 | 113 | }
|
119 | 114 | }
|
120 | 115 |
|
| 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 | + |
121 | 124 | public static <T> T getService(final Class<T> clazz, final String filter, final BundleContext context) {
|
122 | 125 | try {
|
123 | 126 | final Collection<ServiceReference<T>> references = context.getServiceReferences(clazz, filter);
|
|
0 commit comments