You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@tubemq.apache.org by GitBox <gi...@apache.org> on 2020/12/20 07:52:33 UTC

[GitHub] [incubator-tubemq] gosonzhang commented on a change in pull request #354: [TUBEMQ-459] init agent project and add agent common module

gosonzhang commented on a change in pull request #354:
URL: https://github.com/apache/incubator-tubemq/pull/354#discussion_r546336144



##########
File path: tubemq-agent/pom.xml
##########
@@ -0,0 +1,321 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<!--
+
+    Licensed 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.
+
+-->
+<project xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
+    xmlns="http://maven.apache.org/POM/4.0.0"
+    xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
+    <groupId>org.apache.tubemq</groupId>
+    <artifactId>tubemq-agent</artifactId>
+    <packaging>pom</packaging>
+    <version>0.0.1-SNAPSHOT</version>
+    <modelVersion>4.0.0</modelVersion>
+
+    <modules>
+        <module>agent-common</module>
+    </modules>
+
+    <properties>
+        <awaitility.version>4.0.3</awaitility.version>
+        <bytebuddy.version>1.10.10</bytebuddy.version>
+        <common.io>2.6</common.io>
+        <common.lang3.version>3.10</common.lang3.version>
+        <commons.cli.version>1.4</commons.cli.version>
+        <dbutils.version>1.7</dbutils.version>
+        <encoding>UTF-8</encoding>
+        <gson.version>2.8.5</gson.version>
+        <guava.version>12.0.1</guava.version>
+        <jdk.version>1.8</jdk.version>
+        <log4j2.version>2.13.1</log4j2.version>
+        <mockito.version>3.3.3</mockito.version>
+        <plugin.assembly.version>3.2.0</plugin.assembly.version>
+        <plugin.compile.version>3.8.1</plugin.compile.version>
+        <rocksdb.version>6.8.1</rocksdb.version>
+        <sleepycat.version>18.3.12</sleepycat.version>
+        <slf4j.version>1.7.30</slf4j.version>
+        <unit.version>4.13</unit.version>
+        <bussdk.version>1.2.17</bussdk.version>
+        <common.lang.version>2.4</common.lang.version>
+        <spring.version>2.5.6</spring.version>
+        <oro.version>2.0.8</oro.version>
+        <aviator.version>2.2.1</aviator.version>
+        <avro.version>1.7.2</avro.version>
+
+        <netty.version>3.8.0.Final</netty.version>
+        <snappy.version>1.0.4.1</snappy.version>
+        <protobuf.version>2.5.0</protobuf.version>
+        <httpclient.version>4.5.13</httpclient.version>
+        <fastjson.version>1.2.68</fastjson.version>
+        <sleepycat.version>6.4.9</sleepycat.version>

Review comment:
       The 6.4.9 version of sleepycat is not the Apache v2 version
   
   Please check the LICENSE of the code and the dependent component packages before incorporating the code on a large scale.

##########
File path: tubemq-agent/README.zh.md
##########
@@ -0,0 +1,53 @@
+# 项目说明

Review comment:
       Need to translate Chinese into English

##########
File path: tubemq-agent/agent-common/src/main/java/org/apache/tubemq/agent/conf/Configuration.java
##########
@@ -0,0 +1,260 @@
+/**
+ * Licensed 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.tubemq.agent.conf;
+
+import com.google.gson.JsonArray;
+import com.google.gson.JsonElement;
+import com.google.gson.JsonObject;
+import com.google.gson.JsonParser;
+import com.google.gson.JsonPrimitive;
+import java.io.InputStream;
+import java.io.InputStreamReader;
+import java.io.Reader;
+import java.nio.charset.StandardCharsets;
+import java.util.ArrayList;
+import java.util.HashMap;
+import java.util.List;
+import java.util.Map;
+import java.util.Properties;
+import org.apache.commons.lang3.StringUtils;
+import org.apache.tubemq.agent.utils.AgentUtils;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+public abstract class Configuration {
+
+    private static final Logger LOGGER = LoggerFactory.getLogger(Configuration.class);
+    private static final JsonParser JSON_PARSER = new JsonParser();
+
+    private final Map<String, JsonPrimitive> configStorage = new HashMap<>();
+
+    // get config file by class loader
+    private ClassLoader classLoader;
+
+    {
+        classLoader = Thread.currentThread().getContextClassLoader();
+        if (classLoader == null) {
+            classLoader = AgentConfiguration.class.getClassLoader();
+        }
+    }
+
+    public abstract boolean allRequiredKeyExist();
+
+    /**
+     * support load config file from json/properties file.
+     *
+     * @param fileName -  file name
+     * @param isJson - whether is json file
+     */
+    private void loadResource(String fileName, boolean isJson) {
+        Reader reader = null;
+        try {
+            InputStream inputStream = classLoader.getResourceAsStream(fileName);
+            if (inputStream != null) {
+                reader = new InputStreamReader(inputStream, StandardCharsets.UTF_8);
+                if (isJson) {
+                    JsonElement tmpElement = JSON_PARSER.parse(reader).getAsJsonObject();
+                    updateConfig(new HashMap<>(), 0, tmpElement);
+                } else {
+                    Properties properties = new Properties();
+                    properties.load(reader);
+                    properties.forEach((key, value) -> configStorage.put((String) key,
+                            new JsonPrimitive((String) value)));
+                }
+            }
+        } catch (Exception ioe) {
+            LOGGER.error("error init {}", fileName, ioe);
+        } finally {
+            AgentUtils.finallyClose(reader);
+        }
+    }
+
+    /**
+     * load config from json string.
+     *
+     * @param jsonStr - json string
+     */
+    public void loadJsonStrResource(String jsonStr) {
+        JsonElement tmpElement = JSON_PARSER.parse(jsonStr);
+        updateConfig(new HashMap<>(), 0, tmpElement);
+    }
+
+    /**
+     * load config file from CLASS_PATH. config file is json file.
+     *
+     * @param fileName - file name
+     */
+    void loadJsonResource(String fileName) {
+        loadResource(fileName, true);
+    }
+
+    void loadPropertiesResource(String fileName) {
+        loadResource(fileName, false);
+    }
+
+    /**
+     * Convert json string to map
+     *
+     * @param keyDeptPath - map
+     * @param dept - json dept
+     * @param tmpElement - json element
+     */
+    void updateConfig(HashMap<Integer, String> keyDeptPath, int dept, JsonElement tmpElement) {
+        if (tmpElement instanceof JsonObject) {
+            JsonObject tmpJsonObject = tmpElement.getAsJsonObject();
+            for (String key : tmpJsonObject.keySet()) {
+                keyDeptPath.put(dept, key);
+                updateConfig(keyDeptPath, dept + 1, tmpJsonObject.get(key));
+            }
+        } else if (tmpElement instanceof JsonArray) {
+            JsonArray tmpJsonArray = tmpElement.getAsJsonArray();
+            String lastKey = keyDeptPath.getOrDefault(dept - 1, "");
+            for (int index = 0; index < tmpJsonArray.size(); index++) {
+                keyDeptPath.put(dept - 1, lastKey + "[" + index + "]");
+                updateConfig(keyDeptPath, dept, tmpJsonArray.get(index));
+            }
+        } else if (tmpElement instanceof JsonPrimitive) {
+            List<String> builder = new ArrayList<>();
+            for (int index = 0; index < dept; index++) {
+                builder.add(keyDeptPath.getOrDefault(index, ""));
+            }
+            String keyChain = StringUtils.join(builder, ".");
+            if (!StringUtils.isBlank(keyChain)) {
+                configStorage.put(keyChain, tmpElement.getAsJsonPrimitive());
+            }
+        }
+    }
+
+    /**
+     * get int from config
+     *
+     * @param key - key
+     * @param defaultValue - default value
+     * @return value
+     */
+    public int getInt(String key, int defaultValue) {
+        JsonElement value = configStorage.get(key);
+        return value == null ? defaultValue : value.getAsInt();
+    }
+
+    /**
+     * get int from config
+     *
+     * @param key - key
+     * @return value
+     * @throws NullPointerException npe
+     */
+    public int getInt(String key) {
+        JsonElement value = configStorage.get(key);
+        if (value == null) {
+            throw new NullPointerException("null value for key " + key);
+        }
+        return value.getAsInt();
+    }
+
+    /**

Review comment:
       Need to translate Chinese into English, the same below
   
   




----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

For queries about this service, please contact Infrastructure at:
users@infra.apache.org