|
27 | 27 | import java.util.Locale; |
28 | 28 | import java.util.Map; |
29 | 29 | import java.util.Objects; |
| 30 | +import java.util.function.BiFunction; |
30 | 31 | import javax.annotation.Nullable; |
31 | 32 | import org.apache.commons.io.FileUtils; |
32 | 33 | import org.apache.commons.lang3.StringUtils; |
| 34 | +import org.jetbrains.annotations.NotNull; |
33 | 35 | import org.slf4j.Logger; |
34 | 36 | import org.slf4j.LoggerFactory; |
35 | 37 | import org.sonarsource.scanner.lib.internal.FailedBootstrap; |
|
40 | 42 | import org.sonarsource.scanner.lib.internal.endpoint.ScannerEndpoint; |
41 | 43 | import org.sonarsource.scanner.lib.internal.endpoint.ScannerEndpointResolver; |
42 | 44 | import org.sonarsource.scanner.lib.internal.facade.forked.NewScannerEngineFacade; |
| 45 | +import org.sonarsource.scanner.lib.internal.facade.forked.ScannerEngineLauncher; |
43 | 46 | import org.sonarsource.scanner.lib.internal.facade.forked.ScannerEngineLauncherFactory; |
44 | 47 | import org.sonarsource.scanner.lib.internal.facade.inprocess.InProcessScannerEngineFacade; |
45 | 48 | import org.sonarsource.scanner.lib.internal.facade.inprocess.IsolatedLauncherFactory; |
@@ -143,34 +146,55 @@ public ScannerEngineBootstrapResult bootstrap() { |
143 | 146 | // No HTTP call should be made before this point |
144 | 147 | try { |
145 | 148 | scannerHttpClient.init(httpConfig); |
146 | | - |
147 | | - var serverVersion = !isSonarQubeCloud ? getServerVersion(scannerHttpClient) : null; |
148 | | - |
149 | | - if (!isSonarQubeCloud && VersionUtils.isAtLeastIgnoringQualifier(serverVersion, SQ_VERSION_TOKEN_AUTHENTICATION) && Objects.nonNull(httpConfig.getLogin())) { |
150 | | - LOG.warn("Use of '{}' property has been deprecated in favor of '{}' (or the env variable alternative '{}'). Please use the latter when passing a token.", SONAR_LOGIN, |
151 | | - SONAR_TOKEN, TOKEN_ENV_VARIABLE); |
152 | | - } |
153 | | - |
154 | | - ScannerEngineFacade scannerFacade; |
155 | | - if (isSonarQubeCloud || VersionUtils.isAtLeastIgnoringQualifier(serverVersion, SQ_VERSION_NEW_BOOTSTRAPPING)) { |
156 | | - var launcher = scannerEngineLauncherFactory.createLauncher(scannerHttpClient, fileCache, immutableProperties); |
157 | | - |
158 | | - var adaptedProperties = adaptSslPropertiesToScannerProperties(immutableProperties, httpConfig); |
159 | | - |
160 | | - scannerFacade = new NewScannerEngineFacade(adaptedProperties, launcher, isSonarQubeCloud, serverVersion); |
161 | | - } else { |
162 | | - var launcher = launcherFactory.createLauncher(scannerHttpClient, fileCache); |
163 | | - var adaptedProperties = adaptDeprecatedPropertiesForInProcessBootstrapping(immutableProperties, httpConfig); |
164 | | - scannerFacade = new InProcessScannerEngineFacade(adaptedProperties, launcher, false, serverVersion); |
| 149 | + if (isSonarQubeCloud) { |
| 150 | + return bootstrapCloud(fileCache, immutableProperties, httpConfig); |
165 | 151 | } |
166 | | - |
167 | | - logServerType(scannerFacade); |
168 | | - return new SuccessfulBootstrap(scannerFacade); |
| 152 | + return bootstrapServer(fileCache, immutableProperties, httpConfig); |
169 | 153 | } catch (MessageException e) { |
170 | 154 | return handleException(e); |
171 | 155 | } |
172 | 156 | } |
173 | 157 |
|
| 158 | + private ScannerEngineBootstrapResult bootstrapCloud(FileCache fileCache, Map<String, String> immutableProperties, HttpConfig httpConfig) { |
| 159 | + ScannerEngineFacade scannerFacade; |
| 160 | + LOG.info("Communicating with SonarQube Cloud"); |
| 161 | + scannerFacade = buildNewFacade(fileCache, immutableProperties, httpConfig, |
| 162 | + (launcher, adaptedProperties) -> NewScannerEngineFacade.forSonarQubeCloud(adaptedProperties, launcher)); |
| 163 | + return new SuccessfulBootstrap(scannerFacade); |
| 164 | + } |
| 165 | + |
| 166 | + private ScannerEngineBootstrapResult bootstrapServer(FileCache fileCache, Map<String, String> immutableProperties, HttpConfig httpConfig) { |
| 167 | + ScannerEngineFacade scannerFacade; |
| 168 | + var serverVersion = getServerVersion(scannerHttpClient); |
| 169 | + String serverLabel; |
| 170 | + if (VersionUtils.compareMajor(serverVersion, 10) <= 0 || VersionUtils.compareMajor(serverVersion, 2025) >= 0) { |
| 171 | + serverLabel = "SonarQube Server"; |
| 172 | + } else { |
| 173 | + serverLabel = "SonarQube Community Build"; |
| 174 | + } |
| 175 | + LOG.info("Communicating with {} {}", serverLabel, serverVersion); |
| 176 | + if (VersionUtils.isAtLeastIgnoringQualifier(serverVersion, SQ_VERSION_TOKEN_AUTHENTICATION) && Objects.nonNull(httpConfig.getLogin())) { |
| 177 | + LOG.warn("Use of '{}' property has been deprecated in favor of '{}' (or the env variable alternative '{}'). Please use the latter when passing a token.", SONAR_LOGIN, |
| 178 | + SONAR_TOKEN, TOKEN_ENV_VARIABLE); |
| 179 | + } |
| 180 | + if (VersionUtils.isAtLeastIgnoringQualifier(serverVersion, SQ_VERSION_NEW_BOOTSTRAPPING)) { |
| 181 | + scannerFacade = buildNewFacade(fileCache, immutableProperties, httpConfig, |
| 182 | + (launcher, adaptedProperties) -> NewScannerEngineFacade.forSonarQubeServer(adaptedProperties, launcher, serverVersion)); |
| 183 | + } else { |
| 184 | + var launcher = launcherFactory.createLauncher(scannerHttpClient, fileCache); |
| 185 | + var adaptedProperties = adaptDeprecatedPropertiesForInProcessBootstrapping(immutableProperties, httpConfig); |
| 186 | + scannerFacade = new InProcessScannerEngineFacade(adaptedProperties, launcher, false, serverVersion); |
| 187 | + } |
| 188 | + return new SuccessfulBootstrap(scannerFacade); |
| 189 | + } |
| 190 | + |
| 191 | + private ScannerEngineFacade buildNewFacade(FileCache fileCache, Map<String, String> immutableProperties, HttpConfig httpConfig, |
| 192 | + BiFunction<ScannerEngineLauncher, Map<String, String>, ScannerEngineFacade> facadeFactory) { |
| 193 | + var launcher = scannerEngineLauncherFactory.createLauncher(scannerHttpClient, fileCache, immutableProperties); |
| 194 | + var adaptedProperties = adaptSslPropertiesToScannerProperties(immutableProperties, httpConfig); |
| 195 | + return facadeFactory.apply(launcher, adaptedProperties); |
| 196 | + } |
| 197 | + |
174 | 198 | private static ScannerEngineBootstrapResult handleException(MessageException e) { |
175 | 199 | var message = new StringBuilder(e.getMessage()); |
176 | 200 | if (e.getCause() instanceof HttpException) { |
@@ -202,14 +226,6 @@ private static void logWithStacktraceOnlyIfDebug(String message, Throwable t) { |
202 | 226 | } |
203 | 227 | } |
204 | 228 |
|
205 | | - private static void logServerType(ScannerEngineFacade engine) { |
206 | | - if (engine.isSonarQubeCloud()) { |
207 | | - LOG.info("Communicating with SonarQube Cloud"); |
208 | | - } else { |
209 | | - LOG.info("Communicating with {} {}", engine.getServerLabel(), engine.getServerVersion()); |
210 | | - } |
211 | | - } |
212 | | - |
213 | 229 | /** |
214 | 230 | * Older SonarQube versions used to rely on some different properties, or even {@link System} properties. |
215 | 231 | * For backward compatibility, we adapt the new properties to the old format. |
|
0 commit comments