Skip to content

Package: ServiceEventScope

ServiceEventScope

nameinstructionbranchcomplexitylinemethod
begin()
M: 42 C: 0
0%
M: 4 C: 0
0%
M: 3 C: 0
0%
M: 13 C: 0
0%
M: 1 C: 0
0%
end()
M: 22 C: 0
0%
M: 6 C: 0
0%
M: 4 C: 0
0%
M: 7 C: 0
0%
M: 1 C: 0
0%
get()
M: 15 C: 0
0%
M: 4 C: 0
0%
M: 3 C: 0
0%
M: 4 C: 0
0%
M: 1 C: 0
0%
set(ServiceEvent)
M: 12 C: 0
0%
M: 0 C: 0
100%
M: 1 C: 0
0%
M: 4 C: 0
0%
M: 1 C: 0
0%
static {...}
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%

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.ServiceEvent;
17:
18: import java.util.Stack;
19: import java.util.UUID;
20:
21: /**
22: * Utility class to handle the {@link ThreadLocal} context event stack.
23: *
24: * @since 1.0
25: */
26: public class ServiceEventScope {
27:
28: private static ThreadLocal<Stack<ServiceEvent>> eventContextThdLocal = new ThreadLocal<>();
29:
30: private ServiceEventScope() {
31: }
32:
33: /**
34: * Append the Kapua event to the current thread context Kapua event stack (setting a new context id in the Kapua event)
35: *
36: * @return
37: */
38: public static ServiceEvent begin() {
39:
40: // Is it the first call in the stack? Is there already a Stack?
41: Stack<ServiceEvent> eventStack = eventContextThdLocal.get();
42:• if (eventStack == null) {
43: eventStack = new Stack<>();
44: eventContextThdLocal.set(eventStack);
45: }
46:
47: // Is it the first call in the stack?
48: String contextId;
49:• if (!eventStack.empty()) {
50: ServiceEvent lastEvent = eventStack.peek();
51: contextId = lastEvent.getContextId();
52: } else {
53: contextId = UUID.randomUUID().toString();
54: }
55:
56: ServiceEvent newEvent = new ServiceEvent();
57: newEvent.setContextId(contextId);
58: eventStack.push(newEvent);
59:
60: return eventStack.peek();
61: }
62:
63: /**
64: * Create a new thread context Kapua event stack and set the Kapua event at the top
65: *
66: * @param event
67: */
68: public static void set(ServiceEvent event) {
69: Stack<ServiceEvent> eventStack = new Stack<>();
70: eventStack.push(event);
71: eventContextThdLocal.set(eventStack);
72: }
73:
74: /**
75: * Get the current Kapua event from the thread context Kapua event stack
76: *
77: * @return
78: */
79: public static ServiceEvent get() {
80: Stack<ServiceEvent> tmp = eventContextThdLocal.get();
81:• if (tmp != null && !tmp.empty()) {
82: return tmp.peek();
83: }
84: return null;
85: }
86:
87: /**
88: * Clean up the current thread context Kapua event stack
89: */
90: public static void end() {
91: Stack<ServiceEvent> eventStack = eventContextThdLocal.get();
92:• if (eventStack == null || eventStack.empty()) {
93: throw KapuaRuntimeException.internalError("Event stack shouldn't be 'null'");
94: }
95: eventStack.pop();
96:• if (eventStack.empty()) {
97: eventContextThdLocal.set(null);
98: }
99: }
100: }