Skip to content

Package: ServiceEventBusManager

ServiceEventBusManager

nameinstructionbranchcomplexitylinemethod
getInstance()
M: 29 C: 0
0%
M: 4 C: 0
0%
M: 3 C: 0
0%
M: 5 C: 0
0%
M: 1 C: 0
0%
start()
M: 12 C: 0
0%
M: 2 C: 0
0%
M: 2 C: 0
0%
M: 4 C: 0
0%
M: 1 C: 0
0%
static {...}
M: 83 C: 0
0%
M: 4 C: 0
0%
M: 3 C: 0
0%
M: 17 C: 0
0%
M: 1 C: 0
0%
stop()
M: 12 C: 0
0%
M: 2 C: 0
0%
M: 2 C: 0
0%
M: 4 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.eclipse.kapua.KapuaRuntimeException;
16: import org.eclipse.kapua.event.ServiceEventBus;
17: import org.eclipse.kapua.event.ServiceEventBusException;
18: import org.slf4j.Logger;
19: import org.slf4j.LoggerFactory;
20:
21: import java.util.HashMap;
22: import java.util.Map;
23: import java.util.ServiceLoader;
24:
25: /**
26: * Service event bus manager. It handles the service event bus life cycle
27: *
28: * @since 1.0
29: */
30: public class ServiceEventBusManager {
31:
32: private static final Logger LOGGER = LoggerFactory.getLogger(ServiceEventBusManager.class);
33:
34: public static final String JMS_20_EVENT_BUS = "JMS_20_EVENT_BUS";
35:
36: private static Map<String, ServiceEventBusDriver> serviceEventBusDrivers;
37: private static boolean started;
38:
39: static {
40: try {
41: LOGGER.info("Finding event bus driver instance...");
42: serviceEventBusDrivers = new HashMap<>();
43: ServiceLoader<ServiceEventBusDriver> eventBusLoaders = ServiceLoader.load(ServiceEventBusDriver.class);
44:• for (ServiceEventBusDriver eventBusDriverLoader : eventBusLoaders) {
45:• if (serviceEventBusDrivers.containsKey(eventBusDriverLoader.getType())) {
46: LOGGER.warn("Event bus driver instance of type {} is already loaded...SKIPPED", eventBusDriverLoader.getType());
47: continue;
48: }
49: serviceEventBusDrivers.put(eventBusDriverLoader.getType(), eventBusDriverLoader);
50: LOGGER.info("Event bus driver instance {}...ADDED", eventBusDriverLoader.getType());
51: }
52: LOGGER.info("Finding event bus driver instance...DONE");
53: } catch (Exception ex) {
54: LOGGER.error("Error while initializing {}, {}", ServiceEventBusManager.class.getName(), ex.getMessage(), ex);
55: throw KapuaRuntimeException.internalError(ex, String.format("Error while initializing %s", ServiceEventBusManager.class.getName()));
56: }
57: }
58:
59: private ServiceEventBusManager() {
60: }
61:
62: /**
63: * Get the event bus instance
64: *
65: * @return
66: * @throws ServiceEventBusException
67: */
68: public static ServiceEventBus getInstance() throws ServiceEventBusException {
69:• if (!started) {
70: throw new ServiceEventBusException("The event bus isn't initialized! Cannot perform any operation!");
71: }
72:
73: // Currently hard wired to use the provided instance of JMS_20_EVENT_BUS
74: // May be extended in future versions
75:
76:• if (serviceEventBusDrivers.get(JMS_20_EVENT_BUS) == null) {
77: throw new ServiceEventBusException(String.format("No eventbus drivers found for type %s", JMS_20_EVENT_BUS));
78: }
79: return serviceEventBusDrivers.get(JMS_20_EVENT_BUS).getEventBus();
80: }
81:
82: /**
83: * Start the event bus
84: *
85: * @throws ServiceEventBusException
86: */
87: public static void start() throws ServiceEventBusException {
88:• if (serviceEventBusDrivers.get(JMS_20_EVENT_BUS) != null) {
89: serviceEventBusDrivers.get(JMS_20_EVENT_BUS).start();
90: }
91: started = true;
92: }
93:
94: /**
95: * Stop the event bus
96: *
97: * @throws ServiceEventBusException
98: */
99: public static void stop() throws ServiceEventBusException {
100:• if (serviceEventBusDrivers.get(JMS_20_EVENT_BUS) != null) {
101: serviceEventBusDrivers.get(JMS_20_EVENT_BUS).stop();
102: }
103: started = false;
104: }
105:
106: }