You are viewing a plain text version of this content. The canonical link for it is here.
Posted to notifications@shardingsphere.apache.org by ji...@apache.org on 2023/03/16 08:59:57 UTC
[shardingsphere] branch master updated: merge files file-proxy-advisors.yaml and file-jdbc-advisors.yaml into file-advisors.yaml (#24597)
This is an automated email from the ASF dual-hosted git repository.
jianglongtao 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 f84fd2abf45 merge files file-proxy-advisors.yaml and file-jdbc-advisors.yaml into file-advisors.yaml (#24597)
f84fd2abf45 is described below
commit f84fd2abf454791b4e3d4a5fb79c5e40f421df08
Author: jiangML <10...@qq.com>
AuthorDate: Thu Mar 16 16:59:37 2023 +0800
merge files file-proxy-advisors.yaml and file-jdbc-advisors.yaml into file-advisors.yaml (#24597)
* rename file-proxy-advisors.yaml to file-advisors.yaml
* optimize yaml code
* optimzie code about yaml
* fix checkstyle error
* add junit-jupiter-engine for agent e2e test
---
.../loader/YamlAdvisorsConfigurationLoader.java | 4 +--
.../yaml/loader/YamlPluginConfigurationLoader.java | 10 +++---
.../agent/core/yaml/AgentYamlConstructor.java | 40 ++++++++++++++++++++++
.../AgentYamlEngine.java} | 31 ++++++++++++-----
.../YamlAdvisorsConfigurationSwapperTest.java | 5 ++-
.../YamlPluginsConfigurationSwapperTest.java | 5 ++-
...{file-jdbc-advisors.yaml => file-advisors.yaml} | 0
.../META-INF/conf/file-proxy-advisors.yaml | 26 --------------
test/e2e/agent/pom.xml | 4 +++
9 files changed, 77 insertions(+), 48 deletions(-)
diff --git a/agent/core/src/main/java/org/apache/shardingsphere/agent/core/advisor/config/yaml/loader/YamlAdvisorsConfigurationLoader.java b/agent/core/src/main/java/org/apache/shardingsphere/agent/core/advisor/config/yaml/loader/YamlAdvisorsConfigurationLoader.java
index cd3a7f3a630..8979b531f9a 100644
--- a/agent/core/src/main/java/org/apache/shardingsphere/agent/core/advisor/config/yaml/loader/YamlAdvisorsConfigurationLoader.java
+++ b/agent/core/src/main/java/org/apache/shardingsphere/agent/core/advisor/config/yaml/loader/YamlAdvisorsConfigurationLoader.java
@@ -20,7 +20,7 @@ package org.apache.shardingsphere.agent.core.advisor.config.yaml.loader;
import lombok.AccessLevel;
import lombok.NoArgsConstructor;
import org.apache.shardingsphere.agent.core.advisor.config.yaml.entity.YamlAdvisorsConfiguration;
-import org.yaml.snakeyaml.Yaml;
+import org.apache.shardingsphere.agent.core.yaml.AgentYamlEngine;
import java.io.InputStream;
@@ -37,7 +37,7 @@ public final class YamlAdvisorsConfigurationLoader {
* @return loaded advisors configuration
*/
public static YamlAdvisorsConfiguration load(final InputStream inputStream) {
- YamlAdvisorsConfiguration result = new Yaml().loadAs(inputStream, YamlAdvisorsConfiguration.class);
+ YamlAdvisorsConfiguration result = AgentYamlEngine.unmarshalYamlAdvisorsConfiguration(inputStream);
return null == result ? new YamlAdvisorsConfiguration() : result;
}
}
diff --git a/agent/core/src/main/java/org/apache/shardingsphere/agent/core/plugin/config/yaml/loader/YamlPluginConfigurationLoader.java b/agent/core/src/main/java/org/apache/shardingsphere/agent/core/plugin/config/yaml/loader/YamlPluginConfigurationLoader.java
index a5f1e0062db..1805c3fd7f7 100644
--- a/agent/core/src/main/java/org/apache/shardingsphere/agent/core/plugin/config/yaml/loader/YamlPluginConfigurationLoader.java
+++ b/agent/core/src/main/java/org/apache/shardingsphere/agent/core/plugin/config/yaml/loader/YamlPluginConfigurationLoader.java
@@ -20,12 +20,13 @@ package org.apache.shardingsphere.agent.core.plugin.config.yaml.loader;
import lombok.AccessLevel;
import lombok.NoArgsConstructor;
import org.apache.shardingsphere.agent.core.plugin.config.yaml.entity.YamlAgentConfiguration;
+import org.apache.shardingsphere.agent.core.yaml.AgentYamlConstructor;
+import org.apache.shardingsphere.agent.core.yaml.AgentYamlEngine;
import org.yaml.snakeyaml.Yaml;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
-import java.io.InputStreamReader;
import java.util.Optional;
/**
@@ -42,10 +43,9 @@ public final class YamlPluginConfigurationLoader {
* @throws IOException IO exception
*/
public static Optional<YamlAgentConfiguration> load(final File yamlFile) throws IOException {
- try (
- FileInputStream fileInputStream = new FileInputStream(yamlFile);
- InputStreamReader inputStreamReader = new InputStreamReader(fileInputStream)) {
- YamlAgentConfiguration result = new Yaml().loadAs(inputStreamReader, YamlAgentConfiguration.class);
+ Yaml yaml = new Yaml(new AgentYamlConstructor(YamlAgentConfiguration.class));
+ try (FileInputStream fileInputStream = new FileInputStream(yamlFile)) {
+ YamlAgentConfiguration result = AgentYamlEngine.unmarshalYamlAgentConfiguration(fileInputStream);
return null == result ? Optional.empty() : Optional.of(result);
}
}
diff --git a/agent/core/src/main/java/org/apache/shardingsphere/agent/core/yaml/AgentYamlConstructor.java b/agent/core/src/main/java/org/apache/shardingsphere/agent/core/yaml/AgentYamlConstructor.java
new file mode 100644
index 00000000000..9338b3b6954
--- /dev/null
+++ b/agent/core/src/main/java/org/apache/shardingsphere/agent/core/yaml/AgentYamlConstructor.java
@@ -0,0 +1,40 @@
+/*
+ * 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.yaml;
+
+import com.google.common.base.Preconditions;
+import org.yaml.snakeyaml.constructor.Constructor;
+
+/**
+ * Agent YAML constructor.
+ */
+public final class AgentYamlConstructor extends Constructor {
+
+ private final Class<?> rootClass;
+
+ public AgentYamlConstructor(final Class<?> rootClass) {
+ super(rootClass);
+ this.rootClass = rootClass;
+ }
+
+ @Override
+ protected Class<?> getClassForName(final String className) throws ClassNotFoundException {
+ Preconditions.checkArgument(className.equals(rootClass.getName()), "Class `%s` is not accepted", className);
+ return super.getClassForName(className);
+ }
+}
diff --git a/agent/core/src/main/java/org/apache/shardingsphere/agent/core/advisor/config/yaml/loader/YamlAdvisorsConfigurationLoader.java b/agent/core/src/main/java/org/apache/shardingsphere/agent/core/yaml/AgentYamlEngine.java
similarity index 52%
copy from agent/core/src/main/java/org/apache/shardingsphere/agent/core/advisor/config/yaml/loader/YamlAdvisorsConfigurationLoader.java
copy to agent/core/src/main/java/org/apache/shardingsphere/agent/core/yaml/AgentYamlEngine.java
index cd3a7f3a630..c0002ea58b2 100644
--- a/agent/core/src/main/java/org/apache/shardingsphere/agent/core/advisor/config/yaml/loader/YamlAdvisorsConfigurationLoader.java
+++ b/agent/core/src/main/java/org/apache/shardingsphere/agent/core/yaml/AgentYamlEngine.java
@@ -15,29 +15,42 @@
* limitations under the License.
*/
-package org.apache.shardingsphere.agent.core.advisor.config.yaml.loader;
+package org.apache.shardingsphere.agent.core.yaml;
import lombok.AccessLevel;
import lombok.NoArgsConstructor;
import org.apache.shardingsphere.agent.core.advisor.config.yaml.entity.YamlAdvisorsConfiguration;
+import org.apache.shardingsphere.agent.core.plugin.config.yaml.entity.YamlAgentConfiguration;
import org.yaml.snakeyaml.Yaml;
+import java.io.IOException;
import java.io.InputStream;
/**
- * YAML advisors configuration loader.
+ * Agent YAML engine.
*/
@NoArgsConstructor(access = AccessLevel.PRIVATE)
-public final class YamlAdvisorsConfigurationLoader {
+public final class AgentYamlEngine {
/**
- * Load advisors configuration.
- *
+ * Unmarshal YAML agent configuration YAML.
+ *
* @param inputStream input stream
- * @return loaded advisors configuration
+ * @return YAML agent configuration
+ * @throws IOException IO Exception
*/
- public static YamlAdvisorsConfiguration load(final InputStream inputStream) {
- YamlAdvisorsConfiguration result = new Yaml().loadAs(inputStream, YamlAdvisorsConfiguration.class);
- return null == result ? new YamlAdvisorsConfiguration() : result;
+ public static YamlAgentConfiguration unmarshalYamlAgentConfiguration(final InputStream inputStream) {
+ return new Yaml(new AgentYamlConstructor(YamlAgentConfiguration.class)).loadAs(inputStream, YamlAgentConfiguration.class);
+ }
+
+ /**
+ * Unmarshal YAML advisors configuration YAML.
+ *
+ * @param inputStream input stream
+ * @return YAML advisors configuration
+ * @throws IOException IO Exception
+ */
+ public static YamlAdvisorsConfiguration unmarshalYamlAdvisorsConfiguration(final InputStream inputStream) {
+ return new Yaml(new AgentYamlConstructor(YamlAdvisorsConfiguration.class)).loadAs(inputStream, YamlAdvisorsConfiguration.class);
}
}
diff --git a/agent/core/src/test/java/org/apache/shardingsphere/agent/core/advisor/config/yaml/swapper/YamlAdvisorsConfigurationSwapperTest.java b/agent/core/src/test/java/org/apache/shardingsphere/agent/core/advisor/config/yaml/swapper/YamlAdvisorsConfigurationSwapperTest.java
index d0fa6ea2160..352fa3dcb5b 100644
--- a/agent/core/src/test/java/org/apache/shardingsphere/agent/core/advisor/config/yaml/swapper/YamlAdvisorsConfigurationSwapperTest.java
+++ b/agent/core/src/test/java/org/apache/shardingsphere/agent/core/advisor/config/yaml/swapper/YamlAdvisorsConfigurationSwapperTest.java
@@ -20,11 +20,10 @@ package org.apache.shardingsphere.agent.core.advisor.config.yaml.swapper;
import net.bytebuddy.matcher.ElementMatchers;
import org.apache.shardingsphere.agent.core.advisor.config.AdvisorConfiguration;
import org.apache.shardingsphere.agent.core.advisor.config.MethodAdvisorConfiguration;
-import org.apache.shardingsphere.agent.core.advisor.config.yaml.entity.YamlAdvisorsConfiguration;
import org.apache.shardingsphere.agent.core.advisor.config.yaml.fixture.YamlAdviceFixture;
import org.apache.shardingsphere.agent.core.advisor.config.yaml.fixture.YamlTargetObjectFixture;
+import org.apache.shardingsphere.agent.core.yaml.AgentYamlEngine;
import org.junit.jupiter.api.Test;
-import org.yaml.snakeyaml.Yaml;
import java.util.ArrayList;
import java.util.Collection;
@@ -38,7 +37,7 @@ public final class YamlAdvisorsConfigurationSwapperTest {
@Test
public void assertSwapToObject() {
Collection<AdvisorConfiguration> actual = YamlAdvisorsConfigurationSwapper.swap(
- new Yaml().loadAs(getClass().getResourceAsStream("/META-INF/conf/advisors.yaml"), YamlAdvisorsConfiguration.class), "FIXTURE");
+ AgentYamlEngine.unmarshalYamlAdvisorsConfiguration(getClass().getResourceAsStream("/META-INF/conf/advisors.yaml")), "FIXTURE");
assertThat(actual.size(), is(1));
assertAdvisorConfiguration(actual.iterator().next());
}
diff --git a/agent/core/src/test/java/org/apache/shardingsphere/agent/core/plugin/config/yaml/swapper/YamlPluginsConfigurationSwapperTest.java b/agent/core/src/test/java/org/apache/shardingsphere/agent/core/plugin/config/yaml/swapper/YamlPluginsConfigurationSwapperTest.java
index ebd1a5c81a5..5fe941b6b3d 100644
--- a/agent/core/src/test/java/org/apache/shardingsphere/agent/core/plugin/config/yaml/swapper/YamlPluginsConfigurationSwapperTest.java
+++ b/agent/core/src/test/java/org/apache/shardingsphere/agent/core/plugin/config/yaml/swapper/YamlPluginsConfigurationSwapperTest.java
@@ -21,13 +21,12 @@ import org.apache.shardingsphere.agent.api.PluginConfiguration;
import org.apache.shardingsphere.agent.core.plugin.config.yaml.entity.YamlAgentConfiguration;
import org.apache.shardingsphere.agent.core.plugin.config.yaml.entity.YamlPluginCategoryConfiguration;
import org.apache.shardingsphere.agent.core.plugin.config.yaml.entity.YamlPluginConfiguration;
+import org.apache.shardingsphere.agent.core.yaml.AgentYamlEngine;
import org.junit.jupiter.api.Test;
-import org.yaml.snakeyaml.Yaml;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
-import java.io.InputStreamReader;
import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;
import java.util.Collections;
@@ -82,7 +81,7 @@ public final class YamlPluginsConfigurationSwapperTest {
@Test
public void assertSwapWithFile() throws IOException {
- YamlAgentConfiguration yamlAgentConfig = new Yaml().loadAs(new InputStreamReader(new FileInputStream(new File(getResourceURL(), CONFIG_PATH))), YamlAgentConfiguration.class);
+ YamlAgentConfiguration yamlAgentConfig = AgentYamlEngine.unmarshalYamlAgentConfiguration(new FileInputStream(new File(getResourceURL(), CONFIG_PATH)));
Map<String, PluginConfiguration> actual = YamlPluginsConfigurationSwapper.swap(yamlAgentConfig);
assertThat(actual.size(), is(3));
assertLogFixturePluginConfiguration(actual.get("log_fixture"));
diff --git a/agent/plugins/logging/type/file/src/main/resources/META-INF/conf/file-jdbc-advisors.yaml b/agent/plugins/logging/type/file/src/main/resources/META-INF/conf/file-advisors.yaml
similarity index 100%
rename from agent/plugins/logging/type/file/src/main/resources/META-INF/conf/file-jdbc-advisors.yaml
rename to agent/plugins/logging/type/file/src/main/resources/META-INF/conf/file-advisors.yaml
diff --git a/agent/plugins/logging/type/file/src/main/resources/META-INF/conf/file-proxy-advisors.yaml b/agent/plugins/logging/type/file/src/main/resources/META-INF/conf/file-proxy-advisors.yaml
deleted file mode 100644
index 74b2d2496bf..00000000000
--- a/agent/plugins/logging/type/file/src/main/resources/META-INF/conf/file-proxy-advisors.yaml
+++ /dev/null
@@ -1,26 +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.
-#
-
-advisors:
- - target: org.apache.shardingsphere.mode.metadata.MetaDataContextsFactory
- advice: org.apache.shardingsphere.agent.plugin.logging.file.advice.MetaDataContextsFactoryAdvice
- pointcuts:
- - name: create
- type: method
- params:
- - index: 3
- type: java.util.Map
diff --git a/test/e2e/agent/pom.xml b/test/e2e/agent/pom.xml
index 2bee1753f4e..4d8527e46e6 100644
--- a/test/e2e/agent/pom.xml
+++ b/test/e2e/agent/pom.xml
@@ -42,6 +42,10 @@
<groupId>org.awaitility</groupId>
<artifactId>awaitility</artifactId>
</dependency>
+ <dependency>
+ <groupId>org.junit.jupiter</groupId>
+ <artifactId>junit-jupiter-engine</artifactId>
+ </dependency>
</dependencies>
<build>