Skip to content

Package: ServiceMap

ServiceMap

nameinstructionbranchcomplexitylinemethod
getAddress(String)
M: 5 C: 0
0%
M: 0 C: 0
100%
M: 1 C: 0
0%
M: 1 C: 0
0%
M: 1 C: 0
0%
registerServices(String, List)
M: 67 C: 0
0%
M: 6 C: 0
0%
M: 4 C: 0
0%
M: 10 C: 0
0%
M: 1 C: 0
0%
static {...}
M: 8 C: 0
0%
M: 0 C: 0
100%
M: 1 C: 0
0%
M: 2 C: 0
0%
M: 1 C: 0
0%
unregisterServices(List)
M: 31 C: 0
0%
M: 6 C: 0
0%
M: 4 C: 0
0%
M: 8 C: 0
0%
M: 1 C: 0
0%

Coverage

1: /*******************************************************************************
2: * Copyright (c) 2017, 2022 Eurotech and/or its affiliates and others
3: *
4: * This program and the accompanying materials are made
5: * available under the terms of the Eclipse Public License 2.0
6: * which is available at https://www.eclipse.org/legal/epl-2.0/
7: *
8: * SPDX-License-Identifier: EPL-2.0
9: *
10: * Contributors:
11: * Eurotech - initial API and implementation
12: *******************************************************************************/
13: package org.eclipse.kapua.commons.event;
14:
15: import org.slf4j.Logger;
16: import org.slf4j.LoggerFactory;
17:
18: import java.util.HashMap;
19: import java.util.List;
20: import java.util.Map;
21:
22: /**
23: * Utility class used to handle the mapping between services and address used to republish events.
24: *
25: * @since 1.0
26: */
27: public class ServiceMap {
28:
29: private static final Logger LOG = LoggerFactory.getLogger(ServiceMap.class);
30:
31: //no need to have a concurrent map since:
32: //if a service is not available (due to register process in progress) no event is sent
33: //if a service is going to be deregistered may some event is still fired and will remain in the queue until the service consuming these events will come available later
34: private static final Map<String, String> AVAILABLE_SERVICES = new HashMap<>();
35:
36: private ServiceMap() {
37: }
38:
39: /**
40: * Register the list of services to the provided address
41: *
42: * @param serviceDefaultAddress
43: * @param servicesEntryList
44: */
45: public static synchronized void registerServices(String serviceDefaultAddress, List<ServiceEntry> servicesEntryList) {
46:• for (ServiceEntry serviceEntry : servicesEntryList) {
47: //register service name
48: String tmpServiceName = AVAILABLE_SERVICES.get(serviceEntry.getServiceName());
49:• if (tmpServiceName == null) {
50: AVAILABLE_SERVICES.put(serviceEntry.getServiceName(), serviceEntry.getAddress());
51: LOG.info("Bound service '{}' to address '{}'", serviceEntry.getServiceName(), serviceEntry.getAddress());
52:• } else if (!serviceEntry.getAddress().equals(tmpServiceName)) {
53: LOG.warn("The service '{}' is already registered with a different address (old '{}' - new '{}'). No change will be made", serviceEntry.getServiceName(), tmpServiceName, serviceEntry.getAddress());
54: } else {
55: LOG.info("The service '{}' is already registered with address '{}'", serviceEntry.getServiceName(), serviceEntry.getAddress());
56: }
57: }
58: }
59:
60: /**
61: * Unregister the provided services from the address map
62: *
63: * @param servicesNames
64: */
65: public static synchronized void unregisterServices(List<String> servicesNames) {
66:• if (servicesNames != null) {
67:• for (String serviceName : servicesNames) {
68: String tmpServiceName = AVAILABLE_SERVICES.remove(serviceName);
69:• if (tmpServiceName == null) {
70: LOG.warn("Cannot deregister service '{}'. The service wasn't registered!", serviceName);
71: } else {
72: LOG.info("Deregistered service '{}' from address '{}'", serviceName, tmpServiceName);
73: }
74: }
75: }
76: }
77:
78: /**
79: * Get the address associated to the specific service
80: *
81: * @param serviceName
82: * @return
83: */
84: public static String getAddress(String serviceName) {
85: return AVAILABLE_SERVICES.get(serviceName);
86: }
87:
88: }