7
7
import java .net .MalformedURLException ;
8
8
import java .net .URL ;
9
9
import java .net .URLClassLoader ;
10
+ import java .nio .file .Files ;
11
+ import java .nio .file .Path ;
10
12
import java .util .ArrayList ;
11
13
import java .util .HashMap ;
12
14
import java .util .List ;
@@ -139,21 +141,26 @@ List<URL> applicationPropsSources() {
139
141
140
142
static void configSourcesForApplicationProperties (Set <File > sourceDirectories , Consumer <URL > sourceUrls ,
141
143
Consumer <ConfigSource > configSourceConsumer , int ordinal , String [] fileExtensions ) {
142
- URL [] resourceUrls = sourceDirectories .stream ().map (File ::toURI )
143
- .map (u -> {
144
- try {
145
- return u .toURL ();
146
- } catch (MalformedURLException e ) {
147
- throw new RuntimeException (e );
148
- }
149
- })
150
- .toArray (URL []::new );
151
-
152
- for (URL resourceUrl : resourceUrls ) {
153
- URLClassLoader classLoader = new URLClassLoader (new URL [] { resourceUrl });
154
- CombinedConfigSourceProvider configSourceProvider = new CombinedConfigSourceProvider (sourceUrls , ordinal ,
155
- fileExtensions );
156
- configSourceProvider .getConfigSources (classLoader ).forEach (configSourceConsumer );
144
+ for (var sourceDir : sourceDirectories ) {
145
+ var sourceDirPath = sourceDir .toPath ();
146
+ var locations = new ArrayList <String >();
147
+ for (String file : fileExtensions ) {
148
+ Path resolved = sourceDirPath .resolve (file );
149
+ if (Files .exists (resolved )) {
150
+ locations .add (resolved .toUri ().toString ());
151
+ }
152
+ }
153
+ if (!locations .isEmpty ()) {
154
+ URLClassLoader classLoader ;
155
+ try {
156
+ classLoader = new URLClassLoader (new URL [] { sourceDir .toURI ().toURL () });
157
+ } catch (MalformedURLException e ) {
158
+ throw new RuntimeException (e );
159
+ }
160
+ CombinedConfigSourceProvider configSourceProvider = new CombinedConfigSourceProvider (sourceUrls , ordinal ,
161
+ fileExtensions , locations );
162
+ configSourceProvider .getConfigSources (classLoader ).forEach (configSourceConsumer );
163
+ }
157
164
}
158
165
}
159
166
@@ -204,11 +211,13 @@ static final class CombinedConfigSourceProvider extends AbstractLocationConfigSo
204
211
private final Consumer <URL > sourceUrls ;
205
212
private final int ordinal ;
206
213
private final String [] fileExtensions ;
214
+ private final List <String > locations ;
207
215
208
- CombinedConfigSourceProvider (Consumer <URL > sourceUrls , int ordinal , String [] fileExtensions ) {
216
+ CombinedConfigSourceProvider (Consumer <URL > sourceUrls , int ordinal , String [] fileExtensions , List < String > locations ) {
209
217
this .sourceUrls = sourceUrls ;
210
218
this .ordinal = ordinal ;
211
219
this .fileExtensions = fileExtensions ;
220
+ this .locations = locations ;
212
221
}
213
222
214
223
@ Override
@@ -225,8 +234,7 @@ protected ConfigSource loadConfigSource(final URL url, final int ordinal) throws
225
234
226
235
@ Override
227
236
public List <ConfigSource > getConfigSources (final ClassLoader classLoader ) {
228
- // Note:
229
- return loadConfigSources (getFileExtensions (), ordinal , classLoader );
237
+ return loadConfigSources (locations .toArray (new String [0 ]), ordinal , classLoader );
230
238
}
231
239
}
232
240
}
0 commit comments