public List<Path> listFiles(Path dirPath) { List<Path> files = new ArrayList<>(); if (!getFS().isPresent()) { throw new FileSystemNotFoundException(""); } FileStatus[] fileStatuses = new FileStatus[0]; try { fileStatuses = getFS().get().listStatus(dirPath); } catch (IOException e) { log.error(e); } for (FileStatus f : fileStatuses) { if (f.isFile()) { files.add(f.getPath()); } } return files; }
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); }
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 ); } } }
/** * Retrieves a file system using the default {@link FileSystems#getFileSystem(URI)}. If this * throws a * @param uri * @return */ public static FileSystem getFileSystem(URI uri) { try { return FileSystems.getFileSystem(uri); } catch (FileSystemNotFoundException | ProviderNotFoundException e) { LOG.debug("File system scheme " + uri.getScheme() + " not found in the default installed providers list, attempting to find this in the " + "list of additional providers"); } for (WeakReference<FileSystemProvider> providerRef : providers) { FileSystemProvider provider = providerRef.get(); if (provider != null && uri.getScheme().equals(provider.getScheme())) { return provider.getFileSystem(uri); } } throw new ProviderNotFoundException("Could not find provider for scheme '" + uri.getScheme() + "'"); }
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); } }
public MCRIIIFImageInformation getInformation(String identifier) throws MCRIIIFImageNotFoundException, MCRIIIFImageProvidingException, MCRAccessException { try { Path tiledFile = tileFileProvider.getTiledFile(identifier); MCRTiledPictureProps tiledPictureProps = getTiledPictureProps(tiledFile); MCRIIIFImageInformation imageInformation = new MCRIIIFImageInformation(MCRIIIFBase.API_IMAGE_2, buildURL(identifier), DEFAULT_PROTOCOL, tiledPictureProps.getWidth(), tiledPictureProps.getHeight()); MCRIIIFImageTileInformation tileInformation = new MCRIIIFImageTileInformation(256, 256); for (int i = 0; i < tiledPictureProps.getZoomlevel(); i++) { tileInformation.scaleFactors.add((int) Math.pow(2, i)); } imageInformation.tiles.add(tileInformation); return imageInformation; } catch (FileSystemNotFoundException e) { LOGGER.error("Could not find Iview ZIP for {}", identifier, e); throw new MCRIIIFImageNotFoundException(identifier); } }
public static FileSystem getFileSystem(Path iviewFile) throws IOException { URI uri = URI.create("jar:" + iviewFile.toUri()); try { return FileSystems.newFileSystem(uri, Collections.emptyMap(), MCRIView2Tools.class.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); } }
/** * 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"); }
@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)); }
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; } }
private Path getPomPath() throws URISyntaxException { try { String className = getClass().getName(); String classfileName = "/" + className.replace('.', '/') + ".class"; URL classfileResource = getClass().getResource(classfileName); if (classfileResource != null) { Path absolutePackagePath = Paths.get(classfileResource.toURI()) .getParent(); int packagePathSegments = className.length() - className.replace(".", "").length(); Path path = absolutePackagePath; for (int i = 0, segmentsToRemove = packagePathSegments + 2; i < segmentsToRemove; i++) { path = path.getParent(); } return path.resolve("pom.xml"); } } catch (FileSystemNotFoundException e) { log.log(Level.INFO, "Not in Filesystem-Mode: " + e.getMessage(), e); } return null; }
@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; } }
protected AbstractTarFileSystem(AbstractTarFileSystemProvider provider, Path tfpath, Map<String, ?> env) throws IOException { // configurable env setup createNew = "true".equals(env.get("create")); defaultDir = env.containsKey("default.dir") ? (String) env .get("default.dir") : "/"; entriesToData = new HashMap<>(); if (defaultDir.charAt(0) != '/') { throw new IllegalArgumentException("default dir should be absolute"); } this.provider = provider; this.tfpath = tfpath; if (Files.notExists(tfpath)) { if (!createNew) { throw new FileSystemNotFoundException(tfpath.toString()); } } // sm and existence check tfpath.getFileSystem().provider().checkAccess(tfpath, AccessMode.READ); if (!Files.isWritable(tfpath)) { readOnly = true; } defaultdir = new TarPath(this, defaultDir.getBytes()); outputStreams = new ArrayList<>(); mapEntries(); }
public FileSystem getFileSystem( URI uri ) { checkURI( uri ); String id = uriMapper.getSchemeSpecificPart( uri ); FileSystem ret = fileSystems.get( id ); if( ret == null ) { throw new FileSystemNotFoundException( uri.toString() ); } if( !ret.isOpen() ) { fileSystems.remove( id ); // for GC throw new FileSystemNotFoundException( uri.toString() ); } return ret; }
/** * 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); } } }
@Test public void toPathThrowsIfHttpLocalJar() throws Exception { URL jar = new URL("jar:http://host/my/lib.jar!/"); try { ClassPathLocator.toPath(jar); fail("expected FileSystemNotFoundException."); } catch (FileSystemNotFoundException e) { assertThat(e.getMessage(), containsString("Provider \"http\" not installed")); } }
static FileSystem getFileSystem(final URI uri) throws IOException { try { return FileSystems.getFileSystem(uri); } catch (final FileSystemNotFoundException e) { return FileSystems.newFileSystem(uri, Collections.<String, String>emptyMap()); } }
private FileSystem getOrNewFileSystem(URI uri) throws IOException { try { return FileSystems.getFileSystem(uri); } catch (FileSystemNotFoundException exception) { return FileSystems.newFileSystem(uri, new HashMap<>()); } }
@Override public FileSystem getFileSystem(URI uri) { checkUri(uri); FileSystem result = delegate; if (result == null) throw new FileSystemNotFoundException(); return result; }
@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))); }
public static boolean isAvailable() { try { FileSystems.getFileSystem(URI.create("jrt:/")); return true; } catch (ProviderNotFoundException | FileSystemNotFoundException e) { return false; } }
/** * Convert a file uri to a file path, or null if this uri does not represent a file in the local file system. * @param uri * the uri string * @return the file path */ public static String toPath(String uri) { try { return Paths.get(new URI(uri)).toString(); } catch (URISyntaxException | IllegalArgumentException | FileSystemNotFoundException | SecurityException e) { return null; } }
/** * Check a string variable is an uri or not. */ public static boolean isUri(String uriString) { try { URI uri = new URI(uriString); return StringUtils.isNotBlank(uri.getScheme()); } catch (URISyntaxException | IllegalArgumentException | FileSystemNotFoundException | SecurityException e) { return false; } }
@Override public CloudFileSystem getFileSystem(URI uri) { CloudFileSystem cloudFileSystem = cloudHostProvider.getCloudFileSystem(uri); if (cloudFileSystem == null) { throw new FileSystemNotFoundException("Could not find the cloud file system for uri '" + uri.toString() + "'"); } return cloudFileSystem; }
@Override public final FileSystem getFileSystem( URI uri ) { URI fsUri = FileSystemUtils.getFileSystemURI( uri ); try { return newFileSystem( uri, new HashMap<>() ); } catch( IOException ex ) { throw new FileSystemNotFoundException( fsUri.toString() ); } }
/** Determine MIME type from URI. **/ public static MimeType fromURI(URI uri) { try { return fromFilename(Paths.get(uri).toString()); } catch (FileSystemNotFoundException e) { // For non-file URIs, try to guess the MIME type from the URL path, if possible return fromExtension(FilenameUtils.getExtension(uri.toString())); } }
/** * Get path from directory or jar * * @param dir * @return * @throws IOException */ private Path getPath(String ruleset) throws IOException { if (ruleset == null || ruleset.isEmpty()) { throw new IOException("Empty or null ruleset"); } if (!ruleset.startsWith(BUILTIN)) { LOG.info("Using validation queries from directory {}", ruleset); return Paths.get(ruleset); } String builtin = ruleset.replaceFirst(BUILTIN, "/"); LOG.info("Using built-in rulesets {}", builtin); URI uri; try { uri = Validator.class.getResource(builtin).toURI(); } catch (URISyntaxException ex) { throw new IOException(ex); } if (uri.getScheme().equals("jar")) { FileSystem fs; try { fs = FileSystems.getFileSystem(uri); } catch (FileSystemNotFoundException f) { fs = FileSystems.newFileSystem(uri, Collections.emptyMap()); } return fs.getPath(builtin); } return Paths.get(uri); }
private static boolean isPosixCompliant() { try { if (FileSystems.getDefault() .supportedFileAttributeViews() .contains("posix")) { return true; } return false; } catch (FileSystemNotFoundException | ProviderNotFoundException | SecurityException e) { return false; } }
public InternalBundleRepository() throws URISyntaxException, IOException { URI uriToLookup = InternalBundleRepository.class.getResource("/" + PATH_BOOTSTRAP_ARTIFACTS_LIST).toURI(); try { trySetupZipFileSystem(uriToLookup); } catch (FileSystemNotFoundException ex) { String[] splited = uriToLookup.toString().split("!", 2); jarRoot = FileSystems.newFileSystem(new URI(splited[0]), Collections.emptyMap()).getPath("/"); } repoRoot = jarRoot.resolve(PATH_BUNDLES_ROOT); artifacts = readArtifacts(jarRoot.resolve(PATH_ARTIFACTS_LIST)); bootstrapBundles = readArtifacts(jarRoot.resolve(PATH_BOOTSTRAP_ARTIFACTS_LIST)); resolveGA2V(); }
public Path getPath(String name) { try { URI uri = getClass().getResource(ROOT + name).toURI(); try { // first try getting a path via existing file systems return Paths.get(uri);// If file is available via existing file systems. } catch (final FileSystemNotFoundException e) { // not directly on file system, so then it's somewhere else (e.g.: JAR) final Map<String, ?> env = Collections.emptyMap(); return FileSystems.newFileSystem(uri, env).provider().getPath(uri); } } catch (Exception ex) { throw new RuntimeException(ex); } }