Package: AssetTrackerCustomizer

AssetTrackerCustomizer

nameinstructionbranchcomplexitylinemethod
AssetTrackerCustomizer(BundleContext, AssetService)
M: 0 C: 20
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 7
100%
M: 0 C: 1
100%
addService(Asset)
M: 0 C: 17
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 4
100%
M: 0 C: 1
100%
addingService(ServiceReference)
M: 2 C: 15
88%
M: 1 C: 1
50%
M: 1 C: 1
50%
M: 1 C: 4
80%
M: 0 C: 1
100%
getRegisteredAssets()
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%
modifiedService(ServiceReference, Asset)
M: 9 C: 0
0%
M: 0 C: 0
100%
M: 1 C: 0
0%
M: 3 C: 0
0%
M: 1 C: 0
0%
removedService(ServiceReference, Asset)
M: 27 C: 0
0%
M: 4 C: 0
0%
M: 3 C: 0
0%
M: 6 C: 0
0%
M: 1 C: 0
0%
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%

Coverage

1: /*******************************************************************************
2: * Copyright (c) 2016, 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: package org.eclipse.kura.internal.asset.cloudlet;
11:
12: import static java.util.Objects.requireNonNull;
13: import static org.eclipse.kura.configuration.ConfigurationService.KURA_SERVICE_PID;
14:
15: import java.util.Map;
16:
17: import org.eclipse.kura.asset.Asset;
18: import org.eclipse.kura.asset.AssetService;
19: import org.eclipse.kura.util.collection.CollectionUtil;
20: import org.osgi.framework.BundleContext;
21: import org.osgi.framework.ServiceReference;
22: import org.osgi.util.tracker.ServiceTrackerCustomizer;
23: import org.slf4j.Logger;
24: import org.slf4j.LoggerFactory;
25:
26: /**
27: * The Class AssetTrackerCustomizer is responsible for tracking all the existing
28: * asset instances in the OSGi service registry
29: */
30: final class AssetTrackerCustomizer implements ServiceTrackerCustomizer<Asset, Asset> {
31:
32: /** The Logger instance. */
33: private static final Logger logger = LoggerFactory.getLogger(AssetTrackerCustomizer.class);
34:
35: /** The map of assets present in the OSGi service registry. */
36: private final Map<String, Asset> assets;
37:
38: /** The Asset Service dependency. */
39: private final AssetService assetService;
40:
41: /** Bundle Context */
42: private final BundleContext context;
43:
44: /**
45: * Instantiates a new asset tracker.
46: *
47: * @param context
48: * the bundle context
49: * @throws NullPointerException
50: * if any of the arguments is null
51: */
52: AssetTrackerCustomizer(final BundleContext context, final AssetService assetService) {
53: requireNonNull(context, "Bundle context cannot be null");
54: requireNonNull(context, "Asset service instance cannot be null");
55:
56: this.assets = CollectionUtil.newConcurrentHashMap();
57: this.context = context;
58: this.assetService = assetService;
59: }
60:
61: /** {@inheritDoc} */
62: @Override
63: public Asset addingService(final ServiceReference<Asset> reference) {
64: final Asset service = this.context.getService(reference);
65: logger.info("Asset has been found by Asset Cloudlet Tracker... ==> adding service");
66:• if (service != null) {
67: return addService(service);
68: }
69: return null;
70: }
71:
72: /**
73: * Adds the service instance to the map of asset service instances
74: *
75: * @param service
76: * the asset service instance
77: * @throws NullPointerException
78: * if provided service is null
79: * @return Asset service instance
80: */
81: private Asset addService(final Asset service) {
82: requireNonNull(service, "Asset service instance cannot be null");
83: final String assetPid = this.assetService.getAssetPid(service);
84: this.assets.put(assetPid, service);
85: return service;
86: }
87:
88: /**
89: * Returns the list of found assets in the service registry
90: *
91: * @return the map of assets
92: */
93: Map<String, Asset> getRegisteredAssets() {
94: return CollectionUtil.newConcurrentHashMap(this.assets);
95: }
96:
97: /** {@inheritDoc} */
98: @Override
99: public void modifiedService(final ServiceReference<Asset> reference, final Asset service) {
100: removedService(reference, service);
101: addingService(reference);
102: }
103:
104: /** {@inheritDoc} */
105: @Override
106: public void removedService(final ServiceReference<Asset> reference, final Asset service) {
107: final String assetPid = String.valueOf(reference.getProperty(KURA_SERVICE_PID));
108: this.context.ungetService(reference);
109:• if (assetPid != null && this.assets.containsKey(assetPid)) {
110: this.assets.remove(assetPid);
111: }
112: logger.info("Asset has been removed by Asset Cloudlet Tracker... {}", service);
113: }
114:
115: }