Skip to content

Commit

Permalink
make BundlesParser Windows compatible (a jar file uses a ZipFileSyste…
Browse files Browse the repository at this point in the history
…m that is completely different esp. for file matching)
  • Loading branch information
Stefan Hässig committed Nov 23, 2023
1 parent b52b369 commit 7e5785c
Showing 1 changed file with 27 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,12 @@
import com.google.gson.Gson;
import com.google.gson.JsonIOException;
import com.google.gson.JsonSyntaxException;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.BufferedReader;
import java.io.File;
import java.io.IOException;
import java.io.InputStreamReader;
import java.lang.invoke.MethodHandles;
Expand Down Expand Up @@ -50,12 +52,13 @@ private Stream<Bundle> create(final BundlesConfig bundlesConfig) {

private Stream<Bundle> create(final String bundlesDirName, final String path) {
try {
final String filePattern = path + "/**/" + bundlesDirName + "/*.json";
final URI uri = getClass().getClassLoader().getResource(path).toURI();
final FileSystem fileSystem = getFileSystem(uri);
final String filePattern = getFilePattern(fileSystem, bundlesDirName, path);
LOG.info("Load image bundle definitions which match pattern '{}'", filePattern);
final PathMatcher matcher = FileSystems.getDefault().getPathMatcher("glob:" + filePattern);
final PathMatcher matcher = fileSystem.getPathMatcher("glob:" + filePattern);
final Stream.Builder<Stream<Bundle>> bundles = Stream.builder();

final URI uri = getClass().getClassLoader().getResource(path).toURI();
processResource(uri, p -> Files.walkFileTree(p, new SimpleFileVisitor<>() {
@Override
public FileVisitResult visitFile(final Path file, final BasicFileAttributes attrs) {
Expand All @@ -72,6 +75,14 @@ public FileVisitResult visitFile(final Path file, final BasicFileAttributes attr
}
}

private String getFilePattern(final FileSystem fileSystem, final String bundlesDirName, final String path) {
String filePattern = path + "/**/" + bundlesDirName + "/*.json";
if (fileSystem.getSeparator().equals(File.separator)) {
return StringUtils.removeStart(filePattern, '/');
}
return filePattern;
}

private void processResource(final URI uri, final IOConsumer<Path> consumer) throws IOException {
try {
consumer.accept(Paths.get(uri));
Expand All @@ -95,7 +106,19 @@ private Stream<Bundle> create(final Path bundleJson) {
}
}

interface IOConsumer<T> {
private FileSystem getFileSystem(final URI uri) {
try {
return FileSystems.getFileSystem(uri);
} catch (Exception e) {
try {
return FileSystems.newFileSystem(uri, Collections.emptyMap());
} catch (Exception i) {
return FileSystems.getDefault();
}
}
}

interface IOConsumer<T> {
void accept(T t) throws IOException;
}
}

0 comments on commit 7e5785c

Please sign in to comment.