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

Match any file extension from the acceptable list when loading profile locations #1026

Merged
merged 1 commit into from
Oct 11, 2023
Merged
Show file tree
Hide file tree
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 @@ -158,26 +158,28 @@ protected List<ConfigSource> tryJar(final URI uri, final int ordinal) {
}

protected List<ConfigSource> fallbackToUnknownProtocol(final URI uri, final int ordinal, final ClassLoader classLoader) {
final List<ConfigSource> configSources = new ArrayList<>();
List<ConfigSource> configSources = new ArrayList<>();
try {
Enumeration<URL> resources = classLoader.getResources(uri.toString());
while (resources.hasMoreElements()) {
final URL resourceUrl = resources.nextElement();
URL resourceUrl = resources.nextElement();
if (validExtension(resourceUrl.getFile())) {
final ConfigSource mainSource = addConfigSource(resourceUrl, ordinal, configSources);
ConfigSource mainSource = addConfigSource(resourceUrl, ordinal, configSources);
configSources.add(new ConfigurableConfigSource((ProfileConfigSourceFactory) profiles -> {
final List<ConfigSource> profileSources = new ArrayList<>();
List<ConfigSource> profileSources = new ArrayList<>();
for (int i = profiles.size() - 1; i >= 0; i--) {
final int mainOrdinal = mainSource.getOrdinal() + profiles.size() - i + 1;
final URI profileUri = addProfileName(uri, profiles.get(i));
try {
final Enumeration<URL> profileResources = classLoader.getResources(profileUri.toString());
while (profileResources.hasMoreElements()) {
final URL profileUrl = profileResources.nextElement();
addProfileConfigSource(profileUrl, mainOrdinal, profileSources);
int mainOrdinal = mainSource.getOrdinal() + profiles.size() - i + 1;
for (String fileExtension : getFileExtensions()) {
URI profileUri = addProfileName(uri, profiles.get(i), fileExtension);
try {
Enumeration<URL> profileResources = classLoader.getResources(profileUri.toString());
while (profileResources.hasMoreElements()) {
final URL profileUrl = profileResources.nextElement();
addProfileConfigSource(profileUrl, mainOrdinal, profileSources);
}
} catch (IOException e) {
// It is ok to not find the resource here, because it is an optional profile resource.
}
} catch (IOException e) {
// It is ok to not find the resource here, because it is an optional profile resource.
}
}
return profileSources;
Expand All @@ -200,15 +202,18 @@ protected List<ConfigSource> tryHttpResource(final URI uri, final int ordinal) {
}

protected List<ConfigSource> tryProfiles(final URI uri, final ConfigSource mainSource) {
final List<ConfigSource> configSources = new ArrayList<>();
List<ConfigSource> configSources = new ArrayList<>();
configSources.add(new ConfigurableConfigSource(new ProfileConfigSourceFactory() {
@Override
public Iterable<ConfigSource> getProfileConfigSources(final List<String> profiles) {
final List<ConfigSource> profileSources = new ArrayList<>();
List<ConfigSource> profileSources = new ArrayList<>();
for (int i = profiles.size() - 1; i >= 0; i--) {
final int ordinal = mainSource.getOrdinal() + profiles.size() - i;
final URI profileUri = addProfileName(uri, profiles.get(i));
AbstractLocationConfigSourceLoader.this.addProfileConfigSource(toURL(profileUri), ordinal, profileSources);
int ordinal = mainSource.getOrdinal() + profiles.size() - i;
for (String fileExtension : getFileExtensions()) {
URI profileUri = addProfileName(uri, profiles.get(i), fileExtension);
AbstractLocationConfigSourceLoader.this.addProfileConfigSource(toURL(profileUri), ordinal,
profileSources);
}
}
return profileSources;
}
Expand Down Expand Up @@ -273,9 +278,9 @@ private boolean validExtension(final String resourceName) {
return false;
}

private static URI addProfileName(final URI uri, final String profile) {
private static URI addProfileName(final URI uri, final String profile, final String fileExtension) {
if ("jar".equals(uri.getScheme())) {
return URI.create("jar:" + addProfileName(URI.create(uri.getRawSchemeSpecificPart()), profile));
return URI.create("jar:" + addProfileName(URI.create(uri.getRawSchemeSpecificPart()), profile, fileExtension));
}

final String fileName = uri.getPath();
Expand All @@ -284,7 +289,7 @@ private static URI addProfileName(final URI uri, final String profile) {
final int dot = fileName.lastIndexOf(".");
final String fileNameProfile;
if (dot != -1 && dot != 0 && fileName.charAt(dot - 1) != '/') {
fileNameProfile = fileName.substring(0, dot) + "-" + profile + fileName.substring(dot);
fileNameProfile = fileName.substring(0, dot) + "-" + profile + "." + fileExtension;
} else {
fileNameProfile = fileName + "-" + profile;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -429,6 +429,40 @@ void illegalChars(@TempDir Path tempDir) throws Exception {
}
}

@Test
void mixedExtensions(@TempDir Path tempDir) throws Exception {
JavaArchive jar = ShrinkWrap
.create(JavaArchive.class, "resources.jar")
.addAsResource(new StringAsset("my:\n" +
" prop:\n" +
" one: 1234\n"), "resources.yml")
.addAsResource(new StringAsset("my:\n" +
" prop:\n" +
" one: 5678\n"), "resources-prod.yaml");

Path filePath = tempDir.resolve("resources.jar");
jar.as(ZipExporter.class).exportTo(filePath.toFile());

ClassLoader contextClassLoader = Thread.currentThread().getContextClassLoader();
try (URLClassLoader urlClassLoader = new URLClassLoader(new URL[] {
new URL("jar:" + filePath.toUri() + "!/")
}, contextClassLoader)) {
Thread.currentThread().setContextClassLoader(urlClassLoader);

SmallRyeConfig config = new SmallRyeConfigBuilder()
.addDiscoveredSources()
.addDefaultInterceptors()
.withProfile("prod")
.withDefaultValue(SMALLRYE_CONFIG_LOCATIONS, "jar:" + filePath.toUri() + "!/resources.yml")
.build();

assertEquals("5678", config.getRawValue("my.prop.one"));
assertEquals(2, countSources(config, YamlConfigSource.class));
} finally {
Thread.currentThread().setContextClassLoader(contextClassLoader);
}
}

private static URLClassLoader urlClassLoader(ClassLoader parent, String... urls) {
return new URLClassLoader(Stream.of(urls).map(spec -> {
try {
Expand Down