This is an automated email from the ASF dual-hosted git repository.

zhaoqingran pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/hertzbeat.git


The following commit(s) were added to refs/heads/master by this push:
     new 31fcb0d83 [improve] added a method to obtain plugin parameters (#2644)
31fcb0d83 is described below

commit 31fcb0d839619d599a8da025f40233da3f74e47e
Author: liutianyou <[email protected]>
AuthorDate: Sun Sep 1 20:13:28 2024 +0800

    [improve] added a method to obtain plugin parameters (#2644)
    
    Co-authored-by: aias00 <[email protected]>
---
 .../hertzbeat/common/entity/job/Configmap.java     |   3 +-
 .../common/entity/plugin/PluginContext.java        | 119 ++++++++++++++++++++-
 .../common/entity/plugin/PluginContextTest.java    | 100 +++++++++++++++++
 .../manager/service/PluginServiceTest.java         |   2 +-
 4 files changed, 219 insertions(+), 5 deletions(-)

diff --git 
a/common/src/main/java/org/apache/hertzbeat/common/entity/job/Configmap.java 
b/common/src/main/java/org/apache/hertzbeat/common/entity/job/Configmap.java
index f5aacd4d0..ace725f44 100644
--- a/common/src/main/java/org/apache/hertzbeat/common/entity/job/Configmap.java
+++ b/common/src/main/java/org/apache/hertzbeat/common/entity/job/Configmap.java
@@ -17,6 +17,7 @@
 
 package org.apache.hertzbeat.common.entity.job;
 
+import java.io.Serializable;
 import lombok.AllArgsConstructor;
 import lombok.Builder;
 import lombok.Data;
@@ -31,7 +32,7 @@ import lombok.NoArgsConstructor;
 @AllArgsConstructor
 @NoArgsConstructor
 @Builder
-public class Configmap {
+public class Configmap implements Serializable {
 
     /**
      * Parameter key, replace the content with the identifier ^^_key_^^ in the 
protocol
diff --git 
a/common/src/main/java/org/apache/hertzbeat/common/entity/plugin/PluginContext.java
 
b/common/src/main/java/org/apache/hertzbeat/common/entity/plugin/PluginContext.java
index e8433f9ab..4c9fc3f16 100644
--- 
a/common/src/main/java/org/apache/hertzbeat/common/entity/plugin/PluginContext.java
+++ 
b/common/src/main/java/org/apache/hertzbeat/common/entity/plugin/PluginContext.java
@@ -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());
+        }
+    }
 }
diff --git 
a/common/src/test/java/org/apache/hertzbeat/common/entity/plugin/PluginContextTest.java
 
b/common/src/test/java/org/apache/hertzbeat/common/entity/plugin/PluginContextTest.java
new file mode 100644
index 000000000..a434e407f
--- /dev/null
+++ 
b/common/src/test/java/org/apache/hertzbeat/common/entity/plugin/PluginContextTest.java
@@ -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");
+    }
+}
diff --git 
a/manager/src/test/java/org/apache/hertzbeat/manager/service/PluginServiceTest.java
 
b/manager/src/test/java/org/apache/hertzbeat/manager/service/PluginServiceTest.java
index 59dc49b20..a2ccc15e7 100644
--- 
a/manager/src/test/java/org/apache/hertzbeat/manager/service/PluginServiceTest.java
+++ 
b/manager/src/test/java/org/apache/hertzbeat/manager/service/PluginServiceTest.java
@@ -80,7 +80,7 @@ class PluginServiceTest {
     @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());
 


---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to