FileSystem { public static FileSystem get(URI uri) throws IOException { FileSystem fs; URI asked = uri; synchronized (SYNCHRONIZATION_OBJECT) { if (uri.getScheme() == null) { try { if (defaultScheme == null) { defaultScheme = new URI(ConfigConstants.DEFAULT_FILESYSTEM_SCHEME); } uri = new URI(defaultScheme.getScheme(), null, defaultScheme.getHost(), defaultScheme.getPort(), uri.getPath(), null, null); } catch (URISyntaxException e) { try { if (defaultScheme.getScheme().equals("file")) { uri = new URI("file", null, new Path(new File(uri.getPath()).getAbsolutePath()).toUri().getPath(), null); } } catch (URISyntaxException ex) { throw new IOException("The URI '" + uri.toString() + "' is not valid."); } } } if(uri.getScheme() == null) { throw new IOException("The URI '" + uri + "' is invalid.\n" + "The fs.default-scheme = " + defaultScheme + ", the requested URI = " + asked + ", and the final URI = " + uri + "."); } if (uri.getScheme().equals("file") && uri.getAuthority() != null && !uri.getAuthority().isEmpty()) { String supposedUri = "file: throw new IOException("Found local file path with authority '" + uri.getAuthority() + "' in path '" + uri.toString() + "'. Hint: Did you forget a slash? (correct path would be '" + supposedUri + "')"); } final FSKey key = new FSKey(uri.getScheme(), uri.getAuthority()); if (CACHE.containsKey(key)) { return CACHE.get(key); } if (!FSDIRECTORY.containsKey(uri.getScheme())) { Class<?> wrapperClass = getHadoopWrapperClassNameForFileSystem(uri.getScheme()); if (wrapperClass != null) { FSKey wrappedKey = new FSKey(HADOOP_WRAPPER_SCHEME + "+" + uri.getScheme(), uri.getAuthority()); if (CACHE.containsKey(wrappedKey)) { return CACHE.get(wrappedKey); } fs = instantiateHadoopFileSystemWrapper(wrapperClass); fs.initialize(uri); CACHE.put(wrappedKey, fs); } else { throw new IOException("No file system found with scheme " + uri.getScheme() + ", referenced in file URI '" + uri.toString() + "'."); } } else { String fsClass = FSDIRECTORY.get(uri.getScheme()); if (fsClass.equals(HADOOP_WRAPPER_FILESYSTEM_CLASS)) { fs = instantiateHadoopFileSystemWrapper(null); } else { fs = instantiateFileSystem(fsClass); } fs.initialize(uri); CACHE.put(key, fs); } } return fs; }  static FileSystem getLocalFileSystem(); static void setDefaultScheme(Configuration config); static FileSystem get(URI uri); abstract Path getWorkingDirectory(); abstract Path getHomeDirectory(); abstract URI getUri(); abstract void initialize(URI name); abstract FileStatus getFileStatus(Path f); abstract BlockLocation[] getFileBlockLocations(FileStatus file, long start, long len); abstract FSDataInputStream open(Path f, int bufferSize); abstract FSDataInputStream open(Path f); long getDefaultBlockSize(); abstract FileStatus[] listStatus(Path f); boolean exists(final Path f); abstract boolean delete(Path f, boolean recursive); abstract boolean mkdirs(Path f); abstract FSDataOutputStream create(Path f, boolean overwrite, int bufferSize, short replication,
			long blockSize); abstract FSDataOutputStream create(Path f, boolean overwrite); abstract boolean rename(Path src, Path dst); boolean initOutPathLocalFS(Path outPath, WriteMode writeMode, boolean createDirectory); boolean initOutPathDistFS(Path outPath, WriteMode writeMode, boolean createDirectory); abstract boolean isDistributedFS();  }
@Test public void testGet() throws URISyntaxException, IOException { String scheme = "file"; assertTrue(FileSystem.get(new URI(scheme + ": try { FileSystem.get(new URI(scheme + ": } catch (IOException ioe) { assertTrue(ioe.getMessage().startsWith("Found local file path with authority '")); } assertTrue(FileSystem.get(new URI(scheme + ":/test/test")) instanceof LocalFileSystem); assertTrue(FileSystem.get(new URI(scheme + ":test/test")) instanceof LocalFileSystem); assertTrue(FileSystem.get(new URI("/test/test")) instanceof LocalFileSystem); assertTrue(FileSystem.get(new URI("test/test")) instanceof LocalFileSystem); }