Automatically register directories for registered resource hints

When a hint such as `graphql/*.*` is registered for resources that are
looked up via classpath scanning using a pattern such as
`classpath*:graphql/**/*.graphqls`, an appropriate pattern is in fact
registered in the generated `resource-config.json` file for GraalVM
native images; however, classpath scanning fails since GraalVM
currently does not make the `graphql` directory automatically available
as a classpath resource.

This can be very confusing and cumbersome for users since a file such
as `graphql/schema.graphqls` will not be discovered via classpath
scanning even though the file is present in the native image filesystem.

To address this, this commit automatically registers resource hints for
enclosing directories for a registered pattern.

If the GraalVM team later decides to perform automatic directory
registration, we can then remove the code introduced in conjunction
with this issue.

Closes gh-29403
This commit is contained in:
Sam Brannen
2022-10-30 18:37:02 +01:00
parent d03102edc3
commit 29f085bd1a
7 changed files with 123 additions and 37 deletions

View File

@@ -31,6 +31,7 @@ import org.springframework.lang.Nullable;
*
* @author Stephane Nicoll
* @author Brian Clozel
* @author Sam Brannen
* @since 6.0
*/
public final class ResourcePatternHints {
@@ -81,12 +82,57 @@ public final class ResourcePatternHints {
* @return {@code this}, to facilitate method chaining
*/
public Builder includes(@Nullable TypeReference reachableType, String... includes) {
List<ResourcePatternHint> newIncludes = Arrays.stream(includes)
.map(include -> new ResourcePatternHint(include, reachableType)).toList();
this.includes.addAll(newIncludes);
Arrays.stream(includes)
.map(this::expandToIncludeDirectories)
.flatMap(List::stream)
.map(include -> new ResourcePatternHint(include, reachableType))
.forEach(this.includes::add);
return this;
}
/**
* Expand the supplied include pattern into multiple patterns that include
* all parent directories for the ultimate resource or resources.
* <p>This is necessary to support classpath scanning within a GraalVM
* native image.
* @see <a href="https://github.com/spring-projects/spring-framework/issues/29403">gh-29403</a>
*/
private List<String> expandToIncludeDirectories(String includePattern) {
// Root resource or no explicit subdirectories?
if (!includePattern.contains("/")) {
if (includePattern.contains("*")) {
// If it's a root pattern, include the root directory as well as the pattern
return List.of("/", includePattern);
}
else {
// Include only the root resource
return List.of(includePattern);
}
}
List<String> includePatterns = new ArrayList<>();
// Ensure the original pattern is always included
includePatterns.add(includePattern);
StringBuilder path = new StringBuilder();
for (String pathElement : includePattern.split("/")) {
if (pathElement.isEmpty()) {
// Skip empty path elements
continue;
}
if (pathElement.contains("*")) {
// Stop at the first encountered wildcard, since we cannot reliably reason
// any further about the directory structure below this path element.
break;
}
if (!path.isEmpty()) {
path.append("/");
}
path.append(pathElement);
includePatterns.add(path.toString());
}
return includePatterns;
}
/**
* Include resources matching the specified patterns.
* @param includes the include patterns (see {@link ResourcePatternHint} documentation)