|
| 1 | +/* |
| 2 | + * SonarScanner Java Library |
| 3 | + * Copyright (C) 2011-2024 SonarSource SA |
| 4 | + * mailto:info AT sonarsource DOT com |
| 5 | + * |
| 6 | + * This program is free software; you can redistribute it and/or |
| 7 | + * modify it under the terms of the GNU Lesser General Public |
| 8 | + * License as published by the Free Software Foundation; either |
| 9 | + * version 3 of the License, or (at your option) any later version. |
| 10 | + * |
| 11 | + * This program is distributed in the hope that it will be useful, |
| 12 | + * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 13 | + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 14 | + * Lesser General Public License for more details. |
| 15 | + * |
| 16 | + * You should have received a copy of the GNU Lesser General Public License |
| 17 | + * along with this program; if not, write to the Free Software Foundation, |
| 18 | + * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. |
| 19 | + */ |
| 20 | +package org.sonarsource.scanner.lib; |
| 21 | + |
| 22 | +import java.util.Collections; |
| 23 | +import java.util.HashMap; |
| 24 | +import java.util.HashSet; |
| 25 | +import java.util.Map; |
| 26 | +import java.util.Set; |
| 27 | +import org.sonarsource.scanner.lib.internal.ClassloadRules; |
| 28 | +import org.sonarsource.scanner.lib.internal.InternalProperties; |
| 29 | +import org.sonarsource.scanner.lib.internal.IsolatedLauncherFactory; |
| 30 | +import org.sonarsource.scanner.lib.internal.cache.Logger; |
| 31 | + |
| 32 | +/** |
| 33 | + * Entry point to run a Sonar analysis programmatically. |
| 34 | + */ |
| 35 | +public class ScannerEngineBootstrapper { |
| 36 | + private static final String SONAR_HOST_URL_ENV_VAR = "SONAR_HOST_URL"; |
| 37 | + private static final String SONARCLOUD_HOST = "https://sonarcloud.io"; |
| 38 | + private final IsolatedLauncherFactory launcherFactory; |
| 39 | + private final LogOutput logOutput; |
| 40 | + private final Map<String, String> bootstrapProperties = new HashMap<>(); |
| 41 | + private final System2 system; |
| 42 | + |
| 43 | + public ScannerEngineBootstrapper(String app, String version, final LogOutput logOutput) { |
| 44 | + this(app, version, logOutput, new System2()); |
| 45 | + } |
| 46 | + |
| 47 | + /** |
| 48 | + * For testing. |
| 49 | + */ |
| 50 | + public ScannerEngineBootstrapper(String app, String version, final LogOutput logOutput, System2 system2) { |
| 51 | + this(new LoggerAdapter(logOutput), logOutput, system2); |
| 52 | + this.setBootstrapProperty(InternalProperties.SCANNER_APP, app) |
| 53 | + .setBootstrapProperty(InternalProperties.SCANNER_APP_VERSION, version); |
| 54 | + } |
| 55 | + |
| 56 | + private ScannerEngineBootstrapper(Logger logger, LogOutput logOutput, System2 system) { |
| 57 | + this(logOutput, system, new IsolatedLauncherFactory(logger)); |
| 58 | + } |
| 59 | + |
| 60 | + ScannerEngineBootstrapper(LogOutput logOutput, System2 system, IsolatedLauncherFactory launcherFactory) { |
| 61 | + this.launcherFactory = launcherFactory; |
| 62 | + this.logOutput = logOutput; |
| 63 | + this.system = system; |
| 64 | + } |
| 65 | + |
| 66 | + /** |
| 67 | + * Declare technical properties needed to bootstrap (sonar.host.url, credentials, proxy, ...). |
| 68 | + */ |
| 69 | + public ScannerEngineBootstrapper addBootstrapProperties(Map<String, String> p) { |
| 70 | + bootstrapProperties.putAll(p); |
| 71 | + return this; |
| 72 | + } |
| 73 | + |
| 74 | + /** |
| 75 | + * Declare a technical property needed to bootstrap (sonar.host.url, credentials, proxy, ...). |
| 76 | + */ |
| 77 | + public ScannerEngineBootstrapper setBootstrapProperty(String key, String value) { |
| 78 | + bootstrapProperties.put(key, value); |
| 79 | + return this; |
| 80 | + } |
| 81 | + |
| 82 | + /** |
| 83 | + * Bootstrap the scanner-engine. |
| 84 | + */ |
| 85 | + public ScannerEngineFacade bootstrap() { |
| 86 | + initBootstrapDefaultValues(); |
| 87 | + Set<String> unmaskRules = new HashSet<>(); |
| 88 | + unmaskRules.add("org.sonarsource.scanner.lib.internal.batch."); |
| 89 | + ClassloadRules rules = new ClassloadRules(Collections.emptySet(), unmaskRules); |
| 90 | + var properties = Map.copyOf(bootstrapProperties); |
| 91 | + var isSonarCloud = getSonarCloudUrl().equals(properties.get(ScannerProperties.HOST_URL)); |
| 92 | + var launcher = launcherFactory.createLauncher(properties, rules); |
| 93 | + return new ScannerEngineFacade(properties, launcher, logOutput, isSonarCloud); |
| 94 | + } |
| 95 | + |
| 96 | + private void initBootstrapDefaultValues() { |
| 97 | + String sonarHostUrl = system.getEnvironmentVariable(SONAR_HOST_URL_ENV_VAR); |
| 98 | + if (sonarHostUrl != null) { |
| 99 | + setBootstrapPropertyIfNotAlreadySet(ScannerProperties.HOST_URL, sonarHostUrl); |
| 100 | + } else { |
| 101 | + setBootstrapPropertyIfNotAlreadySet(ScannerProperties.HOST_URL, getSonarCloudUrl()); |
| 102 | + } |
| 103 | + } |
| 104 | + |
| 105 | + private String getSonarCloudUrl() { |
| 106 | + return bootstrapProperties.getOrDefault("sonar.scanner.sonarcloudUrl", SONARCLOUD_HOST); |
| 107 | + } |
| 108 | + |
| 109 | + private void setBootstrapPropertyIfNotAlreadySet(String key, String value) { |
| 110 | + if (!bootstrapProperties.containsKey(key)) { |
| 111 | + setBootstrapProperty(key, value); |
| 112 | + } |
| 113 | + } |
| 114 | + |
| 115 | +} |
0 commit comments