You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@eventmesh.apache.org by jo...@apache.org on 2022/12/11 14:43:29 UTC

[incubator-eventmesh] branch master updated: simplify code (#2515)

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

jonyang 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 726cf69a8 simplify code (#2515)
726cf69a8 is described below

commit 726cf69a8ad1276599323bdf0390c5d44701887f
Author: weihubeats <we...@163.com>
AuthorDate: Sun Dec 11 22:43:24 2022 +0800

    simplify code (#2515)
---
 .../apache/eventmesh/common/utils/AssertUtils.java | 12 +++++
 .../rabbitmq/config/ConfigurationHolder.java       | 56 ++++++++--------------
 2 files changed, 33 insertions(+), 35 deletions(-)

diff --git a/eventmesh-common/src/main/java/org/apache/eventmesh/common/utils/AssertUtils.java b/eventmesh-common/src/main/java/org/apache/eventmesh/common/utils/AssertUtils.java
index c7148d5a3..0ba874592 100644
--- a/eventmesh-common/src/main/java/org/apache/eventmesh/common/utils/AssertUtils.java
+++ b/eventmesh-common/src/main/java/org/apache/eventmesh/common/utils/AssertUtils.java
@@ -17,6 +17,8 @@
 
 package org.apache.eventmesh.common.utils;
 
+import org.apache.commons.lang3.StringUtils;
+
 import java.util.Objects;
 
 /**
@@ -46,4 +48,14 @@ public final class AssertUtils {
         }
     }
 
+    /**
+     * assert str is not black
+     * @param str str
+     * @param message message
+     */
+    public static void notBlack(final String str, final String message) {
+        isTrue(StringUtils.isNoneBlank(str), message);
+    }
+
+
 }
diff --git a/eventmesh-connector-plugin/eventmesh-connector-rabbitmq/src/main/java/org/apache/eventmesh/connector/rabbitmq/config/ConfigurationHolder.java b/eventmesh-connector-plugin/eventmesh-connector-rabbitmq/src/main/java/org/apache/eventmesh/connector/rabbitmq/config/ConfigurationHolder.java
index 43f29d4e7..34a7e7189 100644
--- a/eventmesh-connector-plugin/eventmesh-connector-rabbitmq/src/main/java/org/apache/eventmesh/connector/rabbitmq/config/ConfigurationHolder.java
+++ b/eventmesh-connector-plugin/eventmesh-connector-rabbitmq/src/main/java/org/apache/eventmesh/connector/rabbitmq/config/ConfigurationHolder.java
@@ -17,9 +17,8 @@
 
 package org.apache.eventmesh.connector.rabbitmq.config;
 
-import org.apache.commons.lang3.StringUtils;
+import org.apache.eventmesh.common.utils.AssertUtils;
 
-import com.google.common.base.Preconditions;
 import com.rabbitmq.client.BuiltinExchangeType;
 
 import lombok.Data;
@@ -39,41 +38,28 @@ public class ConfigurationHolder {
     public boolean autoAck;
 
     public void init() {
-        String host = ConfigurationWrapper.getProperty(ConfigKey.HOST);
-        Preconditions.checkState(StringUtils.isNotEmpty(host), String.format("%s error", ConfigKey.HOST));
-        this.host = host;
-
-        String port = ConfigurationWrapper.getProperty(ConfigKey.PORT);
-        Preconditions.checkState(StringUtils.isNotEmpty(port), String.format("%s error", ConfigKey.PORT));
-        this.port = Integer.parseInt(port);
-
-        String username = ConfigurationWrapper.getProperty(ConfigKey.USER_NAME);
-        Preconditions.checkState(StringUtils.isNotEmpty(username), String.format("%s error", ConfigKey.USER_NAME));
-        this.username = username;
-
-        String passwd = ConfigurationWrapper.getProperty(ConfigKey.PASSWD);
-        Preconditions.checkState(StringUtils.isNotEmpty(passwd), String.format("%s error", ConfigKey.PASSWD));
-        this.passwd = passwd;
-
+        this.host = getProperty(ConfigKey.HOST);
+        this.port = Integer.parseInt(getProperty(ConfigKey.PORT));
+        this.username = getProperty(ConfigKey.USER_NAME);
+        this.passwd = getProperty(ConfigKey.PASSWD);
         this.virtualHost = ConfigurationWrapper.getProperty(ConfigKey.VIRTUAL_HOST);
+        this.exchangeType = BuiltinExchangeType.valueOf(getProperty(ConfigKey.EXCHANGE_TYPE));
+        this.exchangeName = getProperty(ConfigKey.EXCHANGE_NAME);
+        this.routingKey = getProperty(ConfigKey.ROUTING_KEY);
+        this.queueName = getProperty(ConfigKey.QUEUE_NAME);
+        this.autoAck = Boolean.parseBoolean(getProperty(ConfigKey.AUTO_ACK));
+    }
 
-        String exchangeType = ConfigurationWrapper.getProperty(ConfigKey.EXCHANGE_TYPE);
-        Preconditions.checkState(StringUtils.isNotEmpty(exchangeType), String.format("%s error", ConfigKey.EXCHANGE_TYPE));
-        this.exchangeType = BuiltinExchangeType.valueOf(exchangeType);
-
-        String exchangeName = ConfigurationWrapper.getProperty(ConfigKey.EXCHANGE_NAME);
-        Preconditions.checkState(StringUtils.isNotEmpty(host), String.format("%s error", ConfigKey.EXCHANGE_NAME));
-        this.exchangeName = exchangeName;
-
-        String routingKey = ConfigurationWrapper.getProperty(ConfigKey.ROUTING_KEY);
-        Preconditions.checkState(StringUtils.isNotEmpty(routingKey), String.format("%s error", ConfigKey.ROUTING_KEY));
-        this.routingKey = routingKey;
-
-        String queueName = ConfigurationWrapper.getProperty(ConfigKey.QUEUE_NAME);
-        Preconditions.checkState(StringUtils.isNotEmpty(queueName), String.format("%s error", ConfigKey.QUEUE_NAME));
-        this.queueName = queueName;
+    /**
+     * get property
+     *
+     * @param configKey config key
+     * @return property
+     */
+    private String getProperty(String configKey) {
+        String property = ConfigurationWrapper.getProperty(configKey);
+        AssertUtils.notBlack(property, String.format("%s error", configKey));
+        return property;
 
-        String autoAck = ConfigurationWrapper.getProperty(ConfigKey.AUTO_ACK);
-        this.autoAck = StringUtils.isNotEmpty(autoAck) && Boolean.parseBoolean(autoAck);
     }
 }


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