Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[improve] added a method to obtain plugin parameters #2644

Merged
merged 5 commits into from
Sep 1, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@

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

import java.io.Serializable;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
Expand All @@ -31,7 +32,7 @@
@AllArgsConstructor
@NoArgsConstructor
@Builder
public class Configmap {
public class Configmap implements Serializable {

/**
* Parameter key, replace the content with the identifier ^^_key_^^ in the protocol
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,21 +20,134 @@
package org.apache.hertzbeat.common.entity.plugin;


import java.util.Collections;
import java.util.List;
import java.util.Map;
import java.util.Optional;
import java.util.stream.Collectors;
import lombok.Builder;
import lombok.Data;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.SerializationUtils;
import org.apache.hertzbeat.common.entity.job.Configmap;

/**
* plugin context
*/
@Builder
@Data
@Slf4j
public class PluginContext {

/**
* params
*/
List<Configmap> params;
private List<Configmap> params;
private ParamHolder paramHolder;

public ParamHolder param() {
if (paramHolder == null) {
paramHolder = new ParamHolder(params);
}
return paramHolder;
}

/**
* management parameter operations
*/
public static class ParamHolder {

private final Map<String, Configmap> paramsMap;

public ParamHolder(List<Configmap> params) {
if (params == null) {
paramsMap = Collections.emptyMap();
} else {
this.paramsMap = params.stream().collect(Collectors.toMap(Configmap::getKey, configmap -> configmap));
}
}


/**
* get string param
*
* @param paramName param name;
* @param defaultValue default value
* @return param value
*/
public String getString(String paramName, String defaultValue) {
Configmap configmap = paramsMap.get(paramName);
if (configmap == null) {
return defaultValue;
}
return Optional.ofNullable(configmap.getValue()).map(Object::toString).orElse(defaultValue);
}

/**
* get int param
*
* @param paramName param name;
* @param defaultValue default value
* @return param value
*/
public Integer getInteger(String paramName, Integer defaultValue) {
Configmap configmap = paramsMap.get(paramName);
if (configmap == null) {
return defaultValue;
}
try {
return Optional.ofNullable(configmap.getValue()).map(v -> Integer.parseInt(v.toString())).orElse(defaultValue);
} catch (Exception e) {
log.warn("int parameter type conversion error paramName:{} value:{} defaultValue:{}", paramName, configmap.getValue(), defaultValue);
return defaultValue;
}
}

/**
* get boolean param
*
* @param paramName param name;
* @param defaultValue default value
* @return param value
*/
public Boolean getBoolean(String paramName, Boolean defaultValue) {
Configmap configmap = paramsMap.get(paramName);
if (configmap == null) {
return defaultValue;
}
try {
return Optional.ofNullable(configmap.getValue()).map(v -> Boolean.parseBoolean(v.toString())).orElse(defaultValue);
} catch (Exception e) {
log.warn("boolean parameter type conversion error paramName:{} value:{} defaultValue:{}", paramName, configmap.getValue(), defaultValue);
return defaultValue;
}
}

/**
* get long param
*
* @param paramName param name;
* @param defaultValue default value
* @return param value
*/
public Long getLong(String paramName, Long defaultValue) {
Configmap configmap = paramsMap.get(paramName);
if (configmap == null) {
return defaultValue;
}
try {
return Optional.ofNullable(configmap.getValue()).map(v -> Long.parseLong(v.toString())).orElse(defaultValue);
} catch (Exception e) {
log.warn("long parameter type conversion error paramName:{} value:{} defaultValue:{}", paramName, configmap.getValue(), defaultValue);
return defaultValue;
}
}

/**
* get all params
*
* @return param list
*/
public List<Configmap> allParams() {
return paramsMap.values().stream().map(SerializationUtils::clone).collect(Collectors.toList());
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,100 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/

package org.apache.hertzbeat.common.entity.plugin;



import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertTrue;

import java.util.Arrays;
import java.util.List;
import java.util.Optional;
import org.apache.hertzbeat.common.entity.job.Configmap;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;


/**
* Test case for {@link PluginContext}
*/
class PluginContextTest {

PluginContext pluginContext;
Configmap host = new Configmap("host", "127.0.0.1", (byte) 1);

@BeforeEach
void init() {

List<Configmap> params = Arrays.asList(host,
new Configmap("port", "80", (byte) 0),
new Configmap("enableSsl", "true", (byte) 1),
new Configmap("max", "1725116706000", (byte) 1)
);
pluginContext = PluginContext.builder().params(params).build();
}

@Test
void getString() {
String host = pluginContext.param().getString("host", "192.168.0.1");
assertEquals("127.0.0.1", host);
}

@Test
void getInteger() {
Integer port = pluginContext.param().getInteger("port", 100);
assertEquals(80, port);
}

@Test
void testGetNonExistentParameter() {
Integer num = pluginContext.param().getInteger("num", 100);
assertEquals(100, num);
}

@Test
void testErrorFormat() {
Integer host = pluginContext.param().getInteger("host", 100);
assertEquals(100, host);
}

@Test
void getBoolean() {
boolean enableSsl = pluginContext.param().getBoolean("enableSsl", false);
assertTrue(enableSsl);
}

@Test
void getLong() {
Long max = pluginContext.param().getLong("max", 1800000000000L);
assertEquals(1725116706000L, max);
}

@Test
void testImmutableData() {
List<Configmap> list = pluginContext.param().allParams();
Optional<Configmap> param = list.stream().filter(v -> v.getKey().equals("host")).findAny();
if (param.isEmpty()) {
throw new RuntimeException("error");
}
param.get().setValue("192.168.1.1");
assertEquals(host.getValue(), "127.0.0.1");
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ void setUp() {
@Test
void testSavePlugin() {

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

Expand Down
Loading