Package: DriverDescriptorServiceImpl

DriverDescriptorServiceImpl

nameinstructionbranchcomplexitylinemethod
DriverDescriptorServiceImpl()
M: 0 C: 3
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 1
100%
M: 0 C: 1
100%
activate(ComponentContext)
M: 0 C: 5
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 2
100%
M: 0 C: 1
100%
getDriverDescriptor(String)
M: 0 C: 59
100%
M: 0 C: 2
100%
M: 0 C: 2
100%
M: 0 C: 11
100%
M: 0 C: 1
100%
getDriverServiceReferences(String)
M: 6 C: 0
0%
M: 0 C: 0
100%
M: 1 C: 0
0%
M: 1 C: 0
0%
M: 1 C: 0
0%
listDriverDescriptors()
M: 0 C: 54
100%
M: 0 C: 2
100%
M: 0 C: 2
100%
M: 0 C: 10
100%
M: 0 C: 1
100%
newDriverDescriptor(String, String, Driver)
M: 6 C: 14
70%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 1 C: 4
80%
M: 0 C: 1
100%
static {...}
M: 0 C: 4
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 1
100%
M: 0 C: 1
100%
ungetDriverServiceReferences(ServiceReference[])
M: 5 C: 0
0%
M: 0 C: 0
100%
M: 1 C: 0
0%
M: 2 C: 0
0%
M: 1 C: 0
0%

Coverage

1: /*******************************************************************************
2: * Copyright (c) 2018 Eurotech and/or its affiliates and others
3: *
4: * All rights reserved. This program and the accompanying materials
5: * are made available under the terms of the Eclipse Public License v1.0
6: * which accompanies this distribution, and is available at
7: * http://www.eclipse.org/legal/epl-v10.html
8: *
9: *******************************************************************************/
10:
11: package org.eclipse.kura.internal.driver;
12:
13: import static java.util.Objects.requireNonNull;
14: import static org.eclipse.kura.configuration.ConfigurationService.KURA_SERVICE_PID;
15: import static org.osgi.service.cm.ConfigurationAdmin.SERVICE_FACTORYPID;
16:
17: import java.util.ArrayList;
18: import java.util.List;
19: import java.util.Optional;
20:
21: import org.eclipse.kura.driver.Driver;
22: import org.eclipse.kura.driver.descriptor.DriverDescriptor;
23: import org.eclipse.kura.driver.descriptor.DriverDescriptorService;
24: import org.eclipse.kura.util.service.ServiceUtil;
25: import org.osgi.framework.BundleContext;
26: import org.osgi.framework.ServiceReference;
27: import org.osgi.service.component.ComponentContext;
28: import org.slf4j.Logger;
29: import org.slf4j.LoggerFactory;
30:
31: public class DriverDescriptorServiceImpl implements DriverDescriptorService {
32:
33: private static final Logger logger = LoggerFactory.getLogger(DriverDescriptorServiceImpl.class);
34:
35: private BundleContext bundleContext;
36:
37: public void activate(ComponentContext componentContext) {
38: this.bundleContext = componentContext.getBundleContext();
39: }
40:
41: @Override
42: public Optional<DriverDescriptor> getDriverDescriptor(String driverPid) {
43: requireNonNull(driverPid, "Driver PID cannot be null");
44: DriverDescriptor driverDescriptor = null;
45:
46: String filterString = String.format("(&(kura.service.pid=%s))", driverPid);
47:
48: final ServiceReference<Driver>[] refs = getDriverServiceReferences(filterString);
49: try {
50:• for (final ServiceReference<Driver> driverServiceReference : refs) {
51: String factoryPid = driverServiceReference.getProperty(SERVICE_FACTORYPID).toString();
52: Driver driver = this.bundleContext.getService(driverServiceReference);
53: driverDescriptor = newDriverDescriptor(driverPid, factoryPid, driver);
54: }
55: } finally {
56: ungetDriverServiceReferences(refs);
57: }
58:
59: return Optional.ofNullable(driverDescriptor);
60: }
61:
62: @Override
63: public List<DriverDescriptor> listDriverDescriptors() {
64: List<DriverDescriptor> driverDescriptors = new ArrayList<>();
65:
66: final ServiceReference<Driver>[] refs = getDriverServiceReferences(null);
67: try {
68:• for (final ServiceReference<Driver> driverServiceReference : refs) {
69: String driverPid = driverServiceReference.getProperty(KURA_SERVICE_PID).toString();
70: String factoryPid = driverServiceReference.getProperty(SERVICE_FACTORYPID).toString();
71: Driver driver = this.bundleContext.getService(driverServiceReference);
72: driverDescriptors.add(newDriverDescriptor(driverPid, factoryPid, driver));
73: }
74: } finally {
75: ungetDriverServiceReferences(refs);
76: }
77:
78: return driverDescriptors;
79: }
80:
81: private DriverDescriptor newDriverDescriptor(String driverPid, String factoryPid, Driver driver) {
82: Object channelDescriptorObj = null;
83: try {
84: channelDescriptorObj = driver.getChannelDescriptor().getDescriptor();
85: } catch (Exception e) {
86: logger.warn("Failed to get driver descriptor for pid: {}", driverPid, e);
87: }
88: return new DriverDescriptor(driverPid, factoryPid, channelDescriptorObj);
89: }
90:
91: protected ServiceReference<Driver>[] getDriverServiceReferences(final String filter) {
92: return ServiceUtil.getServiceReferences(this.bundleContext, Driver.class, filter);
93: }
94:
95: protected void ungetDriverServiceReferences(final ServiceReference<Driver>[] refs) {
96: ServiceUtil.ungetServiceReferences(this.bundleContext, refs);
97: }
98: }