Skip to content

Commit 1a7b1de

Browse files
committed
SCANJLIB-266 Refactor the bootstrap method
1 parent cfb64ea commit 1a7b1de

File tree

4 files changed

+63
-58
lines changed

4 files changed

+63
-58
lines changed

lib/src/main/java/org/sonarsource/scanner/lib/ScannerEngineBootstrapper.java

Lines changed: 46 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -27,9 +27,11 @@
2727
import java.util.Locale;
2828
import java.util.Map;
2929
import java.util.Objects;
30+
import java.util.function.BiFunction;
3031
import javax.annotation.Nullable;
3132
import org.apache.commons.io.FileUtils;
3233
import org.apache.commons.lang3.StringUtils;
34+
import org.jetbrains.annotations.NotNull;
3335
import org.slf4j.Logger;
3436
import org.slf4j.LoggerFactory;
3537
import org.sonarsource.scanner.lib.internal.FailedBootstrap;
@@ -40,6 +42,7 @@
4042
import org.sonarsource.scanner.lib.internal.endpoint.ScannerEndpoint;
4143
import org.sonarsource.scanner.lib.internal.endpoint.ScannerEndpointResolver;
4244
import org.sonarsource.scanner.lib.internal.facade.forked.NewScannerEngineFacade;
45+
import org.sonarsource.scanner.lib.internal.facade.forked.ScannerEngineLauncher;
4346
import org.sonarsource.scanner.lib.internal.facade.forked.ScannerEngineLauncherFactory;
4447
import org.sonarsource.scanner.lib.internal.facade.inprocess.InProcessScannerEngineFacade;
4548
import org.sonarsource.scanner.lib.internal.facade.inprocess.IsolatedLauncherFactory;
@@ -143,34 +146,55 @@ public ScannerEngineBootstrapResult bootstrap() {
143146
// No HTTP call should be made before this point
144147
try {
145148
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);
165151
}
166-
167-
logServerType(scannerFacade);
168-
return new SuccessfulBootstrap(scannerFacade);
152+
return bootstrapServer(fileCache, immutableProperties, httpConfig);
169153
} catch (MessageException e) {
170154
return handleException(e);
171155
}
172156
}
173157

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+
174198
private static ScannerEngineBootstrapResult handleException(MessageException e) {
175199
var message = new StringBuilder(e.getMessage());
176200
if (e.getCause() instanceof HttpException) {
@@ -202,14 +226,6 @@ private static void logWithStacktraceOnlyIfDebug(String message, Throwable t) {
202226
}
203227
}
204228

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-
213229
/**
214230
* Older SonarQube versions used to rely on some different properties, or even {@link System} properties.
215231
* For backward compatibility, we adapt the new properties to the old format.

lib/src/main/java/org/sonarsource/scanner/lib/ScannerEngineFacade.java

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -49,10 +49,4 @@ public interface ScannerEngineFacade extends AutoCloseable {
4949
*/
5050
boolean analyze(Map<String, String> analysisProps);
5151

52-
/**
53-
* Get the label of the server that the scanner is connected to. Distinguishes SonarQube Cloud, SonarQube Server and SonarQube Community Build.
54-
*
55-
* @return whether scanner is connected to SonarQube Cloud, Server or Community Build.
56-
*/
57-
String getServerLabel();
5852
}

lib/src/main/java/org/sonarsource/scanner/lib/internal/facade/AbstractScannerEngineFacade.java

Lines changed: 6 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -24,36 +24,35 @@
2424
import javax.annotation.Nullable;
2525
import org.sonarsource.scanner.lib.ScannerEngineFacade;
2626
import org.sonarsource.scanner.lib.internal.facade.forked.JreCacheHit;
27-
import org.sonarsource.scanner.lib.internal.util.VersionUtils;
2827

2928
public abstract class AbstractScannerEngineFacade implements ScannerEngineFacade {
3029

3130
private final Map<String, String> bootstrapProperties;
32-
private final boolean isSonarCloud;
31+
private final boolean isSonarQubeCloud;
3332
private final String serverVersion;
3433
private final boolean wasEngineCacheHit;
3534
private final JreCacheHit wasJreCacheHit;
3635

37-
protected AbstractScannerEngineFacade(Map<String, String> bootstrapProperties, boolean isSonarCloud, @Nullable String serverVersion,
36+
protected AbstractScannerEngineFacade(Map<String, String> bootstrapProperties, boolean isSonarQubeCloud, @Nullable String serverVersion,
3837
boolean wasEngineCacheHit, @Nullable JreCacheHit wasJreCacheHit) {
3938
this.bootstrapProperties = bootstrapProperties;
40-
this.isSonarCloud = isSonarCloud;
39+
this.isSonarQubeCloud = isSonarQubeCloud;
4140
this.serverVersion = serverVersion;
4241
this.wasEngineCacheHit = wasEngineCacheHit;
4342
this.wasJreCacheHit = wasJreCacheHit;
4443
}
4544

4645
@Override
4746
public String getServerVersion() {
48-
if (isSonarCloud) {
49-
throw new UnsupportedOperationException("Server version is not available for SonarCloud.");
47+
if (isSonarQubeCloud) {
48+
throw new UnsupportedOperationException("Server version is not available for SonarQube Cloud.");
5049
}
5150
return serverVersion;
5251
}
5352

5453
@Override
5554
public boolean isSonarQubeCloud() {
56-
return isSonarCloud;
55+
return isSonarQubeCloud;
5756
}
5857

5958
@Override
@@ -84,16 +83,4 @@ public Map<String, String> getBootstrapProperties() {
8483
return bootstrapProperties;
8584
}
8685

87-
@Override
88-
public String getServerLabel() {
89-
if (isSonarQubeCloud()) {
90-
return "SonarQube Cloud";
91-
}
92-
93-
String version = getServerVersion();
94-
if (VersionUtils.compareMajor(version, 10) <= 0 || VersionUtils.compareMajor(version, 2025) >= 0) {
95-
return "SonarQube Server";
96-
}
97-
return "SonarQube Community Build";
98-
}
9986
}

lib/src/main/java/org/sonarsource/scanner/lib/internal/facade/forked/NewScannerEngineFacade.java

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -26,12 +26,20 @@
2626
public class NewScannerEngineFacade extends AbstractScannerEngineFacade {
2727
private final ScannerEngineLauncher launcher;
2828

29-
public NewScannerEngineFacade(Map<String, String> bootstrapProperties, ScannerEngineLauncher launcher,
30-
boolean isSonarCloud, @Nullable String serverVersion) {
31-
super(bootstrapProperties, isSonarCloud, serverVersion, launcher.isEngineCacheHit(), launcher.getJreCacheHit());
29+
private NewScannerEngineFacade(Map<String, String> bootstrapProperties, ScannerEngineLauncher launcher,
30+
boolean isSonarQubeCloud, @Nullable String serverVersion) {
31+
super(bootstrapProperties, isSonarQubeCloud, serverVersion, launcher.isEngineCacheHit(), launcher.getJreCacheHit());
3232
this.launcher = launcher;
3333
}
3434

35+
public static NewScannerEngineFacade forSonarQubeCloud(Map<String, String> bootstrapProperties, ScannerEngineLauncher launcher) {
36+
return new NewScannerEngineFacade(bootstrapProperties, launcher, true, null);
37+
}
38+
39+
public static NewScannerEngineFacade forSonarQubeServer(Map<String, String> bootstrapProperties, ScannerEngineLauncher launcher, String serverVersion) {
40+
return new NewScannerEngineFacade(bootstrapProperties, launcher, false, serverVersion);
41+
}
42+
3543
@Override
3644
protected boolean doAnalyze(Map<String, String> allProps) {
3745
return launcher.execute(allProps);

0 commit comments

Comments
 (0)