From 7b767314a706c363ba466572d2fe8e7d46004b6a Mon Sep 17 00:00:00 2001 From: Daniel Schwarz Date: Mon, 4 Dec 2017 17:59:44 +0100 Subject: Generate class WsClient in sonar-ws --- .../org/sonarqube/wsgenerator/CodeFormatter.java | 15 ++-- .../java/org/sonarqube/wsgenerator/Helper.java | 40 ++++++++++- .../src/main/resources/defaultWsClient.vm | 79 ++++++++++++++++++++++ .../src/main/resources/package-info.vm | 4 ++ sonar-ws-generator/src/main/resources/wsClient.vm | 58 ++++++++++++++++ 5 files changed, 190 insertions(+), 6 deletions(-) create mode 100644 sonar-ws-generator/src/main/resources/defaultWsClient.vm create mode 100644 sonar-ws-generator/src/main/resources/wsClient.vm (limited to 'sonar-ws-generator') diff --git a/sonar-ws-generator/src/main/java/org/sonarqube/wsgenerator/CodeFormatter.java b/sonar-ws-generator/src/main/java/org/sonarqube/wsgenerator/CodeFormatter.java index aebeabf2323..577ff739c36 100644 --- a/sonar-ws-generator/src/main/java/org/sonarqube/wsgenerator/CodeFormatter.java +++ b/sonar-ws-generator/src/main/java/org/sonarqube/wsgenerator/CodeFormatter.java @@ -30,26 +30,31 @@ import java.io.InputStream; import java.io.InputStreamReader; import java.io.StringWriter; import java.io.Writer; -import java.util.HashSet; import java.util.Properties; -import java.util.Set; import org.apache.commons.io.FileUtils; import org.apache.velocity.VelocityContext; import org.apache.velocity.app.VelocityEngine; import static java.nio.charset.StandardCharsets.UTF_8; -import static java.util.Arrays.asList; +import static org.sonarqube.wsgenerator.Helper.PATH_EXCLUSIONS; public class CodeFormatter { - private static final Set PATH_EXCLUSIONS = new HashSet<>(asList("api/orchestrator")); - public static void format(String json) { JsonObject jsonElement = new Gson().fromJson(json, JsonObject.class); JsonArray webServices = (JsonArray) jsonElement.get("webServices"); Helper helper = new Helper(); + VelocityContext globalContext = new VelocityContext(); + globalContext.put("webServices", webServices); + globalContext.put("helper", helper); + String defaultWsClientCode = applyTemplate("defaultWsClient.vm", globalContext); + writeSourceFile(helper.defaultWsClientFile(), defaultWsClientCode); + String wsClientCode = applyTemplate("wsClient.vm", globalContext); + writeSourceFile(helper.wsClientFile(), wsClientCode); + writeSourceFile(helper.packageInfoFile(), applyTemplate("package-info.vm", globalContext)); + for (JsonElement webServiceElement : webServices) { JsonObject webService = (JsonObject) webServiceElement; String webServicePath = webService.get("path").getAsString(); diff --git a/sonar-ws-generator/src/main/java/org/sonarqube/wsgenerator/Helper.java b/sonar-ws-generator/src/main/java/org/sonarqube/wsgenerator/Helper.java index e38ea016dcf..62190c14c54 100644 --- a/sonar-ws-generator/src/main/java/org/sonarqube/wsgenerator/Helper.java +++ b/sonar-ws-generator/src/main/java/org/sonarqube/wsgenerator/Helper.java @@ -25,13 +25,18 @@ import java.io.BufferedReader; import java.io.InputStream; import java.io.InputStreamReader; import java.util.Collections; +import java.util.HashSet; import java.util.List; import java.util.Map; +import java.util.Set; import java.util.stream.Collectors; import java.util.stream.Stream; +import static java.util.Arrays.asList; + public class Helper { + static final Set PATH_EXCLUSIONS = new HashSet<>(asList("api/orchestrator")); private static final String OUTPUT_DIR = "target/generated-sources/results"; private final Map> responseTypes; @@ -43,8 +48,16 @@ public class Helper { .collect(Collectors.groupingBy(arr -> arr[0])); } + public boolean isIncluded(String path) { + return !PATH_EXCLUSIONS.contains(path); + } + + public String packageName() { + return "org.sonarqube.ws.client"; + } + public String packageName(String path) { - return "org.sonarqube.ws.client." + rawName(path).toLowerCase(); + return packageName() + "." + rawName(path).toLowerCase(); } private String rawName(String path) { @@ -59,6 +72,19 @@ public class Helper { String name = rawName(path); return capitalizeFirstLetter(name) + "Service"; } + public String defaultWsClientFieldName(String path) { + String name = rawName(path); + return lowercaseFirstLetter(name) + "Service"; + } + + public String defaultWsClientMethodName(String path) { + String name = rawName(path); + return lowercaseFirstLetter(name); + } + + public String webserviceTypeImport(String path) { + return "import " + packageName(path) + "." + className(path) + ";"; + } private String capitalizeFirstLetter(String name) { return name.substring(0, 1).toUpperCase() + name.substring(1); @@ -164,6 +190,18 @@ public class Helper { return OUTPUT_DIR + "/org/sonarqube/ws/client/" + rawName(path).toLowerCase() + "/" + className(path) + ".java"; } + public String defaultWsClientFile() { + return OUTPUT_DIR + "/org/sonarqube/ws/client/DefaultWsClient.java"; + } + + public String wsClientFile() { + return OUTPUT_DIR + "/org/sonarqube/ws/client/WsClient.java"; + } + + public String packageInfoFile() { + return OUTPUT_DIR + "/org/sonarqube/ws/client/package-info.java"; + } + public String packageInfoFile(String path) { return OUTPUT_DIR + "/org/sonarqube/ws/client/" + rawName(path).toLowerCase() + "/package-info.java"; } diff --git a/sonar-ws-generator/src/main/resources/defaultWsClient.vm b/sonar-ws-generator/src/main/resources/defaultWsClient.vm new file mode 100644 index 00000000000..eb5b11a3c0e --- /dev/null +++ b/sonar-ws-generator/src/main/resources/defaultWsClient.vm @@ -0,0 +1,79 @@ +/* + * SonarQube + * Copyright (C) 2009-2017 SonarSource SA + * mailto:info AT sonarsource DOT com + * + * This program is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation; either + * version 3 of the License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public License + * along with this program; if not, write to the Free Software Foundation, + * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. + */ +package org.sonarqube.ws.client; + +import javax.annotation.Generated; +import org.sonarqube.ws.client.ce.CeService; +import org.sonarqube.ws.client.organization.OrganizationService; +import org.sonarqube.ws.client.projectanalysis.ProjectAnalysisService; +import org.sonarqube.ws.client.projectbranches.ProjectBranchesServiceOld; +import org.sonarqube.ws.client.projectlinks.ProjectLinksService; +import org.sonarqube.ws.client.qualitygates.QualitygatesService; +import org.sonarqube.ws.client.qualityprofile.QualityProfilesService; +import org.sonarqube.ws.client.system.SystemServiceOld; + +#foreach($webService in $webServices) +#if ($helper.isIncluded($webService.path.asString)) +$helper.webserviceTypeImport($webService.path.asString) +#end +#end + +/** + * This class is not public anymore since version 5.5. It is + * created by {@link WsClientFactory} + * + * @since 5.3 + */ +@Generated("sonar-ws-generator") +class DefaultWsClient implements WsClient { + + private final WsConnector wsConnector; + +#foreach($webService in $webServices) +#if ($helper.isIncluded($webService.path.asString)) + private final $helper.className($webService.path.asString) $helper.defaultWsClientFieldName($webService.path.asString); +#end +#end + + DefaultWsClient(WsConnector wsConnector) { + this.wsConnector = wsConnector; + +#foreach($webService in $webServices) +#if ($helper.isIncluded($webService.path.asString)) + this.$helper.defaultWsClientFieldName($webService.path.asString) = new $helper.className($webService.path.asString)(wsConnector); +#end +#end + } + + @Override + @Deprecated + public WsConnector wsConnector() { + return wsConnector; + } +#foreach($webService in $webServices) +#if ($helper.isIncluded($webService.path.asString)) + + @Override + public $helper.className($webService.path.asString) $helper.defaultWsClientMethodName($webService.path.asString)() { + return $helper.defaultWsClientFieldName($webService.path.asString); + } +#end +#end +} diff --git a/sonar-ws-generator/src/main/resources/package-info.vm b/sonar-ws-generator/src/main/resources/package-info.vm index ee1ea241f7b..4eae5072ad8 100644 --- a/sonar-ws-generator/src/main/resources/package-info.vm +++ b/sonar-ws-generator/src/main/resources/package-info.vm @@ -19,7 +19,11 @@ */ @ParametersAreNonnullByDefault @Generated("sonar-ws-generator") +#if ($webService.path.asString) package $helper.packageName($webService.path.asString); +#else +package $helper.packageName(); +#end import javax.annotation.ParametersAreNonnullByDefault; import javax.annotation.Generated; diff --git a/sonar-ws-generator/src/main/resources/wsClient.vm b/sonar-ws-generator/src/main/resources/wsClient.vm new file mode 100644 index 00000000000..667f4187915 --- /dev/null +++ b/sonar-ws-generator/src/main/resources/wsClient.vm @@ -0,0 +1,58 @@ +/* + * SonarQube + * Copyright (C) 2009-2017 SonarSource SA + * mailto:info AT sonarsource DOT com + * + * This program is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation; either + * version 3 of the License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public License + * along with this program; if not, write to the Free Software Foundation, + * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. + */ +package org.sonarqube.ws.client; + +import javax.annotation.Generated; + +#foreach($webService in $webServices) +#if ($helper.isIncluded($webService.path.asString)) +$helper.webserviceTypeImport($webService.path.asString) +#end +#end + +/** + * Allows to request the web services of SonarQube server. Instance is provided by + * {@link WsClientFactory}. + * + *

+ * Usage: + *

+ *   HttpConnector httpConnector = HttpConnector.newBuilder()
+ *     .url("http://localhost:9000")
+ *     .credentials("admin", "admin")
+ *     .build();
+ *   WsClient wsClient = WsClientFactories.getDefault().newClient(httpConnector);
+ *   wsClient.issues().search(issueRequest);
+ * 
+ *

+ * + * @since 5.3 + */ +@Generated("https://github.com/SonarSource/sonar-ws-generator") +public interface WsClient { + + WsConnector wsConnector(); +#foreach($webService in $webServices) +#if ($helper.isIncluded($webService.path.asString)) + + $helper.className($webService.path.asString) $helper.defaultWsClientMethodName($webService.path.asString)(); +#end +#end +} -- cgit v1.2.3