/
VFSUtils.java
768 lines (696 loc) · 22.1 KB
/
VFSUtils.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
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
/*
* JBoss, Home of Professional Open Source
* Copyright 2006, JBoss Inc., and individual contributors as indicated
* by the @authors tag. See the copyright.txt in the distribution for a
* full listing of individual contributors.
*
* This is free software; you can redistribute it and/or modify it
* under the terms of the GNU Lesser General Public License as
* published by the Free Software Foundation; either version 2.1 of
* the License, or (at your option) any later version.
*
* This software is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this software; if not, write to the Free
* Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
* 02110-1301 USA, or see the FSF site: http://www.fsf.org.
*/
package org.jboss.virtual;
import java.io.IOException;
import java.io.InputStream;
import java.io.UnsupportedEncodingException;
import java.net.URI;
import java.net.URISyntaxException;
import java.net.URL;
import java.net.URLDecoder;
import java.net.MalformedURLException;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import java.util.Map;
import java.util.Properties;
import java.util.StringTokenizer;
import java.util.jar.Attributes;
import java.util.jar.JarFile;
import java.util.jar.Manifest;
import org.jboss.logging.Logger;
import org.jboss.util.StringPropertyReplacer;
import org.jboss.util.collection.CollectionsFactory;
import org.jboss.virtual.plugins.copy.CopyMechanism;
import org.jboss.virtual.plugins.copy.ExplodedCopyMechanism;
import org.jboss.virtual.plugins.copy.TempCopyMechanism;
import org.jboss.virtual.plugins.copy.UnpackCopyMechanism;
import org.jboss.virtual.spi.LinkInfo;
import org.jboss.virtual.spi.VFSContext;
import org.jboss.virtual.spi.VirtualFileHandler;
/**
* VFS Utilities
*
* @author <a href="adrian@jboss.com">Adrian Brock</a>
* @author <a href="ales.justin@jboss.com">Ales Justin</a>
* @version $Revision: 1.1 $
*/
public class VFSUtils
{
/** The log */
private static final Logger log = Logger.getLogger(VFSUtils.class);
/** The default encoding */
private static final String DEFAULT_ENCODING = "UTF-8";
/** The link */
public static final String VFS_LINK_PREFIX = ".vfslink";
/** The link name */
public static final String VFS_LINK_NAME = "vfs.link.name";
/** The link target */
public static final String VFS_LINK_TARGET = "vfs.link.target";
/**
* The system no force copy key / query
*/
public static final String FORCE_COPY_KEY = "jboss.vfs.forceCopy";
public static final String USE_COPY_QUERY = "useCopyJarHandler";
/**
* Key used to force fallback from vfszip (default) to vfsjar
*/
public static final String FORCE_VFS_JAR_KEY = "jboss.vfs.forceVfsJar";
/**
* Key used to turn off reaper mode in vfszip - forcing synchronous (slower) handling of files
*/
public static final String FORCE_NO_REAPER_KEY = "jboss.vfs.forceNoReaper";
public static final String NO_REAPER_QUERY = "noReaper";
/**
* Key used to force case sensitive path checking in vfsfile
*/
public static final String FORCE_CASE_SENSITIVE_KEY = "jboss.vfs.forceCaseSensitive";
public static final String CASE_SENSITIVE_QUERY = "caseSensitive";
/**
* Key used to turn on memory optimizations - less cache use at the expense of performance
*/
public static final String OPTIMIZE_FOR_MEMORY_KEY = "jboss.vfs.optimizeForMemory";
/**
* Get the paths string for a collection of virtual files
*
* @param paths the paths
* @return the string
* @throws IllegalArgumentException for null paths
*/
public static String getPathsString(Collection<VirtualFile> paths)
{
if (paths == null)
throw new IllegalArgumentException("Null paths");
StringBuilder buffer = new StringBuilder();
boolean first = true;
for (VirtualFile path : paths)
{
if (path == null)
throw new IllegalArgumentException("Null path in " + paths);
if (first == false)
buffer.append(':');
else
first = false;
buffer.append(path.getPathName());
}
if (first == true)
buffer.append("<empty>");
return buffer.toString();
}
/**
* Add manifest paths
*
* @param file the file
* @param paths the paths to add to
* @throws IOException if there is an error reading the manifest or the
* virtual file is closed
* @throws IllegalStateException if the file has no parent
* @throws IllegalArgumentException for a null file or paths
*/
public static void addManifestLocations(VirtualFile file, List<VirtualFile> paths) throws IOException
{
if (file == null)
throw new IllegalArgumentException("Null file");
if (paths == null)
throw new IllegalArgumentException("Null paths");
Manifest manifest = getManifest(file);
if (manifest == null)
return;
Attributes mainAttributes = manifest.getMainAttributes();
String classPath = mainAttributes.getValue(Attributes.Name.CLASS_PATH);
if (classPath == null)
{
if (log.isTraceEnabled())
log.trace("Manifest has no Class-Path for " + file.getPathName());
return;
}
VirtualFile parent = file.getParent();
if (parent == null)
{
log.debug(file + " has no parent.");
return;
}
URL parentURL;
URL vfsRootURL;
int rootPathLength;
try
{
parentURL = parent.toURL();
vfsRootURL = file.getVFS().getRoot().toURL();
rootPathLength = vfsRootURL.getPath().length();
}
catch(URISyntaxException e)
{
log.debug("Failed to get parent URL for " + file + ", reason=" + e);
return;
}
String parentPath = parentURL.toString();
if(parentPath.endsWith("/") == false)
parentPath += "/";
StringTokenizer tokenizer = new StringTokenizer(classPath);
while (tokenizer.hasMoreTokens())
{
String path = tokenizer.nextToken();
try
{
URL libURL = new URL(parentPath + path);
String libPath = libURL.getPath();
if(rootPathLength > libPath.length())
{
log.debug("Invalid rootPath: " + vfsRootURL + ", libPath: " + libPath);
continue;
}
String vfsLibPath = libPath.substring(rootPathLength);
VirtualFile vf = file.getVFS().getChild(vfsLibPath);
if(vf != null)
{
if(paths.contains(vf) == false)
{
paths.add(vf);
// Recursively process the jar
addManifestLocations(vf, paths);
}
}
}
catch (IOException e)
{
log.debug("Manifest Class-Path entry " + path + " ignored for " + file.getPathName() + " reason=" + e);
}
}
}
/**
* Get a manifest from a virtual file,
* assuming the virtual file is the root of an archive
*
* @param archive the root the archive
* @return the manifest or null if not found
* @throws IOException if there is an error reading the manifest or the
* virtual file is closed
* @throws IllegalArgumentException for a null archive
*/
public static Manifest getManifest(VirtualFile archive) throws IOException
{
if (archive == null)
throw new IllegalArgumentException("Null archive");
VirtualFile manifest = archive.getChild(JarFile.MANIFEST_NAME);
if (manifest == null)
{
if (log.isTraceEnabled())
log.trace("Can't find manifest for " + archive.getPathName());
return null;
}
return readManifest(manifest);
}
/**
* Read the manifest from given manifest VirtualFile.
*
* @param manifest the VF to read from
* @return JAR's manifest
* @throws IOException if problems while opening VF stream occur
*/
public static Manifest readManifest(VirtualFile manifest) throws IOException
{
if (manifest == null)
throw new IllegalArgumentException("Null manifest file");
InputStream stream = manifest.openStream();
try
{
return new Manifest(stream);
}
finally
{
try
{
stream.close();
}
catch (IOException ignored)
{
}
}
}
/**
* Get a manifest from a virtual file system,
* assuming the root of the VFS is the root of an archive
*
* @param archive the vfs
* @return the manifest or null if not found
* @throws IOException if there is an error reading the manifest
* @throws IllegalArgumentException for a null archive
*/
public static Manifest getManifest(VFS archive) throws IOException
{
if (archive == null)
throw new IllegalArgumentException("Null vfs archive");
VirtualFile root = archive.getRoot();
return getManifest(root);
}
/**
* Fix a name (removes any trailing slash)
*
* @param name the name to fix
* @return the fixed name
* @throws IllegalArgumentException for a null name
*/
public static String fixName(String name)
{
if (name == null)
throw new IllegalArgumentException("Null name");
int length = name.length();
if (length <= 1)
return name;
if (name.charAt(length-1) == '/')
return name.substring(0, length-1);
return name;
}
/**
* Decode the path with UTF-8 encoding..
*
* @param path the path to decode
* @return decoded path
*/
public static String decode(String path)
{
return decode(path, DEFAULT_ENCODING);
}
/**
* Decode the path.
*
* @param path the path to decode
* @param encoding the encodeing
* @return decoded path
*/
public static String decode(String path, String encoding)
{
try
{
return URLDecoder.decode(path, encoding);
}
catch (UnsupportedEncodingException e)
{
throw new IllegalArgumentException("Cannot decode: " + path + " [" + encoding + "]", e);
}
}
/**
* Get the name.
*
* @param uri the uri
* @return name from uri's path
*/
public static String getName(URI uri)
{
if (uri == null)
throw new IllegalArgumentException("Null uri");
String name = uri.getPath();
if( name != null )
{
// TODO: Not correct for certain uris like jar:...!/
int lastSlash = name.lastIndexOf('/');
if( lastSlash > 0 )
name = name.substring(lastSlash+1);
}
return name;
}
/**
* Take a URL.getQuery string and parse it into name=value pairs
*
* @param query Possibly empty/null url query string
* @return String[] for the name/value pairs in the query. May be empty but never null.
*/
public static Map<String, String> parseURLQuery(String query)
{
Map<String, String> pairsMap = CollectionsFactory.createLazyMap();
if(query != null)
{
StringTokenizer tokenizer = new StringTokenizer(query, "=&");
while(tokenizer.hasMoreTokens())
{
String name = tokenizer.nextToken();
String value = tokenizer.nextToken();
pairsMap.put(name, value);
}
}
return pairsMap;
}
/**
* Does a vf name contain the VFS link prefix
* @param name - the name portion of a virtual file
* @return true if the name starts with VFS_LINK_PREFIX, false otherwise
*/
public static boolean isLink(String name)
{
if (name == null)
throw new IllegalArgumentException("Null name");
return name.indexOf(VFS_LINK_PREFIX) >= 0;
}
/**
* Read the link information from the stream based on the type as determined
* from the name suffix.
*
* @param is - input stream to the link file contents
* @param name - the name of the virtual file representing the link
* @param props the propertes
* @return a list of the links read from the stream
* @throws IOException on failure to read/parse the stream
* @throws URISyntaxException for an error parsing a URI
*/
public static List<LinkInfo> readLinkInfo(InputStream is, String name, Properties props)
throws IOException, URISyntaxException
{
if (name == null)
throw new IllegalArgumentException("Null name");
if(name.endsWith(".properties"))
{
List<LinkInfo> info = new ArrayList<LinkInfo>();
parseLinkProperties(is, info, props);
return info;
}
else
throw new UnsupportedEncodingException("Unknown link format: " + name);
}
/**
* Parse a properties link file
*
* @param is - input stream to the link file contents
* @param info the link infos
* @param props the propertes
* @throws IOException on failure to read/parse the stream
* @throws URISyntaxException for an error parsing a URI
*/
public static void parseLinkProperties(InputStream is, List<LinkInfo> info, Properties props)
throws IOException, URISyntaxException
{
if (is == null)
throw new IllegalArgumentException("Null input stream");
if (info == null)
throw new IllegalArgumentException("Null info");
if (props == null)
throw new IllegalArgumentException("Null properties");
props.load(is);
// Iterate over the property tuples
for(int n = 0; ; n ++)
{
String nameKey = VFS_LINK_NAME + "." + n;
String name = props.getProperty(nameKey);
String uriKey = VFS_LINK_TARGET + "." + n;
String uri = props.getProperty(uriKey);
// End when the value is null since a link may not have a name
if (uri == null)
{
break;
}
// Replace any system property references
uri = StringPropertyReplacer.replaceProperties(uri);
LinkInfo link = new LinkInfo(name, new URI(uri));
info.add(link);
}
}
/**
* Deal with urls that may include spaces.
*
* @param url the url
* @return uri the uri
* @throws URISyntaxException for any error
*/
public static URI toURI(URL url) throws URISyntaxException
{
if (url == null)
throw new IllegalArgumentException("Null url");
try
{
return url.toURI();
}
catch (URISyntaxException e)
{
String urispec = url.toExternalForm();
// Escape percent sign and spaces
urispec = urispec.replaceAll("%", "%25");
urispec = urispec.replaceAll(" ", "%20");
return new URI(urispec);
}
}
/**
* Ensure the url is convertible to URI by encoding spaces and percent characters if necessary
*
* @param url to be sanitized
* @return sanitized URL
* @throws URISyntaxException if URI conversion can't be fixed
* @throws MalformedURLException if an error occurs
*/
public static URL sanitizeURL(URL url) throws URISyntaxException, MalformedURLException
{
return toURI(url).toURL();
}
/**
* Get the options for this file.
*
* @param file the file
* @return options map
*/
private static Map<String, String> getOptions(VirtualFile file)
{
if (file == null)
throw new IllegalArgumentException("Null file");
VirtualFileHandler handler = file.getHandler();
VFSContext context = handler.getVFSContext();
return context.getOptions();
}
/**
* Get the option.
*
* @param file the file
* @param key the option key
* @return key's option
*/
public static String getOption(VirtualFile file, String key)
{
Map<String, String> options = getOptions(file);
return options != null ? options.get(key) : null;
}
/**
* Enable option.
*
* @param file the file
* @param optionName option name
*/
protected static void enableOption(VirtualFile file, String optionName)
{
Map<String, String> options = getOptions(file);
if (options == null)
throw new IllegalArgumentException("Cannot enable " + optionName + " on null options: " + file);
options.put(optionName, Boolean.TRUE.toString());
}
/**
* Disable option.
*
* @param file the file
* @param optionName option name
*/
protected static void disableOption(VirtualFile file, String optionName)
{
Map<String, String> options = getOptions(file);
if (options == null)
throw new IllegalArgumentException("Cannot disable " + optionName + " on null options: " + file);
options.remove(optionName);
}
/**
* Enable copy for file param.
*
* @param file the file
*/
public static void enableCopy(VirtualFile file)
{
enableOption(file, USE_COPY_QUERY);
}
/**
* Disable copy for file param.
*
* @param file the file
*/
public static void disableCopy(VirtualFile file)
{
disableOption(file, USE_COPY_QUERY);
}
/**
* Enable repaer for file param.
*
* @param file the file
*/
public static void enableNoReaper(VirtualFile file)
{
enableOption(file, NO_REAPER_QUERY);
}
/**
* Disable reaper for file param.
*
* @param file the file
*/
public static void disableNoReaper(VirtualFile file)
{
disableOption(file, NO_REAPER_QUERY);
}
/**
* Enable case sensitive for file param.
*
* @param file the file
*/
public static void enableCaseSensitive(VirtualFile file)
{
enableOption(file, CASE_SENSITIVE_QUERY);
}
/**
* Disable case sensitive for file param.
*
* @param file the file
*/
public static void disableCaseSensitive(VirtualFile file)
{
disableOption(file, CASE_SENSITIVE_QUERY);
}
/**
* Unpack the nested artifact under file param.
*
* @param file the file to unpack
* @return unpacked file
* @throws IOException for any io error
* @throws URISyntaxException for any uri error
*/
public static VirtualFile unpack(VirtualFile file) throws IOException, URISyntaxException
{
return copy(file, UnpackCopyMechanism.INSTANCE);
}
/**
* Force explode.
* Explode archives or nested entries.
*
* @param file the file to explode
* @return exploded file
* @throws IOException for any io error
* @throws URISyntaxException for any uri error
*/
public static VirtualFile explode(VirtualFile file) throws IOException, URISyntaxException
{
return copy(file, ExplodedCopyMechanism.INSTANCE);
}
/**
* Create temp.
*
* @param file the file to temp
* @return temp file
* @throws IOException for any io error
* @throws URISyntaxException for any uri error
*/
public static VirtualFile temp(VirtualFile file) throws IOException, URISyntaxException
{
return copy(file, TempCopyMechanism.INSTANCE);
}
/**
* Create temp.
*
* @param file the file to unpack/explode
* @param mechanism the copy mechanism
* @return temp file
* @throws IOException for any io error
* @throws URISyntaxException for any uri error
*/
protected static VirtualFile copy(VirtualFile file, CopyMechanism mechanism) throws IOException, URISyntaxException
{
if (file == null)
throw new IllegalArgumentException("Null file");
if (mechanism == null)
throw new IllegalArgumentException("Null copy mechanism");
return mechanism.copy(file, file.getHandler());
}
/**
* Is file handle nested.
*
* @param file the file handle to check
* @return true if file/dir is nested otherwise false
* @throws IOException for any error
*/
public static boolean isNestedFile(VirtualFile file) throws IOException
{
if (file == null)
throw new IllegalArgumentException("Null file");
VirtualFileHandler handler = file.getHandler();
return handler.isNested();
}
/**
* Get spec compatilbe url from virtual file.
*
* @param file the virtual file
* @return spec compatible url
* @throws IOException for any error
* @throws URISyntaxException for any uri syntax error
*/
public static URL getCompatibleURL(VirtualFile file) throws IOException, URISyntaxException
{
if (file == null)
throw new IllegalArgumentException("Null file");
URL url = file.toURL();
if (url == null)
throw new IllegalArgumentException("Null url: " + file);
// is not nested, so direct VFS URL is not an option
if (isNestedFile(file) == false)
{
String urlString = url.toExternalForm();
if (urlString.startsWith("vfs"))
{
// treat vfszip as file
if (urlString.startsWith("vfszip"))
url = new URL("file" + urlString.substring(6));
else
url = new URL(urlString.substring(3)); // (vfs)file and (vfs)jar are ok
}
}
return url;
}
/**
* Get spec compatilbe uri from virtual file.
*
* @param file the virtual file
* @return spec compatible uri
* @throws IOException for any error
* @throws URISyntaxException for any uri syntax error
*/
public static URI getCompatibleURI(VirtualFile file) throws IOException, URISyntaxException
{
if (file == null)
throw new IllegalArgumentException("Null file");
URI uri = file.toURI();
if (uri == null)
throw new IllegalArgumentException("Null uri: " + file);
// is not nested, so direct VFS URL is not an option
if (isNestedFile(file) == false)
{
String uriString = uri.toString();
if (uriString.startsWith("vfs"))
{
// treat vfszip as file
if (uriString.startsWith("vfszip"))
uri = new URI("file" + uriString.substring(6));
else
uri = new URI(uriString.substring(3)); // (vfs)file and (vfs)jar are ok
}
}
return uri;
}
}