blob: 59040abc4bf23681c59887cde896c30e8e1ba640 (
plain) (
blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
|
// Copyright Vespa.ai. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
package com.yahoo.vespa.hosted.node.admin.provider;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.ConcurrentMap;
import java.util.function.Supplier;
import java.util.stream.Collectors;
/**
* Class to make it easier to implement a NodeAdminDebugHandler:
* - Forward to sub-NodeAdminDebugHandlers with addHandler,
* - Specify constants with addConstant
* - Forwarding to methods that dynamically build debug objects with addThreadSafeSupplier.
*
* @author hakonhall
*/
public class DebugHandlerHelper implements NodeAdminDebugHandler {
private final ConcurrentMap<String, Supplier<Object>> suppliers = new ConcurrentHashMap<>();
public void addThreadSafeSupplier(String name, Supplier<Object> threadSafeSupplier) {
Supplier<Object> previousSupplier = suppliers.putIfAbsent(name, threadSafeSupplier);
if (previousSupplier != null) {
throw new IllegalArgumentException(name + " is already registered");
}
}
public void addHandler(String name, NodeAdminDebugHandler handler) {
addThreadSafeSupplier(name, handler::getDebugPage);
}
public void addConstant(String name, String value) {
addThreadSafeSupplier(name, () -> value);
}
public void remove(String name) {
Supplier<Object> supplier = suppliers.remove(name);
if (supplier == null) {
throw new IllegalArgumentException(name + " is not registered");
}
}
@Override
public Map<String, Object> getDebugPage() {
return suppliers.entrySet().stream().collect(Collectors.toMap(
Map.Entry::getKey,
entry -> entry.getValue().get()));
}
}
|