Skip to content

Commit ab9c8c2

Browse files
committed
Extract configuration to its own module for reusability
1 parent 454f142 commit ab9c8c2

File tree

68 files changed

+806
-620
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

68 files changed

+806
-620
lines changed

pom.xml

Lines changed: 30 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
<modules>
2323
<module>echo</module>
2424
<module>sonar-orchestrator</module>
25+
<module>sonar-orchestrator-config</module>
2526
<module>sonar-orchestrator-junit4</module>
2627
<module>sonar-orchestrator-junit5</module>
2728
</modules>
@@ -41,8 +42,6 @@
4142
<properties>
4243
<license.name>GNU LGPL v3</license.name>
4344
<version.surefire.plugin>3.5.2</version.surefire.plugin>
44-
<commonsIO.version>2.7</commonsIO.version>
45-
<commonsLang.version>2.6</commonsLang.version>
4645
<commonsExec.version>1.3</commonsExec.version>
4746
<guava.version>18.0</guava.version>
4847
<junit.version>4.13.2</junit.version>
@@ -54,9 +53,7 @@
5453

5554
<!-- used for deployment to SonarSource Artifactory -->
5655
<gitRepositoryName>orchestrator</gitRepositoryName>
57-
<maven.compiler.source>11</maven.compiler.source>
58-
<maven.compiler.target>8</maven.compiler.target>
59-
<maven.compiler.release>8</maven.compiler.release>
56+
<maven.compiler.release>11</maven.compiler.release>
6057
</properties>
6158

6259
<dependencyManagement>
@@ -66,16 +63,43 @@
6663
<artifactId>jsr305</artifactId>
6764
<version>3.0.1</version>
6865
</dependency>
66+
<dependency>
67+
<groupId>commons-io</groupId>
68+
<artifactId>commons-io</artifactId>
69+
<version>2.19.0</version>
70+
</dependency>
71+
<dependency>
72+
<groupId>org.apache.commons</groupId>
73+
<artifactId>commons-lang3</artifactId>
74+
<version>3.18.0</version>
75+
</dependency>
76+
<dependency>
77+
<groupId>org.apache.commons</groupId>
78+
<artifactId>commons-text</artifactId>
79+
<version>1.14.0</version>
80+
</dependency>
81+
<dependency>
82+
<groupId>org.junit</groupId>
83+
<artifactId>junit-bom</artifactId>
84+
<version>5.13.4</version>
85+
<type>pom</type>
86+
<scope>import</scope>
87+
</dependency>
6988
<dependency>
7089
<groupId>org.assertj</groupId>
7190
<artifactId>assertj-core</artifactId>
72-
<version>3.9.1</version>
91+
<version>3.27.3</version>
7392
</dependency>
7493
<dependency>
7594
<groupId>org.mockito</groupId>
7695
<artifactId>mockito-core</artifactId>
7796
<version>5.16.0</version>
7897
</dependency>
98+
<dependency>
99+
<groupId>org.wiremock</groupId>
100+
<artifactId>wiremock-standalone</artifactId>
101+
<version>3.13.1</version>
102+
</dependency>
79103
</dependencies>
80104
</dependencyManagement>
81105
<build>

sonar-orchestrator-config/pom.xml

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
3+
<modelVersion>4.0.0</modelVersion>
4+
<parent>
5+
<groupId>org.sonarsource.orchestrator</groupId>
6+
<artifactId>orchestrator-parent</artifactId>
7+
<version>5.6.2-SNAPSHOT</version>
8+
</parent>
9+
<artifactId>sonar-orchestrator-config</artifactId>
10+
<name>Orchestrator Configuration</name>
11+
12+
<dependencies>
13+
<dependency>
14+
<groupId>com.google.code.findbugs</groupId>
15+
<artifactId>jsr305</artifactId>
16+
<scope>provided</scope>
17+
</dependency>
18+
<dependency>
19+
<groupId>commons-io</groupId>
20+
<artifactId>commons-io</artifactId>
21+
</dependency>
22+
<dependency>
23+
<groupId>org.apache.commons</groupId>
24+
<artifactId>commons-lang3</artifactId>
25+
</dependency>
26+
<dependency>
27+
<groupId>org.apache.commons</groupId>
28+
<artifactId>commons-text</artifactId>
29+
</dependency>
30+
<dependency>
31+
<groupId>org.junit.jupiter</groupId>
32+
<artifactId>junit-jupiter</artifactId>
33+
<scope>test</scope>
34+
</dependency>
35+
<dependency>
36+
<groupId>org.assertj</groupId>
37+
<artifactId>assertj-core</artifactId>
38+
<scope>test</scope>
39+
</dependency>
40+
<dependency>
41+
<groupId>org.wiremock</groupId>
42+
<artifactId>wiremock-standalone</artifactId>
43+
<scope>test</scope>
44+
</dependency>
45+
</dependencies>
46+
47+
</project>

sonar-orchestrator/src/main/java/com/sonar/orchestrator/config/Configuration.java renamed to sonar-orchestrator-config/src/main/java/com/sonar/orchestrator/config/Configuration.java

Lines changed: 47 additions & 87 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Orchestrator
2+
* Orchestrator Configuration
33
* Copyright (C) 2011-2025 SonarSource SA
44
* mailto:info AT sonarsource DOT com
55
*
@@ -19,12 +19,11 @@
1919
*/
2020
package com.sonar.orchestrator.config;
2121

22-
import com.sonar.orchestrator.locator.ArtifactoryFactory;
23-
import com.sonar.orchestrator.locator.FileLocation;
24-
import com.sonar.orchestrator.locator.Locators;
2522
import java.io.File;
2623
import java.net.MalformedURLException;
2724
import java.net.URI;
25+
import java.nio.file.Files;
26+
import java.nio.file.Path;
2827
import java.util.Collections;
2928
import java.util.HashMap;
3029
import java.util.Map;
@@ -35,62 +34,45 @@
3534
import javax.annotation.CheckForNull;
3635
import javax.annotation.Nullable;
3736
import org.apache.commons.io.IOUtils;
38-
import org.apache.commons.lang.text.StrSubstitutor;
37+
import org.apache.commons.text.StringSubstitutor;
3938

40-
import static com.sonar.orchestrator.util.OrchestratorUtils.checkState;
41-
import static com.sonar.orchestrator.util.OrchestratorUtils.defaultIfNull;
42-
import static com.sonar.orchestrator.util.OrchestratorUtils.isEmpty;
43-
import static java.lang.String.format;
4439
import static java.nio.charset.StandardCharsets.UTF_8;
45-
import static java.util.Objects.requireNonNull;
4640
import static org.apache.commons.io.FileUtils.getUserDirectory;
41+
import static org.apache.commons.lang3.ObjectUtils.getIfNull;
42+
import static org.apache.commons.lang3.StringUtils.isEmpty;
4743

4844
public class Configuration {
49-
private static final String ENV_SHARED_DIR = "SONAR_IT_SOURCES";
50-
private static final String PROP_SHARED_DIR = "orchestrator.it_sources";
5145

5246
private final Map<String, String> props;
5347
private final FileSystem fileSystem;
54-
private final Locators locators;
5548

56-
private Configuration(File homeDir, Map<String, String> props) {
49+
private Configuration(Path homeDir, Map<String, String> props) {
5750
this.props = Collections.unmodifiableMap(new HashMap<>(props));
5851
this.fileSystem = new FileSystem(homeDir, this);
59-
this.locators = new Locators(this.fileSystem, ArtifactoryFactory.createArtifactory(this));
6052
}
6153

62-
public FileSystem fileSystem() {
63-
return fileSystem;
54+
public static Configuration createEnv() {
55+
return builder().addEnvVariables().addSystemProperties().build();
6456
}
6557

66-
public Locators locators() {
67-
return locators;
58+
public static Configuration create(Properties properties) {
59+
return builder().addProperties(properties).build();
6860
}
6961

70-
/**
71-
* File located in the shared directory defined by the system property orchestrator.it_sources or environment variable SONAR_IT_SOURCES.
72-
* Example : getFileLocationOfShared("javascript/performancing/pom.xml")
73-
*/
74-
public FileLocation getFileLocationOfShared(String relativePath) {
75-
// try to read it_sources
76-
// in the System.getProperties
77-
// in the prop file (from orchestrator.properties file)
78-
// in the environment variable
79-
String rootPath;
80-
rootPath = System.getProperty(PROP_SHARED_DIR);
81-
if (rootPath == null) {
82-
rootPath = props.get(PROP_SHARED_DIR);
83-
}
84-
if (rootPath == null) {
85-
rootPath = System.getenv(ENV_SHARED_DIR);
86-
}
87-
requireNonNull(rootPath, format("Property '%s' or environment variable '%s' is missing", PROP_SHARED_DIR, ENV_SHARED_DIR));
62+
public static Configuration create(Map<String, String> properties) {
63+
return builder().addProperties(properties).build();
64+
}
65+
66+
public static Configuration create() {
67+
return builder().build();
68+
}
8869

89-
File rootDir = new File(rootPath);
90-
checkState(rootDir.isDirectory() && rootDir.exists(),
91-
"Please check the definition of it_sources (%s or %s) because the directory does not exist: %s", PROP_SHARED_DIR, ENV_SHARED_DIR, rootDir);
70+
public static Builder builder() {
71+
return new Builder();
72+
}
9273

93-
return FileLocation.of(new File(rootDir, relativePath));
74+
public FileSystem fileSystem() {
75+
return fileSystem;
9476
}
9577

9678
public String getString(String key) {
@@ -102,7 +84,7 @@ public String getString(String key) {
10284
}
10385

10486
public String getString(String key, @Nullable String defaultValue) {
105-
return defaultIfNull(props.get(key), defaultValue);
87+
return getIfNull(props.get(key), defaultValue);
10688
}
10789

10890
@CheckForNull
@@ -133,32 +115,24 @@ public Map<String, String> asMap() {
133115
return props;
134116
}
135117

136-
public static Configuration createEnv() {
137-
return builder().addEnvVariables().addSystemProperties().build();
138-
}
139-
140-
public static Configuration create(Properties properties) {
141-
return builder().addProperties(properties).build();
142-
}
143-
144-
public static Configuration create(Map<String, String> properties) {
145-
return builder().addProperties(properties).build();
146-
}
147-
148-
public static Configuration create() {
149-
return builder().build();
150-
}
151-
152-
public static Builder builder() {
153-
return new Builder();
154-
}
155-
156118
public static final class Builder {
157-
private Map<String, String> props = new HashMap<>();
119+
private final Map<String, String> props = new HashMap<>();
158120

159121
private Builder() {
160122
}
161123

124+
private static Map<String, String> interpolateProperties(Map<String, String> map) {
125+
Map<String, String> copy = new HashMap<>();
126+
for (Map.Entry<String, String> entry : map.entrySet()) {
127+
copy.put(entry.getKey(), interpolate(entry.getValue(), map));
128+
}
129+
return copy;
130+
}
131+
132+
private static String interpolate(String prop, Map<String, String> with) {
133+
return StringSubstitutor.replace(prop, with, "${", "}");
134+
}
135+
162136
public Builder addConfiguration(Configuration c) {
163137
return addMap(c.asMap());
164138
}
@@ -180,9 +154,7 @@ public Builder addProperties(Properties p) {
180154
}
181155

182156
public Builder addProperties(Map<String, String> p) {
183-
for (Map.Entry<String, String> entry : p.entrySet()) {
184-
props.put(entry.getKey(), entry.getValue());
185-
}
157+
props.putAll(p);
186158
return this;
187159
}
188160

@@ -198,30 +170,30 @@ public Builder setProperty(String key, @Nullable String value) {
198170
return this;
199171
}
200172

201-
public Builder setProperty(String key, File file) {
202-
props.put(key, file.getAbsolutePath());
173+
public Builder setProperty(String key, Path file) {
174+
props.put(key, file.toAbsolutePath().toString());
203175
return this;
204176
}
205177

206-
private File loadProperties() {
207-
File homeDir = Stream.of(
178+
private Path loadProperties() {
179+
Path homeDir = Stream.of(
208180
props.get("orchestrator.home"),
209181
props.get("ORCHESTRATOR_HOME"),
210182
props.get("SONAR_USER_HOME"))
211183
.filter(s -> !isEmpty(s))
212184
.findFirst()
213-
.map(File::new)
214-
.orElse(new File(getUserDirectory(), ".sonar/orchestrator"));
185+
.map(Path::of)
186+
.orElse(getUserDirectory().toPath().resolve(".sonar/orchestrator"));
215187

216188
String configUrl = Stream.of(
217189
props.get("orchestrator.configUrl"),
218190
props.get("ORCHESTRATOR_CONFIG_URL"))
219191
.filter(s -> !isEmpty(s))
220192
.findFirst()
221193
.orElseGet(() -> {
222-
File file = new File(homeDir, "orchestrator.properties");
194+
Path file = homeDir.resolve("orchestrator.properties");
223195
try {
224-
return file.exists() ? file.getAbsoluteFile().toURI().toURL().toString() : null;
196+
return Files.exists(file) ? file.toAbsolutePath().toUri().toURL().toString() : null;
225197
} catch (MalformedURLException e) {
226198
throw new IllegalStateException("Unable to read configuration file", e);
227199
}
@@ -245,20 +217,8 @@ private File loadProperties() {
245217
return homeDir;
246218
}
247219

248-
private static Map<String, String> interpolateProperties(Map<String, String> map) {
249-
Map<String, String> copy = new HashMap<>();
250-
for (Map.Entry<String, String> entry : map.entrySet()) {
251-
copy.put(entry.getKey(), interpolate(entry.getValue(), map));
252-
}
253-
return copy;
254-
}
255-
256-
private static String interpolate(String prop, Map<String, String> with) {
257-
return StrSubstitutor.replace(prop, with, "${", "}");
258-
}
259-
260220
public Configuration build() {
261-
File homeDir = loadProperties();
221+
Path homeDir = loadProperties();
262222
Map<String, String> interpolatedProperties = interpolateProperties(props);
263223
return new Configuration(homeDir, interpolatedProperties);
264224
}

0 commit comments

Comments
 (0)