-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathVfs.java
More file actions
135 lines (109 loc) · 4.49 KB
/
Vfs.java
File metadata and controls
135 lines (109 loc) · 4.49 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
package net.dempsy.vfs;
import static net.dempsy.util.Functional.recheck;
import static net.dempsy.util.Functional.uncheck;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.net.URI;
import java.util.Arrays;
import java.util.Set;
import java.util.concurrent.ConcurrentHashMap;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import net.dempsy.util.QuietCloseable;
import net.dempsy.util.io.UriOpener;
import net.dempsy.vfs.apache.ApacheVfsFileSystem;
import net.dempsy.vfs.local.LocalFileSystem;
/**
* Virtual file system facade that manages a registry of {@link FileSystem}
* implementations keyed by URI scheme. Provides methods to resolve URIs
* to {@link Path} instances, open input streams, and convert URIs to local files.
*/
public class Vfs implements QuietCloseable, UriOpener {
private static final Logger LOGGER = LoggerFactory.getLogger(Vfs.class);
private final ConcurrentHashMap<String, FileSystem> fileSystems = new ConcurrentHashMap<>();
private final OpContext globalContext;
public Vfs(final FileSystem... fileSystems) throws IOException {
if(fileSystems != null && fileSystems.length > 0)
recheck(() -> Arrays.stream(fileSystems).forEach(s -> uncheck(() -> register(s))), IOException.class);
globalContext = operation();
registerStandardFileSystems();
}
public String[] supportedSchemes() {
return fileSystems.keySet().stream().toArray(String[]::new);
}
public FileSystem fileSystemFromScheme(final String scheme) {
if(scheme == null)
throw new NullPointerException("Null scheme used to look up a FileSystem");
return fileSystems.get(scheme);
}
public FileSystem fileSystem(final URI uri) {
final String scheme = uri.getScheme();
if(scheme == null) {
final String msg = String.format("URI (%s) has no scheme.", uri.toString());
LOGGER.info(msg);
throw new IllegalArgumentException(msg);
}
return fileSystems.get(scheme);
}
public OpContext operation() {
return new OpContext(this, null);
}
public Path toPath(final URI uri) throws IOException {
return globalContext.toPath(uri);
}
public File toFile(final URI uri) throws IOException {
final FileSystem fs = fileSystem(uri);
if(fs == null)
throw new IOException("Unsupported scheme \"" + uri.getScheme() + "\" for URI " + uri);
return fs.toFile(uri);
}
@Override
public InputStream open(final URI uri) throws IOException {
final var path = toPath(uri);
if(path == null)
throw new IOException("Failed to convert URI \"" + uri + "\" to a Vfs.Path. Maybe the scheme is unsupported.");
return path.read();
}
@Override
public void close() {
globalContext.close();
}
public static class SpringHackDummyFs extends FileSystem {
@Override
protected Path doCreatePath(final URI uri, final OpContext ctx) throws IOException {
return null;
}
@Override
public String[] supportedSchemes() {
return new String[] {};
}
}
private void register(final FileSystem fs) {
Arrays.stream(fs.supportedSchemes()).forEach(s -> register(s, fs));
}
private void register(final String scheme, final FileSystem fs) {
if(fileSystems.containsKey(scheme))
LOGGER.warn(
"File system for scheme \"{}\" of type {} is being ignored because a previously registered filesystem of type {} is already handling it.",
scheme, valueOfClass(fs), valueOfClass(fileSystems.get(scheme)));
else {
fileSystems.put(scheme, fs);
fs.setVfs(this);
}
}
private void registerStandardFileSystems() throws IOException {
final Set<String> knownSchemes = fileSystems.keySet();
if(!knownSchemes.contains("classpath"))
register("classpath", new ClasspathFileSystem());
if(!knownSchemes.contains("file"))
register("file", new LocalFileSystem());
final ApacheVfsFileSystem afs = new ApacheVfsFileSystem();
for(final String scheme: afs.supportedSchemes())
if(!knownSchemes.contains(scheme) && !"hdfs".equals(scheme))
register(scheme, afs);
}
private static String valueOfClass(final Object o) {
return o == null ? "[null object has no class]" : o.getClass().getName();
}
}