Advertisement
dutoitns

Paths.get(URI) that adds FileSystems

Jul 8th, 2023 (edited)
954
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.30 KB | Source Code | 0 0
  1. /**
  2.  * Developer notes on {@link FileSystemNotFoundException}:
  3.  * ================================================================================================================
  4.  * {@link FileSystemNotFoundException} will be thrown by {@link Paths#get(URI)} if you are trying to load a resource
  5.  * that is located within a Jar.
  6.  * https://stackoverflow.com/questions/15713119/java-nio-file-path-for-a-classpath-resource
  7.  *
  8.  * {@link java.nio.file.FileSystem} implements {@link Closeable}.  That said, I am not using it inside a
  9.  * <code>try-with-resource</code> statement because the {@link java.nio.file.FileSystem} is only created once - and
  10.  * then other resources in the same area can be loaded with the same {@link java.nio.file.FileSystem}
  11.  * (ie you don't have to repeatedly create and close {@link java.nio.file.FileSystem}s).
  12.  *
  13.  * Also, closing {@link java.nio.file.FileSystem}s are problematic, because the associated {@link Path} can then
  14.  * no longer be used.
  15.  * ================================================================================================================
  16.  */
  17. private static Path getPathFromUri(URI fileUri) {
  18.     Assert.isNotNull(fileUri);
  19.    
  20.     Path fileOrDirectory = null;
  21.     try {
  22.         fileOrDirectory = Paths.get(fileUri);
  23.     } catch (FileSystemNotFoundException e) {
  24.         // Not sure how threadsafe FileSystems are, so being safe, rather than sorry.
  25.         nfsLock.lock();
  26.         try {
  27.             // Try again, maybe another thread has created the required FileSystem
  28.             fileOrDirectory = Paths.get(fileUri);
  29.         } catch (FileSystemNotFoundException fsnfe) {
  30.             // Other threads have not yet created the required FileSystem
  31.            
  32.             if (logger.isInfoEnabled()) {
  33.                 logger.info("Creating a FileSystem for classpath resource: " + fileUri);
  34.             }
  35.            
  36.             try {
  37.                 FileSystems.newFileSystem(fileUri, Collections.<String, Object>emptyMap());
  38.             } catch (IOException ioe) {
  39.                 String errorMessage = "A problem occurred creating a FileSystem for classpath resource: " + fileUri;
  40.                 throw new RuntimeException(errorMessage, ioe);
  41.             }
  42.            
  43.             // Try again, now that the FileSystem has been created
  44.             fileOrDirectory = Paths.get(fileUri);
  45.         } finally {
  46.             nfsLock.unlock();
  47.         }
  48.     }
  49.     return fileOrDirectory;
  50. }
  51. private final static ReentrantLock nfsLock = new ReentrantLock();; // Lock for creating new FileSystems
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement