Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Gradle plugin: use full URI for configuration source locations #36803

Merged
merged 1 commit into from
Nov 2, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLClassLoader;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
Expand Down Expand Up @@ -139,21 +141,26 @@ List<URL> applicationPropsSources() {

static void configSourcesForApplicationProperties(Set<File> sourceDirectories, Consumer<URL> sourceUrls,
Consumer<ConfigSource> configSourceConsumer, int ordinal, String[] fileExtensions) {
URL[] resourceUrls = sourceDirectories.stream().map(File::toURI)
.map(u -> {
try {
return u.toURL();
} catch (MalformedURLException e) {
throw new RuntimeException(e);
}
})
.toArray(URL[]::new);

for (URL resourceUrl : resourceUrls) {
URLClassLoader classLoader = new URLClassLoader(new URL[] { resourceUrl });
CombinedConfigSourceProvider configSourceProvider = new CombinedConfigSourceProvider(sourceUrls, ordinal,
fileExtensions);
configSourceProvider.getConfigSources(classLoader).forEach(configSourceConsumer);
for (var sourceDir : sourceDirectories) {
var sourceDirPath = sourceDir.toPath();
var locations = new ArrayList<String>();
for (String file : fileExtensions) {
Path resolved = sourceDirPath.resolve(file);
if (Files.exists(resolved)) {
locations.add(resolved.toUri().toString());
}
}
if (!locations.isEmpty()) {
URLClassLoader classLoader;
try {
classLoader = new URLClassLoader(new URL[] { sourceDir.toURI().toURL() });
} catch (MalformedURLException e) {
throw new RuntimeException(e);
}
CombinedConfigSourceProvider configSourceProvider = new CombinedConfigSourceProvider(sourceUrls, ordinal,
fileExtensions, locations);
configSourceProvider.getConfigSources(classLoader).forEach(configSourceConsumer);
}
}
}

Expand Down Expand Up @@ -204,11 +211,13 @@ static final class CombinedConfigSourceProvider extends AbstractLocationConfigSo
private final Consumer<URL> sourceUrls;
private final int ordinal;
private final String[] fileExtensions;
private final List<String> locations;

CombinedConfigSourceProvider(Consumer<URL> sourceUrls, int ordinal, String[] fileExtensions) {
CombinedConfigSourceProvider(Consumer<URL> sourceUrls, int ordinal, String[] fileExtensions, List<String> locations) {
this.sourceUrls = sourceUrls;
this.ordinal = ordinal;
this.fileExtensions = fileExtensions;
this.locations = locations;
}

@Override
Expand All @@ -225,8 +234,7 @@ protected ConfigSource loadConfigSource(final URL url, final int ordinal) throws

@Override
public List<ConfigSource> getConfigSources(final ClassLoader classLoader) {
// Note:
return loadConfigSources(getFileExtensions(), ordinal, classLoader);
return loadConfigSources(locations.toArray(new String[0]), ordinal, classLoader);
}
}
}
Loading