-
-
Notifications
You must be signed in to change notification settings - Fork 693
/
Vfs.java
294 lines (255 loc) · 12.1 KB
/
Vfs.java
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
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
package org.reflections.vfs;
import org.reflections.Reflections;
import org.reflections.ReflectionsException;
import org.reflections.util.ClasspathHelper;
import java.io.IOException;
import java.io.InputStream;
import java.io.UnsupportedEncodingException;
import java.net.JarURLConnection;
import java.net.URISyntaxException;
import java.net.URL;
import java.net.URLConnection;
import java.net.URLDecoder;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.List;
import java.util.function.Predicate;
import java.util.jar.JarFile;
import java.util.stream.Stream;
import java.util.stream.StreamSupport;
/**
* a simple virtual file system bridge
* <p>use the {@link org.reflections.vfs.Vfs#fromURL(java.net.URL)} to get a {@link org.reflections.vfs.Vfs.Dir},
* then use {@link org.reflections.vfs.Vfs.Dir#getFiles()} to iterate over the {@link org.reflections.vfs.Vfs.File}
* <p>for example:
* <pre>
* Vfs.Dir dir = Vfs.fromURL(url);
* Iterable<Vfs.File> files = dir.getFiles();
* for (Vfs.File file : files) {
* InputStream is = file.openInputStream();
* }
* </pre>
* <p>{@link org.reflections.vfs.Vfs#fromURL(java.net.URL)} uses static {@link org.reflections.vfs.Vfs.DefaultUrlTypes} to resolve URLs.
* It contains VfsTypes for handling for common resources such as local jar file, local directory, jar url, jar input stream and more.
* <p>It can be plugged in with other {@link org.reflections.vfs.Vfs.UrlType} using {@link org.reflections.vfs.Vfs#addDefaultURLTypes(org.reflections.vfs.Vfs.UrlType)} or {@link org.reflections.vfs.Vfs#setDefaultURLTypes(java.util.List)}.
* <p>for example:
* <pre>
* Vfs.addDefaultURLTypes(new Vfs.UrlType() {
* public boolean matches(URL url) {
* return url.getProtocol().equals("http");
* }
* public Vfs.Dir createDir(final URL url) {
* return new HttpDir(url); //implement this type... (check out a naive implementation on VfsTest)
* }
* });
*
* Vfs.Dir dir = Vfs.fromURL(new URL("http://mirrors.ibiblio.org/pub/mirrors/maven2/org/slf4j/slf4j-api/1.5.6/slf4j-api-1.5.6.jar"));
* </pre>
* <p>use {@link org.reflections.vfs.Vfs#findFiles(java.util.Collection, java.util.function.Predicate)} to get an
* iteration of files matching given name predicate over given list of urls
*/
public abstract class Vfs {
private static List<UrlType> defaultUrlTypes = new ArrayList<>(Arrays.asList(DefaultUrlTypes.values()));
/** an abstract vfs dir */
public interface Dir extends AutoCloseable {
String getPath();
Iterable<File> getFiles();
default void close() {}
}
/** an abstract vfs file */
public interface File {
String getName();
String getRelativePath();
InputStream openInputStream() throws IOException;
}
/** a matcher and factory for a url */
public interface UrlType {
boolean matches(URL url) throws Exception;
Dir createDir(URL url) throws Exception;
}
/** the default url types that will be used when issuing {@link org.reflections.vfs.Vfs#fromURL(java.net.URL)} */
public static List<UrlType> getDefaultUrlTypes() {
return defaultUrlTypes;
}
/** sets the static default url types. can be used to statically plug in urlTypes */
public static void setDefaultURLTypes(final List<UrlType> urlTypes) {
defaultUrlTypes = urlTypes;
}
/** add a static default url types to the beginning of the default url types list. can be used to statically plug in urlTypes */
public static void addDefaultURLTypes(UrlType urlType) {
defaultUrlTypes.add(0, urlType);
}
/** tries to create a Dir from the given url, using the defaultUrlTypes */
public static Dir fromURL(final URL url) {
return fromURL(url, defaultUrlTypes);
}
/** tries to create a Dir from the given url, using the given urlTypes*/
public static Dir fromURL(final URL url, final List<UrlType> urlTypes) {
for (UrlType type : urlTypes) {
try {
if (type.matches(url)) {
Dir dir = type.createDir(url);
if (dir != null) return dir;
}
} catch (Throwable e) {
if (Reflections.log != null) {
Reflections.log.warn("could not create Dir using " + type + " from url " + url.toExternalForm() + ". skipping.", e);
}
}
}
throw new ReflectionsException("could not create Vfs.Dir from url, no matching UrlType was found [" + url.toExternalForm() + "]\n" +
"either use fromURL(final URL url, final List<UrlType> urlTypes) or " +
"use the static setDefaultURLTypes(final List<UrlType> urlTypes) or addDefaultURLTypes(UrlType urlType) " +
"with your specialized UrlType.");
}
/** tries to create a Dir from the given url, using the given urlTypes*/
public static Dir fromURL(final URL url, final UrlType... urlTypes) {
return fromURL(url, Arrays.asList(urlTypes));
}
/** return an iterable of all {@link org.reflections.vfs.Vfs.File} in given urls, starting with given packagePrefix and matching nameFilter */
public static Iterable<File> findFiles(final Collection<URL> inUrls, final String packagePrefix, final Predicate<String> nameFilter) {
Predicate<File> fileNamePredicate = file -> {
String path = file.toString().replace('\\','/');
if (path.contains(packagePrefix)) {
String filename = path.substring(path.indexOf(packagePrefix) + packagePrefix.length());
return !filename.isEmpty() && nameFilter.test(filename.substring(1));
} else {
return false;
}
};
return findFiles(inUrls, fileNamePredicate);
}
/** return an iterable of all {@link org.reflections.vfs.Vfs.File} in given urls, matching filePredicate */
public static Iterable<File> findFiles(final Collection<URL> urls, final Predicate<File> filePredicate) {
return () -> urls.stream()
.flatMap(url -> {
try {
return StreamSupport.stream(fromURL(url).getFiles().spliterator(), false);
} catch (Throwable e) {
if (Reflections.log != null) {
Reflections.log.error("could not findFiles for url. continuing. [" + url + "]", e);
}
return Stream.of();
}
}).filter(filePredicate).iterator();
}
/**try to get {@link java.io.File} from url*/
public static java.io.File getFile(URL url) {
java.io.File file;
String path;
try {
path = url.toURI().getSchemeSpecificPart();
if ((file = new java.io.File(path)).exists()) return file;
} catch (URISyntaxException ignored) {
}
try {
path = URLDecoder.decode(url.getPath(), "UTF-8");
if (path.contains(".jar!")) path = path.substring(0, path.lastIndexOf(".jar!") + ".jar".length());
if ((file = new java.io.File(path)).exists()) return file;
} catch (UnsupportedEncodingException ignored) {
}
try {
path = url.toExternalForm();
if (path.startsWith("jar:")) path = path.substring("jar:".length());
if (path.startsWith("wsjar:")) path = path.substring("wsjar:".length());
if (path.startsWith("file:")) path = path.substring("file:".length());
if (path.contains(".jar!")) path = path.substring(0, path.indexOf(".jar!") + ".jar".length());
if (path.contains(".war!")) path = path.substring(0, path.indexOf(".war!") + ".war".length());
if ((file = new java.io.File(path)).exists()) return file;
path = path.replace("%20", " ");
if ((file = new java.io.File(path)).exists()) return file;
} catch (Exception ignored) {
}
return null;
}
private static boolean hasJarFileInPath(URL url) {
return url.toExternalForm().matches(".*\\.jar(!.*|$)");
}
private static boolean hasInnerJarFileInPath(URL url) {
return url.toExternalForm().matches(".+\\.jar!/.+");
}
/** default url types used by {@link org.reflections.vfs.Vfs#fromURL(java.net.URL)}
* <p>
* <p>jarFile - creates a {@link org.reflections.vfs.ZipDir} over jar file
* <p>jarUrl - creates a {@link org.reflections.vfs.ZipDir} over a jar url, using Java's {@link JarURLConnection}
* <p>directory - creates a {@link org.reflections.vfs.SystemDir} over a file system directory
* <p>jboss vfs - for protocols vfs, using jboss vfs (should be provided in classpath)
* <p>jboss vfsfile - creates a {@link UrlTypeVFS} for protocols vfszip and vfsfile.
* <p>bundle - for bundle protocol, using eclipse FileLocator (should be provided in classpath)
* <p>jarInputStream - creates a {@link JarInputDir} over jar files (contains ".jar!/" in it's name), using Java's JarInputStream
* */
public enum DefaultUrlTypes implements UrlType {
jarFile {
public boolean matches(URL url) {
return url.getProtocol().equals("file") && hasJarFileInPath(url);
}
public Dir createDir(final URL url) throws Exception {
return new ZipDir(new JarFile(getFile(url)));
}
},
jarUrl {
public boolean matches(URL url) {
return ("jar".equals(url.getProtocol()) || "zip".equals(url.getProtocol()) || "wsjar".equals(url.getProtocol())) && !hasInnerJarFileInPath(url);
}
public Dir createDir(URL url) throws Exception {
try {
URLConnection urlConnection = url.openConnection();
if (urlConnection instanceof JarURLConnection) {
urlConnection.setUseCaches(false);
return new ZipDir(((JarURLConnection) urlConnection).getJarFile());
}
} catch (Throwable e) { /*fallback*/ }
java.io.File file = getFile(url);
if (file != null) {
return new ZipDir(new JarFile(file));
}
return null;
}
},
directory {
public boolean matches(URL url) {
if (url.getProtocol().equals("file") && !hasJarFileInPath(url)) {
java.io.File file = getFile(url);
return file != null && file.isDirectory();
} else return false;
}
public Dir createDir(final URL url) throws Exception {
return new SystemDir(getFile(url));
}
},
jboss_vfs {
public boolean matches(URL url) {
return url.getProtocol().equals("vfs");
}
public Vfs.Dir createDir(URL url) throws Exception {
return JbossDir.createDir(url);
}
},
jboss_vfsfile {
public boolean matches(URL url) throws Exception {
return "vfszip".equals(url.getProtocol()) || "vfsfile".equals(url.getProtocol());
}
public Dir createDir(URL url) throws Exception {
return new UrlTypeVFS().createDir(url);
}
},
bundle {
public boolean matches(URL url) throws Exception {
return url.getProtocol().startsWith("bundle");
}
public Dir createDir(URL url) throws Exception {
return fromURL((URL) ClasspathHelper.contextClassLoader().
loadClass("org.eclipse.core.runtime.FileLocator").getMethod("resolve", URL.class).invoke(null, url));
}
},
jarInputStream {
public boolean matches(URL url) throws Exception {
return url.toExternalForm().contains(".jar");
}
public Dir createDir(final URL url) throws Exception {
return new JarInputDir(url);
}
}
}
}