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

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


The following commit(s) were added to refs/heads/master by this push:
     new 0a355fb6b80 Refactor PluginConfiguration and remove 
PluginConfigurationException (#21173)
0a355fb6b80 is described below

commit 0a355fb6b8022be5a3d5beacf65b15474cf18cff
Author: Liang Zhang <[email protected]>
AuthorDate: Sun Sep 25 19:47:45 2022 +0800

    Refactor PluginConfiguration and remove PluginConfigurationException 
(#21173)
---
 .../agent/config/PluginConfiguration.java          | 12 +++++++++
 .../exception/PluginConfigurationException.java    | 30 ----------------------
 .../service/JaegerTracingPluginBootService.java    | 14 +++++-----
 .../service/ZipkinTracingPluginBootService.java    |  6 +----
 4 files changed, 19 insertions(+), 43 deletions(-)

diff --git 
a/shardingsphere-agent/shardingsphere-agent-api/src/main/java/org/apache/shardingsphere/agent/config/PluginConfiguration.java
 
b/shardingsphere-agent/shardingsphere-agent-api/src/main/java/org/apache/shardingsphere/agent/config/PluginConfiguration.java
index cf5abe99155..fec7dde0cb7 100644
--- 
a/shardingsphere-agent/shardingsphere-agent-api/src/main/java/org/apache/shardingsphere/agent/config/PluginConfiguration.java
+++ 
b/shardingsphere-agent/shardingsphere-agent-api/src/main/java/org/apache/shardingsphere/agent/config/PluginConfiguration.java
@@ -17,6 +17,8 @@
 
 package org.apache.shardingsphere.agent.config;
 
+import com.google.common.base.Preconditions;
+import com.google.common.base.Strings;
 import lombok.Getter;
 import lombok.RequiredArgsConstructor;
 
@@ -36,4 +38,14 @@ public final class PluginConfiguration {
     private final String password;
     
     private final Properties props;
+    
+    /**
+     * Validate.
+     * 
+     * @param type plugin component type
+     */
+    public void validate(final String type) {
+        Preconditions.checkArgument(!Strings.isNullOrEmpty(host), "Hostname of 
%s is required", type);
+        Preconditions.checkArgument(port > 0, "Port `%d` of %s must be a 
positive number", port, type);
+    }
 }
diff --git 
a/shardingsphere-agent/shardingsphere-agent-api/src/main/java/org/apache/shardingsphere/agent/exception/PluginConfigurationException.java
 
b/shardingsphere-agent/shardingsphere-agent-api/src/main/java/org/apache/shardingsphere/agent/exception/PluginConfigurationException.java
deleted file mode 100644
index 69fb4498fd8..00000000000
--- 
a/shardingsphere-agent/shardingsphere-agent-api/src/main/java/org/apache/shardingsphere/agent/exception/PluginConfigurationException.java
+++ /dev/null
@@ -1,30 +0,0 @@
-/*
- * 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.shardingsphere.agent.exception;
-
-/**
- * Plugin configuration exception.
- */
-public final class PluginConfigurationException extends RuntimeException {
-    
-    private static final long serialVersionUID = -3298813951573088711L;
-    
-    public PluginConfigurationException(final String errorMessage, final 
Object... args) {
-        super(String.format(errorMessage, args));
-    }
-}
diff --git 
a/shardingsphere-agent/shardingsphere-agent-plugins/shardingsphere-agent-plugin-tracing/shardingsphere-agent-tracing-jaeger/src/main/java/org/apache/shardingsphere/agent/plugin/tracing/jaeger/service/JaegerTracingPluginBootService.java
 
b/shardingsphere-agent/shardingsphere-agent-plugins/shardingsphere-agent-plugin-tracing/shardingsphere-agent-tracing-jaeger/src/main/java/org/apache/shardingsphere/agent/plugin/tracing/jaeger/service/JaegerTracingPluginBootService.java
index 716fb01eb44..5c919257d6f 100644
--- 
a/shardingsphere-agent/shardingsphere-agent-plugins/shardingsphere-agent-plugin-tracing/shardingsphere-agent-tracing-jaeger/src/main/java/org/apache/shardingsphere/agent/plugin/tracing/jaeger/service/JaegerTracingPluginBootService.java
+++ 
b/shardingsphere-agent/shardingsphere-agent-plugins/shardingsphere-agent-plugin-tracing/shardingsphere-agent-tracing-jaeger/src/main/java/org/apache/shardingsphere/agent/plugin/tracing/jaeger/service/JaegerTracingPluginBootService.java
@@ -17,10 +17,11 @@
 
 package org.apache.shardingsphere.agent.plugin.tracing.jaeger.service;
 
+import com.google.common.base.Preconditions;
+import com.google.common.base.Strings;
 import io.jaegertracing.Configuration;
 import io.opentracing.util.GlobalTracer;
 import org.apache.shardingsphere.agent.config.PluginConfiguration;
-import org.apache.shardingsphere.agent.exception.PluginConfigurationException;
 import org.apache.shardingsphere.agent.spi.boot.PluginBootService;
 
 import java.util.Optional;
@@ -35,9 +36,7 @@ public final class JaegerTracingPluginBootService implements 
PluginBootService {
     @SuppressWarnings("AccessOfSystemProperties")
     @Override
     public void start(final PluginConfiguration pluginConfig) {
-        if (!checkConfiguration(pluginConfig)) {
-            throw new PluginConfigurationException("jaeger config error, host 
is null or port is %s", pluginConfig.getPort());
-        }
+        pluginConfig.validate("Jaeger");
         pluginConfig.getProps().forEach((key, value) -> 
System.setProperty(String.valueOf(key), String.valueOf(value)));
         Configuration.SamplerConfiguration samplerConfig = 
Configuration.SamplerConfiguration.fromEnv();
         Configuration.ReporterConfiguration reporterConfig = 
Configuration.ReporterConfiguration.fromEnv()
@@ -49,10 +48,9 @@ public final class JaegerTracingPluginBootService implements 
PluginBootService {
         }
     }
     
-    private boolean checkConfiguration(final PluginConfiguration pluginConfig) 
{
-        String host = pluginConfig.getHost();
-        int port = pluginConfig.getPort();
-        return null != host && !"".equalsIgnoreCase(host) && port > 0;
+    private void checkConfiguration(final PluginConfiguration pluginConfig) {
+        
Preconditions.checkArgument(!Strings.isNullOrEmpty(pluginConfig.getHost()), 
"Jaeger hostname is required");
+        Preconditions.checkArgument(pluginConfig.getPort() > 0, "Jaeger port 
`%d` must be a positive number");
     }
     
     @Override
diff --git 
a/shardingsphere-agent/shardingsphere-agent-plugins/shardingsphere-agent-plugin-tracing/shardingsphere-agent-tracing-zipkin/src/main/java/org/apache/shardingsphere/agent/plugin/tracing/zipkin/service/ZipkinTracingPluginBootService.java
 
b/shardingsphere-agent/shardingsphere-agent-plugins/shardingsphere-agent-plugin-tracing/shardingsphere-agent-tracing-zipkin/src/main/java/org/apache/shardingsphere/agent/plugin/tracing/zipkin/service/ZipkinTracingPluginBootService.java
index 37362c96359..aa7738af02b 100644
--- 
a/shardingsphere-agent/shardingsphere-agent-plugins/shardingsphere-agent-plugin-tracing/shardingsphere-agent-tracing-zipkin/src/main/java/org/apache/shardingsphere/agent/plugin/tracing/zipkin/service/ZipkinTracingPluginBootService.java
+++ 
b/shardingsphere-agent/shardingsphere-agent-plugins/shardingsphere-agent-plugin-tracing/shardingsphere-agent-tracing-zipkin/src/main/java/org/apache/shardingsphere/agent/plugin/tracing/zipkin/service/ZipkinTracingPluginBootService.java
@@ -21,9 +21,7 @@ import brave.Tracing;
 import brave.sampler.BoundarySampler;
 import brave.sampler.RateLimitingSampler;
 import brave.sampler.Sampler;
-import com.google.common.base.Strings;
 import org.apache.shardingsphere.agent.config.PluginConfiguration;
-import org.apache.shardingsphere.agent.exception.PluginConfigurationException;
 import org.apache.shardingsphere.agent.spi.boot.PluginBootService;
 import zipkin2.reporter.brave.AsyncZipkinSpanHandler;
 import zipkin2.reporter.okhttp3.OkHttpSender;
@@ -44,9 +42,7 @@ public final class ZipkinTracingPluginBootService implements 
PluginBootService {
     
     @Override
     public void start(final PluginConfiguration pluginConfig) {
-        if (Strings.isNullOrEmpty(pluginConfig.getHost()) || 
pluginConfig.getPort() < 1) {
-            throw new PluginConfigurationException("zipkin config error, host 
is null or port is %s", pluginConfig.getPort());
-        }
+        pluginConfig.validate("Zipkin");
         Properties props = pluginConfig.getProps();
         String urlVersion = 
Optional.ofNullable(props.getProperty("URL_VERSION")).orElse("/api/v2/spans");
         String serviceName = 
Optional.ofNullable(props.getProperty("SERVICE_NAME")).orElse("shardingsphere-agent");

Reply via email to