2 * Copyright (c) 2016 Red Hat, Inc. and others. All rights reserved.
4 * This program and the accompanying materials are made available under the
5 * terms of the Eclipse Public License v1.0 which accompanies this distribution,
6 * and is available at http://www.eclipse.org/legal/epl-v10.html
8 package org.opendaylight.odlparent.bundles4test;
10 import java.util.Arrays;
11 import java.util.Collections;
12 import java.util.HashMap;
13 import java.util.List;
15 import java.util.stream.Collectors;
16 import org.osgi.framework.Bundle;
17 import org.osgi.framework.ServiceReference;
18 import org.slf4j.Logger;
19 import org.slf4j.LoggerFactory;
22 * Utilities for OSGi's {@link ServiceReference}.
24 * @author Michael Vorburger.ch
26 public class ServiceReferenceUtil {
28 private static final Logger LOG = LoggerFactory.getLogger(ServiceReferenceUtil.class);
30 public Map<String, Object> getProperties(ServiceReference<?> serviceRef) {
31 String[] propertyKeys = serviceRef.getPropertyKeys();
32 Map<String, Object> properties = new HashMap<>(propertyKeys.length);
33 for (String propertyKey : propertyKeys) {
34 Object propertyValue = serviceRef.getProperty(propertyKey);
35 if (propertyValue != null) {
36 if (propertyValue.getClass().isArray()) {
37 propertyValue = Arrays.asList((Object[]) propertyValue);
40 // maintain the null value in the property map anyway
41 properties.put(propertyKey, propertyValue);
46 public List<String> getUsingBundleSymbolicNames(ServiceReference<?> serviceRef) {
47 Bundle[] usingBundles = serviceRef.getUsingBundles();
48 if (usingBundles == null) {
49 return Collections.emptyList();
51 return Arrays.asList(usingBundles).stream()
52 .map(bundle -> bundle.getSymbolicName()).collect(Collectors.toList());