-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added improved FileSystem abstraction
- Loading branch information
Showing
11 changed files
with
389 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -3,6 +3,6 @@ | |
public interface PathSys<T, S> | ||
extends Path<T> | ||
{ | ||
// @Override | ||
@Override | ||
S getSystem(); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
25 changes: 25 additions & 0 deletions
25
aksw-commons-paths/src/main/java/org/aksw/commons/path/nio/FileSystem2.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
} | ||
} | ||
} |
67 changes: 67 additions & 0 deletions
67
aksw-commons-paths/src/main/java/org/aksw/commons/path/nio/FileSystemSpec.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} |
21 changes: 21 additions & 0 deletions
21
aksw-commons-paths/src/main/java/org/aksw/commons/path/nio/FileSystemWithCloseShield.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} | ||
} |
18 changes: 18 additions & 0 deletions
18
aksw-commons-paths/src/main/java/org/aksw/commons/path/nio/FileSystemWithSpec.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} |
19 changes: 19 additions & 0 deletions
19
aksw-commons-paths/src/main/java/org/aksw/commons/path/nio/FileSystemWrapper.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} |
105 changes: 105 additions & 0 deletions
105
aksw-commons-paths/src/main/java/org/aksw/commons/path/nio/FileSystemWrapperBase.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
} | ||
} |
Oops, something went wrong.