This is an automated email from the ASF dual-hosted git repository.
duanzhengqiang 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 8360a5d4568 Remove ShardingSphereAgentException (#21271)
8360a5d4568 is described below
commit 8360a5d456830029b54feae56c3843789852a0f8
Author: Liang Zhang <[email protected]>
AuthorDate: Thu Sep 29 19:45:23 2022 +0800
Remove ShardingSphereAgentException (#21271)
---
.../transformer/ShardingSphereTransformer.java | 5 +-
.../config/loader/AgentConfigurationLoader.java | 6 +--
.../agent/core/config/path/AgentPathBuilder.java | 39 +++++++--------
.../swapper/YamlAgentConfigurationSwapper.java | 8 ++--
.../exception/ShardingSphereAgentException.java | 55 ----------------------
.../{ => loader}/AgentConfigurationLoaderTest.java | 8 +---
6 files changed, 29 insertions(+), 92 deletions(-)
diff --git
a/shardingsphere-agent/shardingsphere-agent-core/src/main/java/org/apache/shardingsphere/agent/core/bytebuddy/transformer/ShardingSphereTransformer.java
b/shardingsphere-agent/shardingsphere-agent-core/src/main/java/org/apache/shardingsphere/agent/core/bytebuddy/transformer/ShardingSphereTransformer.java
index 10949b39e23..622401671a7 100644
---
a/shardingsphere-agent/shardingsphere-agent-core/src/main/java/org/apache/shardingsphere/agent/core/bytebuddy/transformer/ShardingSphereTransformer.java
+++
b/shardingsphere-agent/shardingsphere-agent-core/src/main/java/org/apache/shardingsphere/agent/core/bytebuddy/transformer/ShardingSphereTransformer.java
@@ -195,7 +195,7 @@ public final class ShardingSphereTransformer implements
Transformer {
// CHECKSTYLE:OFF
} catch (final Throwable ex) {
// CHECKSTYLE:ON
- log.error("Failed to load advice class: {}",
description.getTypeName(), ex);
+ log.error("Failed to load advice class `{}`",
description.getTypeName(), ex);
}
}
return result;
@@ -230,7 +230,8 @@ public final class ShardingSphereTransformer implements
Transformer {
instanceMethodAroundAdvices.add(pluginLoader.getOrCreateInstance(each.getAdvice()));
}
}
- return isArgsOverride ? new
ShardingSphereTransformationPoint<>(methodDescription, new
ComposeInstanceMethodInterceptorArgsOverride(instanceMethodAroundAdvices))
+ return isArgsOverride
+ ? new ShardingSphereTransformationPoint<>(methodDescription,
new ComposeInstanceMethodInterceptorArgsOverride(instanceMethodAroundAdvices))
: new ShardingSphereTransformationPoint<>(methodDescription,
new ComposeInstanceMethodAroundInterceptor(instanceMethodAroundAdvices));
}
}
diff --git
a/shardingsphere-agent/shardingsphere-agent-core/src/main/java/org/apache/shardingsphere/agent/core/config/loader/AgentConfigurationLoader.java
b/shardingsphere-agent/shardingsphere-agent-core/src/main/java/org/apache/shardingsphere/agent/core/config/loader/AgentConfigurationLoader.java
index 0ce0ed296ae..50ad55a0605 100644
---
a/shardingsphere-agent/shardingsphere-agent-core/src/main/java/org/apache/shardingsphere/agent/core/config/loader/AgentConfigurationLoader.java
+++
b/shardingsphere-agent/shardingsphere-agent-core/src/main/java/org/apache/shardingsphere/agent/core/config/loader/AgentConfigurationLoader.java
@@ -38,9 +38,9 @@ public final class AgentConfigurationLoader {
private static final String DEFAULT_CONFIG_PATH = "/conf/agent.yaml";
/**
- * Load configuration of ShardingSphere-Agent.
+ * Load configuration of ShardingSphere agent.
*
- * @return configuration of ShardingSphere-Agent
+ * @return configuration of ShardingSphere agent
* @throws IOException IO exception
*/
public static AgentConfiguration load() throws IOException {
@@ -50,7 +50,7 @@ public final class AgentConfigurationLoader {
private static YamlAgentConfiguration load(final File yamlFile) throws
IOException {
YamlAgentConfiguration result = YamlEngine.unmarshal(yamlFile,
YamlAgentConfiguration.class);
- Preconditions.checkNotNull(result, "Agent configuration file `%s` is
invalid.", yamlFile.getName());
+ Preconditions.checkNotNull(result, "Agent configuration file `%s` is
invalid", yamlFile.getName());
return result;
}
}
diff --git
a/shardingsphere-agent/shardingsphere-agent-core/src/main/java/org/apache/shardingsphere/agent/core/config/path/AgentPathBuilder.java
b/shardingsphere-agent/shardingsphere-agent-core/src/main/java/org/apache/shardingsphere/agent/core/config/path/AgentPathBuilder.java
index 1a13142523c..f89989285a6 100644
---
a/shardingsphere-agent/shardingsphere-agent-core/src/main/java/org/apache/shardingsphere/agent/core/config/path/AgentPathBuilder.java
+++
b/shardingsphere-agent/shardingsphere-agent-core/src/main/java/org/apache/shardingsphere/agent/core/config/path/AgentPathBuilder.java
@@ -17,22 +17,21 @@
package org.apache.shardingsphere.agent.core.config.path;
+import com.google.common.base.Preconditions;
import lombok.AccessLevel;
import lombok.Getter;
import lombok.NoArgsConstructor;
-import lombok.extern.slf4j.Slf4j;
-import
org.apache.shardingsphere.agent.core.exception.ShardingSphereAgentException;
import java.io.File;
import java.net.MalformedURLException;
import java.net.URISyntaxException;
import java.net.URL;
+import java.util.Objects;
/**
* Agent path builder.
*/
@NoArgsConstructor(access = AccessLevel.PRIVATE)
-@Slf4j
public final class AgentPathBuilder {
@Getter
@@ -48,35 +47,31 @@ public final class AgentPathBuilder {
private static File buildAgentPath() {
String classResourcePath = String.join("",
AgentPathBuilder.class.getName().replaceAll("\\.", "/"), ".class");
- URL resource =
ClassLoader.getSystemClassLoader().getResource(classResourcePath);
- if (null != resource) {
- String url = resource.toString();
- log.debug("The beacon class location is {}", url);
- int existFileInJarIndex = url.indexOf('!');
- boolean isInJar = existFileInJarIndex > -1;
- return isInJar ? getFileInJar(url, existFileInJarIndex) :
getFileInResource(url, classResourcePath);
- }
- throw new ShardingSphereAgentException("Can not locate agent jar
file.");
- }
-
- private static File getFileInResource(final String url, final String
classResourcePath) {
- int prefixLength = "file:".length();
- String classLocation = url.substring(prefixLength, url.length() -
classResourcePath.length());
- return new File(classLocation);
+ URL resource =
Objects.requireNonNull(ClassLoader.getSystemClassLoader().getResource(classResourcePath),
"Can not locate agent jar file");
+ String url = resource.toString();
+ int existFileInJarIndex = url.indexOf('!');
+ boolean isInJar = existFileInJarIndex > -1;
+ return isInJar ? getFileInJar(url, existFileInJarIndex) :
getFileInResource(url, classResourcePath);
}
private static File getFileInJar(final String url, final int
fileInJarIndex) {
String realUrl = url.substring(url.indexOf("file:"), fileInJarIndex);
try {
File agentJarFile = new File(new URL(realUrl).toURI());
- return agentJarFile.exists() ? agentJarFile.getParentFile() : null;
+ Preconditions.checkState(agentJarFile.exists(), "Can not locate
agent jar file by URL `%s`", url);
+ return agentJarFile.getParentFile();
} catch (final MalformedURLException | URISyntaxException ex) {
- log.error(String.format("Can not locate agent jar file by url %s",
url), ex);
- return null;
+ throw new IllegalStateException(String.format("Can not locate
agent jar file by URL `%s`", url));
}
}
+ private static File getFileInResource(final String url, final String
classResourcePath) {
+ int prefixLength = "file:".length();
+ String classLocation = url.substring(prefixLength, url.length() -
classResourcePath.length());
+ return new File(classLocation);
+ }
+
private static File buildAgentPluginPath() {
- return new File(String.join("", agentPath.getPath(), "/plugins"));
+ return new File(String.join("/", agentPath.getPath(), "plugins"));
}
}
diff --git
a/shardingsphere-agent/shardingsphere-agent-core/src/main/java/org/apache/shardingsphere/agent/core/config/yaml/swapper/YamlAgentConfigurationSwapper.java
b/shardingsphere-agent/shardingsphere-agent-core/src/main/java/org/apache/shardingsphere/agent/core/config/yaml/swapper/YamlAgentConfigurationSwapper.java
index 47d18faaa20..5b55ec7a8d2 100644
---
a/shardingsphere-agent/shardingsphere-agent-core/src/main/java/org/apache/shardingsphere/agent/core/config/yaml/swapper/YamlAgentConfigurationSwapper.java
+++
b/shardingsphere-agent/shardingsphere-agent-core/src/main/java/org/apache/shardingsphere/agent/core/config/yaml/swapper/YamlAgentConfigurationSwapper.java
@@ -53,13 +53,13 @@ public final class YamlAgentConfigurationSwapper {
}
private static Map<String, PluginConfiguration>
transformPluginConfigurationMap(final Map<String, YamlPluginConfiguration>
yamlConfigurationMap) {
- Map<String, PluginConfiguration> configurationMap = new
LinkedHashMap<>();
- if (null != yamlConfigurationMap && yamlConfigurationMap.size() > 0) {
+ Map<String, PluginConfiguration> result = new LinkedHashMap<>();
+ if (null != yamlConfigurationMap) {
for (Entry<String, YamlPluginConfiguration> entry :
yamlConfigurationMap.entrySet()) {
- configurationMap.put(entry.getKey(),
transform(entry.getValue()));
+ result.put(entry.getKey(), transform(entry.getValue()));
}
}
- return configurationMap;
+ return result;
}
private static PluginConfiguration transform(final YamlPluginConfiguration
yamlConfig) {
diff --git
a/shardingsphere-agent/shardingsphere-agent-core/src/main/java/org/apache/shardingsphere/agent/core/exception/ShardingSphereAgentException.java
b/shardingsphere-agent/shardingsphere-agent-core/src/main/java/org/apache/shardingsphere/agent/core/exception/ShardingSphereAgentException.java
deleted file mode 100644
index 592be0df4ac..00000000000
---
a/shardingsphere-agent/shardingsphere-agent-core/src/main/java/org/apache/shardingsphere/agent/core/exception/ShardingSphereAgentException.java
+++ /dev/null
@@ -1,55 +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.core.exception;
-
-/**
- * Basic exception of ShardingSphere agent.
- */
-public final class ShardingSphereAgentException extends RuntimeException {
-
- private static final long serialVersionUID = -1343739516839252250L;
-
- /**
- * Constructs an exception with formatted error message and arguments.
- *
- * @param errorMessage formatted error message
- * @param args arguments of error message
- */
- public ShardingSphereAgentException(final String errorMessage, final
Object... args) {
- super(String.format(errorMessage, args));
- }
-
- /**
- * Constructs an exception with error message and cause.
- *
- * @param message error message
- * @param cause error cause
- */
- public ShardingSphereAgentException(final String message, final Exception
cause) {
- super(message, cause);
- }
-
- /**
- * Constructs an exception with cause.
- *
- * @param cause error cause
- */
- public ShardingSphereAgentException(final Exception cause) {
- super(cause);
- }
-}
diff --git
a/shardingsphere-agent/shardingsphere-agent-core/src/test/java/org/apache/shardingsphere/agent/core/config/AgentConfigurationLoaderTest.java
b/shardingsphere-agent/shardingsphere-agent-core/src/test/java/org/apache/shardingsphere/agent/core/config/loader/AgentConfigurationLoaderTest.java
similarity index 85%
rename from
shardingsphere-agent/shardingsphere-agent-core/src/test/java/org/apache/shardingsphere/agent/core/config/AgentConfigurationLoaderTest.java
rename to
shardingsphere-agent/shardingsphere-agent-core/src/test/java/org/apache/shardingsphere/agent/core/config/loader/AgentConfigurationLoaderTest.java
index 7988e8d0879..458cc7ea946 100644
---
a/shardingsphere-agent/shardingsphere-agent-core/src/test/java/org/apache/shardingsphere/agent/core/config/AgentConfigurationLoaderTest.java
+++
b/shardingsphere-agent/shardingsphere-agent-core/src/test/java/org/apache/shardingsphere/agent/core/config/loader/AgentConfigurationLoaderTest.java
@@ -15,9 +15,8 @@
* limitations under the License.
*/
-package org.apache.shardingsphere.agent.core.config;
+package org.apache.shardingsphere.agent.core.config.loader;
-import
org.apache.shardingsphere.agent.core.config.loader.AgentConfigurationLoader;
import org.apache.shardingsphere.agent.core.config.path.AgentPathBuilder;
import org.apache.shardingsphere.agent.core.util.ReflectiveUtil;
import org.junit.Test;
@@ -41,9 +40,6 @@ public final class AgentConfigurationLoaderTest {
private String getResourceUrl() {
URL url =
AgentConfigurationLoader.class.getClassLoader().getResource("");
- if (null != url) {
- return URLDecoder.decode(url.getFile());
- }
- return DEFAULT_CONFIG_PATH;
+ return null == url ? DEFAULT_CONFIG_PATH :
URLDecoder.decode(url.getFile());
}
}