@CheckForNull public static Path getJRTFS() throws IOException { final File java9 = getJava9Home(); if (java9 == null) { return null; } final File jrtFsProvider = new File(java9,"jrt-fs.jar"); //NOI18N if (jrtFsProvider.exists() && jrtFsProvider.isFile() && jrtFsProvider.canRead()) { final ClassLoader cl = new URLClassLoader(new URL[]{ BaseUtilities.toURI(jrtFsProvider).toURL() }); final ServiceLoader<FileSystemProvider> sl = ServiceLoader.load(FileSystemProvider.class, cl); FileSystemProvider jrtp = null; for (FileSystemProvider fsp : sl) { if ("jrt".equals(fsp.getScheme())) { //NOI18N jrtp = fsp; break; } } if (jrtp != null) { return jrtp.getPath(URI.create("jrt:/")); //NOI18N } } return null; }
public ArchiveContainer(Path archivePath) throws IOException, ProviderNotFoundException, SecurityException { this.archivePath = archivePath; if (multiReleaseValue != null && archivePath.toString().endsWith(".jar")) { Map<String,String> env = Collections.singletonMap("multi-release", multiReleaseValue); FileSystemProvider jarFSProvider = fsInfo.getJarFSProvider(); Assert.checkNonNull(jarFSProvider, "should have been caught before!"); this.fileSystem = jarFSProvider.newFileSystem(archivePath, env); } else { this.fileSystem = FileSystems.newFileSystem(archivePath, null); } packages = new HashMap<>(); for (Path root : fileSystem.getRootDirectories()) { Files.walkFileTree(root, EnumSet.noneOf(FileVisitOption.class), Integer.MAX_VALUE, new SimpleFileVisitor<Path>() { @Override public FileVisitResult preVisitDirectory(Path dir, BasicFileAttributes attrs) { if (isValid(dir.getFileName())) { packages.put(new RelativeDirectory(root.relativize(dir).toString()), dir); return FileVisitResult.CONTINUE; } else { return FileVisitResult.SKIP_SUBTREE; } } }); } }
private FileSystem newFileSystem(String targetHome, URI uri, Map<String, ?> env) throws IOException { Objects.requireNonNull(targetHome); Path jrtfs = FileSystems.getDefault().getPath(targetHome, "lib", JRT_FS_JAR); if (Files.notExists(jrtfs)) { throw new IOException(jrtfs.toString() + " not exist"); } Map<String,?> newEnv = new HashMap<>(env); newEnv.remove("java.home"); ClassLoader cl = newJrtFsLoader(jrtfs); try { Class<?> c = Class.forName(JrtFileSystemProvider.class.getName(), false, cl); @SuppressWarnings("deprecation") Object tmp = c.newInstance(); return ((FileSystemProvider)tmp).newFileSystem(uri, newEnv); } catch (ClassNotFoundException | IllegalAccessException | InstantiationException e) { throw new IOException(e); } }
private FileSystem newFileSystem(String targetHome, URI uri, Map<String, ?> env) throws IOException { Objects.requireNonNull(targetHome); Path jrtfs = FileSystems.getDefault().getPath(targetHome, JRT_FS_JAR); if (Files.notExists(jrtfs)) { throw new IOException(jrtfs.toString() + " not exist"); } Map<String,?> newEnv = new HashMap<>(env); newEnv.remove("java.home"); ClassLoader cl = newJrtFsLoader(jrtfs); try { Class<?> c = Class.forName(JrtFileSystemProvider.class.getName(), false, cl); @SuppressWarnings("deprecation") Object tmp = c.newInstance(); return ((FileSystemProvider)tmp).newFileSystem(uri, newEnv); } catch (ClassNotFoundException | IllegalAccessException | InstantiationException e) { throw new IOException(e); } }
private boolean deletePath(FileSystemProvider provider, Path path) { // Simple file delete try { if (provider.deleteIfExists(path)) { System.out.println("Deleted path '" + path.toAbsolutePath() + "'"); } else { System.err.println("Could not delete path '" + path.toAbsolutePath() + "', it may not exist"); } } catch (Exception e) { System.err.println("Could not delete path '" + path.toAbsolutePath() + "': " + e.getMessage()); e.printStackTrace(); return false; } return true; }
@Override public CloudFileSystem createCloudFileSystem(FileSystemProvider provider, URI uri, Map<String, ?> env) { String cloudType = (String)env.get(CLOUD_TYPE_ENV); if (StringUtils.isBlank(cloudType)) { throw new IllegalArgumentException("The file system map must contain a '" + CLOUD_TYPE_ENV + "' which identifies the " + "cloud type to target. Accepted cloud types are: " + CloudHostConfigurationBuilder.getAllCloudHostSettingTypes()); } // Filter out the cloudType parameter Map<String,?> remainingEnv = env.entrySet() .stream() .filter(p -> !p.getKey().equals(CLOUD_TYPE_ENV)) .collect(Collectors.toMap(p -> p.getKey(), p -> p.getValue())); LOG.debug("Creating cloud host settings for environment type '{}', filesystem host '{}'", cloudType, uri.getHost()); CloudHostConfiguration config = new CloudHostConfigurationBuilder() .setType(cloudType) .setName(uri.getHost()) .setAttributes(remainingEnv) .build(); LOG.debug("Created cloud host settings for environment type '{}', filesystem host '{}'", cloudType, uri.getHost()); return createCloudFilesystemInternal(provider, config); }
/** * 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() + "'"); }
@Test public void testCreateCloudFileSystemInternalWillCreateTheFileSystemIfItHasntBeenCreatedYet() { CloudHostConfiguration config = context.mock(CloudHostConfiguration.class); FileSystemProvider provider = context.mock(FileSystemProvider.class); BlobStoreContext blobStoreContext = context.mock(BlobStoreContext.class); context.checking(new Expectations() {{ allowing(config).getName(); will(returnValue("test-config")); exactly(1).of(config).createBlobStoreContext(); will(returnValue(blobStoreContext)); }}); Assert.assertTrue(((Map<?,?>)WhiteboxImpl.getInternalState(impl, "cloudFileSystems")).isEmpty()); impl.createCloudFilesystemInternal(provider, config); Map<String,CloudFileSystem> cloudFileSystemsMap = ((Map<String,CloudFileSystem>)WhiteboxImpl.getInternalState(impl, "cloudFileSystems")); Assert.assertTrue(cloudFileSystemsMap.containsKey("test-config")); Assert.assertNotNull(cloudFileSystemsMap.get("test-config")); }
@Test public void testCreateCloudFileSystemInternalWillThrowAnErrorIfAnAttemptIsMadeToCreateTheFilesystemMoreThanOnce() { CloudHostConfiguration config = context.mock(CloudHostConfiguration.class); FileSystemProvider provider = context.mock(FileSystemProvider.class); BlobStoreContext blobStoreContext = context.mock(BlobStoreContext.class); context.checking(new Expectations() {{ allowing(config).getName(); will(returnValue("test-config")); exactly(1).of(config).createBlobStoreContext(); will(returnValue(blobStoreContext)); }}); impl.createCloudFilesystemInternal(provider, config); try { impl.createCloudFilesystemInternal(provider, config); Assert.fail("Expected an exception"); } catch (DuplicateCloudHostnameException e) { // OK } }
@Test public void testCreateCloudFileSystemInternalWillAllowTheFileSystemToBeCreatedAgainAfterItHasBeenClosed() throws IOException { CloudHostConfiguration config = context.mock(CloudHostConfiguration.class); FileSystemProvider provider = context.mock(FileSystemProvider.class); BlobStoreContext blobStoreContext = context.mock(BlobStoreContext.class); context.checking(new Expectations() {{ allowing(config).getName(); will(returnValue("test-config")); exactly(2).of(config).createBlobStoreContext(); will(returnValue(blobStoreContext)); exactly(1).of(blobStoreContext).close(); }}); // Create and close CloudFileSystem fs = impl.createCloudFilesystemInternal(provider, config); fs.close(); // Should now be able to create again CloudFileSystem fs2 = impl.createCloudFilesystemInternal(provider, config); Assert.assertNotEquals(fs, fs2); }
@Test public void testCreateCloudFileSystemWillUseTheCloudHostConfigurationBuilderToCreateACloudFileSystem() throws URISyntaxException, IOException { FileSystemProvider provider = context.mock(FileSystemProvider.class); URI uri = new URI("cloud", "mock-fs", "/path", "fragment"); // The host holds the name Map<String,Object> env = new HashMap<>(); env.put(JCloudsCloudHostProvider.CLOUD_TYPE_ENV, "mock-test"); // Test we can create the FS Assert.assertTrue(((Map<?,?>)WhiteboxImpl.getInternalState(impl, "cloudFileSystems")).isEmpty()); impl.createCloudFileSystem(provider, uri, env); Map<String,CloudFileSystem> cloudFileSystemsMap = ((Map<String,CloudFileSystem>)WhiteboxImpl.getInternalState(impl, "cloudFileSystems")); Assert.assertTrue(cloudFileSystemsMap.containsKey("mock-fs")); Assert.assertNotNull(cloudFileSystemsMap.get("mock-fs")); // Now get the FS back CloudFileSystem cloudFileSystem = impl.getCloudFileSystem(uri); Assert.assertNotNull(cloudFileSystem); Assert.assertEquals(provider, cloudFileSystem.provider()); Assert.assertEquals(MockCloudHostConfiguration.class, cloudFileSystem.getCloudHostConfiguration().getClass()); Assert.assertEquals("mock-fs", cloudFileSystem.getCloudHostConfiguration().getName()); // Close it and make sure we don't get it back cloudFileSystem.close(); Assert.assertNull(impl.getCloudFileSystem(uri)); }
protected CloudBasicFileAttributes mockStorageTypeAttributes(CloudFileSystem fs, final StorageType storageType, final Path path) throws IOException { final FileSystemProvider provider = context.mock(FileSystemProvider.class); final CloudBasicFileAttributes basicAttributes = storageType == null ? null : context.mock(CloudBasicFileAttributes.class); context.checking(new Expectations() {{ allowing(fs).provider(); will(returnValue(provider)); if (basicAttributes == null) { exactly(1).of(provider).readAttributes(path, CloudBasicFileAttributes.class); will(throwException(new FileNotFoundException())); } else { exactly(1).of(provider).readAttributes(path, CloudBasicFileAttributes.class); will(returnValue(basicAttributes)); allowing(basicAttributes).getStorageType(); will(returnValue(storageType)); } }}); return basicAttributes; }
public AbstractLocalFileSystem( URI uri, FileSystemProvider provider, Path cachePath, Map<String, Object> env, BiFunction<Path, Map<String, ?>, FileSystemIO> fileSystemIO ) throws IOException { super( uri, provider, env, cachePath, fileSystemIO ); this.cachePath = getFileSystemIO().getBaseDirectory(); if( Files.notExists( cachePath ) ) { Files.createDirectories( cachePath ); } // sm and existence check cachePath.getFileSystem().provider().checkAccess( cachePath, AccessMode.READ ); if( !Files.isWritable( cachePath ) ) { setReadOnly( true ); } }
/** * 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"); }
public HadoopFileSystem(FileSystemProvider provider, String host, int uriPort) throws IOException { this.provider = provider; int port = uriPort; if (port == -1) { port = 8020; // Default Hadoop port } // Create dynamic configuration Configuration conf = new Configuration(); if (host == null) { String defaultScheme = org.apache.hadoop.fs.FileSystem.getDefaultUri(conf).getScheme(); if (!"hdfs".equals(defaultScheme)) { throw new NullPointerException("Null host not permitted if default " + "Hadoop filesystem is not HDFS."); } } else { conf.set("fs.defaultFS", "hdfs://" + host + ":" + port + "/"); } this.fs = org.apache.hadoop.fs.FileSystem.get(conf); this.userPrincipalLookupService = new HadoopUserPrincipalLookupService(this); }
private static FileSystemProvider loadFileSystemProvider(URI uri) throws IOException { if (Objects.equals(uri.getScheme(), "file")) { return FileSystems.getFileSystem(URI.create("file:///")).provider(); } else { return FileSystems.newFileSystem(uri, emptyMap(), Hypelet.class.getClassLoader()).provider(); } }
/** * Initialize the FileSystemProvider. This method should be called before * system property os.name is set. See see bug 235739. */ private static void initFileSystemProvider() { try { FileSystemProvider.installedProviders(); new File(".").isFile(); } catch (Exception e) { Logger.getLogger(NbKeymapTest.class.getName()).log( Level.INFO, null, e); } }
private static Path getModulesRoot() { try { final File javaHome = new File(JDK9_HOME == null ? System.getProperty("java.home") : JDK9_HOME); //NOI18N final File jrtProvider = new File(new File(javaHome, "lib"), "jrt-fs.jar"); //NOI18N if (!jrtProvider.exists()) { return null; } final ClassLoader cl = new URLClassLoader( new URL[]{jrtProvider.toURI().toURL()}, ModuleTest.class.getClassLoader()); FileSystemProvider provider = null; for (FileSystemProvider p : ServiceLoader.load(FileSystemProvider.class, cl)) { if ("jrt".equals(p.getScheme())) { //NOI18N provider = p; break; } } if (provider == null) { return null; } final Path jimageRoot = provider.getPath(URI.create("jrt:///")); //NOI18N final Path modules = jimageRoot.resolve("modules"); return Files.exists(modules) ? modules : jimageRoot; } catch (IOException ioe) { LOG.log(Level.WARNING, "Cannot load jrt nio provider.", ioe); //NOI18N return null; } }
@Before public void before() { Path mockedPath = mock(Path.class); when(mockedPath.resolve(ArgumentMatchers.anyString())).thenReturn(mockedPath); FileSystem mockedFS = mock(FileSystem.class); FileSystemProvider fsProvider = mock(FileSystemProvider.class); when(mockedFS.provider()).thenReturn(fsProvider); pool = new UploaderPool(mockedPath); when(mockedPath.getFileSystem()).thenReturn(mockedFS); }
@BeforeClass public static void installMockUsableSpaceFS() throws Exception { FileSystem current = PathUtils.getDefaultFileSystem(); aPathPart = current.getSeparator() + 'a' + current.getSeparator(); bPathPart = current.getSeparator() + 'b' + current.getSeparator(); FileSystemProvider mock = new MockUsableSpaceFileSystemProvider(current); PathUtilsForTesting.installMock(mock.getFileSystem(null)); }
private static FileSystem defaultFileSystem() { // load default provider FileSystemProvider provider = AccessController .doPrivileged(new PrivilegedAction<FileSystemProvider>() { public FileSystemProvider run() { return getDefaultProvider(); } }); // return file system return provider.getFileSystem(URI.create("file:///")); }
/** * Returns the default FileSystemProvider. */ public static FileSystemProvider create() { String osname = AccessController .doPrivileged(new GetPropertyAction("os.name")); if (osname.equals("SunOS")) return createProvider("sun.nio.fs.SolarisFileSystemProvider"); if (osname.equals("Linux")) return createProvider("sun.nio.fs.LinuxFileSystemProvider"); if (osname.contains("OS X")) return createProvider("sun.nio.fs.MacOSXFileSystemProvider"); if (osname.equals("AIX")) return createProvider("sun.nio.fs.AixFileSystemProvider"); throw new AssertionError("Platform not recognized"); }
/** * Creates a new "pass through" file system. Useful for test environments * where the provider might not be deployed. */ static FileSystem create() throws IOException { FileSystemProvider provider = new PassThroughProvider(); Map<String,?> env = Collections.emptyMap(); URI uri = URI.create("pass:///"); return provider.newFileSystem(uri, env); }
public synchronized FileSystemProvider getJarFSProvider() { if (jarFSProvider != null) { return jarFSProvider; } for (FileSystemProvider provider: FileSystemProvider.installedProviders()) { if (provider.getScheme().equals("jar")) { return (jarFSProvider = provider); } } return null; }
private static FileSystem defaultFileSystem() { // load default provider FileSystemProvider provider = AccessController .doPrivileged(new PrivilegedAction<>() { public FileSystemProvider run() { return getDefaultProvider(); } }); // return file system return provider.getFileSystem(URI.create("file:///")); }
private FileSystemProvider getJarFSProvider() throws IOException { if (jarFSProvider != null) { return jarFSProvider; } for (FileSystemProvider provider: FileSystemProvider.installedProviders()) { if (provider.getScheme().equals("jar")) { return (jarFSProvider = provider); } } throw new ProviderNotFoundException("no provider found for .jar files"); }
/** * Returns the default FileSystemProvider. */ public static FileSystemProvider create() { String osname = GetPropertyAction.privilegedGetProperty("os.name"); if (osname.equals("SunOS")) return createProvider("sun.nio.fs.SolarisFileSystemProvider"); if (osname.equals("Linux")) return createProvider("sun.nio.fs.LinuxFileSystemProvider"); if (osname.contains("OS X")) return createProvider("sun.nio.fs.MacOSXFileSystemProvider"); if (osname.equals("AIX")) return createProvider("sun.nio.fs.AixFileSystemProvider"); throw new AssertionError("Platform not recognized"); }
protected int listDirectoryContents(FileSystemProvider provider, FileSystem fileSystem, Filter<Path> pathFilters, Path path, boolean recursive) { AtomicInteger pathsCounter = new AtomicInteger(0); FileSystemProviderHelper.iterateOverDirectoryContents(fileSystem, Optional.ofNullable(path), PathFilters.ACCEPT_ALL_FILTER, recursive, subPath -> { pathsCounter.addAndGet(printCloudPathAttributes(fileSystem, pathFilters, subPath.getResultPath())); return true; }); return pathsCounter.get(); }
protected CloudFileSystem createCloudFilesystemInternal(FileSystemProvider provider, CloudHostConfiguration config) { // Check the closed filesystems and remove them from the map removeClosedFilesystems(); String fsKey = config.getName(); cloudFileSystemsLock.readLock().lock(); try { // Test if the filesystem has already been created if (cloudFileSystems.containsKey(fsKey)) { throw new DuplicateCloudHostnameException(fsKey); } // Upgrade to a write lock cloudFileSystemsLock.readLock().unlock(); cloudFileSystemsLock.writeLock().lock(); try { // Create the blob store LOG.debug("Creating BlobStoreContext for '{}'", fsKey); BlobStoreContext context = config.createBlobStoreContext(); LOG.debug("Created BlobStoreContext for '{}'", fsKey); // Create the filesystem LOG.debug("Creating FileSystem for '{}'", fsKey); CloudFileSystem fs = new CloudFileSystem(provider, config, context); LOG.debug("Created FileSystem for '{}'", fsKey); // Add it to the map cloudFileSystems.put(fsKey, fs); } finally { // Downgrade to a read lock cloudFileSystemsLock.readLock().lock(); cloudFileSystemsLock.writeLock().unlock(); } return cloudFileSystems.get(fsKey); } finally { cloudFileSystemsLock.readLock().unlock(); } }
/** * Invoke this method instead of {@link FileSystems#newFileSystem(URI, Map, ClassLoader)} which * saves the provider for use by {@link #getFileSystem(URI)} * * @param uri * @param env * @param loader * @return * @throws IOException */ public static FileSystem newFileSystem(URI uri, Map<String,?> env, ClassLoader loader) throws IOException { FileSystem fileSystem = FileSystems.newFileSystem(uri, env, loader); if (fileSystem != null) { FileSystemProvider provider = fileSystem.provider(); providers.add(new WeakReference<FileSystemProvider>(provider)); } return fileSystem; }
@Before public void setUp() { impl = new DefaultCloudFileSystemImplementation(); provider = context.mock(FileSystemProvider.class); fs = context.mock(CloudFileSystem.class); config = context.mock(CloudHostConfiguration.class); context.checking(new Expectations() {{ allowing(fs).provider(); will(returnValue(provider)); allowing(fs).getCloudHostConfiguration(); will(returnValue(config)); }}); }
@Before public void setUp() { blobStore = context.mock(BlobStore.class); blobStoreContext = context.mock(BlobStoreContext.class); cloudPath = context.mock(CloudPath.class); view = new CloudFileAttributesView(blobStoreContext, cloudPath); provider = context.mock(FileSystemProvider.class); fs = context.mock(CloudFileSystem.class); config = context.mock(CloudHostConfiguration.class); context.checking(new Expectations() {{ allowing(blobStoreContext).getBlobStore(); will(returnValue(blobStore)); allowing(fs).provider(); will(returnValue(provider)); allowing(fs).getCloudHostConfiguration(); will(returnValue(config)); allowing(cloudPath).getFileSystem(); will(returnValue(fs)); allowing(cloudPath).getContainerName(); will(returnValue(TEST_CONTAINER)); allowing(cloudPath).getPathName(); will(returnValue(TEST_PATH)); }}); }
@Before public void setUp() { provider = context.mock(FileSystemProvider.class); cloudHostSettings = context.mock(CloudHostConfiguration.class); blobStoreContext = context.mock(BlobStoreContext.class); impl = new CloudFileSystem(provider, cloudHostSettings, blobStoreContext); }
@SuppressWarnings("LeakingThisInConstructor") protected AbstractFileSystem( URI uri, FileSystemProvider provider, Map<String, Object> env, Path path, BiFunction<Path, Map<String, ?>, FileSystemIO> fileSystemIO ) throws IOException { this.uri = uri; this.provider = provider; env.put( FileSystem.class.getName(), this ); this.fileSystemIO = fileSystemIO.apply( path, env ); readOnly = FileSystemUtils.isTrue( env, "readOnly" ); }