/
AbstractVFSContext.java
540 lines (484 loc) · 15.3 KB
/
AbstractVFSContext.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
/*
* 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.plugins.context;
import java.io.IOException;
import java.net.MalformedURLException;
import java.net.URI;
import java.net.URISyntaxException;
import java.net.URL;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.TreeMap;
import java.util.HashMap;
import java.util.Set;
import java.util.HashSet;
import java.util.concurrent.CopyOnWriteArrayList;
import org.jboss.logging.Logger;
import org.jboss.virtual.VFS;
import org.jboss.virtual.VFSUtils;
import org.jboss.virtual.VirtualFile;
import org.jboss.virtual.VirtualFileFilter;
import org.jboss.virtual.VisitorAttributes;
import org.jboss.virtual.spi.ExceptionHandler;
import org.jboss.virtual.spi.Options;
import org.jboss.virtual.spi.TempInfo;
import org.jboss.virtual.spi.VFSContext;
import org.jboss.virtual.spi.VirtualFileHandler;
import org.jboss.virtual.spi.VirtualFileHandlerVisitor;
/**
* AbstractVFSContext.
*
* @author <a href="adrian@jboss.com">Adrian Brock</a>
* @author Scott.Stark@jboss.org
* @author <a href="ales.justin@jboss.com">Ales Justin</a>
* @version $Revision: 1.1 $
*/
public abstract class AbstractVFSContext implements VFSContext
{
/** The log */
protected final Logger log = Logger.getLogger(getClass());
/** The VFS wrapper */
private VFS vfs;
/** The root url */
private final URI rootURI;
/** Options associated with the root URL */
private Options options = createNewOptions();
/** Root's peer within another context */
private VirtualFileHandler rootPeer;
/** The temp handlers */
private final Map<String, List<TempInfo>> tempInfos = Collections.synchronizedMap(new HashMap<String, List<TempInfo>>());
/**
* Create a new AbstractVFSContext.
*
* @param rootURI the root url
* @throws IllegalArgumentException if rootURI is null
*/
protected AbstractVFSContext(URI rootURI)
{
if (rootURI == null)
throw new IllegalArgumentException("Null rootURI");
this.rootURI = rootURI;
String query = rootURI.getQuery();
Map<String, ?> map = VFSUtils.parseURLQuery(query);
options.addOptions(map);
}
/**
* Create a new AbstractVFSContext.
*
* @param rootURL the root url
* @throws URISyntaxException for illegal URL
* @throws IllegalArgumentException if rootURI is null
*/
protected AbstractVFSContext(URL rootURL) throws URISyntaxException
{
this(rootURL.toURI());
}
/**
* Create options.
*
* @return the new options
*/
protected Options createNewOptions()
{
return new DefaultOptions();
}
public VFS getVFS()
{
if (vfs == null)
vfs = new VFS(this);
return vfs;
}
public URI getRootURI()
{
return rootURI;
}
public void setRootPeer(VirtualFileHandler handler)
{
this.rootPeer = handler;
}
public VirtualFileHandler getRootPeer()
{
return rootPeer;
}
public Options getOptions()
{
return options;
}
protected void setOption(String name, Object option)
{
if (option == null)
options.removeOption(name);
else
options.addOption(name, option);
}
protected Object getOption(String name)
{
return options.getOption(name);
}
protected <T> T getOption(Class<T> expectedType)
{
if (expectedType == null)
throw new IllegalArgumentException("Null expectedType");
return getOption(expectedType.getName(), expectedType);
}
protected <T> T getOption(String name, Class<T> expectedType)
{
return options.getOption(name, expectedType);
}
/**
* Get peer vfs context.
*
* @return the peer context
*/
protected VFSContext getPeerContext()
{
VirtualFileHandler peer = getRootPeer();
return peer != null ? peer.getVFSContext() : null;
}
/**
* Helper method to set options on an URL
*
* @param url url to set options on
* @return url with query parameters
* @throws java.net.MalformedURLException if url manipulation fails
*/
protected URL setOptionsToURL(URL url) throws MalformedURLException
{
Map<String, String> map = options.getOptions(String.class);
if (map.isEmpty())
return url;
StringBuilder sb = new StringBuilder(url.toString());
sb.append("?");
int i = 0;
for (Map.Entry<String, String> ent : map.entrySet())
{
if (i > 0)
sb.append("&");
sb.append(ent.getKey()).append("=").append(ent.getValue());
i++;
}
return new URL(sb.toString());
}
public List<VirtualFileHandler> getChildren(VirtualFileHandler parent, boolean ignoreErrors) throws IOException
{
if (parent == null)
throw new IllegalArgumentException("Null parent");
return parent.getChildren(ignoreErrors);
}
public VirtualFileHandler getChild(VirtualFileHandler parent, String path) throws IOException
{
if (parent == null)
throw new IllegalArgumentException("Null parent");
if (path == null)
throw new IllegalArgumentException("Null path");
return parent.getChild(path);
}
/**
* Construct a URL from a given parent and a name
*
* @param parent a parent
* @param name a name of the child
* @return URL corresponding to a child
* @throws IOException for any error
*/
public URL getChildURL(VirtualFileHandler parent, String name) throws IOException
{
if(parent != null)
{
VFSContext parentCtx = parent.getVFSContext();
if (parentCtx != this)
{
if (parentCtx instanceof AbstractVFSContext)
{
return ((AbstractVFSContext) parentCtx).getChildURL(parent, name);
}
else
{
StringBuilder urlStr = new StringBuilder(512);
try
{
urlStr.append(parent.toURI());
if (urlStr.charAt( urlStr.length()-1) != '/')
urlStr.append("/");
urlStr.append(name);
return new URL(urlStr.toString());
}
catch (URISyntaxException e)
{
throw new RuntimeException("Failed to create child URL: " + parent + " + " + name, e);
}
}
}
}
StringBuilder urlStr = new StringBuilder(512);
URI rootUri = getRootURI();
urlStr.append(rootUri.getScheme())
.append(":").append(rootUri.getPath());
if(parent != null)
{
if (urlStr.charAt( urlStr.length()-1) != '/')
urlStr.append("/");
String pPathName = parent.getPathName();
if(pPathName.length() != 0)
urlStr.append(pPathName);
if (urlStr.charAt( urlStr.length()-1) != '/')
urlStr.append("/");
urlStr.append(name);
}
return new URL(urlStr.toString());
}
public void visit(VirtualFileHandler handler, VirtualFileHandlerVisitor visitor) throws IOException
{
if (handler == null)
throw new IllegalArgumentException("Null handler");
if (visitor == null)
throw new IllegalArgumentException("Null visitor");
VisitorAttributes attributes = visitor.getAttributes();
boolean includeRoot = attributes.isIncludeRoot();
boolean leavesOnly = attributes.isLeavesOnly();
boolean ignoreErrors = attributes.isIgnoreErrors();
boolean includeHidden = attributes.isIncludeHidden();
VirtualFileFilter recurseFilter = attributes.getRecurseFilter();
visit(handler, visitor, includeRoot, leavesOnly, ignoreErrors, includeHidden, recurseFilter);
}
/**
* Visit. the file system, recursive death checking is left to the visitor
* or otherwise a stack overflow.
*
* @param handler the reference handler
* @param visitor the visitor
* @param includeRoot whether to visit the root
* @param leavesOnly whether to visit leaves only
* @param ignoreErrors whether to ignore errors
* @param includeHidden whether to include hidden files
* @param recurseFilter the recurse filter
* @throws IOException for any problem accessing the virtual file system
*/
protected void visit(VirtualFileHandler handler, VirtualFileHandlerVisitor visitor,
boolean includeRoot, boolean leavesOnly, boolean ignoreErrors,
boolean includeHidden, VirtualFileFilter recurseFilter)
throws IOException
{
// Visit the root when asked
if (includeRoot)
visitor.visit(handler);
// Visit the children
boolean trace = log.isTraceEnabled();
List<VirtualFileHandler> children;
try
{
children = getChildren(handler, ignoreErrors);
}
catch (IOException e)
{
if (ignoreErrors == false)
throw e;
if( trace )
log.trace("Ignored: " + e);
return;
}
// Look through each child
for (VirtualFileHandler child : children)
{
// Ignore hidden if asked
if (includeHidden == false && child.isHidden())
{
if( trace )
log.trace("Ignoring hidden file: "+child);
continue;
}
// Visit the leaf or non-leaves when asked
boolean isLeaf = child.isLeaf();
if (leavesOnly == false || isLeaf)
visitor.visit(child);
else if( trace )
{
log.trace("Skipping non-leaf file: "+child);
}
// Recurse when asked
VirtualFile file = child.getVirtualFile();
if ( isLeaf == false && recurseFilter != null && recurseFilter.accepts(file))
{
try
{
if (handler instanceof DelegatingHandler)
child.getVFSContext().visit(child, visitor);
else
visit(child, visitor, false, leavesOnly, ignoreErrors, includeHidden, recurseFilter);
}
catch (StackOverflowError e)
{
log.debug("Original: " + child, e);
throw new IOException("Stack overflow, the file system is too complicated? " + child);
}
}
}
}
public void addTempInfo(TempInfo tempInfo)
{
String key = tempInfo.getPath();
List<TempInfo> list;
synchronized (tempInfos)
{
list = tempInfos.get(key);
if (list == null)
{
list = new CopyOnWriteArrayList<TempInfo>();
tempInfos.put(key, list);
}
}
list.add(tempInfo);
}
public TempInfo getTempInfo(String path)
{
TempInfo result = null;
List<TempInfo> list = tempInfos.get(path);
if (list != null && list.isEmpty())
{
Iterator<TempInfo> iter = list.iterator();
while(iter.hasNext())
{
TempInfo ti = iter.next();
if (ti.isValid() == false)
{
iter.remove();
}
else if (result == null)
{
result = ti;
}
}
}
return result;
}
public Iterable<TempInfo> getTempInfos()
{
Map<String, TempInfo> result = new TreeMap<String, TempInfo>();
Iterator<Map.Entry<String, List<TempInfo>>> iter = tempInfos.entrySet().iterator();
while(iter.hasNext())
{
Map.Entry<String, List<TempInfo>> entry = iter.next();
List<TempInfo> list = entry.getValue();
if (list != null && list.isEmpty() == false)
{
Set<TempInfo> invalidTempInfos = new HashSet<TempInfo>();
Iterator<TempInfo> listIter = list.iterator();
while(listIter.hasNext())
{
TempInfo ti = listIter.next();
if (ti.isValid())
{
String path = ti.getPath();
if (result.containsKey(path) == false)
{
result.put(path, ti);
}
}
else
{
invalidTempInfos.add(ti);
}
}
for (TempInfo ti : invalidTempInfos) {
list.remove(ti);
}
}
if (list == null || list.isEmpty())
{
iter.remove();
}
}
return result.values();
}
public void cleanupTempInfo(String path)
{
boolean trace = log.isTraceEnabled();
List<String> info = null;
Iterator<Map.Entry<String, List<TempInfo>>> iter = tempInfos.entrySet().iterator();
while (iter.hasNext())
{
Map.Entry<String, List<TempInfo>> entry = iter.next();
if (entry.getKey().startsWith(path))
{
List<TempInfo> list = entry.getValue();
if (trace)
{
if (info == null)
info = new ArrayList<String>();
if (list != null && list.isEmpty() == false)
info.add(list.toString());
}
for (TempInfo ti : list)
{
try
{
ti.cleanup();
}
catch (Throwable ignored)
{
}
}
iter.remove();
}
}
if (trace)
log.trace("Removing temp info for path: '" + path + "', temps: " + info);
}
public ExceptionHandler getExceptionHandler()
{
return getOption(ExceptionHandler.class);
}
public void setExceptionHandler(ExceptionHandler exceptionHandler)
{
setOption(ExceptionHandler.class.getName(), exceptionHandler);
}
@Override
public String toString()
{
StringBuilder buffer = new StringBuilder();
buffer.append(getClass().getSimpleName());
buffer.append('@');
buffer.append(System.identityHashCode(this));
buffer.append('[');
buffer.append(rootURI);
buffer.append(']');
return buffer.toString();
}
@Override
public int hashCode()
{
return rootURI.hashCode();
}
@Override
public boolean equals(Object obj)
{
if (this == obj)
return true;
if (obj == null || obj instanceof VFSContext == false)
return false;
VFSContext other = (VFSContext) obj;
return rootURI.equals(other.getRootURI());
}
}