767c7ebb51d88c727214a7154accedd7c4b672ed
[odlparent.git] / bundles4-test / src / main / java / org / opendaylight / odlparent / bundles4test / 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.bundles4test;
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.Bundle;
17 import org.osgi.framework.ServiceReference;
18 import org.slf4j.Logger;
19 import org.slf4j.LoggerFactory;
20
21 /**
22  * Utilities for OSGi's {@link ServiceReference}.
23  *
24  * @author Michael Vorburger.ch
25  */
26 public class ServiceReferenceUtil {
27
28     private static final Logger LOG = LoggerFactory.getLogger(ServiceReferenceUtil.class);
29
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);
38                 }
39             }
40             // maintain the null value in the property map anyway
41             properties.put(propertyKey, propertyValue);
42         }
43         return properties;
44     }
45
46     public List<String> getUsingBundleSymbolicNames(ServiceReference<?> serviceRef) {
47         Bundle[] usingBundles = serviceRef.getUsingBundles();
48         if (usingBundles == null) {
49             return Collections.emptyList();
50         } else {
51             return Arrays.asList(usingBundles).stream()
52                 .map(bundle -> bundle.getSymbolicName()).collect(Collectors.toList());
53         }
54     }
55
56 }