Skip to content

Commit

Permalink
Merge pull request #283 from strangelookingnerd/bugfix/JENKINS-73714
Browse files Browse the repository at this point in the history
[JENKINS-73714] Calling `FontAwesomeIcons#getAvailableIcons()` causes `ClosedFileSystemException`
  • Loading branch information
uhafner authored Sep 8, 2024
2 parents 3d36626 + f6a3c13 commit 6e253be
Showing 1 changed file with 22 additions and 27 deletions.
49 changes: 22 additions & 27 deletions src/main/java/io/jenkins/plugins/fontawesome/FontAwesomeIcons.java
Original file line number Diff line number Diff line change
Expand Up @@ -68,31 +68,7 @@ public static Map<String, String> getAvailableIcons(@CheckForNull final FontAwes
return getIconsFromClasspath(filter);
}

private static Map<String, String> getIconsFromClasspath(@CheckForNull final FontAwesomeStyle styleFilter) {
try {
return createIconStream(getIconFolder(), styleFilter)
.filter(Objects::nonNull)
.filter(icon -> icon.getFileName() != null)
.filter(FontAwesomeIcons::isSvgImage)
.filter(icon -> icon.getParent() != null)
.filter(icon -> icon.getParent().getFileName() != null)
.sorted()
.map(FontAwesomeIcons::createFileName)
.collect(Collectors.toMap(icon -> icon, FontAwesomeIcons::getIconClassName));
}
catch (IOException exception) {
throw new IllegalStateException("Unable to find icons: Resource unavailable.", exception);
}
}

private static Stream<Path> createIconStream(final Path iconFolder, @CheckForNull final FontAwesomeStyle filter) throws IOException {
if (filter == null) {
return Files.walk(iconFolder, 2);
}
return Files.walk(iconFolder.resolve(filter.name().toLowerCase(Locale.ENGLISH)), 1);
}

private static Path getIconFolder() {
private static Map<String, String> getIconsFromClasspath(@CheckForNull final FontAwesomeStyle filter) {
try {
Enumeration<URL> urls = FontAwesomeIcons.class.getClassLoader().getResources(IMAGES_SYMBOLS_PATH);

Expand All @@ -104,10 +80,10 @@ private static Path getIconFolder() {

if (StringUtils.equals(uri.getScheme(), "jar")) {
try (FileSystem fileSystem = FileSystems.newFileSystem(uri, Collections.emptyMap())) {
return fileSystem.getPath(IMAGES_SYMBOLS_PATH);
return filterIcons(fileSystem.getPath(IMAGES_SYMBOLS_PATH), filter);
}
}
return Paths.get(uri);
return filterIcons(Paths.get(uri), filter);
}
}
}
Expand All @@ -117,6 +93,25 @@ private static Path getIconFolder() {
throw new IllegalStateException("Unable to find icons: Resource unavailable.");
}

private static Map<String, String> filterIcons(final Path iconFolder, @CheckForNull final FontAwesomeStyle filter) throws IOException {
return createIconStream(iconFolder, filter)
.filter(Objects::nonNull)
.filter(icon -> icon.getFileName() != null)
.filter(FontAwesomeIcons::isSvgImage)
.filter(icon -> icon.getParent() != null)
.filter(icon -> icon.getParent().getFileName() != null)
.sorted()
.map(FontAwesomeIcons::createFileName)
.collect(Collectors.toMap(icon -> icon, FontAwesomeIcons::getIconClassName));
}

private static Stream<Path> createIconStream(final Path iconFolder, @CheckForNull final FontAwesomeStyle filter) throws IOException {
if (filter == null) {
return Files.walk(iconFolder, 2);
}
return Files.walk(iconFolder.resolve(filter.name().toLowerCase(Locale.ENGLISH)), 1);
}

private static String createFileName(final Path icon) {
return icon.getParent().getFileName() + "/"
+ StringUtils.removeEnd(icon.getFileName().toString(), SVG_FILE_ENDING);
Expand Down

0 comments on commit 6e253be

Please sign in to comment.