Skip to content

Commit 31fcb0d

Browse files
LiuTianyouAias00
andauthored
[improve] added a method to obtain plugin parameters (apache#2644)
Co-authored-by: aias00 <[email protected]>
1 parent 3e1d87b commit 31fcb0d

File tree

4 files changed

+219
-5
lines changed

4 files changed

+219
-5
lines changed

common/src/main/java/org/apache/hertzbeat/common/entity/job/Configmap.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717

1818
package org.apache.hertzbeat.common.entity.job;
1919

20+
import java.io.Serializable;
2021
import lombok.AllArgsConstructor;
2122
import lombok.Builder;
2223
import lombok.Data;
@@ -31,7 +32,7 @@
3132
@AllArgsConstructor
3233
@NoArgsConstructor
3334
@Builder
34-
public class Configmap {
35+
public class Configmap implements Serializable {
3536

3637
/**
3738
* Parameter key, replace the content with the identifier ^^_key_^^ in the protocol

common/src/main/java/org/apache/hertzbeat/common/entity/plugin/PluginContext.java

Lines changed: 116 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,21 +20,134 @@
2020
package org.apache.hertzbeat.common.entity.plugin;
2121

2222

23+
import java.util.Collections;
2324
import java.util.List;
25+
import java.util.Map;
26+
import java.util.Optional;
27+
import java.util.stream.Collectors;
2428
import lombok.Builder;
25-
import lombok.Data;
29+
import lombok.extern.slf4j.Slf4j;
30+
import org.apache.commons.lang3.SerializationUtils;
2631
import org.apache.hertzbeat.common.entity.job.Configmap;
2732

2833
/**
2934
* plugin context
3035
*/
3136
@Builder
32-
@Data
37+
@Slf4j
3338
public class PluginContext {
3439

3540
/**
3641
* params
3742
*/
38-
List<Configmap> params;
43+
private List<Configmap> params;
44+
private ParamHolder paramHolder;
3945

46+
public ParamHolder param() {
47+
if (paramHolder == null) {
48+
paramHolder = new ParamHolder(params);
49+
}
50+
return paramHolder;
51+
}
52+
53+
/**
54+
* management parameter operations
55+
*/
56+
public static class ParamHolder {
57+
58+
private final Map<String, Configmap> paramsMap;
59+
60+
public ParamHolder(List<Configmap> params) {
61+
if (params == null) {
62+
paramsMap = Collections.emptyMap();
63+
} else {
64+
this.paramsMap = params.stream().collect(Collectors.toMap(Configmap::getKey, configmap -> configmap));
65+
}
66+
}
67+
68+
69+
/**
70+
* get string param
71+
*
72+
* @param paramName param name;
73+
* @param defaultValue default value
74+
* @return param value
75+
*/
76+
public String getString(String paramName, String defaultValue) {
77+
Configmap configmap = paramsMap.get(paramName);
78+
if (configmap == null) {
79+
return defaultValue;
80+
}
81+
return Optional.ofNullable(configmap.getValue()).map(Object::toString).orElse(defaultValue);
82+
}
83+
84+
/**
85+
* get int param
86+
*
87+
* @param paramName param name;
88+
* @param defaultValue default value
89+
* @return param value
90+
*/
91+
public Integer getInteger(String paramName, Integer defaultValue) {
92+
Configmap configmap = paramsMap.get(paramName);
93+
if (configmap == null) {
94+
return defaultValue;
95+
}
96+
try {
97+
return Optional.ofNullable(configmap.getValue()).map(v -> Integer.parseInt(v.toString())).orElse(defaultValue);
98+
} catch (Exception e) {
99+
log.warn("int parameter type conversion error paramName:{} value:{} defaultValue:{}", paramName, configmap.getValue(), defaultValue);
100+
return defaultValue;
101+
}
102+
}
103+
104+
/**
105+
* get boolean param
106+
*
107+
* @param paramName param name;
108+
* @param defaultValue default value
109+
* @return param value
110+
*/
111+
public Boolean getBoolean(String paramName, Boolean defaultValue) {
112+
Configmap configmap = paramsMap.get(paramName);
113+
if (configmap == null) {
114+
return defaultValue;
115+
}
116+
try {
117+
return Optional.ofNullable(configmap.getValue()).map(v -> Boolean.parseBoolean(v.toString())).orElse(defaultValue);
118+
} catch (Exception e) {
119+
log.warn("boolean parameter type conversion error paramName:{} value:{} defaultValue:{}", paramName, configmap.getValue(), defaultValue);
120+
return defaultValue;
121+
}
122+
}
123+
124+
/**
125+
* get long param
126+
*
127+
* @param paramName param name;
128+
* @param defaultValue default value
129+
* @return param value
130+
*/
131+
public Long getLong(String paramName, Long defaultValue) {
132+
Configmap configmap = paramsMap.get(paramName);
133+
if (configmap == null) {
134+
return defaultValue;
135+
}
136+
try {
137+
return Optional.ofNullable(configmap.getValue()).map(v -> Long.parseLong(v.toString())).orElse(defaultValue);
138+
} catch (Exception e) {
139+
log.warn("long parameter type conversion error paramName:{} value:{} defaultValue:{}", paramName, configmap.getValue(), defaultValue);
140+
return defaultValue;
141+
}
142+
}
143+
144+
/**
145+
* get all params
146+
*
147+
* @return param list
148+
*/
149+
public List<Configmap> allParams() {
150+
return paramsMap.values().stream().map(SerializationUtils::clone).collect(Collectors.toList());
151+
}
152+
}
40153
}
Lines changed: 100 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,100 @@
1+
/*
2+
* Licensed to the Apache Software Foundation (ASF) under one
3+
* or more contributor license agreements. See the NOTICE file
4+
* distributed with this work for additional information
5+
* regarding copyright ownership. The ASF licenses this file
6+
* to you under the Apache License, Version 2.0 (the
7+
* "License"); you may not use this file except in compliance
8+
* with the License. You may obtain a copy of the License at
9+
*
10+
* http://www.apache.org/licenses/LICENSE-2.0
11+
*
12+
* Unless required by applicable law or agreed to in writing,
13+
* software distributed under the License is distributed on an
14+
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15+
* KIND, either express or implied. See the License for the
16+
* specific language governing permissions and limitations
17+
* under the License.
18+
*/
19+
20+
package org.apache.hertzbeat.common.entity.plugin;
21+
22+
23+
24+
import static org.junit.jupiter.api.Assertions.assertEquals;
25+
import static org.junit.jupiter.api.Assertions.assertTrue;
26+
27+
import java.util.Arrays;
28+
import java.util.List;
29+
import java.util.Optional;
30+
import org.apache.hertzbeat.common.entity.job.Configmap;
31+
import org.junit.jupiter.api.BeforeEach;
32+
import org.junit.jupiter.api.Test;
33+
34+
35+
/**
36+
* Test case for {@link PluginContext}
37+
*/
38+
class PluginContextTest {
39+
40+
PluginContext pluginContext;
41+
Configmap host = new Configmap("host", "127.0.0.1", (byte) 1);
42+
43+
@BeforeEach
44+
void init() {
45+
46+
List<Configmap> params = Arrays.asList(host,
47+
new Configmap("port", "80", (byte) 0),
48+
new Configmap("enableSsl", "true", (byte) 1),
49+
new Configmap("max", "1725116706000", (byte) 1)
50+
);
51+
pluginContext = PluginContext.builder().params(params).build();
52+
}
53+
54+
@Test
55+
void getString() {
56+
String host = pluginContext.param().getString("host", "192.168.0.1");
57+
assertEquals("127.0.0.1", host);
58+
}
59+
60+
@Test
61+
void getInteger() {
62+
Integer port = pluginContext.param().getInteger("port", 100);
63+
assertEquals(80, port);
64+
}
65+
66+
@Test
67+
void testGetNonExistentParameter() {
68+
Integer num = pluginContext.param().getInteger("num", 100);
69+
assertEquals(100, num);
70+
}
71+
72+
@Test
73+
void testErrorFormat() {
74+
Integer host = pluginContext.param().getInteger("host", 100);
75+
assertEquals(100, host);
76+
}
77+
78+
@Test
79+
void getBoolean() {
80+
boolean enableSsl = pluginContext.param().getBoolean("enableSsl", false);
81+
assertTrue(enableSsl);
82+
}
83+
84+
@Test
85+
void getLong() {
86+
Long max = pluginContext.param().getLong("max", 1800000000000L);
87+
assertEquals(1725116706000L, max);
88+
}
89+
90+
@Test
91+
void testImmutableData() {
92+
List<Configmap> list = pluginContext.param().allParams();
93+
Optional<Configmap> param = list.stream().filter(v -> v.getKey().equals("host")).findAny();
94+
if (param.isEmpty()) {
95+
throw new RuntimeException("error");
96+
}
97+
param.get().setValue("192.168.1.1");
98+
assertEquals(host.getValue(), "127.0.0.1");
99+
}
100+
}

manager/src/test/java/org/apache/hertzbeat/manager/service/PluginServiceTest.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,7 @@ void setUp() {
8080
@Test
8181
void testSavePlugin() {
8282

83-
List<PluginItem> pluginItems = Collections.singletonList(new PluginItem("org.apache.hertzbear.PluginTest", PluginType.POST_ALERT));
83+
List<PluginItem> pluginItems = Collections.singletonList(new PluginItem("org.apache.hertzbeat.PluginTest", PluginType.POST_ALERT));
8484
PluginServiceImpl service = spy(pluginService);
8585
doReturn(pluginItems).when(service).validateJarFile(any());
8686

0 commit comments

Comments
 (0)