package org.opendaylight.odlparent.bundlestest.lib;
import java.util.Arrays;
-import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
*
* @author Michael Vorburger.ch
*/
-// intentionally just package-local
-class ServiceReferenceUtil {
+final class ServiceReferenceUtil {
+ private ServiceReferenceUtil() {
+ // Hidden on purpose
+ }
- public Map<String, Object> getProperties(ServiceReference<?> serviceRef) {
- String[] propertyKeys = serviceRef.getPropertyKeys();
- Map<String, Object> properties = new HashMap<>(propertyKeys.length);
- for (String propertyKey : propertyKeys) {
- Object propertyValue = serviceRef.getProperty(propertyKey);
+ static Map<String, Object> getProperties(final ServiceReference<?> serviceRef) {
+ final var propertyKeys = serviceRef.getPropertyKeys();
+ final var properties = new HashMap<String, Object>(propertyKeys.length);
+ for (var propertyKey : propertyKeys) {
+ var propertyValue = serviceRef.getProperty(propertyKey);
if (propertyValue != null) {
if (propertyValue.getClass().isArray()) {
propertyValue = Arrays.asList((Object[]) propertyValue);
return properties;
}
- public List<String> getUsingBundleSymbolicNames(ServiceReference<?> serviceRef) {
- Bundle[] usingBundles = serviceRef.getUsingBundles();
- if (usingBundles == null) {
- return Collections.emptyList();
- } else {
- return Arrays.stream(usingBundles).map(Bundle::getSymbolicName).collect(Collectors.toList());
- }
+ static List<String> getUsingBundleSymbolicNames(final ServiceReference<?> serviceRef) {
+ final var usingBundles = serviceRef.getUsingBundles();
+ return usingBundles == null ? List.of()
+ : Arrays.stream(usingBundles).map(Bundle::getSymbolicName).collect(Collectors.toList());
}
-
}
@SuppressFBWarnings(value = "CRLF_INJECTION_LOGS",
justification = "multi-line logs are internal, without input from untrusted external source")
public class TestBundleDiag {
-
private static final Logger LOG = LoggerFactory.getLogger(TestBundleDiag.class);
private final BundleContext bundleContext;
}
private void logOSGiServices() {
- ServiceReferenceUtil util = new ServiceReferenceUtil();
LOG.info("Now going to log all known services, to help diagnose root cause of "
+ "diag failure BundleService reported bundle(s) which are not active");
try {
// serviceRef.getBundle() can return null if the bundle was destroyed
if (bundle != null) {
LOG.info("{} defines OSGi Service {} used by {}", bundle.getSymbolicName(),
- util.getProperties(serviceRef), util.getUsingBundleSymbolicNames(serviceRef));
+ ServiceReferenceUtil.getProperties(serviceRef),
+ ServiceReferenceUtil.getUsingBundleSymbolicNames(serviceRef));
} else {
LOG.trace("skipping reporting service reference as the underlying bundle is null");
}
LOG.error("logOSGiServices() failed due to InvalidSyntaxException", e);
}
}
-
}
@Test
public void testGetUsingBundleSymbolicNames() {
- assertEquals(List.of(), new ServiceReferenceUtil().getUsingBundleSymbolicNames(getServiceReference()));
+ assertEquals(List.of(), ServiceReferenceUtil.getUsingBundleSymbolicNames(getServiceReference()));
}
@Test
public void testGetProperties() {
- final var map = new ServiceReferenceUtil().getProperties(getServiceReference());
+ final var map = ServiceReferenceUtil.getProperties(getServiceReference());
assertEquals(3, map.size());
assertEquals("value1", map.get("property1"));
assertEquals(List.of("value2.1", "value2.2"), map.get("property2"));