542e39a71b97040464292cd846ae298dddbff9cd
[odlparent.git] / bundles-test / src / main / java / org / opendaylight / odlparent / bundlestest / ServiceReferenceUtil.java
1 /*
2  * Copyright (c) 2016 Red Hat, Inc. and others. All rights reserved.
3  *
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
7  */
8 package org.opendaylight.odlparent.bundlestest;
9
10 import java.util.Arrays;
11 import java.util.Collections;
12 import java.util.HashMap;
13 import java.util.List;
14 import java.util.Map;
15 import java.util.stream.Collectors;
16 import org.osgi.framework.ServiceReference;
17
18 /**
19  * Utilities for OSGi's {@link ServiceReference}.
20  *
21  * @author Michael Vorburger.ch
22  */
23 public class ServiceReferenceUtil {
24
25     public Map<String, Object> getProperties(ServiceReference<?> serviceRef) {
26         String[] propertyKeys = serviceRef.getPropertyKeys();
27         Map<String, Object> properties = new HashMap<>(propertyKeys.length);
28         for (String propertyKey : propertyKeys) {
29             Object propertyValue = serviceRef.getProperty(propertyKey);
30             if (propertyValue.getClass().isArray()) {
31                 propertyValue = Arrays.asList((Object[]) propertyValue);
32             }
33             properties.put(propertyKey, propertyValue);
34         }
35         return properties;
36     }
37
38     public List<String> getUsingBundleSymbolicNames(ServiceReference<?> serviceRef) {
39         if (serviceRef.getUsingBundles() == null) {
40             return Collections.emptyList();
41         } else {
42             return Arrays.asList(serviceRef.getUsingBundles()).stream()
43                 .map(bundle -> bundle.getSymbolicName()).collect(Collectors.toList());
44         }
45     }
46
47 }