37 class PassThroughFileSystem extends FileSystem { 38 private final FileSystemProvider provider; 39 private final FileSystem delegate; 40 41 PassThroughFileSystem(FileSystemProvider provider, FileSystem delegate) { 42 this.provider = provider; 43 this.delegate = delegate; 44 } 45 46 /** 47 * Creates a new "pass through" file system. Useful for test environments 48 * where the provider might not be deployed. 49 */ 50 static FileSystem create() throws IOException { 51 FileSystemProvider provider = new PassThroughProvider(); 52 Map<String,?> env = Collections.emptyMap(); 53 URI uri = URI.create("pass:///"); 54 return provider.newFileSystem(uri, env); 55 } 56 57 @Override 58 public FileSystemProvider provider() { 59 return provider; 60 } 61 62 @Override 63 public void close() throws IOException { 64 delegate.close(); 65 } 66 67 @Override 68 public boolean isOpen() { 69 return delegate.isOpen(); 70 } 71 72 @Override 73 public boolean isReadOnly() { 74 return delegate.isReadOnly(); 75 } 76 100 itr.remove(); 101 } 102 }; 103 } 104 }; 105 } 106 107 @Override 108 public Iterable<FileStore> getFileStores() { 109 // assume that unwrapped objects aren't exposed 110 return delegate.getFileStores(); 111 } 112 113 @Override 114 public Set<String> supportedFileAttributeViews() { 115 // assume that unwrapped objects aren't exposed 116 return delegate.supportedFileAttributeViews(); 117 } 118 119 @Override 120 public Path getPath(String path) { 121 return new PassThroughPath(this, delegate.getPath(path)); 122 } 123 124 @Override 125 public PathMatcher getPathMatcher(String syntaxAndPattern) { 126 final PathMatcher matcher = delegate.getPathMatcher(syntaxAndPattern); 127 return new PathMatcher() { 128 @Override 129 public boolean matches(Path path) { 130 return matcher.matches(PassThroughPath.unwrap(path)); 131 } 132 }; 133 } 134 135 @Override 136 public UserPrincipalLookupService getUserPrincipalLookupService() { 137 // assume that unwrapped objects aren't exposed 138 return delegate.getUserPrincipalLookupService(); 139 } 140 141 @Override 142 public WatchService newWatchService() throws IOException { 143 // to keep it simple 144 throw new UnsupportedOperationException(); 145 } 146 147 static class PassThroughProvider extends FileSystemProvider { 148 private static final String SCHEME = "pass"; 149 private static volatile PassThroughFileSystem delegate; 150 182 } 183 184 @Override 185 public FileSystem getFileSystem(URI uri) { 186 checkUri(uri); 187 FileSystem result = delegate; 188 if (result == null) 189 throw new FileSystemNotFoundException(); 190 return result; 191 } 192 193 @Override 194 public Path getPath(URI uri) { 195 checkScheme(uri); 196 if (delegate == null) 197 throw new FileSystemNotFoundException(); 198 uri = URI.create(delegate.provider().getScheme() + ":" + 199 uri.getSchemeSpecificPart()); 200 return new PassThroughPath(delegate, delegate.provider().getPath(uri)); 201 } 202 } 203 204 static class PassThroughPath extends Path { 205 private final FileSystem fs; 206 private final Path delegate; 207 208 PassThroughPath(FileSystem fs, Path delegate) { 209 this.fs = fs; 210 this.delegate = delegate; 211 } 212 213 private Path wrap(Path path) { 214 return (path != null) ? new PassThroughPath(fs, path) : null; 215 } 216 217 static Path unwrap(Path wrapper) { 218 if (!(wrapper instanceof PassThroughPath)) 219 throw new ProviderMismatchException(); 220 return ((PassThroughPath)wrapper).delegate; 221 } 222 223 @Override 224 public FileSystem getFileSystem() { 225 return fs; 226 } 227 228 @Override 229 public boolean isAbsolute() { 230 return delegate.isAbsolute(); 231 } 232 233 @Override 234 public Path getRoot() { 235 return wrap(delegate.getRoot()); 236 } 237 238 239 @Override 240 public Path getName() { 241 return wrap(delegate.getName()); 242 } 243 244 @Override 245 public Path getParent() { 246 return wrap(delegate.getParent()); 247 } 248 249 @Override 250 public int getNameCount() { 251 return delegate.getNameCount(); 252 } 253 254 @Override 255 public Path getName(int index) { 256 return wrap(delegate.getName(index)); 257 } 258 259 @Override 260 public Path subpath(int beginIndex, int endIndex) { 261 return wrap(delegate.subpath(beginIndex, endIndex)); 262 } 263 264 @Override 265 public boolean startsWith(Path other) { 266 return delegate.startsWith(unwrap(other)); 267 } 268 269 @Override 270 public boolean endsWith(Path other) { 271 return delegate.endsWith(unwrap(other)); 272 } 273 274 @Override 275 public Path normalize() { 276 return wrap(delegate.normalize()); 277 } 278 279 @Override 280 public Path resolve(Path other) { 281 return wrap(delegate.resolve(unwrap(other))); 282 } 283 284 @Override 285 public Path resolve(String other) { 286 return wrap(delegate.resolve(other)); 287 } 288 289 @Override 290 public Path relativize(Path other) { 291 return wrap(delegate.relativize(unwrap(other))); 292 } 293 294 @Override 295 public void setAttribute(String attribute, Object value, LinkOption... options) 296 throws IOException 297 { 298 delegate.setAttribute(attribute, value, options); 299 } 300 301 @Override 302 public Object getAttribute(String attribute, LinkOption... options) 303 throws IOException 304 { 305 // assume that unwrapped objects aren't exposed 306 return delegate.getAttribute(attribute, options); 307 } 308 309 @Override 310 public Map<String,?> readAttributes(String attributes, LinkOption... options) 311 throws IOException 312 { 313 // assume that unwrapped objects aren't exposed 314 return delegate.readAttributes(attributes, options); 315 } 316 317 @Override 318 public <V extends FileAttributeView> V getFileAttributeView(Class<V> type, 319 LinkOption... options) 320 { 321 return delegate.getFileAttributeView(type, options); 322 } 323 324 @Override 325 public void delete() throws IOException { 326 delegate.delete(); 327 } 328 329 @Override 330 public void deleteIfExists() throws IOException { 331 delegate.deleteIfExists(); 332 } 333 334 @Override 335 public Path createSymbolicLink(Path target, FileAttribute<?>... attrs) 336 throws IOException 337 { 338 delegate.createSymbolicLink(unwrap(target), attrs); 339 return this; 340 } 341 342 @Override 343 public Path createLink(Path existing) throws IOException { 344 delegate.createLink(unwrap(existing)); 345 return this; 346 } 347 348 @Override 349 public Path readSymbolicLink() throws IOException { 350 return wrap(delegate.readSymbolicLink()); 351 } 352 353 @Override 354 public URI toUri() { 355 String ssp = delegate.toUri().getSchemeSpecificPart(); 356 return URI.create(fs.provider().getScheme() + ":" + ssp); 357 } 358 359 @Override 360 public Path toAbsolutePath() { 361 return wrap(delegate.toAbsolutePath()); 362 } 363 364 @Override 365 public Path toRealPath(boolean resolveLinks) throws IOException { 366 return wrap(delegate.toRealPath(resolveLinks)); 367 } 368 369 @Override 370 public Path copyTo(Path target, CopyOption... options) throws IOException { 371 return wrap(delegate.copyTo(unwrap(target), options)); 372 } 373 374 @Override 375 public Path moveTo(Path target, CopyOption... options) throws IOException { 376 return wrap(delegate.copyTo(unwrap(target), options)); 377 } 378 379 private DirectoryStream<Path> wrap(final DirectoryStream<Path> stream) { 380 return new DirectoryStream<Path>() { 381 @Override 382 public Iterator<Path> iterator() { 383 final Iterator<Path> itr = stream.iterator(); 384 return new Iterator<Path>() { 385 @Override 386 public boolean hasNext() { 387 return itr.hasNext(); 388 } 389 @Override 390 public Path next() { 391 return wrap(itr.next()); 392 } 393 @Override 394 public void remove() { 395 itr.remove(); 396 } 397 }; 398 } 399 @Override 400 public void close() throws IOException { 401 stream.close(); 402 } 403 }; 404 } 405 406 @Override 407 public DirectoryStream<Path> newDirectoryStream() throws IOException { 408 return wrap(delegate.newDirectoryStream()); 409 } 410 411 @Override 412 public DirectoryStream<Path> newDirectoryStream(String glob) 413 throws IOException 414 { 415 return wrap(delegate.newDirectoryStream(glob)); 416 } 417 418 @Override 419 public DirectoryStream<Path> newDirectoryStream(DirectoryStream.Filter<? super Path> filter) 420 throws IOException 421 { 422 return wrap(delegate.newDirectoryStream(filter)); 423 } 424 425 @Override 426 public Path createFile(FileAttribute<?>... attrs) throws IOException { 427 delegate.createFile(attrs); 428 return this; 429 } 430 431 @Override 432 public Path createDirectory(FileAttribute<?>... attrs) 433 throws IOException 434 { 435 delegate.createDirectory(attrs); 436 return this; 437 } 438 439 @Override 440 public SeekableByteChannel newByteChannel(Set<? extends OpenOption> options, 441 FileAttribute<?>... attrs) 442 throws IOException 443 { 444 return delegate.newByteChannel(options, attrs); 445 } 446 447 @Override 448 public SeekableByteChannel newByteChannel(OpenOption... options) 449 throws IOException 450 { 451 return delegate.newByteChannel(options); 452 } 453 454 @Override 455 public InputStream newInputStream(OpenOption... options) throws IOException { 456 return delegate.newInputStream(); 457 } 458 459 @Override 460 public OutputStream newOutputStream(OpenOption... options) 461 throws IOException 462 { 463 return delegate.newOutputStream(options); 464 } 465 466 @Override 467 public boolean isHidden() throws IOException { 468 return delegate.isHidden(); 469 } 470 471 @Override 472 public void checkAccess(AccessMode... modes) throws IOException { 473 delegate.checkAccess(modes); 474 } 475 476 @Override 477 public boolean exists() { 478 return delegate.exists(); 479 } 480 481 @Override 482 public boolean notExists() { 483 return delegate.notExists(); 484 } 485 486 @Override 487 public FileStore getFileStore() throws IOException { 488 return delegate.getFileStore(); 489 } 490 491 @Override 492 public WatchKey register(WatchService watcher, 493 WatchEvent.Kind<?>[] events, 494 WatchEvent.Modifier... modifiers) 495 { 496 throw new UnsupportedOperationException(); 497 } 498 499 @Override 500 public WatchKey register(WatchService watcher, 501 WatchEvent.Kind<?>... events) 502 { 503 throw new UnsupportedOperationException(); 504 } 505 506 507 @Override 508 public Iterator<Path> iterator() { 509 final Iterator<Path> itr = delegate.iterator(); 510 return new Iterator<Path>() { 511 @Override 512 public boolean hasNext() { 513 return itr.hasNext(); 514 } 515 @Override 516 public Path next() { 517 return wrap(itr.next()); 518 } 519 @Override 520 public void remove() { 521 itr.remove(); 522 } 523 }; 524 } 525 526 @Override 527 public int compareTo(Path other) { 528 return delegate.compareTo(unwrap(other)); 529 } 530 531 @Override 532 public boolean isSameFile(Path other) throws IOException { 533 return delegate.isSameFile(unwrap(other)); 534 } 535 536 537 @Override 538 public boolean equals(Object other) { 539 if (!(other instanceof PassThroughPath)) 540 return false; 541 return delegate.equals(unwrap((PassThroughPath)other)); 542 } 543 544 @Override 545 public int hashCode() { 546 return delegate.hashCode(); 547 } 548 549 @Override 550 public String toString() { 551 return delegate.toString(); 552 } 553 } 554 } | 37 class PassThroughFileSystem extends FileSystem { 38 private final FileSystemProvider provider; 39 private final FileSystem delegate; 40 41 PassThroughFileSystem(FileSystemProvider provider, FileSystem delegate) { 42 this.provider = provider; 43 this.delegate = delegate; 44 } 45 46 /** 47 * Creates a new "pass through" file system. Useful for test environments 48 * where the provider might not be deployed. 49 */ 50 static FileSystem create() throws IOException { 51 FileSystemProvider provider = new PassThroughProvider(); 52 Map<String,?> env = Collections.emptyMap(); 53 URI uri = URI.create("pass:///"); 54 return provider.newFileSystem(uri, env); 55 } 56 57 static Path unwrap(Path wrapper) { 58 if (wrapper == null) 59 throw new NullPointerException(); 60 if (!(wrapper instanceof PassThroughPath)) 61 throw new ProviderMismatchException(); 62 return ((PassThroughPath)wrapper).delegate; 63 } 64 65 @Override 66 public FileSystemProvider provider() { 67 return provider; 68 } 69 70 @Override 71 public void close() throws IOException { 72 delegate.close(); 73 } 74 75 @Override 76 public boolean isOpen() { 77 return delegate.isOpen(); 78 } 79 80 @Override 81 public boolean isReadOnly() { 82 return delegate.isReadOnly(); 83 } 84 108 itr.remove(); 109 } 110 }; 111 } 112 }; 113 } 114 115 @Override 116 public Iterable<FileStore> getFileStores() { 117 // assume that unwrapped objects aren't exposed 118 return delegate.getFileStores(); 119 } 120 121 @Override 122 public Set<String> supportedFileAttributeViews() { 123 // assume that unwrapped objects aren't exposed 124 return delegate.supportedFileAttributeViews(); 125 } 126 127 @Override 128 public Path getPath(String first, String... more) { 129 return new PassThroughPath(this, delegate.getPath(first, more)); 130 } 131 132 @Override 133 public PathMatcher getPathMatcher(String syntaxAndPattern) { 134 final PathMatcher matcher = delegate.getPathMatcher(syntaxAndPattern); 135 return new PathMatcher() { 136 @Override 137 public boolean matches(Path path) { 138 return matcher.matches(unwrap(path)); 139 } 140 }; 141 } 142 143 @Override 144 public UserPrincipalLookupService getUserPrincipalLookupService() { 145 // assume that unwrapped objects aren't exposed 146 return delegate.getUserPrincipalLookupService(); 147 } 148 149 @Override 150 public WatchService newWatchService() throws IOException { 151 // to keep it simple 152 throw new UnsupportedOperationException(); 153 } 154 155 static class PassThroughProvider extends FileSystemProvider { 156 private static final String SCHEME = "pass"; 157 private static volatile PassThroughFileSystem delegate; 158 190 } 191 192 @Override 193 public FileSystem getFileSystem(URI uri) { 194 checkUri(uri); 195 FileSystem result = delegate; 196 if (result == null) 197 throw new FileSystemNotFoundException(); 198 return result; 199 } 200 201 @Override 202 public Path getPath(URI uri) { 203 checkScheme(uri); 204 if (delegate == null) 205 throw new FileSystemNotFoundException(); 206 uri = URI.create(delegate.provider().getScheme() + ":" + 207 uri.getSchemeSpecificPart()); 208 return new PassThroughPath(delegate, delegate.provider().getPath(uri)); 209 } 210 211 @Override 212 public void setAttribute(Path file, String attribute, Object value, LinkOption... options) 213 throws IOException 214 { 215 Files.setAttribute(unwrap(file), attribute, value, options); 216 } 217 218 @Override 219 public Map<String,Object> readAttributes(Path file, String attributes, LinkOption... options) 220 throws IOException 221 { 222 return Files.readAttributes(unwrap(file), attributes, options); 223 } 224 225 @Override 226 public <V extends FileAttributeView> V getFileAttributeView(Path file, 227 Class<V> type, 228 LinkOption... options) 229 { 230 return Files.getFileAttributeView(unwrap(file), type, options); 231 } 232 233 @Override 234 public <A extends BasicFileAttributes> A readAttributes(Path file, 235 Class<A> type, 236 LinkOption... options) 237 throws IOException 238 { 239 return Files.readAttributes(unwrap(file), type, options); 240 } 241 242 @Override 243 public void delete(Path file) throws IOException { 244 Files.delete(unwrap(file)); 245 } 246 247 @Override 248 public void createSymbolicLink(Path link, Path target, FileAttribute<?>... attrs) 249 throws IOException 250 { 251 Files.createSymbolicLink(unwrap(link), unwrap(target), attrs); 252 } 253 254 @Override 255 public void createLink(Path link, Path existing) throws IOException { 256 Files.createLink(unwrap(link), unwrap(existing)); 257 } 258 259 @Override 260 public Path readSymbolicLink(Path link) throws IOException { 261 Path target = Files.readSymbolicLink(unwrap(link)); 262 return new PassThroughPath(delegate, target); 263 } 264 265 266 @Override 267 public void copy(Path source, Path target, CopyOption... options) throws IOException { 268 Files.copy(unwrap(source), unwrap(target), options); 269 } 270 271 @Override 272 public void move(Path source, Path target, CopyOption... options) throws IOException { 273 Files.move(unwrap(source), unwrap(target), options); 274 } 275 276 private DirectoryStream<Path> wrap(final DirectoryStream<Path> stream) { 277 return new DirectoryStream<Path>() { 278 @Override 279 public Iterator<Path> iterator() { 280 final Iterator<Path> itr = stream.iterator(); 281 return new Iterator<Path>() { 282 @Override 283 public boolean hasNext() { 284 return itr.hasNext(); 285 } 286 @Override 287 public Path next() { 288 return new PassThroughPath(delegate, itr.next()); 289 } 290 @Override 291 public void remove() { 292 itr.remove(); 293 } 294 }; 295 } 296 @Override 297 public void close() throws IOException { 298 stream.close(); 299 } 300 }; 301 } 302 303 @Override 304 public DirectoryStream<Path> newDirectoryStream(Path dir, DirectoryStream.Filter<? super Path> filter) 305 throws IOException 306 { 307 return wrap(Files.newDirectoryStream(dir, filter)); 308 } 309 310 @Override 311 public void createDirectory(Path dir, FileAttribute<?>... attrs) 312 throws IOException 313 { 314 Files.createDirectory(unwrap(dir), attrs); 315 } 316 317 @Override 318 public SeekableByteChannel newByteChannel(Path file, 319 Set<? extends OpenOption> options, 320 FileAttribute<?>... attrs) 321 throws IOException 322 { 323 return Files.newByteChannel(unwrap(file), options, attrs); 324 } 325 326 327 @Override 328 public boolean isHidden(Path file) throws IOException { 329 return Files.isHidden(unwrap(file)); 330 } 331 332 @Override 333 public FileStore getFileStore(Path file) throws IOException { 334 return Files.getFileStore(unwrap(file)); 335 } 336 337 @Override 338 public boolean isSameFile(Path file, Path other) throws IOException { 339 return Files.isSameFile(unwrap(file), unwrap(other)); 340 } 341 342 @Override 343 public void checkAccess(Path file, AccessMode... modes) 344 throws IOException 345 { 346 // hack 347 if (modes.length == 0) { 348 if (Files.exists(unwrap(file))) 349 return; 350 else 351 throw new NoSuchFileException(file.toString()); 352 } 353 throw new RuntimeException("not implemented yet"); 354 } 355 } 356 357 static class PassThroughPath implements Path { 358 private final FileSystem fs; 359 private final Path delegate; 360 361 PassThroughPath(FileSystem fs, Path delegate) { 362 this.fs = fs; 363 this.delegate = delegate; 364 } 365 366 private Path wrap(Path path) { 367 return (path != null) ? new PassThroughPath(fs, path) : null; 368 } 369 370 @Override 371 public FileSystem getFileSystem() { 372 return fs; 373 } 374 375 @Override 376 public boolean isAbsolute() { 377 return delegate.isAbsolute(); 378 } 379 380 @Override 381 public Path getRoot() { 382 return wrap(delegate.getRoot()); 383 } 384 385 @Override 386 public Path getParent() { 387 return wrap(delegate.getParent()); 388 } 389 390 @Override 391 public int getNameCount() { 392 return delegate.getNameCount(); 393 } 394 395 @Override 396 public Path getFileName() { 397 return wrap(delegate.getFileName()); 398 } 399 400 @Override 401 public Path getName(int index) { 402 return wrap(delegate.getName(index)); 403 } 404 405 @Override 406 public Path subpath(int beginIndex, int endIndex) { 407 return wrap(delegate.subpath(beginIndex, endIndex)); 408 } 409 410 @Override 411 public boolean startsWith(Path other) { 412 return delegate.startsWith(unwrap(other)); 413 } 414 415 @Override 416 public boolean startsWith(String other) { 417 return delegate.startsWith(other); 418 } 419 420 @Override 421 public boolean endsWith(Path other) { 422 return delegate.endsWith(unwrap(other)); 423 } 424 425 @Override 426 public boolean endsWith(String other) { 427 return delegate.endsWith(other); 428 } 429 430 @Override 431 public Path normalize() { 432 return wrap(delegate.normalize()); 433 } 434 435 @Override 436 public Path resolve(Path other) { 437 return wrap(delegate.resolve(unwrap(other))); 438 } 439 440 @Override 441 public Path resolve(String other) { 442 return wrap(delegate.resolve(other)); 443 } 444 445 @Override 446 public Path resolveSibling(Path other) { 447 return wrap(delegate.resolveSibling(unwrap(other))); 448 } 449 450 @Override 451 public Path resolveSibling(String other) { 452 return wrap(delegate.resolveSibling(other)); 453 } 454 455 @Override 456 public Path relativize(Path other) { 457 return wrap(delegate.relativize(unwrap(other))); 458 } 459 460 @Override 461 public boolean equals(Object other) { 462 if (!(other instanceof PassThroughPath)) 463 return false; 464 return delegate.equals(unwrap((PassThroughPath)other)); 465 } 466 467 @Override 468 public int hashCode() { 469 return delegate.hashCode(); 470 } 471 472 @Override 473 public String toString() { 474 return delegate.toString(); 475 } 476 477 @Override 478 public URI toUri() { 479 String ssp = delegate.toUri().getSchemeSpecificPart(); 480 return URI.create(fs.provider().getScheme() + ":" + ssp); 481 } 482 483 @Override 484 public Path toAbsolutePath() { 485 return wrap(delegate.toAbsolutePath()); 486 } 487 488 @Override 489 public Path toRealPath(boolean resolveLinks) throws IOException { 490 return wrap(delegate.toRealPath(resolveLinks)); 491 } 492 493 @Override 494 public File toFile() { 495 return delegate.toFile(); 496 } 497 498 @Override 499 public Iterator<Path> iterator() { 500 final Iterator<Path> itr = delegate.iterator(); 501 return new Iterator<Path>() { 502 @Override 503 public boolean hasNext() { 504 return itr.hasNext(); 505 } 506 @Override 507 public Path next() { 508 return wrap(itr.next()); 509 } 510 @Override 511 public void remove() { 512 itr.remove(); 513 } 514 }; 515 } 516 517 @Override 518 public int compareTo(Path other) { 519 return delegate.compareTo(unwrap(other)); 520 } 521 522 @Override 523 public WatchKey register(WatchService watcher, 524 WatchEvent.Kind<?>[] events, 525 WatchEvent.Modifier... modifiers) 526 { 527 throw new UnsupportedOperationException(); 528 } 529 530 @Override 531 public WatchKey register(WatchService watcher, 532 WatchEvent.Kind<?>... events) 533 { 534 throw new UnsupportedOperationException(); 535 } 536 } 537 } |