Skip to content

Commit 856dcd4

Browse files
committed
Extract properties as constants
1 parent a973502 commit 856dcd4

7 files changed

Lines changed: 73 additions & 46 deletions

File tree

lib/src/main/java/org/sonarsource/scanner/lib/ScanProperties.java renamed to lib/src/main/java/org/sonarsource/scanner/lib/AnalysisProperties.java

Lines changed: 15 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -22,55 +22,52 @@
2222
/**
2323
* Most commonly used properties for a SonarQube analysis. These properties are passed to EmbeddedScanner#runAnalysis(java.util.Properties).
2424
* See <a href="http://docs.sonarqube.org/display/SONAR/Analysis+Parameters">documentation</a> for more properties.
25-
*
26-
* @since 2.2
2725
*/
28-
public interface ScanProperties {
26+
public final class AnalysisProperties {
27+
28+
private AnalysisProperties() {
29+
// only constants
30+
}
2931

3032
/**
3133
* Required project key
3234
*/
33-
String PROJECT_KEY = "sonar.projectKey";
35+
public static final String PROJECT_KEY = "sonar.projectKey";
3436

3537
/**
3638
* Used to define the exact key of each module.
3739
* If {@link #PROJECT_KEY} is used instead on a module, then final key of the module will be &lt;parent module key&gt;:&lt;PROJECT_KEY&gt;.
3840
* @since SonarQube 4.1
3941
*/
40-
String MODULE_KEY = "sonar.moduleKey";
42+
public static final String MODULE_KEY = "sonar.moduleKey";
4143

42-
String PROJECT_NAME = "sonar.projectName";
44+
public static final String PROJECT_NAME = "sonar.projectName";
4345

44-
String PROJECT_VERSION = "sonar.projectVersion";
46+
public static final String PROJECT_VERSION = "sonar.projectVersion";
4547

4648
/**
4749
* Optional description
4850
*/
49-
String PROJECT_DESCRIPTION = "sonar.projectDescription";
51+
public static final String PROJECT_DESCRIPTION = "sonar.projectDescription";
5052

5153
/**
5254
* Required paths to source directories, separated by commas, for example: "srcDir1,srcDir2"
5355
*/
54-
String PROJECT_SOURCE_DIRS = "sonar.sources";
56+
public static final String PROJECT_SOURCE_DIRS = "sonar.sources";
5557

5658
/**
5759
* Optional paths to test directories, separated by commas, for example: "testDir1,testDir2"
5860
*/
59-
String PROJECT_TEST_DIRS = "sonar.tests";
61+
public static final String PROJECT_TEST_DIRS = "sonar.tests";
6062

6163
/**
6264
* Property used to specify the base directory of the project to analyse. Default is ".".
6365
*/
64-
String PROJECT_BASEDIR = "sonar.projectBaseDir";
65-
66-
/**
67-
* Encoding of source and test files. By default it's the platform encoding.
68-
*/
69-
String PROJECT_SOURCE_ENCODING = "sonar.sourceEncoding";
66+
public static final String PROJECT_BASEDIR = "sonar.projectBaseDir";
7067

7168
/**
72-
* Skip analysis.
69+
* Encoding of source and test files. By default, it's the platform encoding.
7370
*/
74-
String SKIP = "sonar.scanner.skip";
71+
public static final String PROJECT_SOURCE_ENCODING = "sonar.sourceEncoding";
7572

7673
}

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -35,12 +35,12 @@ class Dirs {
3535
}
3636

3737
void init(Map<String, String> p) {
38-
String pathString = Optional.ofNullable(p.get(ScanProperties.PROJECT_BASEDIR)).orElse("");
38+
String pathString = Optional.ofNullable(p.get(AnalysisProperties.PROJECT_BASEDIR)).orElse("");
3939
Path absoluteProjectPath = Paths.get(pathString).toAbsolutePath().normalize();
4040
if (!Files.isDirectory(absoluteProjectPath)) {
4141
throw new IllegalStateException("Project home must be an existing directory: " + pathString);
4242
}
43-
p.put(ScanProperties.PROJECT_BASEDIR, absoluteProjectPath.toString());
43+
p.put(AnalysisProperties.PROJECT_BASEDIR, absoluteProjectPath.toString());
4444

4545
Path workDirPath;
4646
pathString = Optional.ofNullable(p.get(ScannerProperties.WORK_DIR)).orElse("");

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ public static Map<String, String> load(LogOutput logger) {
4646
return load(System.getenv(), logger);
4747
}
4848

49-
static Map<String, String> load(Map<String, String> env, LogOutput logger) {
49+
public static Map<String, String> load(Map<String, String> env, LogOutput logger) {
5050
var loadedProps = new HashMap<String, String>();
5151
Optional.ofNullable(env.get(SONAR_HOST_URL_ENV_VAR)).ifPresent(url -> loadedProps.put(ScannerProperties.HOST_URL, url));
5252
Optional.ofNullable(env.get(SONAR_USER_HOME_ENV_VAR)).ifPresent(path -> loadedProps.put(ScannerProperties.SONAR_USER_HOME, path));

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -68,12 +68,12 @@ private void initAnalysisProperties(Map<String, String> p) {
6868
}
6969

7070
private void initSourceEncoding(Map<String, String> p) {
71-
String sourceEncoding = Optional.ofNullable(p.get(ScanProperties.PROJECT_SOURCE_ENCODING)).orElse("");
71+
String sourceEncoding = Optional.ofNullable(p.get(AnalysisProperties.PROJECT_SOURCE_ENCODING)).orElse("");
7272
boolean platformDependent = false;
7373
if ("".equals(sourceEncoding)) {
7474
sourceEncoding = Charset.defaultCharset().name();
7575
platformDependent = true;
76-
p.put(ScanProperties.PROJECT_SOURCE_ENCODING, sourceEncoding);
76+
p.put(AnalysisProperties.PROJECT_SOURCE_ENCODING, sourceEncoding);
7777
}
7878
logger.info("Default locale: \"" + Locale.getDefault() + "\", source code encoding: \"" + sourceEncoding + "\""
7979
+ (platformDependent ? " (analysis is platform dependent)" : ""));

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

Lines changed: 32 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -22,38 +22,61 @@
2222
/**
2323
* Mostly used properties that can be passed to EmbeddedScanner#addGlobalProperties(java.util.Properties).
2424
* See <a href="https://docs.sonarqube.org/latest/analysis/analysis-parameters/">documentation</a> for more properties.
25-
*
26-
* @since 2.2
2725
*/
28-
public interface ScannerProperties {
26+
public final class ScannerProperties {
27+
28+
private ScannerProperties() {
29+
// only constants
30+
}
31+
2932
/**
3033
* URL of the Sonar server, default to SonarCloud
3134
*/
32-
String HOST_URL = "sonar.host.url";
35+
public static final String HOST_URL = "sonar.host.url";
3336

3437
/**
3538
* Working directory containing generated reports and temporary data.
3639
*/
37-
String WORK_DIR = "sonar.working.directory";
40+
public static final String WORK_DIR = "sonar.working.directory";
3841

3942
/**
4043
* Base dir for various locations (cache, SSL, …). Default to ~/.sonar
4144
*/
42-
String SONAR_USER_HOME = "sonar.userHome";
45+
public static final String SONAR_USER_HOME = "sonar.userHome";
4346

4447
/**
4548
* Authentication token for connecting to the Sonar server.
4649
*/
47-
String SONAR_TOKEN = "sonar.token";
50+
public static final String SONAR_TOKEN = "sonar.token";
4851

4952
/**
5053
* Authentication username for connecting to the Sonar server.
5154
*/
52-
String SONAR_LOGIN = "sonar.login";
55+
public static final String SONAR_LOGIN = "sonar.login";
5356

5457
/**
5558
* Authentication password for connecting to the Sonar server.
5659
*/
57-
String SONAR_PASSWORD = "sonar.password";
60+
public static final String SONAR_PASSWORD = "sonar.password";
61+
62+
/**
63+
* HTTP client properties
64+
*/
65+
public static final String SONAR_SCANNER_PROXY_PORT = "sonar.scanner.proxyPort";
66+
public static final String SONAR_SCANNER_CONNECT_TIMEOUT = "sonar.scanner.connectTimeout";
67+
public static final String SONAR_SCANNER_SOCKET_TIMEOUT = "sonar.scanner.socketTimeout";
68+
public static final String SONAR_SCANNER_RESPONSE_TIMEOUT = "sonar.scanner.responseTimeout";
69+
public static final String SONAR_SCANNER_PROXY_HOST = "sonar.scanner.proxyHost";
70+
public static final String SONAR_SCANNER_PROXY_USER = "sonar.scanner.proxyUser";
71+
public static final String SONAR_SCANNER_PROXY_PASSWORD = "sonar.scanner.proxyPassword";
72+
public static final String SONAR_SCANNER_KEYSTORE_PATH = "sonar.scanner.keystorePath";
73+
public static final String SONAR_SCANNER_KEYSTORE_PASSWORD = "sonar.scanner.keystorePassword";
74+
public static final String SONAR_SCANNER_TRUSTSTORE_PATH = "sonar.scanner.truststorePath";
75+
public static final String SONAR_SCANNER_TRUSTSTORE_PASSWORD = "sonar.scanner.truststorePassword";
76+
77+
/**
78+
* Skip analysis.
79+
*/
80+
public static final String SKIP = "sonar.scanner.skip";
5881

5982
}

lib/src/main/java/org/sonarsource/scanner/lib/internal/http/OkHttpClientFactory.java

Lines changed: 18 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,17 @@
4545
import static java.util.Arrays.asList;
4646
import static org.apache.commons.lang3.StringUtils.defaultIfBlank;
4747
import static org.apache.commons.lang3.StringUtils.isNotBlank;
48+
import static org.sonarsource.scanner.lib.ScannerProperties.SONAR_SCANNER_CONNECT_TIMEOUT;
49+
import static org.sonarsource.scanner.lib.ScannerProperties.SONAR_SCANNER_KEYSTORE_PASSWORD;
50+
import static org.sonarsource.scanner.lib.ScannerProperties.SONAR_SCANNER_KEYSTORE_PATH;
51+
import static org.sonarsource.scanner.lib.ScannerProperties.SONAR_SCANNER_PROXY_HOST;
52+
import static org.sonarsource.scanner.lib.ScannerProperties.SONAR_SCANNER_PROXY_PASSWORD;
53+
import static org.sonarsource.scanner.lib.ScannerProperties.SONAR_SCANNER_PROXY_PORT;
54+
import static org.sonarsource.scanner.lib.ScannerProperties.SONAR_SCANNER_PROXY_USER;
55+
import static org.sonarsource.scanner.lib.ScannerProperties.SONAR_SCANNER_RESPONSE_TIMEOUT;
56+
import static org.sonarsource.scanner.lib.ScannerProperties.SONAR_SCANNER_SOCKET_TIMEOUT;
57+
import static org.sonarsource.scanner.lib.ScannerProperties.SONAR_SCANNER_TRUSTSTORE_PASSWORD;
58+
import static org.sonarsource.scanner.lib.ScannerProperties.SONAR_SCANNER_TRUSTSTORE_PATH;
4859

4960
public class OkHttpClientFactory {
5061

@@ -57,10 +68,6 @@ public class OkHttpClientFactory {
5768
static final int DEFAULT_RESPONSE_TIMEOUT = 0;
5869
static final String READ_TIMEOUT_SEC_PROPERTY = "sonar.ws.timeout";
5970
static final int DEFAULT_READ_TIMEOUT_SEC = 60;
60-
public static final String SONAR_SCANNER_PROXY_PORT = "sonar.scanner.proxyPort";
61-
public static final String SONAR_SCANNER_CONNECT_TIMEOUT = "sonar.scanner.connectTimeout";
62-
public static final String SONAR_SCANNER_SOCKET_TIMEOUT = "sonar.scanner.socketTimeout";
63-
public static final String SONAR_SCANNER_RESPONSE_TIMEOUT = "sonar.scanner.responseTimeout";
6471

6572
private OkHttpClientFactory() {
6673
// only statics
@@ -94,18 +101,18 @@ static OkHttpClient create(Map<String, String> bootstrapProperties, Path sonarUs
94101
okHttpClientBuilder.connectionSpecs(asList(tls, ConnectionSpec.CLEARTEXT));
95102

96103
// OkHttp detects 'http.proxyHost' java property already, so just focus on sonar properties
97-
String proxyHost = defaultIfBlank(bootstrapProperties.get("sonar.scanner.proxyHost"), null);
104+
String proxyHost = defaultIfBlank(bootstrapProperties.get(SONAR_SCANNER_PROXY_HOST), null);
98105
if (proxyHost != null) {
99106
var defaultProxyPort = bootstrapProperties.get(ScannerProperties.HOST_URL).startsWith("https") ? "443" : "80";
100107
String proxyPortStr = defaultIfBlank(bootstrapProperties.get(SONAR_SCANNER_PROXY_PORT), defaultProxyPort);
101108
var proxyPort = parseIntProperty(proxyPortStr, SONAR_SCANNER_PROXY_PORT);
102109
okHttpClientBuilder.proxy(new Proxy(Proxy.Type.HTTP, new InetSocketAddress(proxyHost, proxyPort)));
103110
}
104111

105-
var scannerProxyUser = bootstrapProperties.get("sonar.scanner.proxyUser");
112+
var scannerProxyUser = bootstrapProperties.get(SONAR_SCANNER_PROXY_USER);
106113
String proxyUser = scannerProxyUser != null ? scannerProxyUser : System.getProperty("http.proxyUser", "");
107114
if (isNotBlank(proxyUser)) {
108-
var scannerProxyPwd = bootstrapProperties.get("sonar.scanner.proxyPassword");
115+
var scannerProxyPwd = bootstrapProperties.get(SONAR_SCANNER_PROXY_PASSWORD);
109116
String proxyPassword = scannerProxyPwd != null ? scannerProxyPwd : System.getProperty("http.proxyPassword", "");
110117
okHttpClientBuilder.proxyAuthenticator((route, response) -> {
111118
if (response.request().header(PROXY_AUTHORIZATION) != null) {
@@ -144,10 +151,10 @@ private static int parseIntProperty(String propValue, String propKey) {
144151
}
145152

146153
private static SslConfig parseSslConfig(Map<String, String> bootstrapProperties, Path sonarUserHome) {
147-
var keyStorePath = defaultIfBlank(bootstrapProperties.get("sonar.scanner.keystorePath"), sonarUserHome.resolve("ssl/keystore.p12").toString());
148-
var keyStorePassword = defaultIfBlank(bootstrapProperties.get("sonar.scanner.keystorePassword"), CertificateStore.DEFAULT_PASSWORD);
149-
var trustStorePath = defaultIfBlank(bootstrapProperties.get("sonar.scanner.truststorePath"), sonarUserHome.resolve("ssl/truststore.p12").toString());
150-
var trustStorePassword = defaultIfBlank(bootstrapProperties.get("sonar.scanner.truststorePassword"), CertificateStore.DEFAULT_PASSWORD);
154+
var keyStorePath = defaultIfBlank(bootstrapProperties.get(SONAR_SCANNER_KEYSTORE_PATH), sonarUserHome.resolve("ssl/keystore.p12").toString());
155+
var keyStorePassword = defaultIfBlank(bootstrapProperties.get(SONAR_SCANNER_KEYSTORE_PASSWORD), CertificateStore.DEFAULT_PASSWORD);
156+
var trustStorePath = defaultIfBlank(bootstrapProperties.get(SONAR_SCANNER_TRUSTSTORE_PATH), sonarUserHome.resolve("ssl/truststore.p12").toString());
157+
var trustStorePassword = defaultIfBlank(bootstrapProperties.get(SONAR_SCANNER_TRUSTSTORE_PASSWORD), CertificateStore.DEFAULT_PASSWORD);
151158
var keyStore = new CertificateStore(Path.of(keyStorePath), keyStorePassword);
152159
var trustStore = new CertificateStore(Path.of(trustStorePath), trustStorePassword);
153160
return new SslConfig(keyStore, trustStore);

lib/src/test/java/org/sonarsource/scanner/lib/DirsTest.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ public class DirsTest {
4141
public void should_init_default_project_dirs() throws Exception {
4242
new Dirs(mock(Logger.class)).init(p);
4343

44-
File projectDir = new File(p.get(ScanProperties.PROJECT_BASEDIR));
44+
File projectDir = new File(p.get(AnalysisProperties.PROJECT_BASEDIR));
4545
File workDir = new File(p.get(ScannerProperties.WORK_DIR));
4646

4747
assertThat(projectDir).isNotNull().isDirectory();
@@ -56,10 +56,10 @@ public void should_init_default_project_dirs() throws Exception {
5656
public void should_set_relative_path_to_project_work_dir() throws Exception {
5757
File initialProjectDir = temp.getRoot();
5858
p.put(ScannerProperties.WORK_DIR, "relative/path");
59-
p.put(ScanProperties.PROJECT_BASEDIR, initialProjectDir.getAbsolutePath());
59+
p.put(AnalysisProperties.PROJECT_BASEDIR, initialProjectDir.getAbsolutePath());
6060
new Dirs(mock(Logger.class)).init(p);
6161

62-
File projectDir = new File(p.get(ScanProperties.PROJECT_BASEDIR));
62+
File projectDir = new File(p.get(AnalysisProperties.PROJECT_BASEDIR));
6363
File workDir = new File(p.get(ScannerProperties.WORK_DIR));
6464

6565
assertThat(projectDir).isNotNull().isDirectory();

0 commit comments

Comments
 (0)