You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@eventmesh.apache.org by mi...@apache.org on 2022/10/17 08:17:17 UTC

[incubator-eventmesh] branch master updated: use common properties loading method in trace plugin

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

mikexue pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/incubator-eventmesh.git


The following commit(s) were added to refs/heads/master by this push:
     new 5b55ae4f use common properties loading method in trace plugin
     new 87e6ebd5 Merge pull request #1604 from horoc/add-common-properties-reader
5b55ae4f is described below

commit 5b55ae4fbbd4f1459365ed525e2b77dce4d3ad92
Author: horoc <ho...@gmail.com>
AuthorDate: Sat Oct 15 19:08:28 2022 +0800

    use common properties loading method in trace plugin
---
 .../admin/rocketmq/handler/TopicsHandler.java      |  2 +-
 .../eventmesh/common/utils/PropertiesUtils.java    | 23 ++++++++++++++++++----
 .../common/utils/PropertiesUtilsTest.java          | 15 ++++++++++++++
 .../kafka/producer/KafkaProducerImpl.java          |  8 +++++---
 .../trace/api/config/ExporterConfiguration.java    |  8 +++-----
 .../trace/zipkin/config/ZipkinConfiguration.java   |  6 ++----
 6 files changed, 45 insertions(+), 17 deletions(-)

diff --git a/eventmesh-admin/eventmesh-admin-rocketmq/src/main/java/org/apache/eventmesh/admin/rocketmq/handler/TopicsHandler.java b/eventmesh-admin/eventmesh-admin-rocketmq/src/main/java/org/apache/eventmesh/admin/rocketmq/handler/TopicsHandler.java
index 282b6452..fce96618 100644
--- a/eventmesh-admin/eventmesh-admin-rocketmq/src/main/java/org/apache/eventmesh/admin/rocketmq/handler/TopicsHandler.java
+++ b/eventmesh-admin/eventmesh-admin-rocketmq/src/main/java/org/apache/eventmesh/admin/rocketmq/handler/TopicsHandler.java
@@ -94,7 +94,7 @@ public class TopicsHandler implements HttpHandler {
             httpExchange.getResponseHeaders().add(CONTENT_TYPE, APPLICATION_JSON);
             httpExchange.sendResponseHeaders(500, 0);
             result = TOPIC_ERROR;
-            logger.error(result,e);
+            logger.error(result, e);
             out.write(result.getBytes(Constants.DEFAULT_CHARSET));
         } finally {
             if (out != null) {
diff --git a/eventmesh-common/src/main/java/org/apache/eventmesh/common/utils/PropertiesUtils.java b/eventmesh-common/src/main/java/org/apache/eventmesh/common/utils/PropertiesUtils.java
index 2272b1fd..ee7e9b0b 100644
--- a/eventmesh-common/src/main/java/org/apache/eventmesh/common/utils/PropertiesUtils.java
+++ b/eventmesh-common/src/main/java/org/apache/eventmesh/common/utils/PropertiesUtils.java
@@ -23,8 +23,11 @@ import org.apache.commons.lang3.StringUtils;
 
 import java.io.BufferedReader;
 import java.io.File;
-import java.io.FileReader;
+import java.io.FileInputStream;
 import java.io.IOException;
+import java.io.InputStreamReader;
+import java.nio.charset.Charset;
+import java.nio.charset.StandardCharsets;
 import java.util.Properties;
 
 import com.google.common.base.Preconditions;
@@ -73,9 +76,10 @@ public class PropertiesUtils {
      *
      * @param properties
      * @param path
+     * @param cs
      * @throws IOException Exception when loading properties, like illegal content, file permission denies
      */
-    public static void loadPropertiesWhenFileExist(Properties properties, String path) throws IOException {
+    public static void loadPropertiesWhenFileExist(Properties properties, String path, Charset cs) throws IOException {
         Preconditions.checkNotNull(properties, "Properties can not be null");
 
         File file = new File(path);
@@ -83,10 +87,21 @@ public class PropertiesUtils {
             return;
         }
 
-        try (FileReader reader = new FileReader(path)) {
-            properties.load(new BufferedReader(reader));
+        try (FileInputStream reader = new FileInputStream(file)) {
+            properties.load(new BufferedReader(new InputStreamReader(reader, cs)));
         } catch (IOException e) {
             throw e;
         }
     }
+
+    /**
+     * Load properties from file when file is exist
+     *
+     * @param properties
+     * @param path
+     * @throws IOException Exception when loading properties, like illegal content, file permission denies
+     */
+    public static void loadPropertiesWhenFileExist(Properties properties, String path) throws IOException {
+        loadPropertiesWhenFileExist(properties, path, StandardCharsets.UTF_8);
+    }
 }
diff --git a/eventmesh-common/src/test/java/org/apache/eventmesh/common/utils/PropertiesUtilsTest.java b/eventmesh-common/src/test/java/org/apache/eventmesh/common/utils/PropertiesUtilsTest.java
index 04f578cc..2efdfe53 100644
--- a/eventmesh-common/src/test/java/org/apache/eventmesh/common/utils/PropertiesUtilsTest.java
+++ b/eventmesh-common/src/test/java/org/apache/eventmesh/common/utils/PropertiesUtilsTest.java
@@ -17,6 +17,8 @@
 
 package org.apache.eventmesh.common.utils;
 
+import org.apache.eventmesh.common.config.ConfigurationWrapperTest;
+
 import java.util.Properties;
 
 import org.junit.Assert;
@@ -40,4 +42,17 @@ public class PropertiesUtilsTest {
         Assert.assertEquals(3, to.size());
         Assert.assertEquals(2, ((Properties) to.get("c")).size());
     }
+
+    @Test
+    public void testLoadPropertiesWhenFileExist() {
+        Properties properties = new Properties();
+        String path = ConfigurationWrapperTest.class.getResource("/configuration.properties").getPath();
+        try {
+            PropertiesUtils.loadPropertiesWhenFileExist(properties, path);
+            Assert.assertEquals(properties.get("eventMesh.server.env").toString(), "value1");
+            Assert.assertEquals(properties.get("eventMesh.server.idc").toString(), "value2");
+        } catch (Exception e) {
+            Assert.fail();
+        }
+    }
 }
diff --git a/eventmesh-connector-plugin/eventmesh-connector-kafka/src/main/java/org/apache/eventmesh/connector/kafka/producer/KafkaProducerImpl.java b/eventmesh-connector-plugin/eventmesh-connector-kafka/src/main/java/org/apache/eventmesh/connector/kafka/producer/KafkaProducerImpl.java
index 28cdcf3a..f5c1355b 100644
--- a/eventmesh-connector-plugin/eventmesh-connector-kafka/src/main/java/org/apache/eventmesh/connector/kafka/producer/KafkaProducerImpl.java
+++ b/eventmesh-connector-plugin/eventmesh-connector-kafka/src/main/java/org/apache/eventmesh/connector/kafka/producer/KafkaProducerImpl.java
@@ -17,14 +17,16 @@
 
 package org.apache.eventmesh.connector.kafka.producer;
 
-import java.util.Properties;
-
-import io.cloudevents.CloudEvent;
 import org.apache.eventmesh.api.RequestReplyCallback;
 import org.apache.eventmesh.api.SendCallback;
 import org.apache.eventmesh.api.producer.Producer;
+
 import org.apache.kafka.clients.producer.ProducerConfig;
 
+import java.util.Properties;
+
+import io.cloudevents.CloudEvent;
+
 public class KafkaProducerImpl implements Producer {
 
     private ProducerImpl producer;
diff --git a/eventmesh-trace-plugin/eventmesh-trace-api/src/main/java/org/apache/eventmesh/trace/api/config/ExporterConfiguration.java b/eventmesh-trace-plugin/eventmesh-trace-api/src/main/java/org/apache/eventmesh/trace/api/config/ExporterConfiguration.java
index ad1a45c8..ef07c261 100644
--- a/eventmesh-trace-plugin/eventmesh-trace-api/src/main/java/org/apache/eventmesh/trace/api/config/ExporterConfiguration.java
+++ b/eventmesh-trace-plugin/eventmesh-trace-api/src/main/java/org/apache/eventmesh/trace/api/config/ExporterConfiguration.java
@@ -17,6 +17,7 @@
 
 package org.apache.eventmesh.trace.api.config;
 
+import org.apache.eventmesh.common.utils.PropertiesUtils;
 import org.apache.eventmesh.trace.api.common.EventMeshTraceConstants;
 
 import org.apache.commons.lang3.StringUtils;
@@ -107,11 +108,8 @@ public class ExporterConfiguration {
         }
         // get from config home
         try {
-            String configPath =
-                System.getProperty("confPath", System.getenv("confPath")) + File.separator + CONFIG_FILE;
-            if (new File(configPath).exists()) {
-                properties.load(new BufferedReader(new FileReader(configPath)));
-            }
+            String configPath = System.getProperty("confPath", System.getenv("confPath")) + File.separator + CONFIG_FILE;
+            PropertiesUtils.loadPropertiesWhenFileExist(properties, configPath);
         } catch (IOException e) {
             throw new IllegalArgumentException("Cannot load exporter.properties file from conf");
         }
diff --git a/eventmesh-trace-plugin/eventmesh-trace-zipkin/src/main/java/org/apache/eventmesh/trace/zipkin/config/ZipkinConfiguration.java b/eventmesh-trace-plugin/eventmesh-trace-zipkin/src/main/java/org/apache/eventmesh/trace/zipkin/config/ZipkinConfiguration.java
index c567e977..fd462c8a 100644
--- a/eventmesh-trace-plugin/eventmesh-trace-zipkin/src/main/java/org/apache/eventmesh/trace/zipkin/config/ZipkinConfiguration.java
+++ b/eventmesh-trace-plugin/eventmesh-trace-zipkin/src/main/java/org/apache/eventmesh/trace/zipkin/config/ZipkinConfiguration.java
@@ -18,13 +18,13 @@
 package org.apache.eventmesh.trace.zipkin.config;
 
 import org.apache.eventmesh.common.Constants;
+import org.apache.eventmesh.common.utils.PropertiesUtils;
 import org.apache.eventmesh.trace.zipkin.common.ZipkinConstants;
 
 import org.apache.commons.lang3.StringUtils;
 
 import java.io.BufferedReader;
 import java.io.File;
-import java.io.FileReader;
 import java.io.IOException;
 import java.io.InputStream;
 import java.io.InputStreamReader;
@@ -88,9 +88,7 @@ public class ZipkinConfiguration {
         // get from config home
         try {
             String configPath = Constants.EVENTMESH_CONF_HOME + File.separator + CONFIG_FILE;
-            if (new File(configPath).exists()) {
-                properties.load(new BufferedReader(new FileReader(configPath)));
-            }
+            PropertiesUtils.loadPropertiesWhenFileExist(properties, configPath);
         } catch (IOException e) {
             throw new IllegalArgumentException("Cannot load zipkin.properties file from conf");
         }


---------------------------------------------------------------------
To unsubscribe, e-mail: commits-unsubscribe@eventmesh.apache.org
For additional commands, e-mail: commits-help@eventmesh.apache.org