You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@servicecomb.apache.org by ni...@apache.org on 2019/06/21 07:09:28 UTC
[servicecomb-toolkit] 12/49: add new programming model POJO
This is an automated email from the ASF dual-hosted git repository.
ningjiang pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/servicecomb-toolkit.git
commit 0e69c4d1e074049642ddbf189ceab225e9eff3d2
Author: kakulisen <18...@163.com>
AuthorDate: Thu May 23 22:15:14 2019 +0800
add new programming model POJO
Signed-off-by: kakulisen <18...@163.com>
---
.../toolkit/codegen/RemoveImplSuffixLambda.java | 39 +++++++
.../toolkit/codegen/ServiceCombCodegen.java | 27 +++++
.../libraries/POJO/Application.mustache | 11 ++
.../ServiceComb/libraries/POJO/api.mustache | 38 ++++++
.../libraries/POJO/apiInterface.mustache | 23 ++++
.../ServiceComb/libraries/POJO/bodyParams.mustache | 1 +
.../ServiceComb/libraries/POJO/formParams.mustache | 2 +
.../libraries/POJO/headerParams.mustache | 1 +
.../ServiceComb/libraries/POJO/pathParams.mustache | 1 +
.../ServiceComb/libraries/POJO/pom.mustache | 127 +++++++++++++++++++++
.../libraries/POJO/queryParams.mustache | 1 +
.../servicecomb/toolkit/codegen/GeneratorTest.java | 1 +
.../apache/servicecomb/toolkit/cli/CliTest.java | 2 +-
13 files changed, 273 insertions(+), 1 deletion(-)
diff --git a/code-generator/src/main/java/org/apache/servicecomb/toolkit/codegen/RemoveImplSuffixLambda.java b/code-generator/src/main/java/org/apache/servicecomb/toolkit/codegen/RemoveImplSuffixLambda.java
new file mode 100644
index 0000000..519a584
--- /dev/null
+++ b/code-generator/src/main/java/org/apache/servicecomb/toolkit/codegen/RemoveImplSuffixLambda.java
@@ -0,0 +1,39 @@
+/*
+ * 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.servicecomb.toolkit.codegen;
+
+import com.samskivert.mustache.Mustache;
+import com.samskivert.mustache.Template;
+
+import java.io.IOException;
+import java.io.Writer;
+
+public class RemoveImplSuffixLambda implements Mustache.Lambda {
+
+ @Override
+ public void execute(Template.Fragment fragment, Writer writer) throws IOException {
+
+ String text = fragment.execute();
+ if(text.endsWith("Impl")){
+ text = text.substring(0,text.lastIndexOf("Impl"));
+ }
+ writer.write(text);
+
+ }
+
+}
diff --git a/code-generator/src/main/java/org/apache/servicecomb/toolkit/codegen/ServiceCombCodegen.java b/code-generator/src/main/java/org/apache/servicecomb/toolkit/codegen/ServiceCombCodegen.java
index c0ded7c..36efe5b 100755
--- a/code-generator/src/main/java/org/apache/servicecomb/toolkit/codegen/ServiceCombCodegen.java
+++ b/code-generator/src/main/java/org/apache/servicecomb/toolkit/codegen/ServiceCombCodegen.java
@@ -35,6 +35,7 @@ public class ServiceCombCodegen extends AbstractJavaCodegen implements CodegenCo
private static final String DEFAULT_LIBRARY = "SpringMVC";
+ private static final String POJO_LIBRARY = "POJO";
private String mainClassPackage;
@@ -58,6 +59,7 @@ public class ServiceCombCodegen extends AbstractJavaCodegen implements CodegenCo
private String modelConsumerTemplate = consumerTemplateFolder + "/model.mustache";
+ private String pojoApiInterfaceTemplate = "apiInterface.mustache";
private int modelSwitch = 1;
@@ -98,6 +100,7 @@ public class ServiceCombCodegen extends AbstractJavaCodegen implements CodegenCo
mainClassPackage = groupId + ".example";
supportedLibraries.put(DEFAULT_LIBRARY, "ServiceComb Server application using the springboot programming model.");
+ supportedLibraries.put(POJO_LIBRARY, "ServiceComb Server application using the pojo programming model.");
setLibrary(DEFAULT_LIBRARY);
@@ -124,9 +127,19 @@ public class ServiceCombCodegen extends AbstractJavaCodegen implements CodegenCo
String suffix = apiTemplateFiles().get(templateName);
return apiConsumerFolder() + File.separator + toApiFilename(tag) + suffix;
}
+ if (pojoApiInterfaceTemplate.equals(templateName)) {
+ String suffix = apiTemplateFiles().get(templateName);
+ String pojoApiInterfaceName = pojoApiInterfaceFolder() + File.separator + camelize(tag) + "Api" + suffix;
+ additionalProperties.put("pojoApiInterfaceName",camelize(tag) + "Api");
+ return pojoApiInterfaceName;
+ }
return super.apiFilename(templateName, tag);
}
+ private String pojoApiInterfaceFolder() {
+ return outputFolder + "/" + modelProject + "/" + sourceFolder + "/" + apiPackage().replace('.', '/');
+ }
+
private String apiConsumerFolder() {
return outputFolder + "/" + consumerProject + "/" + sourceFolder + "/" + apiPackage().replace('.', '/');
}
@@ -141,6 +154,7 @@ public class ServiceCombCodegen extends AbstractJavaCodegen implements CodegenCo
additionalProperties.put("mainClassPackage", mainClassPackage);
additionalProperties.put("camelcase", new CamelCaseLambda());
additionalProperties.put("getGenericClassType", new GetGenericClassTypeLambda());
+ additionalProperties.put("removeImplSuffix", new RemoveImplSuffixLambda());
additionalProperties.put("applicationId", applicationId);
additionalProperties.put("microserviceName", microserviceName);
@@ -148,6 +162,15 @@ public class ServiceCombCodegen extends AbstractJavaCodegen implements CodegenCo
processProviderProjectOpts();
processConsumerOpts();
processModelProjectOpts();
+ processPojo();
+ }
+
+ private void processPojo() {
+ if (!POJO_LIBRARY.equals(getLibrary())) {
+ return;
+ }
+ apiTemplateFiles.put(pojoApiInterfaceTemplate, ".java");
+ additionalProperties.put("isPOJO", true);
}
@Override
@@ -241,6 +264,10 @@ public class ServiceCombCodegen extends AbstractJavaCodegen implements CodegenCo
return apiName;
}
+ if(POJO_LIBRARY.equals(getLibrary())){
+ return initialCaps(name) + "ApiImpl";
+ }
+
return initialCaps(name) + "Controller";
}
diff --git a/code-generator/src/main/resources/ServiceComb/libraries/POJO/Application.mustache b/code-generator/src/main/resources/ServiceComb/libraries/POJO/Application.mustache
new file mode 100644
index 0000000..fda8d43
--- /dev/null
+++ b/code-generator/src/main/resources/ServiceComb/libraries/POJO/Application.mustache
@@ -0,0 +1,11 @@
+package {{mainClassPackage}};
+
+import org.apache.servicecomb.foundation.common.utils.BeanUtils;
+
+public class Application {
+
+ public static void main(String[] args) throws Exception {
+ System.setProperty("local.registry.file", "notExistJustForceLocal");
+ BeanUtils.init();
+ }
+}
\ No newline at end of file
diff --git a/code-generator/src/main/resources/ServiceComb/libraries/POJO/api.mustache b/code-generator/src/main/resources/ServiceComb/libraries/POJO/api.mustache
new file mode 100644
index 0000000..abb8da0
--- /dev/null
+++ b/code-generator/src/main/resources/ServiceComb/libraries/POJO/api.mustache
@@ -0,0 +1,38 @@
+package {{apiPackage}};
+
+import {{modelPackage}}.*;
+
+{{#imports}}import {{import}};
+{{/imports}}
+
+import org.apache.servicecomb.provider.pojo.RpcSchema;
+import org.springframework.http.HttpStatus;
+import org.springframework.http.ResponseEntity;
+import org.springframework.web.bind.annotation.PathVariable;
+import org.springframework.web.bind.annotation.RequestBody;
+import org.springframework.web.bind.annotation.RequestHeader;
+import org.springframework.web.bind.annotation.RequestMapping;
+import org.springframework.web.bind.annotation.RequestMethod;
+import org.springframework.web.bind.annotation.RequestParam;
+import org.springframework.web.bind.annotation.RequestPart;
+import org.springframework.web.multipart.MultipartFile;
+import java.util.List;
+import java.util.Map;
+import java.io.File;
+import static org.springframework.http.MediaType.*;
+
+@RpcSchema(schemaId = "{{#camelcase}}{{classname}}{{/camelcase}}")
+{{#operations}}
+public class {{classname}} implements {{#removeImplSuffix}}{{classname}}{{/removeImplSuffix}} {
+ {{#operation}}
+
+ @Override
+ public {{>returnTypes}} {{operationId}}({{#allParams}}{{>queryParams}}{{>pathParams}}{{>headerParams}}{{>bodyParams}}{{>formParams}}{{#hasMore}},
+ {{/hasMore}}{{/allParams}}) {
+ // do some magic!
+ return null;
+ }
+
+ {{/operation}}
+}
+{{/operations}}
\ No newline at end of file
diff --git a/code-generator/src/main/resources/ServiceComb/libraries/POJO/apiInterface.mustache b/code-generator/src/main/resources/ServiceComb/libraries/POJO/apiInterface.mustache
new file mode 100644
index 0000000..d7c5b3e
--- /dev/null
+++ b/code-generator/src/main/resources/ServiceComb/libraries/POJO/apiInterface.mustache
@@ -0,0 +1,23 @@
+package {{apiPackage}};
+
+import {{modelPackage}}.*;
+
+{{#imports}}import {{import}};
+{{/imports}}
+
+import java.util.List;
+import java.util.Map;
+import java.util.HashMap;
+import java.io.File;
+
+
+{{#operations}}
+public interface {{#removeImplSuffix}}{{classname}}{{/removeImplSuffix}} {
+ {{#operation}}
+
+ public {{>returnTypes}} {{operationId}}({{#allParams}}{{>queryParams}}{{>pathParams}}{{>headerParams}}{{>bodyParams}}{{>formParams}}{{#hasMore}},
+ {{/hasMore}}{{/allParams}});
+
+ {{/operation}}
+ }
+{{/operations}}
\ No newline at end of file
diff --git a/code-generator/src/main/resources/ServiceComb/libraries/POJO/bodyParams.mustache b/code-generator/src/main/resources/ServiceComb/libraries/POJO/bodyParams.mustache
new file mode 100644
index 0000000..bb1d6ff
--- /dev/null
+++ b/code-generator/src/main/resources/ServiceComb/libraries/POJO/bodyParams.mustache
@@ -0,0 +1 @@
+{{#isBodyParam}} {{{dataType}}} {{paramName}}{{/isBodyParam}}
\ No newline at end of file
diff --git a/code-generator/src/main/resources/ServiceComb/libraries/POJO/formParams.mustache b/code-generator/src/main/resources/ServiceComb/libraries/POJO/formParams.mustache
new file mode 100644
index 0000000..96f17c8
--- /dev/null
+++ b/code-generator/src/main/resources/ServiceComb/libraries/POJO/formParams.mustache
@@ -0,0 +1,2 @@
+{{#isFormParam}}{{#notFile}}
+{{{dataType}}} {{paramName}}{{/notFile}}{{#isFile}} File {{baseName}}{{/isFile}}{{/isFormParam}}
\ No newline at end of file
diff --git a/code-generator/src/main/resources/ServiceComb/libraries/POJO/headerParams.mustache b/code-generator/src/main/resources/ServiceComb/libraries/POJO/headerParams.mustache
new file mode 100644
index 0000000..edeeb85
--- /dev/null
+++ b/code-generator/src/main/resources/ServiceComb/libraries/POJO/headerParams.mustache
@@ -0,0 +1 @@
+{{#isHeaderParam}} {{{dataType}}} {{paramName}}{{/isHeaderParam}}
\ No newline at end of file
diff --git a/code-generator/src/main/resources/ServiceComb/libraries/POJO/pathParams.mustache b/code-generator/src/main/resources/ServiceComb/libraries/POJO/pathParams.mustache
new file mode 100644
index 0000000..9a0630b
--- /dev/null
+++ b/code-generator/src/main/resources/ServiceComb/libraries/POJO/pathParams.mustache
@@ -0,0 +1 @@
+{{#isPathParam}} {{{dataType}}} {{paramName}}{{/isPathParam}}
\ No newline at end of file
diff --git a/code-generator/src/main/resources/ServiceComb/libraries/POJO/pom.mustache b/code-generator/src/main/resources/ServiceComb/libraries/POJO/pom.mustache
new file mode 100644
index 0000000..c2ceb61
--- /dev/null
+++ b/code-generator/src/main/resources/ServiceComb/libraries/POJO/pom.mustache
@@ -0,0 +1,127 @@
+<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
+
+ <parent>
+ <artifactId>{{artifactId}}</artifactId>
+ <groupId>{{groupId}}</groupId>
+ <version>{{artifactVersion}}</version>
+ </parent>
+ <modelVersion>4.0.0</modelVersion>
+ <artifactId>provider</artifactId>
+ <packaging>jar</packaging>
+ <name>{{artifactId}}</name>
+
+ <properties>
+ <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
+ <java-chassis.version>1.2.0</java-chassis.version>
+ </properties>
+
+ <dependencyManagement>
+ <dependencies>
+ <dependency>
+ <groupId>org.apache.servicecomb</groupId>
+ <artifactId>java-chassis-dependencies</artifactId>
+ <version>${java-chassis.version}</version>
+ <type>pom</type>
+ <scope>import</scope>
+ </dependency>
+ </dependencies>
+ </dependencyManagement>
+
+ <dependencies>
+
+ <dependency>
+ <groupId>{{groupId}}</groupId>
+ <artifactId>model</artifactId>
+ <version>${project.version}</version>
+ </dependency>
+ <dependency>
+ <groupId>org.apache.servicecomb</groupId>
+ <artifactId>handler-bizkeeper</artifactId>
+ </dependency>
+ <dependency>
+ <groupId>org.apache.servicecomb</groupId>
+ <artifactId>handler-loadbalance</artifactId>
+ </dependency>
+ <dependency>
+ <groupId>org.apache.servicecomb</groupId>
+ <artifactId>transport-highway</artifactId>
+ </dependency>
+ <dependency>
+ <groupId>org.apache.servicecomb</groupId>
+ <artifactId>transport-rest-vertx</artifactId>
+ </dependency>
+ <dependency>
+ <groupId>org.apache.servicecomb</groupId>
+ <artifactId>handler-flowcontrol-qps</artifactId>
+ </dependency>
+ <dependency>
+ <groupId>org.apache.servicecomb</groupId>
+ <artifactId>provider-pojo</artifactId>
+ </dependency>
+ <!--log4j2-->
+ <dependency>
+ <groupId>org.apache.logging.log4j</groupId>
+ <artifactId>log4j-slf4j-impl</artifactId>
+ </dependency>
+ <dependency>
+ <groupId>org.apache.logging.log4j</groupId>
+ <artifactId>log4j-api</artifactId>
+ </dependency>
+ <dependency>
+ <groupId>org.apache.logging.log4j</groupId>
+ <artifactId>log4j-core</artifactId>
+ </dependency>
+
+ </dependencies>
+
+ <!--for package and deploy-->
+ <build>
+ <plugins>
+ <plugin>
+ <groupId>org.apache.maven.plugins</groupId>
+ <artifactId>maven-jar-plugin</artifactId>
+ <version>2.6</version>
+ <configuration>
+ <archive>
+ <manifest>
+ <addClasspath>true</addClasspath>
+ <classpathPrefix>lib/</classpathPrefix>
+ <!--change to your main class-->
+ <mainClass>${package}.Application</mainClass>
+ </manifest>
+ <manifestEntries>
+ <Class-Path>. </Class-Path>
+ </manifestEntries>
+ </archive>
+ </configuration>
+ </plugin>
+ <plugin>
+ <groupId>org.apache.maven.plugins</groupId>
+ <artifactId>maven-dependency-plugin</artifactId>
+ <executions>
+ <execution>
+ <id>copy-dependencies</id>
+ <phase>package</phase>
+ <goals>
+ <goal>copy-dependencies</goal>
+ </goals>
+ <configuration>
+ <outputDirectory>target/lib</outputDirectory>
+ </configuration>
+ </execution>
+ </executions>
+ </plugin>
+ <plugin>
+ <groupId>org.apache.maven.plugins</groupId>
+ <artifactId>maven-compiler-plugin</artifactId>
+ <version>3.1</version>
+ <configuration>
+ <source>1.8</source>
+ <target>1.8</target>
+ </configuration>
+ </plugin>
+
+ </plugins>
+ </build>
+
+</project>
\ No newline at end of file
diff --git a/code-generator/src/main/resources/ServiceComb/libraries/POJO/queryParams.mustache b/code-generator/src/main/resources/ServiceComb/libraries/POJO/queryParams.mustache
new file mode 100644
index 0000000..157ad86
--- /dev/null
+++ b/code-generator/src/main/resources/ServiceComb/libraries/POJO/queryParams.mustache
@@ -0,0 +1 @@
+{{#isQueryParam}} {{{dataType}}} {{paramName}}{{/isQueryParam}}
\ No newline at end of file
diff --git a/code-generator/src/test/java/org/apache/servicecomb/toolkit/codegen/GeneratorTest.java b/code-generator/src/test/java/org/apache/servicecomb/toolkit/codegen/GeneratorTest.java
index e011c7b..3156fae 100755
--- a/code-generator/src/test/java/org/apache/servicecomb/toolkit/codegen/GeneratorTest.java
+++ b/code-generator/src/test/java/org/apache/servicecomb/toolkit/codegen/GeneratorTest.java
@@ -38,6 +38,7 @@ public class GeneratorTest {
public void generateProgrammingModels() throws IOException, URISyntaxException {
generateCode("SpringMVC");
+ generateCode("POJO");
}
private void generateCode(String programmingModel) throws IOException, URISyntaxException {
diff --git a/toolkit-cli/src/test/java/org/apache/servicecomb/toolkit/cli/CliTest.java b/toolkit-cli/src/test/java/org/apache/servicecomb/toolkit/cli/CliTest.java
index 35885ad..f8ef465 100755
--- a/toolkit-cli/src/test/java/org/apache/servicecomb/toolkit/cli/CliTest.java
+++ b/toolkit-cli/src/test/java/org/apache/servicecomb/toolkit/cli/CliTest.java
@@ -31,7 +31,7 @@ public class CliTest {
@Test
public void generateServiceCombCodeFromSingleContract() throws IOException {
- String[] programModels = new String[] {"SpringMVC"};
+ String[] programModels = new String[] {"SpringMVC","POJO"};
Path tempDir = Files.createTempDirectory(null);
Arrays.stream(programModels).forEach(model -> {
try {