java.nio.file.FileSystemNotFoundException Java Examples

The following examples show how to use java.nio.file.FileSystemNotFoundException. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: Module.java    From synthea with Apache License 2.0 7 votes vote down vote up
private static void fixPathFromJar(URI uri) throws IOException {
  // this function is a hack to enable reading modules from within a JAR file
  // see https://stackoverflow.com/a/48298758
  if ("jar".equals(uri.getScheme())) {
    for (FileSystemProvider provider: FileSystemProvider.installedProviders()) {
      if (provider.getScheme().equalsIgnoreCase("jar")) {
        try {
          provider.getFileSystem(uri);
        } catch (FileSystemNotFoundException e) {
          // in this case we need to initialize it first:
          provider.newFileSystem(uri, Collections.emptyMap());
        }
      }
    }
  }
}
 
Example #2
Source File: SystemImage.java    From openjdk-jdk9 with GNU General Public License v2.0 7 votes vote down vote up
static SystemImage open() throws IOException {
    if (modulesImageExists) {
        // open a .jimage and build directory structure
        final ImageReader image = ImageReader.open(moduleImageFile);
        image.getRootDirectory();
        return new SystemImage() {
            @Override
            Node findNode(String path) throws IOException {
                return image.findNode(path);
            }
            @Override
            byte[] getResource(Node node) throws IOException {
                return image.getResource(node);
            }
            @Override
            void close() throws IOException {
                image.close();
            }
        };
    }
    if (Files.notExists(explodedModulesDir))
        throw new FileSystemNotFoundException(explodedModulesDir.toString());
    return new ExplodedImage(explodedModulesDir);
}
 
Example #3
Source File: NIOFileUtil.java    From Hadoop-BAM with MIT License 6 votes vote down vote up
/**
 * Convert the given path {@link URI} to a {@link Path} object.
 * @param uri the path to convert
 * @return a {@link Path} object
 */
public static Path asPath(URI uri) {
  try {
    return Paths.get(uri);
  } catch (FileSystemNotFoundException e) {
    ClassLoader cl = Thread.currentThread().getContextClassLoader();
    if (cl == null) {
      throw e;
    }
    try {
      return FileSystems.newFileSystem(uri, new HashMap<>(), cl).provider().getPath(uri);
    } catch (IOException ex) {
      throw new RuntimeException("Cannot create filesystem for " + uri, ex);
    }
  }
}
 
Example #4
Source File: FileSystemHandler.java    From tiny-remapper with GNU Lesser General Public License v3.0 6 votes vote down vote up
public static synchronized FileSystem open(URI uri) throws IOException {
	FileSystem ret = null;

	try {
		ret = FileSystems.getFileSystem(uri);
	} catch (FileSystemNotFoundException e) { }

	boolean opened;

	if (ret == null) {
		ret = FileSystems.newFileSystem(uri, Collections.emptyMap());
		opened = true;
	} else {
		opened = false;
	}

	RefData data = fsRefs.get(ret);

	if (data == null) {
		fsRefs.put(ret, new RefData(opened, 1));
	} else {
		data.refs++;
	}

	return ret;
}
 
Example #5
Source File: BundleFileSystemProvider.java    From incubator-taverna-language with Apache License 2.0 6 votes vote down vote up
@Override
public BundleFileSystem getFileSystem(URI uri) {
	synchronized (openFilesystems) {
		URI baseURI = baseURIFor(uri);
		WeakReference<BundleFileSystem> ref = openFilesystems.get(baseURI);
		if (ref == null) {
			throw new FileSystemNotFoundException(uri.toString());
		}
		BundleFileSystem fs = ref.get();
		if (fs == null) {
			openFilesystems.remove(baseURI);
			throw new FileSystemNotFoundException(uri.toString());
		}
		return fs;
	}
}
 
Example #6
Source File: ElasticMappingUtils.java    From vind with Apache License 2.0 6 votes vote down vote up
private static FileSystem getFileSystem(URI jarUri) {
    FileSystem fs = fileSystems.get(jarUri);
    if (fs == null) {
        synchronized (fileSystems) {
            fs = fileSystems.get(jarUri);
            if (fs == null) {
                try {
                    fs = FileSystems.getFileSystem(jarUri);
                } catch (FileSystemNotFoundException e1) {
                    try {
                        fs = FileSystems.newFileSystem(jarUri, Collections.emptyMap());
                    } catch (IOException e2) {
                        throw new IllegalStateException("Could not create FileSystem for " + jarUri, e2);
                    }
                }
                fileSystems.put(jarUri, fs);
            }
        }
    }
    return fs;
}
 
Example #7
Source File: SFTPFileSystemProviderTest.java    From sftp-fs with Apache License 2.0 6 votes vote down vote up
@Test
public void testRemoveFileSystem() throws IOException {
    addDirectory("/foo/bar");

    SFTPFileSystemProvider provider = new SFTPFileSystemProvider();
    URI uri;
    try (SFTPFileSystem fs = newFileSystem(provider, createEnv())) {
        SFTPPath path = new SFTPPath(fs, "/foo/bar");

        uri = path.toUri();

        assertFalse(provider.isHidden(path));
    }
    FileSystemNotFoundException exception = assertThrows(FileSystemNotFoundException.class, () -> provider.getPath(uri));
    assertEquals(uri.toString(), exception.getMessage());
}
 
Example #8
Source File: PathUtil.java    From manifold with Apache License 2.0 6 votes vote down vote up
public static Path create( URI uri )
{
  try
  {
    return Paths.get( uri );
  }
  catch( FileSystemNotFoundException nfe )
  {
    try
    {
      Map<String, String> env = new HashMap<>();
      env.put( "create", "true" ); // creates zip/jar file if not already exists
      FileSystem fs = FileSystems.newFileSystem( uri, env );
      return fs.provider().getPath( uri );
    }
    catch( IOException e )
    {
      throw new RuntimeException( e );
    }
  }
}
 
Example #9
Source File: JFileSystemProvider.java    From baratine with GNU General Public License v2.0 6 votes vote down vote up
private JFileSystem getLocalSystem()
{
  synchronized (_localSystem) {
    JFileSystem localSystem = _localSystem.getLevel();
    
    if (localSystem == null) {
      BartenderFileSystem fileSystem = BartenderFileSystem.getCurrent();

      if (fileSystem == null) {
        throw new FileSystemNotFoundException(L.l("cannot find local bfs file system"));
      }

      ServiceRef root = fileSystem.getRootServiceRef();

      localSystem = new JFileSystem(this, root);
      
      _localSystem.set(localSystem);
    }
    
    return localSystem;
  }
}
 
Example #10
Source File: MCRFileSystemProvider.java    From mycore with GNU General Public License v3.0 6 votes vote down vote up
@Override
public Path getPath(final URI uri) {
    if (!FS_URI.getScheme().equals(Objects.requireNonNull(uri).getScheme())) {
        throw new FileSystemNotFoundException("Unkown filesystem: " + uri);
    }
    String path = uri.getPath().substring(1);//URI path is absolute -> remove first slash
    String owner = null;
    for (int i = 0; i < path.length(); i++) {
        if (path.charAt(i) == MCRAbstractFileSystem.SEPARATOR) {
            break;
        }
        if (path.charAt(i) == ':') {
            owner = path.substring(0, i);
            path = path.substring(i + 1);
            break;
        }

    }
    return MCRAbstractFileSystem.getPath(owner, path, getFileSystemFromPathURI(FS_URI));
}
 
Example #11
Source File: Repository.java    From huntbugs with Apache License 2.0 6 votes vote down vote up
static Repository createDetectorsRepo(Class<?> clazz, String pluginName, Set<Path> paths) {
    CodeSource codeSource = clazz.getProtectionDomain().getCodeSource();
    if (codeSource == null) {
        throw new RuntimeException(format("Initializing plugin '%s' could not get code source for class %s", pluginName, clazz.getName()));
    }

    URL url = codeSource.getLocation();
    try {
        Path path = Paths.get(url.toURI());
        if(paths.add(path)) {
            if(Files.isDirectory(path)) {
                return new DirRepository(path);
            } else {
                return new JarRepository(new JarFile(path.toFile()));
            }
        } else {
            return createNullRepository();
        }
    } catch (URISyntaxException | FileSystemNotFoundException | IllegalArgumentException
            | IOException | UnsupportedOperationException e) {
        String errorMessage = format("Error creating detector repository for plugin '%s'", pluginName);
        throw new RuntimeException(errorMessage, e);
    }
}
 
Example #12
Source File: MCRFileSystemProvider.java    From mycore with GNU General Public License v3.0 6 votes vote down vote up
@Override
public Path getPath(final URI uri) {
    if (!FS_URI.getScheme().equals(Objects.requireNonNull(uri).getScheme())) {
        throw new FileSystemNotFoundException("Unkown filesystem: " + uri);
    }
    String path = uri.getPath().substring(1);//URI path is absolute -> remove first slash
    String owner = null;
    for (int i = 0; i < path.length(); i++) {
        if (path.charAt(i) == MCRAbstractFileSystem.SEPARATOR) {
            break;
        }
        if (path.charAt(i) == ':') {
            owner = path.substring(0, i);
            path = path.substring(i + 1);
            break;
        }

    }
    return MCRAbstractFileSystem.getPath(owner, path, getFileSystemFromPathURI(FS_URI));
}
 
Example #13
Source File: MCRIView2Tools.java    From mycore with GNU General Public License v3.0 6 votes vote down vote up
public static FileSystem getFileSystem(Path iviewFile) throws IOException {
    URI uri = URI.create("jar:" + iviewFile.toUri());
    try {
        return FileSystems.newFileSystem(uri, Collections.emptyMap(), MCRClassTools.getClassLoader());
    } catch (FileSystemAlreadyExistsException exc) {
        // block until file system is closed
        try {
            FileSystem fileSystem = FileSystems.getFileSystem(uri);
            while (fileSystem.isOpen()) {
                try {
                    Thread.sleep(10);
                } catch (InterruptedException ie) {
                    // get out of here
                    throw new IOException(ie);
                }
            }
        } catch (FileSystemNotFoundException fsnfe) {
            // seems closed now -> do nothing and try to return the file system again
            LOGGER.debug("Filesystem not found", fsnfe);
        }
        return getFileSystem(iviewFile);
    }
}
 
Example #14
Source File: MCRAbstractFileSystem.java    From mycore with GNU General Public License v3.0 6 votes vote down vote up
/**
 * Returns any subclass that implements and handles the given scheme.
 * @param scheme a valid {@link URI} scheme
 * @see FileSystemProvider#getScheme()
 * @throws FileSystemNotFoundException if no filesystem handles this scheme
 */
public static MCRAbstractFileSystem getInstance(String scheme) {
    URI uri;
    try {
        uri = MCRPaths.getURI(scheme, "helper", SEPARATOR_STRING);
    } catch (URISyntaxException e) {
        throw new MCRException(e);
    }
    for (FileSystemProvider provider : Iterables.concat(MCRPaths.webAppProvider,
        FileSystemProvider.installedProviders())) {
        if (provider.getScheme().equals(scheme)) {
            return (MCRAbstractFileSystem) provider.getFileSystem(uri);
        }
    }
    throw new FileSystemNotFoundException("Provider \"" + scheme + "\" not found");
}
 
Example #15
Source File: PathOps.java    From yajsync with GNU General Public License v3.0 5 votes vote down vote up
public static FileSystem fileSystemOf(String fsPathName)
        throws IOException, URISyntaxException
{
    URI uri = new URI(fsPathName);
    try {
        return FileSystems.getFileSystem(uri);
    } catch (FileSystemNotFoundException e) {
        Map<String, Object> empty = Collections.emptyMap();
        return FileSystems.newFileSystem(uri, empty);
    }
}
 
Example #16
Source File: MappingUriExtensions.java    From smarthome with Eclipse Public License 2.0 5 votes vote down vote up
@Override
public String toPath(java.net.URI uri) {
    java.net.URI ret = uri;
    try {
        ret = Paths.get(uri).toUri();
    } catch (FileSystemNotFoundException e) {
        // fall-back to the argument
    }
    String clientPath = removeTrailingSlash(ret.toASCIIString());
    if (clientLocation != null) {
        clientPath = clientPath.replace(serverLocation, clientLocation);
    }
    return clientPath;
}
 
Example #17
Source File: PathsTest.java    From ParallelGit with Apache License 2.0 5 votes vote down vote up
@Test(expected = FileSystemNotFoundException.class)
public void getArbitraryFileFromUri_invalidSid() {
  URI uri = GfsUriBuilder.fromFileSystem(gfs)
              .file("/test_file.txt")
              .sid("some_invalid_sid").build();
  Paths.get(uri);
}
 
Example #18
Source File: FaultyFileSystem.java    From jdk8u-dev-jdk with GNU General Public License v2.0 5 votes vote down vote up
@Override
public FileSystem getFileSystem(URI uri) {
    checkUri(uri);
    FileSystem result = delegate;
    if (result == null)
        throw new FileSystemNotFoundException();
    return result;
}
 
Example #19
Source File: FaultyFileSystem.java    From jdk8u-jdk with GNU General Public License v2.0 5 votes vote down vote up
@Override
public Path getPath(URI uri) {
    checkScheme(uri);
    if (delegate == null)
        throw new FileSystemNotFoundException();

    // only allow absolute path
    String path = uri.getSchemeSpecificPart();
    if (! path.startsWith("///")) {
        throw new IllegalArgumentException();
    }
    return new PassThroughFileSystem.PassThroughPath(delegate, delegate.root.resolve(path.substring(3)));
}
 
Example #20
Source File: FaultyFileSystem.java    From jdk8u-jdk with GNU General Public License v2.0 5 votes vote down vote up
@Override
public FileSystem getFileSystem(URI uri) {
    checkUri(uri);
    FileSystem result = delegate;
    if (result == null)
        throw new FileSystemNotFoundException();
    return result;
}
 
Example #21
Source File: NativeUtils.java    From native-utils with MIT License 5 votes vote down vote up
private static boolean isPosixCompliant() {
    try {
        return FileSystems.getDefault()
                .supportedFileAttributeViews()
                .contains("posix");
    } catch (FileSystemNotFoundException
            | ProviderNotFoundException
            | SecurityException e) {
        return false;
    }
}
 
Example #22
Source File: MCRStoreManagerTest.java    From mycore with GNU General Public License v3.0 5 votes vote down vote up
StoreConfig() throws IOException {
    String fsName = MCRStoreManagerTest.class.getSimpleName();
    URI jimfsURI = URI.create("jimfs://" + fsName);
    FileSystem fileSystem = Jimfs.newFileSystem(fsName, Configuration.unix());
    try {
        fileSystem = FileSystems.getFileSystem(jimfsURI);
    } catch (FileSystemNotFoundException e) {
        fileSystem = FileSystems.newFileSystem(jimfsURI, Map.of("fileSystem", fileSystem));
    }
    baseDir = fileSystem.getPath("/");
}
 
Example #23
Source File: FaultyFileSystem.java    From jdk8u_jdk with GNU General Public License v2.0 5 votes vote down vote up
@Override
public FileSystem getFileSystem(URI uri) {
    checkUri(uri);
    FileSystem result = delegate;
    if (result == null)
        throw new FileSystemNotFoundException();
    return result;
}
 
Example #24
Source File: FaultyFileSystem.java    From openjdk-8 with GNU General Public License v2.0 5 votes vote down vote up
@Override
public Path getPath(URI uri) {
    checkScheme(uri);
    if (delegate == null)
        throw new FileSystemNotFoundException();

    // only allow absolute path
    String path = uri.getSchemeSpecificPart();
    if (! path.startsWith("///")) {
        throw new IllegalArgumentException();
    }
    return new PassThroughFileSystem.PassThroughPath(delegate, delegate.root.resolve(path.substring(3)));
}
 
Example #25
Source File: FaultyFileSystem.java    From openjdk-8 with GNU General Public License v2.0 5 votes vote down vote up
@Override
public FileSystem getFileSystem(URI uri) {
    checkUri(uri);
    FileSystem result = delegate;
    if (result == null)
        throw new FileSystemNotFoundException();
    return result;
}
 
Example #26
Source File: SFTPFileSystemProviderTest.java    From sftp-fs with Apache License 2.0 5 votes vote down vote up
@Test
public void testGetPathFileSystemNotFound() {
    SFTPFileSystemProvider provider = new SFTPFileSystemProvider();
    URI uri = URI.create("sftp://sftp.github.com/");
    FileSystemNotFoundException exception = assertThrows(FileSystemNotFoundException.class, () -> provider.getPath(uri));
    assertEquals(uri.toString(), exception.getMessage());
}
 
Example #27
Source File: SFTPFileSystemProviderTest.java    From sftp-fs with Apache License 2.0 5 votes vote down vote up
@Test
public void testPathsAndFilesSupportFileSystemNotFound() {
    URI uri = URI.create("sftp://sftp.github.com/");
    FileSystemNotFoundException exception = assertThrows(FileSystemNotFoundException.class,
            () -> Paths.get(uri));
    assertEquals(uri.toString(), exception.getMessage());
}
 
Example #28
Source File: SFTPFileSystemProvider.java    From sftp-fs with Apache License 2.0 5 votes vote down vote up
private SFTPFileSystem getExistingFileSystem(URI uri) {
    URI normalizedURI = normalizeWithoutPassword(uri);
    synchronized (fileSystems) {
        SFTPFileSystem fs = fileSystems.get(normalizedURI);
        if (fs == null) {
            throw new FileSystemNotFoundException(uri.toString());
        }
        return fs;
    }
}
 
Example #29
Source File: NativeUtils.java    From WarpPI with Apache License 2.0 5 votes vote down vote up
private static boolean isPosixCompliant() {
	try {
		if (FileSystems.getDefault().supportedFileAttributeViews().contains("posix"))
			return true;
		return false;
	} catch (FileSystemNotFoundException | ProviderNotFoundException | SecurityException e) {
		return false;
	}
}
 
Example #30
Source File: GitFileSystemProviderGetFileSystemTest.java    From ParallelGit with Apache License 2.0 5 votes vote down vote up
@Test(expected = FileSystemNotFoundException.class)
public void getFileSystemForUriWithNoSessionId_shouldThrowFileSystemNotFoundException() {
  URI uri = GfsUriBuilder.fromFileSystem(gfs)
              .sid(null)
              .build();
  provider.getFileSystem(uri);
}