1 16 package org.apache.commons.vfs.impl; 17 18 import org.apache.commons.logging.Log; 19 import org.apache.commons.vfs.FileContentInfoFactory; 20 import org.apache.commons.vfs.FileName; 21 import org.apache.commons.vfs.FileObject; 22 import org.apache.commons.vfs.FileSystem; 23 import org.apache.commons.vfs.FileSystemConfigBuilder; 24 import org.apache.commons.vfs.FileSystemException; 25 import org.apache.commons.vfs.FileSystemManager; 26 import org.apache.commons.vfs.FileSystemOptions; 27 import org.apache.commons.vfs.FileType; 28 import org.apache.commons.vfs.FilesCache; 29 import org.apache.commons.vfs.NameScope; 30 import org.apache.commons.vfs.VFS; 31 import org.apache.commons.vfs.cache.SoftRefFilesCache; 32 import org.apache.commons.vfs.provider.AbstractFileName; 33 import org.apache.commons.vfs.provider.AbstractFileProvider; 34 import org.apache.commons.vfs.provider.DefaultURLStreamHandler; 35 import org.apache.commons.vfs.provider.FileProvider; 36 import org.apache.commons.vfs.provider.FileReplicator; 37 import org.apache.commons.vfs.provider.LocalFileProvider; 38 import org.apache.commons.vfs.provider.TemporaryFileStore; 39 import org.apache.commons.vfs.provider.UriParser; 40 import org.apache.commons.vfs.provider.VfsComponent; 41 42 import java.io.File ; 43 import java.net.URLStreamHandler ; 44 import java.net.URLStreamHandlerFactory ; 45 import java.util.ArrayList ; 46 import java.util.Collection ; 47 import java.util.HashMap ; 48 import java.util.Iterator ; 49 import java.util.Map ; 50 51 56 public class DefaultFileSystemManager 57 implements FileSystemManager 58 { 59 62 private LocalFileProvider localFileProvider; 63 64 67 private FileProvider defaultProvider; 68 69 72 private FileReplicator fileReplicator; 73 74 77 private final Map providers = new HashMap (); 78 79 82 private final ArrayList components = new ArrayList (); 83 84 87 private FileObject baseFile; 88 89 92 private FilesCache filesCache; 93 94 97 private FileContentInfoFactory fileContentInfoFactory; 98 99 102 private Log log; 103 104 107 private final DefaultVfsComponentContext context = 108 new DefaultVfsComponentContext(this); 109 110 private TemporaryFileStore tempFileStore; 111 private final FileTypeMap map = new FileTypeMap(); 112 private final VirtualFileProvider vfsProvider = new VirtualFileProvider(); 113 private boolean init; 114 115 118 protected Log getLogger() 119 { 120 return log; 121 } 122 123 130 public void addProvider(final String urlScheme, 131 final FileProvider provider) 132 throws FileSystemException 133 { 134 addProvider(new String []{urlScheme}, provider); 135 } 136 137 144 public void addProvider(final String [] urlSchemes, 145 final FileProvider provider) 146 throws FileSystemException 147 { 148 for (int i = 0; i < urlSchemes.length; i++) 150 { 151 final String scheme = urlSchemes[i]; 152 if (providers.containsKey(scheme)) 153 { 154 throw new FileSystemException("vfs.impl/multiple-providers-for-scheme.error", scheme); 155 } 156 } 157 158 setupComponent(provider); 160 161 for (int i = 0; i < urlSchemes.length; i++) 163 { 164 final String scheme = urlSchemes[i]; 165 providers.put(scheme, provider); 166 } 167 168 if (provider instanceof LocalFileProvider) 169 { 170 localFileProvider = (LocalFileProvider) provider; 171 } 172 } 173 174 177 public boolean hasProvider(final String scheme) 178 { 179 return providers.containsKey(scheme); 180 } 181 182 188 public void addExtensionMap(final String extension, final String scheme) 189 { 190 map.addExtension(extension, scheme); 191 } 192 193 199 public void addMimeTypeMap(final String mimeType, final String scheme) 200 { 201 map.addMimeType(mimeType, scheme); 202 } 203 204 209 public void setDefaultProvider(final FileProvider provider) 210 throws FileSystemException 211 { 212 setupComponent(provider); 213 defaultProvider = provider; 214 } 215 216 219 public FilesCache getFilesCache() 220 { 221 return filesCache; 222 } 223 224 227 public void setFilesCache(FilesCache filesCache) throws FileSystemException 228 { 229 if (init) 230 { 231 throw new FileSystemException("vfs.impl/already-inited.error"); 232 } 233 234 this.filesCache = filesCache; 235 } 236 237 240 public FileContentInfoFactory getFileContentInfoFactory() 241 { 242 return fileContentInfoFactory; 243 } 244 245 248 public void setFileContentInfoFactory(FileContentInfoFactory fileContentInfoFactory) throws FileSystemException 249 { 250 if (init) 251 { 252 throw new FileSystemException("vfs.impl/already-inited.error"); 253 } 254 255 this.fileContentInfoFactory = fileContentInfoFactory; 256 } 257 258 262 public void setReplicator(final FileReplicator replicator) 263 throws FileSystemException 264 { 265 setupComponent(replicator); 266 fileReplicator = replicator; 267 } 268 269 273 public void setTemporaryFileStore(final TemporaryFileStore tempFileStore) 274 throws FileSystemException 275 { 276 setupComponent(tempFileStore); 277 this.tempFileStore = tempFileStore; 278 } 279 280 283 public void setLogger(final Log log) 284 { 285 this.log = log; 286 } 287 288 291 private void setupComponent(final Object component) 292 throws FileSystemException 293 { 294 if (!components.contains(component)) 295 { 296 if (component instanceof VfsComponent) 297 { 298 final VfsComponent vfsComponent = (VfsComponent) component; 299 vfsComponent.setLogger(getLogger()); 300 vfsComponent.setContext(context); 301 vfsComponent.init(); 302 } 303 components.add(component); 304 } 305 } 306 307 310 private void closeComponent(final Object component) 311 { 312 if (component != null && components.contains(component)) 313 { 314 if (component instanceof VfsComponent) 315 { 316 final VfsComponent vfsComponent = (VfsComponent) component; 317 vfsComponent.close(); 318 } 319 components.remove(component); 320 } 321 } 322 323 328 public FileReplicator getReplicator() 329 throws FileSystemException 330 { 331 if (fileReplicator == null) 332 { 333 throw new FileSystemException("vfs.impl/no-replicator.error"); 334 } 335 return fileReplicator; 336 } 337 338 343 public TemporaryFileStore getTemporaryFileStore() 344 throws FileSystemException 345 { 346 if (tempFileStore == null) 347 { 348 throw new FileSystemException("vfs.impl/no-temp-file-store.error"); 349 } 350 return tempFileStore; 351 } 352 353 356 public void init() throws FileSystemException 357 { 358 if (filesCache == null) 359 { 360 filesCache = new SoftRefFilesCache(); 362 } 363 if (fileContentInfoFactory == null) 364 { 365 fileContentInfoFactory = new FileContentInfoFilenameFactory(); 366 } 367 368 setupComponent(filesCache); 369 setupComponent(vfsProvider); 370 371 init = true; 372 } 373 374 378 public void close() 379 { 380 if (!init) 381 { 382 return; 383 } 384 385 for (Iterator iterator = providers.values().iterator(); iterator.hasNext();) 387 { 388 final Object provider = iterator.next(); 389 closeComponent(provider); 390 } 391 392 closeComponent(defaultProvider); 394 closeComponent(fileReplicator); 395 closeComponent(tempFileStore); 396 397 components.clear(); 398 providers.clear(); 399 filesCache.close(); 400 localFileProvider = null; 401 defaultProvider = null; 402 fileReplicator = null; 403 tempFileStore = null; 404 init = false; 405 } 406 407 410 public void freeUnusedResources() 411 { 412 if (!init) 413 { 414 return; 415 } 416 417 for (Iterator iterator = providers.values().iterator(); iterator.hasNext();) 419 { 420 final AbstractFileProvider provider = (AbstractFileProvider) iterator.next(); 421 provider.freeUnusedResources(); 422 } 423 } 424 425 428 public void setBaseFile(final FileObject baseFile) 430 throws FileSystemException 431 { 432 this.baseFile = baseFile; 433 } 434 435 438 public void setBaseFile(final File baseFile) throws FileSystemException 439 { 440 this.baseFile = getLocalFileProvider().findLocalFile(baseFile); 441 } 442 443 446 public FileObject getBaseFile() throws FileSystemException 447 { 448 return baseFile; 449 } 450 451 454 public FileObject resolveFile(final String uri) throws FileSystemException 455 { 456 return resolveFile(getBaseFile(), uri); 458 } 459 460 463 464 public FileObject resolveFile(final String uri, final FileSystemOptions fileSystemOptions) throws FileSystemException 465 { 466 return resolveFile(getBaseFile(), uri, fileSystemOptions); 468 } 469 470 473 public FileObject resolveFile(final File baseFile, final String uri) 474 throws FileSystemException 475 { 476 final FileObject baseFileObj = 477 getLocalFileProvider().findLocalFile(baseFile); 478 return resolveFile(baseFileObj, uri); 479 } 480 481 484 public FileObject resolveFile(final FileObject baseFile, final String uri) 485 throws FileSystemException 486 { 487 return resolveFile(baseFile, uri, baseFile == null ? null : baseFile.getFileSystem().getFileSystemOptions()); 488 } 489 490 493 public FileObject resolveFile(final FileObject baseFile, final String uri, final FileSystemOptions fileSystemOptions) 494 throws FileSystemException 495 { 496 final FileObject realBaseFile; 497 if (baseFile != null && VFS.isUriStyle() && baseFile.getName().getType() == FileType.FILE) 498 { 499 realBaseFile = baseFile.getParent(); 500 } 501 else 502 { 503 realBaseFile = baseFile; 504 } 505 507 508 UriParser.checkUriEncoding(uri); 509 510 if (uri == null) 511 { 512 throw new IllegalArgumentException (); 513 } 514 515 final String scheme = UriParser.extractScheme(uri); 517 if (scheme != null) 518 { 519 final FileProvider provider = (FileProvider) providers.get(scheme); 521 if (provider != null) 522 { 523 return provider.findFile(realBaseFile, uri, fileSystemOptions); 524 } 525 } 527 528 if (localFileProvider != null 530 && localFileProvider.isAbsoluteLocalName(uri)) 531 { 532 return localFileProvider.findLocalFile(uri); 533 } 534 535 if (scheme != null) 536 { 537 if (defaultProvider == null) 539 { 540 throw new FileSystemException("vfs.impl/unknown-scheme.error", new Object []{scheme, uri}); 541 } 542 return defaultProvider.findFile(realBaseFile, uri, fileSystemOptions); 543 } 544 545 if (realBaseFile == null) 547 { 548 throw new FileSystemException("vfs.impl/find-rel-file.error", uri); 549 } 550 551 return realBaseFile.resolveFile(uri); 552 } 553 554 560 public FileName resolveName(final FileName root, final String path) throws FileSystemException 561 { 562 return resolveName(root, path, NameScope.FILE_SYSTEM); 563 } 564 565 574 public FileName resolveName(final FileName base, 575 final String name, 576 final NameScope scope) 577 throws FileSystemException 578 { 579 final FileName realBase; 580 if (base != null && VFS.isUriStyle() && base.getType() == FileType.FILE) 581 { 582 realBase = base.getParent(); 583 } 584 else 585 { 586 realBase = base; 587 } 588 589 final StringBuffer buffer = new StringBuffer (name); 590 591 UriParser.fixSeparators(buffer); 593 594 if (name.length() == 0 || name.charAt(0) != FileName.SEPARATOR_CHAR) 596 { 597 if (!VFS.isUriStyle()) 599 { 600 buffer.insert(0, FileName.SEPARATOR_CHAR); 602 } 603 buffer.insert(0, realBase.getPath()); 604 } 605 606 608 FileType fileType = UriParser.normalisePath(buffer); 610 611 final String resolvedPath = buffer.toString(); 613 if (!AbstractFileName.checkName(realBase.getPath(), resolvedPath, scope)) 614 { 615 throw new FileSystemException("vfs.provider/invalid-descendent-name.error", name); 616 } 617 618 String scheme = realBase.getScheme(); 619 String fullPath = realBase.getRootURI() + resolvedPath; 620 final FileProvider provider = (FileProvider) providers.get(scheme); 621 if (provider != null) 622 { 623 628 return provider.parseUri(realBase, fullPath); 629 } 630 631 if (scheme != null) 632 { 633 if (defaultProvider != null) 635 { 636 return defaultProvider.parseUri(realBase, fullPath); 637 } 638 } 639 640 return ((AbstractFileName) realBase).createName(resolvedPath, fileType); 643 } 644 645 650 public FileName resolveURI(String uri) throws FileSystemException 651 { 652 UriParser.checkUriEncoding(uri); 653 654 if (uri == null) 655 { 656 throw new IllegalArgumentException (); 657 } 658 659 final String scheme = UriParser.extractScheme(uri); 661 if (scheme != null) 662 { 663 final FileProvider provider = (FileProvider) providers.get(scheme); 665 if (provider != null) 666 { 667 return provider.parseUri(null, uri); 668 } 669 670 } 672 673 if (localFileProvider != null 675 && localFileProvider.isAbsoluteLocalName(uri)) 676 { 677 return localFileProvider.parseUri(null, uri); 678 } 679 680 if (scheme != null) 681 { 682 if (defaultProvider == null) 684 { 685 throw new FileSystemException("vfs.impl/unknown-scheme.error", new Object []{scheme, uri}); 686 } 687 return defaultProvider.parseUri(null, uri); 688 } 689 690 if (baseFile == null) 692 { 693 throw new FileSystemException("vfs.impl/find-rel-file.error", uri); 694 } 695 696 return resolveName(baseFile.getName(), uri, NameScope.FILE_SYSTEM); 697 } 698 699 702 public FileObject toFileObject(final File file) 703 throws FileSystemException 704 { 705 return getLocalFileProvider().findLocalFile(file); 706 } 707 708 711 public FileObject createFileSystem(final String scheme, 712 final FileObject file) 713 throws FileSystemException 714 { 715 final FileProvider provider = (FileProvider) providers.get(scheme); 716 if (provider == null) 717 { 718 throw new FileSystemException("vfs.impl/unknown-provider.error", new Object []{scheme, file}); 719 } 720 return provider.createFileSystem(scheme, file, file.getFileSystem().getFileSystemOptions()); 721 } 722 723 726 public FileObject createFileSystem(final FileObject file) 727 throws FileSystemException 728 { 729 final String scheme = map.getScheme(file); 730 if (scheme == null) 731 { 732 throw new FileSystemException("vfs.impl/no-provider-for-file.error", file); 733 } 734 735 return createFileSystem(scheme, file); 736 } 737 738 743 public boolean canCreateFileSystem(final FileObject file) throws FileSystemException 744 { 745 return (map.getScheme(file) != null); 746 } 747 748 751 public FileObject createVirtualFileSystem(final FileObject rootFile) 752 throws FileSystemException 753 { 754 return vfsProvider.createFileSystem(rootFile); 755 } 756 757 760 public FileObject createVirtualFileSystem(final String rootUri) 761 throws FileSystemException 762 { 763 return vfsProvider.createFileSystem(rootUri); 764 } 765 766 769 private LocalFileProvider getLocalFileProvider() 770 throws FileSystemException 771 { 772 if (localFileProvider == null) 773 { 774 throw new FileSystemException("vfs.impl/no-local-file-provider.error"); 775 } 776 return localFileProvider; 777 } 778 779 782 public URLStreamHandlerFactory getURLStreamHandlerFactory() 783 { 784 return new VfsStreamHandlerFactory(); 785 } 786 787 public void closeFileSystem(FileSystem filesystem) 788 { 789 FileProvider provider = (FileProvider) providers.get(filesystem.getRootName().getScheme()); 790 if (provider != null) 791 { 792 ((AbstractFileProvider) provider).closeFileSystem(filesystem); 793 } 794 } 795 796 800 final class VfsStreamHandlerFactory implements URLStreamHandlerFactory 801 { 802 public URLStreamHandler createURLStreamHandler(final String protocol) 803 { 804 FileProvider provider = (FileProvider) providers.get(protocol); 805 if (provider != null) 806 { 807 return new DefaultURLStreamHandler(context); 808 } 809 810 return new URLStreamHandlerProxy(); 812 } 813 } 814 815 818 public String [] getSchemes() 819 { 820 String schemes[] = new String [providers.size()]; 821 providers.keySet().toArray(schemes); 822 return schemes; 823 } 824 825 830 public Collection getProviderCapabilities(final String scheme) throws FileSystemException 831 { 832 FileProvider provider = (FileProvider) providers.get(scheme); 833 if (provider == null) 834 { 835 throw new FileSystemException("vfs.impl/unknown-scheme.error", new Object []{scheme}); 836 } 837 838 return provider.getCapabilities(); 839 } 840 841 846 public FileSystemConfigBuilder getFileSystemConfigBuilder(final String scheme) throws FileSystemException 847 { 848 FileProvider provider = (FileProvider) providers.get(scheme); 849 if (provider == null) 850 { 851 throw new FileSystemException("vfs.impl/unknown-scheme.error", new Object []{scheme}); 852 } 853 854 return provider.getConfigBuilder(); 855 } 856 } 857 | Popular Tags |