Skip to content

Commit

Permalink
More cleanup and removal of UnionFS implementation details.
Browse files Browse the repository at this point in the history
  • Loading branch information
LexManos committed Jan 9, 2024
1 parent f9c9208 commit 6f55169
Show file tree
Hide file tree
Showing 5 changed files with 156 additions and 206 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -19,28 +19,23 @@
import java.util.Map;
import java.util.Optional;

public class LayeredZipFileSystemProvider extends PathFileSystemProvider
{
public class LayeredZipFileSystemProvider extends PathFileSystemProvider {
public static final String SCHEME = "jij";
public static final String URI_SPLIT_REGEX = COMPONENT_SEPERATOR;


@Override
public String getScheme()
{
public String getScheme() {
return SCHEME;
}

@Override
public FileSystem newFileSystem(final URI uri, final Map<String, ?> env) throws IOException
{
public FileSystem newFileSystem(final URI uri, final Map<String, ?> env) throws IOException {
final String[] sections = uri.getRawSchemeSpecificPart().split(URI_SPLIT_REGEX);

FileSystem workingSystem = FileSystems.getDefault(); //Grab the normal disk FS.
String keyPrefix = "";

if (sections.length > 1)
{
if (sections.length > 1) {
final AdaptedURIWithPrefixSelection adaptedURI = adaptUriSections(sections);
keyPrefix = adaptedURI.getPrefix();
workingSystem = adaptedURI.getFileSystem();
Expand All @@ -50,26 +45,19 @@ public FileSystem newFileSystem(final URI uri, final Map<String, ?> env) throws
if (lastSection.startsWith("//"))
lastSection = lastSection.substring(2);

if (env.containsKey("packagePath"))
{ //User requests specific package as a target;
try
{
return super.newFileSystem(new URI(super.getScheme() + ":" + uri.getRawSchemeSpecificPart()),
env);
} catch (Exception e)
{
throw new UncheckedIOException("Failed to create intermediary FS.", new IOException("Failed to " +
"process data.",
e));
if (env.containsKey("packagePath")) { //User requests specific package as a target;
try {
return super.newFileSystem(new URI(super.getScheme() + ":" + uri.getRawSchemeSpecificPart()), env);
} catch (Exception e) {
throw new UncheckedIOException("Failed to create intermediary FS.", new IOException("Failed to process data.", e));
}
}

final Path lastPath = workingSystem.getPath(lastSection).toAbsolutePath();
return getOrCreateNewSystem(keyPrefix, lastPath);
}

private String handleAbsolutePrefixOnWindows(final FileSystem workingSystem, String section)
{
private String handleAbsolutePrefixOnWindows(final FileSystem workingSystem, String section) {
if (workingSystem.getClass().getName().toLowerCase(Locale.ROOT).contains("windows"))
{
//This special casing is needed, since else the rooted paths crash on Windows system because:
Expand All @@ -83,24 +71,19 @@ private String handleAbsolutePrefixOnWindows(final FileSystem workingSystem, Str
return section;
}

private FileSystem getOrCreateNewSystem(final Path path)
{
private FileSystem getOrCreateNewSystem(Path path) {
return getOrCreateNewSystem("", path);
}

private FileSystem getOrCreateNewSystem(String keyPrefix, final Path path)
{
private FileSystem getOrCreateNewSystem(String keyPrefix, Path path) {
final Map<String, Object> args = new HashMap<>();
args.put("packagePath", path.toAbsolutePath());

try
{
return super.newFileSystem(new URI(super.getScheme() + ":" + keyPrefix + path.toString()
.replace("\\", "/")), args);
} catch (Exception e)
{
throw new UncheckedIOException("Failed to create intermediary FS.", new IOException("Failed to process " +
"data.", e));
try {
URI uri = new URI(super.getScheme() + ':' + keyPrefix + path.toUri().toString().replace('\\', '/'));
return super.newFileSystem(uri, args);
} catch (Exception e) {
throw new UncheckedIOException("Failed to create intermediary FS.", new IOException("Failed to process data.", e));
}
}

Expand Down Expand Up @@ -210,8 +193,8 @@ private AdaptedURIWithPrefixSelection adaptUriSections(final String[] sections)
//First try a reverse lookup of a key based approach:
final Optional<FileSystem> rootKnownCandidateSystem = super.getFileSystemFromKey(sections[0]);
if (rootKnownCandidateSystem.isPresent()) {
//Okey special case: We have a file system in the root that is known to us.
//We will recursively resolve this untill we have handled all sections:
//Okay special case: We have a file system in the root that is known to us.
//We will recursively resolve this until we have handled all sections:
//First deal with the case that we do not have any other paths:
if (sections.length == 1) {
return new AdaptedURIWithPrefixSelection(rootKnownCandidateSystem.get(), sections[0]);
Expand Down Expand Up @@ -254,19 +237,16 @@ private final class AdaptedURIWithPrefixSelection {
private final String prefix;
private final FileSystem fileSystem;

private AdaptedURIWithPrefixSelection(final FileSystem fileSystem, final String prefix)
{
private AdaptedURIWithPrefixSelection(final FileSystem fileSystem, final String prefix) {
this.prefix = prefix;
this.fileSystem = fileSystem;
}

public String getPrefix()
{
public String getPrefix() {
return prefix;
}

public FileSystem getFileSystem()
{
public FileSystem getFileSystem() {
return fileSystem;
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,68 +10,54 @@
import java.util.Iterator;
import java.util.function.Function;

class PathFSUtils
{

private PathFSUtils()
{
class PathFSUtils {
private PathFSUtils() {
throw new IllegalStateException("Can not instantiate an instance of: PathFSUtils. This is a utility class");
}

public static final DirectoryStream<Path> NULL_STREAM = new DirectoryStream<Path>()
{
public static final DirectoryStream<Path> NULL_STREAM = new DirectoryStream<Path>() {
@Override
public Iterator<Path> iterator()
{
return new Iterator<Path>()
{
public Iterator<Path> iterator() {
return new Iterator<Path>() {
@Override
public boolean hasNext()
{
public boolean hasNext() {
return false;
}

@Override
public Path next()
{
public Path next() {
return null;
}
};
}

@Override
public void close() throws IOException
{

public void close() throws IOException {
}
};

public static DirectoryStream<Path> adapt(final DirectoryStream<Path> inner, final Function<Path, Path> adapter) {
return new DirectoryStream<Path>() {
@Override
public Iterator<Path> iterator()
{
public Iterator<Path> iterator() {
final Iterator<Path> targetIterator = inner.iterator();

return new Iterator<Path>() {
@Override
public boolean hasNext()
{
public boolean hasNext() {
return targetIterator.hasNext();
}

@Override
public Path next()
{
public Path next() {
final Path targetPath = targetIterator.next();
return adapter.apply(targetPath);
}
};
}

@Override
public void close() throws IOException
{
public void close() throws IOException {
inner.close();
}
};
Expand Down
Loading

0 comments on commit 6f55169

Please sign in to comment.