Skip to content

Commit

Permalink
Added improved FileSystem abstraction
Browse files Browse the repository at this point in the history
  • Loading branch information
Aklakan committed Oct 6, 2024
1 parent c0ff285 commit 8a88c00
Show file tree
Hide file tree
Showing 11 changed files with 389 additions and 6 deletions.
4 changes: 2 additions & 2 deletions aksw-commons-bom/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -60,11 +60,11 @@
<rxjava.version>3.1.8</rxjava.version>
<commons-validator.version>1.7</commons-validator.version>
<protonpack.version>1.16</protonpack.version>
<commons-io.version>2.15.0</commons-io.version>
<commons-io.version>2.17.0</commons-io.version>
<jcraft.version>0.0.9</jcraft.version>
<jgit.version>5.6.0.201912101111-r</jgit.version>

<hadoop.version>3.3.6</hadoop.version>
<hadoop.version>3.4.0</hadoop.version>
<!-- <slf4j.version>2.0.0-alpha5</slf4j.version> -->
<jackson.version>2.15.2</jackson.version>
<streamex.version>0.8.2</streamex.version>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,6 @@
public interface PathSys<T, S>
extends Path<T>
{
// @Override
@Override
S getSystem();
}
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ public Iterator<O> iterator() {
public void close() throws IOException {
base.close();
}
};
}

private static class DirectoryStreamFilter<T>
implements DirectoryStream<T>
Expand Down Expand Up @@ -74,5 +74,5 @@ public Iterator<T> iterator() {
public void close() throws IOException {
base.close();
}
};
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package org.aksw.commons.path.nio;

import java.io.IOException;
import java.nio.file.FileSystem;

/** A nested file system. Closing the outer one also recursively closes the inner ones. */
public class FileSystem2
extends FileSystemWrapper
{
protected FileSystem underlying;

public FileSystem2(FileSystem delegate, FileSystem underlying) {
super(delegate);
this.underlying = underlying;
}

@Override
public void close() throws IOException {
try {
super.close();
} finally {
underlying.close();
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
package org.aksw.commons.path.nio;

import java.io.IOException;
import java.net.URI;
import java.nio.file.FileSystem;
import java.nio.file.FileSystems;
import java.nio.file.Path;
import java.util.List;
import java.util.Map;
import java.util.Objects;

import org.aksw.commons.path.core.PathSys;

public class FileSystemSpec {
protected Map<String, ?> env;
protected ClassLoader loader;

protected URI uri;
protected PathSys<String, FileSystemSpec> basePath;

protected FileSystemSpec(Map<String, ?> env, ClassLoader loader, PathSys<String, FileSystemSpec> basePath) {
super();
this.env = env;
this.loader = loader;
this.basePath = Objects.requireNonNull(basePath);
}

protected FileSystemSpec(Map<String, ?> env, ClassLoader loader, URI uri) {
super();
this.env = env;
this.loader = loader;
this.uri = Objects.requireNonNull(uri);
}

public static FileSystemSpec of(Map<String, ?> env, ClassLoader loader, URI uri) {
return new FileSystemSpec(env, loader, uri);
}

public static FileSystemSpec of(Map<String, ?> env, ClassLoader loader, PathSys<String, FileSystemSpec> basePath) {
return new FileSystemSpec(env, loader, basePath);
}

public boolean isUri() {
return uri != null;
}

public boolean isPath() {
return basePath != null;
}

public FileSystem resolve() throws IOException {
FileSystem result;
if (isUri()) {
result = new FileSystemWithSpec(FileSystems.newFileSystem(uri, env, loader), this);
} else {
FileSystemSpec baseSpec = basePath.getSystem();
FileSystem baseFs = baseSpec.resolve();
List<String> segments = basePath.getSegments();
String first = segments.isEmpty() ? "" : segments.get(0);
String[] rest = segments.isEmpty() ? new String[0] : segments.subList(1, segments.size()).toArray(new String[0]);
Path path = baseFs.getPath(first, rest);
FileSystem outer = new FileSystemWithSpec(FileSystems.newFileSystem(path, env, loader), this);
result = new FileSystem2(outer, baseFs);
}
return result;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package org.aksw.commons.path.nio;

import java.io.IOException;
import java.nio.file.FileSystem;

public class FileSystemWithCloseShield
extends FileSystemWrapper
{
protected FileSystemWithCloseShield(FileSystem delegate) {
super(delegate);
}

public static FileSystem of(FileSystem delegate) {
return new FileSystemWithCloseShield(delegate);
}

@Override
public void close() throws IOException {
// No op
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package org.aksw.commons.path.nio;

import java.nio.file.FileSystem;

public class FileSystemWithSpec
extends FileSystemWrapper
{
protected FileSystemSpec spec;

public FileSystemWithSpec(FileSystem delegate, FileSystemSpec spec) {
super(delegate);
this.spec = spec;
}

public FileSystemSpec getSpec() {
return spec;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package org.aksw.commons.path.nio;

import java.nio.file.FileSystem;

public class FileSystemWrapper
extends FileSystemWrapperBase
{
protected FileSystem delegate;

public FileSystemWrapper(FileSystem delegate) {
super();
this.delegate = delegate;
}

@Override
protected FileSystem getDelegate() {
return delegate;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,105 @@
package org.aksw.commons.path.nio;

import java.io.IOException;
import java.nio.file.FileStore;
import java.nio.file.FileSystem;
import java.nio.file.Path;
import java.nio.file.PathMatcher;
import java.nio.file.WatchService;
import java.nio.file.attribute.UserPrincipalLookupService;
import java.nio.file.spi.FileSystemProvider;
import java.util.Set;
import java.util.Spliterators;
import java.util.stream.StreamSupport;

/**
* Base class for wrappers of file systems.
* Implementations need to implement {@link #getDelegate()}.
*/
public abstract class FileSystemWrapperBase
extends FileSystem
{
protected abstract FileSystem getDelegate();

public Path wrap(Path path) {
return new PathWrapper<>(path, this);
}

public Path unwrap(Path path) {
return unwrap(path, false);
}

public static Path unwrap(Path path, boolean repeatedly) {
Path result = path;
while (result instanceof PathWrapper pw) {
result = pw.getDelegate();

if (!repeatedly) {
break;
}
}
return result;
}

@Override
public FileSystemProvider provider() {
return getDelegate().provider();
}

@Override
public void close() throws IOException {
getDelegate().close();
}

@Override
public boolean isOpen() {
return getDelegate().isOpen();
}

@Override
public boolean isReadOnly() {
return getDelegate().isReadOnly();
}

@Override
public String getSeparator() {
return getDelegate().getSeparator();
}

@Override
public Iterable<Path> getRootDirectories() {
return () -> StreamSupport.stream(Spliterators.spliteratorUnknownSize(getDelegate().getRootDirectories().iterator(), 0), false)
.map(this::wrap)
.iterator();
}

@Override
public Iterable<FileStore> getFileStores() {
return getDelegate().getFileStores();
}

@Override
public Set<String> supportedFileAttributeViews() {
return getDelegate().supportedFileAttributeViews();
}

@Override
public Path getPath(String first, String... more) {
return wrap(getDelegate().getPath(first, more));
}

@Override
public PathMatcher getPathMatcher(String syntaxAndPattern) {
return getDelegate().getPathMatcher(syntaxAndPattern);
}

@Override
public UserPrincipalLookupService getUserPrincipalLookupService() {
return getDelegate().getUserPrincipalLookupService();
}

@Override
public WatchService newWatchService() throws IOException {
return getDelegate().newWatchService();
}
}
Loading

0 comments on commit 8a88c00

Please sign in to comment.