Skip to content

Commit a8d9090

Browse files
committed
SCANJLIB-204 Rework the API and add isSonarCloud()
1 parent 3f69d50 commit a8d9090

11 files changed

Lines changed: 438 additions & 472 deletions

File tree

its/it-simple-scanner/src/main/java/com/sonar/scanner/lib/it/Main.java

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -21,8 +21,8 @@
2121

2222
import java.util.HashMap;
2323
import java.util.Map;
24-
import org.sonarsource.scanner.lib.EmbeddedScanner;
2524
import org.sonarsource.scanner.lib.LogOutput;
25+
import org.sonarsource.scanner.lib.ScannerEngineBootstrapper;
2626
import org.sonarsource.scanner.lib.StdOutLogOutput;
2727

2828
public class Main {
@@ -43,11 +43,12 @@ public static void main(String[] args) {
4343
System.exit(0);
4444
}
4545

46-
private static void runProject(Map<String, String> props) {
46+
private static void runProject(Map<String, String> props) throws Exception {
4747
LogOutput logOutput = new StdOutLogOutput();
48-
EmbeddedScanner scanner = EmbeddedScanner.create("Simple Scanner", "1.0", logOutput);
49-
scanner.addGlobalProperties(props);
50-
scanner.start();
51-
scanner.execute(props);
48+
try (var scannerEngine = new ScannerEngineBootstrapper("Simple Scanner", "1.0", logOutput)
49+
.addBootstrapProperties(props)
50+
.bootstrap()) {
51+
scannerEngine.analyze(props);
52+
}
5253
}
5354
}

lib/pom.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@
4444
<!-- unit tests -->
4545
<dependency>
4646
<groupId>org.junit.jupiter</groupId>
47-
<artifactId>junit-jupiter-engine</artifactId>
47+
<artifactId>junit-jupiter-engine</artifactId>
4848
<scope>test</scope>
4949
</dependency>
5050
<dependency>

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

Lines changed: 0 additions & 188 deletions
This file was deleted.

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

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,6 @@
1919
*/
2020
package org.sonarsource.scanner.lib;
2121

22-
@FunctionalInterface
2322
public interface LogOutput {
2423

2524
void log(String formattedMessage, Level level);
Lines changed: 115 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,115 @@
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

Comments
 (0)