1
1
/*
2
- * Orchestrator
2
+ * Orchestrator Configuration
3
3
* Copyright (C) 2011-2025 SonarSource SA
4
4
* mailto:info AT sonarsource DOT com
5
5
*
19
19
*/
20
20
package com .sonar .orchestrator .config ;
21
21
22
- import com .sonar .orchestrator .locator .ArtifactoryFactory ;
23
- import com .sonar .orchestrator .locator .FileLocation ;
24
- import com .sonar .orchestrator .locator .Locators ;
25
22
import java .io .File ;
26
23
import java .net .MalformedURLException ;
27
24
import java .net .URI ;
25
+ import java .nio .file .Files ;
26
+ import java .nio .file .Path ;
28
27
import java .util .Collections ;
29
28
import java .util .HashMap ;
30
29
import java .util .Map ;
35
34
import javax .annotation .CheckForNull ;
36
35
import javax .annotation .Nullable ;
37
36
import org .apache .commons .io .IOUtils ;
38
- import org .apache .commons .lang . text .StrSubstitutor ;
37
+ import org .apache .commons .text .StringSubstitutor ;
39
38
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 ;
44
39
import static java .nio .charset .StandardCharsets .UTF_8 ;
45
- import static java .util .Objects .requireNonNull ;
46
40
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 ;
47
43
48
44
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" ;
51
45
52
46
private final Map <String , String > props ;
53
47
private final FileSystem fileSystem ;
54
- private final Locators locators ;
55
48
56
- private Configuration (File homeDir , Map <String , String > props ) {
49
+ private Configuration (Path homeDir , Map <String , String > props ) {
57
50
this .props = Collections .unmodifiableMap (new HashMap <>(props ));
58
51
this .fileSystem = new FileSystem (homeDir , this );
59
- this .locators = new Locators (this .fileSystem , ArtifactoryFactory .createArtifactory (this ));
60
52
}
61
53
62
- public FileSystem fileSystem () {
63
- return fileSystem ;
54
+ public static Configuration createEnv () {
55
+ return builder (). addEnvVariables (). addSystemProperties (). build () ;
64
56
}
65
57
66
- public Locators locators ( ) {
67
- return locators ;
58
+ public static Configuration create ( Properties properties ) {
59
+ return builder (). addProperties ( properties ). build () ;
68
60
}
69
61
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
+ }
88
69
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
+ }
92
73
93
- return FileLocation .of (new File (rootDir , relativePath ));
74
+ public FileSystem fileSystem () {
75
+ return fileSystem ;
94
76
}
95
77
96
78
public String getString (String key ) {
@@ -102,7 +84,7 @@ public String getString(String key) {
102
84
}
103
85
104
86
public String getString (String key , @ Nullable String defaultValue ) {
105
- return defaultIfNull (props .get (key ), defaultValue );
87
+ return getIfNull (props .get (key ), defaultValue );
106
88
}
107
89
108
90
@ CheckForNull
@@ -133,32 +115,24 @@ public Map<String, String> asMap() {
133
115
return props ;
134
116
}
135
117
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
-
156
118
public static final class Builder {
157
- private Map <String , String > props = new HashMap <>();
119
+ private final Map <String , String > props = new HashMap <>();
158
120
159
121
private Builder () {
160
122
}
161
123
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
+
162
136
public Builder addConfiguration (Configuration c ) {
163
137
return addMap (c .asMap ());
164
138
}
@@ -180,9 +154,7 @@ public Builder addProperties(Properties p) {
180
154
}
181
155
182
156
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 );
186
158
return this ;
187
159
}
188
160
@@ -198,30 +170,30 @@ public Builder setProperty(String key, @Nullable String value) {
198
170
return this ;
199
171
}
200
172
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 ());
203
175
return this ;
204
176
}
205
177
206
- private File loadProperties () {
207
- File homeDir = Stream .of (
178
+ private Path loadProperties () {
179
+ Path homeDir = Stream .of (
208
180
props .get ("orchestrator.home" ),
209
181
props .get ("ORCHESTRATOR_HOME" ),
210
182
props .get ("SONAR_USER_HOME" ))
211
183
.filter (s -> !isEmpty (s ))
212
184
.findFirst ()
213
- .map (File :: new )
214
- .orElse (new File ( getUserDirectory (), ".sonar/orchestrator" ));
185
+ .map (Path :: of )
186
+ .orElse (getUserDirectory (). toPath (). resolve ( ".sonar/orchestrator" ));
215
187
216
188
String configUrl = Stream .of (
217
189
props .get ("orchestrator.configUrl" ),
218
190
props .get ("ORCHESTRATOR_CONFIG_URL" ))
219
191
.filter (s -> !isEmpty (s ))
220
192
.findFirst ()
221
193
.orElseGet (() -> {
222
- File file = new File ( homeDir , "orchestrator.properties" );
194
+ Path file = homeDir . resolve ( "orchestrator.properties" );
223
195
try {
224
- return file .exists () ? file .getAbsoluteFile ().toURI ().toURL ().toString () : null ;
196
+ return Files .exists (file ) ? file .toAbsolutePath ().toUri ().toURL ().toString () : null ;
225
197
} catch (MalformedURLException e ) {
226
198
throw new IllegalStateException ("Unable to read configuration file" , e );
227
199
}
@@ -245,20 +217,8 @@ private File loadProperties() {
245
217
return homeDir ;
246
218
}
247
219
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
-
260
220
public Configuration build () {
261
- File homeDir = loadProperties ();
221
+ Path homeDir = loadProperties ();
262
222
Map <String , String > interpolatedProperties = interpolateProperties (props );
263
223
return new Configuration (homeDir , interpolatedProperties );
264
224
}
0 commit comments