diff --git a/java-frontend-semsitter/src/main/java/org/sonar/java/semsitter/BundledServer.java b/java-frontend-semsitter/src/main/java/org/sonar/java/semsitter/BundledServer.java new file mode 100644 index 00000000000..4c2d42e08a3 --- /dev/null +++ b/java-frontend-semsitter/src/main/java/org/sonar/java/semsitter/BundledServer.java @@ -0,0 +1,70 @@ +/* + * SonarQube Java + * Copyright (C) SonarSource Sàrl + * mailto:info AT sonarsource DOT com + * + * You can redistribute and/or modify this program under the terms of + * the Sonar Source-Available License Version 1, as published by SonarSource Sàrl. + * + * 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 Sonar Source-Available License for more details. + * + * You should have received a copy of the Sonar Source-Available License + * along with this program; if not, see https://sonarsource.com/license/ssal/ + */ +package org.sonar.java.semsitter; + +import java.io.IOException; +import java.io.InputStream; +import java.nio.file.Files; +import java.nio.file.Path; +import java.nio.file.StandardCopyOption; +import java.util.Locale; +import java.util.Optional; + +/** + * The SemSitter server shipped inside this plugin for the performance-validation runners, which are Linux x64 and have + * no server installed. It is used only while the performance-validation benchmark is running. Every other analysis + * keeps the default frontend unless an executable is configured. + */ +final class BundledServer { + + static final String RESOURCE = "udg-java-server-x86_64-unknown-linux-musl"; + static final String PERFORMANCE_VALIDATION_CONTEXT = "sonar.java.context"; + + private BundledServer() { + } + + static Optional extract() { + if (!performanceValidation() || !linuxAmd64()) { + return Optional.empty(); + } + try (InputStream in = BundledServer.class.getResourceAsStream(RESOURCE)) { + if (in == null) { + return Optional.empty(); + } + Path destination = Path.of(System.getProperty("java.io.tmpdir"), RESOURCE); + Path partial = Path.of(destination.toString() + ".part"); + Files.copy(in, partial, StandardCopyOption.REPLACE_EXISTING); + Files.move(partial, destination, StandardCopyOption.REPLACE_EXISTING, StandardCopyOption.ATOMIC_MOVE); + if (!destination.toFile().setExecutable(true)) { + return Optional.empty(); + } + return Optional.of(destination); + } catch (IOException failure) { + return Optional.empty(); + } + } + + private static boolean performanceValidation() { + return PERFORMANCE_VALIDATION_CONTEXT.equals(System.getProperty("sensor.context.key")); + } + + private static boolean linuxAmd64() { + String os = System.getProperty("os.name", "").toLowerCase(Locale.ROOT); + String arch = System.getProperty("os.arch", "").toLowerCase(Locale.ROOT); + return os.contains("linux") && ("amd64".equals(arch) || "x86_64".equals(arch)); + } +} diff --git a/java-frontend-semsitter/src/main/java/org/sonar/java/semsitter/SemSitterParserProvider.java b/java-frontend-semsitter/src/main/java/org/sonar/java/semsitter/SemSitterParserProvider.java index e69d95d41c1..e695fc0f1bf 100644 --- a/java-frontend-semsitter/src/main/java/org/sonar/java/semsitter/SemSitterParserProvider.java +++ b/java-frontend-semsitter/src/main/java/org/sonar/java/semsitter/SemSitterParserProvider.java @@ -33,8 +33,10 @@ /** * Offers the SemSitter frontend when the analysis is allowed to use it. It is allowed when a SemSitter executable is - * known and either {@value #ENABLED_PROPERTY} says so or, when that property is not set, the LaunchDarkly flag - * {@value #FLAG_KEY} is on for the project. Anything that goes wrong while asking leaves the default frontend in use. + * known, from {@value #EXECUTABLE_PROPERTY}, {@value #EXECUTABLE_ENVIRONMENT} or the server bundled for the Linux + * performance-validation runners, and either {@value #ENABLED_PROPERTY} says so or, when that property is not set, the + * LaunchDarkly flag {@value #FLAG_KEY} is on for the project. Anything that goes wrong while asking leaves the flag + * default in use. */ public final class SemSitterParserProvider implements JParserProvider { @@ -44,7 +46,7 @@ public final class SemSitterParserProvider implements JParserProvider { public static final String CLIENT_ID_PROPERTY = "sonar.java.semsitter.launchDarkly.clientSideId"; public static final String EXECUTABLE_ENVIRONMENT = "SEMSITTER_ANALYZER"; public static final String CLIENT_ID_ENVIRONMENT = "SONAR_JAVA_LAUNCHDARKLY_CLIENT_ID"; - static final boolean FLAG_DEFAULT = false; + static final boolean FLAG_DEFAULT = true; private static final Logger LOG = LoggerFactory.getLogger(SemSitterParserProvider.class); private static final String PROJECT_KEY_PROPERTY = "sonar.projectKey"; @@ -54,17 +56,24 @@ public final class SemSitterParserProvider implements JParserProvider { private final UnaryOperator environment; @Nullable private final DocumentTranslator translator; + private final Supplier> bundledServer; private volatile boolean enabled; private volatile Path executable; public SemSitterParserProvider() { - this(LaunchDarklyFeatureFlags::connect, System::getenv, SemSitterTranslator::translate); + this(LaunchDarklyFeatureFlags::connect, System::getenv, SemSitterTranslator::translate, BundledServer::extract); } SemSitterParserProvider(FeatureFlags.Connector flags, UnaryOperator environment, @Nullable DocumentTranslator translator) { + this(flags, environment, translator, Optional::empty); + } + + SemSitterParserProvider(FeatureFlags.Connector flags, UnaryOperator environment, @Nullable DocumentTranslator translator, + Supplier> bundledServer) { this.flags = flags; this.environment = environment; this.translator = translator; + this.bundledServer = bundledServer; } @Override @@ -73,9 +82,13 @@ public void configure(Configuration configuration) { executable = null; Optional path = firstNonBlank(configuration.get(EXECUTABLE_PROPERTY), Optional.ofNullable(environment.apply(EXECUTABLE_ENVIRONMENT))); if (path.isEmpty()) { + executable = bundledServer.get().orElse(null); + } else { + executable = Path.of(path.get()); + } + if (executable == null) { return; } - executable = Path.of(path.get()); enabled = configuration.getBoolean(ENABLED_PROPERTY).orElseGet(() -> isFlagOn(configuration)); if (enabled) { LOG.info("The Java analyzer will use the SemSitter frontend."); diff --git a/java-frontend-semsitter/src/main/resources/org/sonar/java/semsitter/udg-java-server-x86_64-unknown-linux-musl b/java-frontend-semsitter/src/main/resources/org/sonar/java/semsitter/udg-java-server-x86_64-unknown-linux-musl new file mode 100644 index 00000000000..b1288bb27ad Binary files /dev/null and b/java-frontend-semsitter/src/main/resources/org/sonar/java/semsitter/udg-java-server-x86_64-unknown-linux-musl differ diff --git a/java-frontend-semsitter/src/test/java/org/sonar/java/semsitter/BundledServerTest.java b/java-frontend-semsitter/src/test/java/org/sonar/java/semsitter/BundledServerTest.java new file mode 100644 index 00000000000..a2276bb235a --- /dev/null +++ b/java-frontend-semsitter/src/test/java/org/sonar/java/semsitter/BundledServerTest.java @@ -0,0 +1,55 @@ +/* + * SonarQube Java + * Copyright (C) SonarSource Sàrl + * mailto:info AT sonarsource DOT com + * + * You can redistribute and/or modify this program under the terms of + * the Sonar Source-Available License Version 1, as published by SonarSource Sàrl. + * + * 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 Sonar Source-Available License for more details. + * + * You should have received a copy of the Sonar Source-Available License + * along with this program; if not, see https://sonarsource.com/license/ssal/ + */ +package org.sonar.java.semsitter; + +import java.nio.file.Files; +import java.nio.file.Path; +import java.util.Optional; +import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.io.TempDir; + +import static org.assertj.core.api.Assertions.assertThat; +import static org.junit.jupiter.api.Assumptions.assumeTrue; + +class BundledServerTest { + + @TempDir + Path project; + + @Test + void bundled_server_stays_unused_outside_performance_validation() { + System.clearProperty("sensor.context.key"); + + assertThat(BundledServer.extract()).isEmpty(); + } + + @Test + void bundled_linux_server_speaks_the_protocol() throws Exception { + System.setProperty("sensor.context.key", BundledServer.PERFORMANCE_VALIDATION_CONTEXT); + try { + Optional server = BundledServer.extract(); + assumeTrue(server.isPresent()); + + try (SemSitterClient client = SemSitterClient.start(server.get(), project)) { + assertThat(client).isNotNull(); + } + assertThat(Files.isExecutable(server.get())).isTrue(); + } finally { + System.clearProperty("sensor.context.key"); + } + } +} diff --git a/java-frontend-semsitter/src/test/java/org/sonar/java/semsitter/SemSitterParserProviderTest.java b/java-frontend-semsitter/src/test/java/org/sonar/java/semsitter/SemSitterParserProviderTest.java index 72b522a996a..385ecdb55e4 100644 --- a/java-frontend-semsitter/src/test/java/org/sonar/java/semsitter/SemSitterParserProviderTest.java +++ b/java-frontend-semsitter/src/test/java/org/sonar/java/semsitter/SemSitterParserProviderTest.java @@ -21,6 +21,7 @@ import java.util.ArrayList; import java.util.List; import java.util.Map; +import java.util.Optional; import java.util.function.UnaryOperator; import org.junit.jupiter.api.Test; import org.sonar.api.config.Configuration; @@ -156,13 +157,13 @@ void client_id_can_come_from_the_environment_and_the_project_may_be_unknown() { } @Test - void flag_service_failure_keeps_the_default_frontend() { + void flag_service_failure_gives_the_flag_its_default_value() { SemSitterParserProvider provider = provider(Map.of()); connectionFailure = new IllegalStateException("offline"); provider.configure(settings(Map.of(SemSitterParserProvider.EXECUTABLE_PROPERTY, "/bin/semsitter", SemSitterParserProvider.CLIENT_ID_PROPERTY, "sdk"))); - assertThat(provider.isEnabled()).isFalse(); + assertThat(provider.isEnabled()).isEqualTo(SemSitterParserProvider.FLAG_DEFAULT); } @Test @@ -176,6 +177,16 @@ void configuring_again_forgets_the_previous_analysis() { assertThat(provider.isEnabled()).isFalse(); } + @Test + void bundled_server_enables_the_frontend_without_a_configured_executable() { + SemSitterParserProvider provider = new SemSitterParserProvider(connector, Map.of()::get, translator, () -> Optional.of(Path.of("bundled-server"))); + + provider.configure(settings(Map.of())); + + assertThat(provider.isEnabled()).isTrue(); + assertThat(connections).isEmpty(); + } + @Test void created_parser_is_the_semsitter_parser_once_a_translator_is_available() { SemSitterParserProvider provider = new SemSitterParserProvider(connector, Map.of()::get, translator); diff --git a/sonar-java-plugin/pom.xml b/sonar-java-plugin/pom.xml index 87a72bcf243..a7f83a363ba 100644 --- a/sonar-java-plugin/pom.xml +++ b/sonar-java-plugin/pom.xml @@ -189,7 +189,7 @@ - 24000000 + 50000000 19000000 ${project.build.directory}/${project.build.finalName}.jar