You are viewing a plain text version of this content. The canonical link for it is here.
Posted to notifications@shardingsphere.apache.org by du...@apache.org on 2022/04/04 06:58:53 UTC
[shardingsphere] branch master updated: Refactor jdbc and proxy generator (#16584)
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 4a0460111e7 Refactor jdbc and proxy generator (#16584)
4a0460111e7 is described below
commit 4a0460111e7f94802c46f462b1238252887d934c
Author: Guocheng Tang <to...@apache.org>
AuthorDate: Mon Apr 4 14:58:29 2022 +0800
Refactor jdbc and proxy generator (#16584)
---
.../example/generator/core/ExampleGenerator.java | 33 +++++++++++++++++---
.../generator/core/impl/JDBCExampleGenerator.java | 26 +---------------
.../generator/core/impl/ProxyExampleGenerator.java | 35 +++++-----------------
3 files changed, 37 insertions(+), 57 deletions(-)
diff --git a/examples/shardingsphere-example-generator/src/main/java/org/apache/shardingsphere/example/generator/core/ExampleGenerator.java b/examples/shardingsphere-example-generator/src/main/java/org/apache/shardingsphere/example/generator/core/ExampleGenerator.java
index 0a0b8db57ff..020d8caa3f8 100644
--- a/examples/shardingsphere-example-generator/src/main/java/org/apache/shardingsphere/example/generator/core/ExampleGenerator.java
+++ b/examples/shardingsphere-example-generator/src/main/java/org/apache/shardingsphere/example/generator/core/ExampleGenerator.java
@@ -20,8 +20,11 @@ package org.apache.shardingsphere.example.generator.core;
import freemarker.template.Configuration;
import freemarker.template.TemplateException;
import org.apache.shardingsphere.example.generator.core.yaml.config.YamlExampleConfiguration;
+import org.apache.shardingsphere.infra.autogen.version.ShardingSphereVersion;
import java.io.IOException;
+import java.util.LinkedHashMap;
+import java.util.Map;
/**
* Example generator.
@@ -32,15 +35,37 @@ public interface ExampleGenerator {
String RESOURCES_PATH = "src/main/resources";
+ default void generate(final Configuration templateConfig, final YamlExampleConfiguration configuration) throws IOException, TemplateException {
+ for (String eachFramework : configuration.getFrameworks()) {
+ for (String eachFeature : GenerateUtil.generateCombination(configuration.getFeatures())) {
+ generate(templateConfig, buildDataModel(configuration, eachFramework, eachFeature), eachFramework, eachFeature);
+ }
+ }
+ }
+
+ default Map<String, String> buildDataModel(final YamlExampleConfiguration configuration, final String framework, final String feature) {
+ Map<String, String> result = new LinkedHashMap<>();
+ configuration.getProps().forEach((key, value) -> result.put(key.toString(), value.toString()));
+ result.put("product", getType());
+ // TODO support mode & transaction combination
+ result.put("mode", configuration.getModes().size() > 0 ? configuration.getModes().get(0) : "");
+ result.put("transaction", configuration.getTransactions().size() > 0 ? configuration.getTransactions().get(0) : "");
+ result.put("feature", feature);
+ result.put("framework", framework);
+ result.put("shardingsphereVersion", ShardingSphereVersion.VERSION);
+ return result;
+ }
+
/**
- * Generate file.
- *
+ * Generate.
* @param templateConfig template configuration
- * @param configuration example configuration
+ * @param dataModel data model
+ * @param framework framework
+ * @param feature feature
* @throws IOException IO exception
* @throws TemplateException template exception
*/
- void generate(Configuration templateConfig, YamlExampleConfiguration configuration) throws IOException, TemplateException;
+ void generate(final Configuration templateConfig, final Map<String, String> dataModel, final String framework, final String feature) throws IOException, TemplateException;
/**
* Get generator type.
diff --git a/examples/shardingsphere-example-generator/src/main/java/org/apache/shardingsphere/example/generator/core/impl/JDBCExampleGenerator.java b/examples/shardingsphere-example-generator/src/main/java/org/apache/shardingsphere/example/generator/core/impl/JDBCExampleGenerator.java
index 79d1b43e7fe..81358b4f76a 100644
--- a/examples/shardingsphere-example-generator/src/main/java/org/apache/shardingsphere/example/generator/core/impl/JDBCExampleGenerator.java
+++ b/examples/shardingsphere-example-generator/src/main/java/org/apache/shardingsphere/example/generator/core/impl/JDBCExampleGenerator.java
@@ -21,12 +21,9 @@ import freemarker.template.Configuration;
import freemarker.template.TemplateException;
import org.apache.shardingsphere.example.generator.core.ExampleGenerator;
import org.apache.shardingsphere.example.generator.core.GenerateUtil;
-import org.apache.shardingsphere.example.generator.core.yaml.config.YamlExampleConfiguration;
import org.apache.shardingsphere.example.generator.scenario.ExampleScenarioFactory;
-import org.apache.shardingsphere.infra.autogen.version.ShardingSphereVersion;
import java.io.IOException;
-import java.util.LinkedHashMap;
import java.util.Map;
/**
@@ -39,28 +36,7 @@ public final class JDBCExampleGenerator implements ExampleGenerator {
+ "${package}/${framework?replace('-', '/')}";
@Override
- public void generate(final Configuration templateConfig, final YamlExampleConfiguration configuration) throws IOException, TemplateException {
- for (String eachFramework : configuration.getFrameworks()) {
- for (String eachFeature : GenerateUtil.generateCombination(configuration.getFeatures())) {
- generate(templateConfig, buildDataModel(configuration, eachFramework, eachFeature), eachFramework, eachFeature);
- }
- }
- }
-
- private Map<String, String> buildDataModel(final YamlExampleConfiguration configuration, final String framework, final String feature) {
- Map<String, String> result = new LinkedHashMap<>();
- configuration.getProps().forEach((key, value) -> result.put(key.toString(), value.toString()));
- result.put("product", getType());
- // TODO support mode & transaction combination
- result.put("mode", configuration.getModes().size() > 0 ? configuration.getModes().get(0) : "");
- result.put("transaction", configuration.getTransactions().size() > 0 ? configuration.getTransactions().get(0) : "");
- result.put("feature", feature);
- result.put("framework", framework);
- result.put("shardingsphereVersion", ShardingSphereVersion.VERSION);
- return result;
- }
-
- private void generate(final Configuration templateConfig, final Map<String, String> dataModel, final String framework, final String feature) throws IOException, TemplateException {
+ public void generate(final Configuration templateConfig, final Map<String, String> dataModel, final String framework, final String feature) throws IOException, TemplateException {
GenerateUtil.generateDirs(templateConfig, dataModel, new ExampleScenarioFactory(feature, framework).getJavaClassPaths(), OUTPUT_PATH + JAVA_CLASS_PATH);
GenerateUtil.generateDirs(templateConfig, dataModel, new ExampleScenarioFactory(feature, framework).getResourcePaths(), OUTPUT_PATH + RESOURCES_PATH);
GenerateUtil.generateFile(templateConfig, getType(), dataModel, new ExampleScenarioFactory(feature, framework).getJavaClassTemplateMap(), OUTPUT_PATH + JAVA_CLASS_PATH);
diff --git a/examples/shardingsphere-example-generator/src/main/java/org/apache/shardingsphere/example/generator/core/impl/ProxyExampleGenerator.java b/examples/shardingsphere-example-generator/src/main/java/org/apache/shardingsphere/example/generator/core/impl/ProxyExampleGenerator.java
index 690cd11c698..89c93a6baf6 100644
--- a/examples/shardingsphere-example-generator/src/main/java/org/apache/shardingsphere/example/generator/core/impl/ProxyExampleGenerator.java
+++ b/examples/shardingsphere-example-generator/src/main/java/org/apache/shardingsphere/example/generator/core/impl/ProxyExampleGenerator.java
@@ -21,12 +21,9 @@ import freemarker.template.Configuration;
import freemarker.template.TemplateException;
import org.apache.shardingsphere.example.generator.core.ExampleGenerator;
import org.apache.shardingsphere.example.generator.core.GenerateUtil;
-import org.apache.shardingsphere.example.generator.core.yaml.config.YamlExampleConfiguration;
-import org.apache.shardingsphere.infra.autogen.version.ShardingSphereVersion;
import java.io.IOException;
import java.util.Collections;
-import java.util.LinkedHashMap;
import java.util.Map;
/**
@@ -34,39 +31,21 @@ import java.util.Map;
*/
public final class ProxyExampleGenerator implements ExampleGenerator {
- @Override
- public void generate(final Configuration templateConfig, final YamlExampleConfiguration configuration) throws IOException, TemplateException {
- for (String eachFramework : configuration.getFrameworks()) {
- for (String eachFeature : GenerateUtil.generateCombination(configuration.getFeatures())) {
- // TODO refactor proxy process
- Map<String, String> dataModel = new LinkedHashMap<>();
- dataModel.put("mode", configuration.getModes().size() > 0 ? configuration.getModes().get(0) : "");
- dataModel.put("transaction", configuration.getTransactions().size() > 0 ? configuration.getTransactions().get(0) : "");
- dataModel.put("shardingsphereVersion", ShardingSphereVersion.VERSION);
- generate(templateConfig, dataModel, eachFramework, eachFeature);
- }
- }
- }
-
- private void generate(final Configuration templateConfig, final Map<String, String> dataModel, final String framework, final String feature) throws IOException, TemplateException {
- dataModel.put("feature", feature);
- dataModel.put("framework", framework);
- dataModel.put("product", getType());
+ public void generate(final Configuration templateConfig, final Map<String, String> dataModel, final String framework, final String feature) throws IOException, TemplateException {
GenerateUtil.generateDirs(templateConfig, dataModel, Collections.singleton("conf"), OUTPUT_PATH + RESOURCES_PATH);
String outputPath = GenerateUtil.generatePath(templateConfig, dataModel, OUTPUT_PATH);
- processFile(feature, templateConfig, dataModel, outputPath);
+ processFile(templateConfig, dataModel, outputPath);
+ }
+
+ public String getType() {
+ return "proxy";
}
- private void processFile(final String feature, final Configuration templateConfig, final Map<String, String> dataModel,
+ private void processFile(final Configuration templateConfig, final Map<String, String> dataModel,
final String baseOutputPath) throws TemplateException, IOException {
String outputPath = baseOutputPath + RESOURCES_PATH + "/conf/";
GenerateUtil.processFile(templateConfig, dataModel, getType() + "/config-example_db.ftl", outputPath + "config-example_db.yaml");
GenerateUtil.processFile(templateConfig, dataModel, getType() + "/server.ftl", outputPath + "server.yaml");
GenerateUtil.processFile(templateConfig, dataModel, getType() + "/pom.ftl", baseOutputPath + "pom.xml");
}
-
- @Override
- public String getType() {
- return "proxy";
- }
}