Path: blob/master/test/jdk/java/nio/file/Files/PassThroughFileSystem.java
41153 views
/*1* Copyright (c) 2010, 2013, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation.7*8* This code is distributed in the hope that it will be useful, but WITHOUT9* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or10* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License11* version 2 for more details (a copy is included in the LICENSE file that12* accompanied this code).13*14* You should have received a copy of the GNU General Public License version15* 2 along with this work; if not, write to the Free Software Foundation,16* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.17*18* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA19* or visit www.oracle.com if you need additional information or have any20* questions.21*/2223import java.nio.file.*;24import java.nio.file.attribute.*;25import java.nio.file.spi.FileSystemProvider;26import java.nio.channels.SeekableByteChannel;27import java.net.URI;28import java.util.*;29import java.io.*;3031/**32* A "pass through" file system implementation that passes through, or delegates,33* everything to the default file system.34*/3536class PassThroughFileSystem extends FileSystem {37private final FileSystemProvider provider;38private final FileSystem delegate;3940PassThroughFileSystem(FileSystemProvider provider, FileSystem delegate) {41this.provider = provider;42this.delegate = delegate;43}4445/**46* Creates a new "pass through" file system. Useful for test environments47* where the provider might not be deployed.48*/49static FileSystem create() throws IOException {50FileSystemProvider provider = new PassThroughProvider();51Map<String,?> env = Collections.emptyMap();52URI uri = URI.create("pass:///");53return provider.newFileSystem(uri, env);54}5556static Path unwrap(Path wrapper) {57if (wrapper == null)58throw new NullPointerException();59if (!(wrapper instanceof PassThroughPath))60throw new ProviderMismatchException();61return ((PassThroughPath)wrapper).delegate;62}6364@Override65public FileSystemProvider provider() {66return provider;67}6869@Override70public void close() throws IOException {71delegate.close();72}7374@Override75public boolean isOpen() {76return delegate.isOpen();77}7879@Override80public boolean isReadOnly() {81return delegate.isReadOnly();82}8384@Override85public String getSeparator() {86return delegate.getSeparator();87}8889@Override90public Iterable<Path> getRootDirectories() {91final Iterable<Path> roots = delegate.getRootDirectories();92return new Iterable<Path>() {93@Override94public Iterator<Path> iterator() {95final Iterator<Path> itr = roots.iterator();96return new Iterator<Path>() {97@Override98public boolean hasNext() {99return itr.hasNext();100}101@Override102public Path next() {103return new PassThroughPath(delegate, itr.next());104}105@Override106public void remove() {107itr.remove();108}109};110}111};112}113114@Override115public Iterable<FileStore> getFileStores() {116// assume that unwrapped objects aren't exposed117return delegate.getFileStores();118}119120@Override121public Set<String> supportedFileAttributeViews() {122// assume that unwrapped objects aren't exposed123return delegate.supportedFileAttributeViews();124}125126@Override127public Path getPath(String first, String... more) {128return new PassThroughPath(this, delegate.getPath(first, more));129}130131@Override132public PathMatcher getPathMatcher(String syntaxAndPattern) {133final PathMatcher matcher = delegate.getPathMatcher(syntaxAndPattern);134return new PathMatcher() {135@Override136public boolean matches(Path path) {137return matcher.matches(unwrap(path));138}139};140}141142@Override143public UserPrincipalLookupService getUserPrincipalLookupService() {144// assume that unwrapped objects aren't exposed145return delegate.getUserPrincipalLookupService();146}147148@Override149public WatchService newWatchService() throws IOException {150// to keep it simple151throw new UnsupportedOperationException();152}153154static class PassThroughProvider extends FileSystemProvider {155private static final String SCHEME = "pass";156private static volatile PassThroughFileSystem delegate;157158public PassThroughProvider() { }159160@Override161public String getScheme() {162return SCHEME;163}164165private void checkScheme(URI uri) {166if (!uri.getScheme().equalsIgnoreCase(SCHEME))167throw new IllegalArgumentException();168}169170private void checkUri(URI uri) {171checkScheme(uri);172if (!uri.getSchemeSpecificPart().equals("///"))173throw new IllegalArgumentException();174}175176@Override177public FileSystem newFileSystem(URI uri, Map<String,?> env)178throws IOException179{180checkUri(uri);181synchronized (PassThroughProvider.class) {182if (delegate != null)183throw new FileSystemAlreadyExistsException();184PassThroughFileSystem result =185new PassThroughFileSystem(this, FileSystems.getDefault());186delegate = result;187return result;188}189}190191@Override192public FileSystem getFileSystem(URI uri) {193checkUri(uri);194FileSystem result = delegate;195if (result == null)196throw new FileSystemNotFoundException();197return result;198}199200@Override201public Path getPath(URI uri) {202checkScheme(uri);203if (delegate == null)204throw new FileSystemNotFoundException();205uri = URI.create(delegate.provider().getScheme() + ":" +206uri.getSchemeSpecificPart());207return new PassThroughPath(delegate, delegate.provider().getPath(uri));208}209210@Override211public void setAttribute(Path file, String attribute, Object value, LinkOption... options)212throws IOException213{214Files.setAttribute(unwrap(file), attribute, value, options);215}216217@Override218public Map<String,Object> readAttributes(Path file, String attributes, LinkOption... options)219throws IOException220{221return Files.readAttributes(unwrap(file), attributes, options);222}223224@Override225public <V extends FileAttributeView> V getFileAttributeView(Path file,226Class<V> type,227LinkOption... options)228{229return Files.getFileAttributeView(unwrap(file), type, options);230}231232@Override233public <A extends BasicFileAttributes> A readAttributes(Path file,234Class<A> type,235LinkOption... options)236throws IOException237{238return Files.readAttributes(unwrap(file), type, options);239}240241@Override242public void delete(Path file) throws IOException {243Files.delete(unwrap(file));244}245246@Override247public void createSymbolicLink(Path link, Path target, FileAttribute<?>... attrs)248throws IOException249{250Files.createSymbolicLink(unwrap(link), unwrap(target), attrs);251}252253@Override254public void createLink(Path link, Path existing) throws IOException {255Files.createLink(unwrap(link), unwrap(existing));256}257258@Override259public Path readSymbolicLink(Path link) throws IOException {260Path target = Files.readSymbolicLink(unwrap(link));261return new PassThroughPath(delegate, target);262}263264265@Override266public void copy(Path source, Path target, CopyOption... options) throws IOException {267Files.copy(unwrap(source), unwrap(target), options);268}269270@Override271public void move(Path source, Path target, CopyOption... options) throws IOException {272Files.move(unwrap(source), unwrap(target), options);273}274275private DirectoryStream<Path> wrap(final DirectoryStream<Path> stream) {276return new DirectoryStream<Path>() {277@Override278public Iterator<Path> iterator() {279final Iterator<Path> itr = stream.iterator();280return new Iterator<Path>() {281@Override282public boolean hasNext() {283return itr.hasNext();284}285@Override286public Path next() {287return new PassThroughPath(delegate, itr.next());288}289@Override290public void remove() {291itr.remove();292}293};294}295@Override296public void close() throws IOException {297stream.close();298}299};300}301302@Override303public DirectoryStream<Path> newDirectoryStream(Path dir, DirectoryStream.Filter<? super Path> filter)304throws IOException305{306return wrap(Files.newDirectoryStream(unwrap(dir), filter));307}308309@Override310public void createDirectory(Path dir, FileAttribute<?>... attrs)311throws IOException312{313Files.createDirectory(unwrap(dir), attrs);314}315316@Override317public SeekableByteChannel newByteChannel(Path file,318Set<? extends OpenOption> options,319FileAttribute<?>... attrs)320throws IOException321{322return Files.newByteChannel(unwrap(file), options, attrs);323}324325326@Override327public boolean isHidden(Path file) throws IOException {328return Files.isHidden(unwrap(file));329}330331@Override332public FileStore getFileStore(Path file) throws IOException {333return Files.getFileStore(unwrap(file));334}335336@Override337public boolean isSameFile(Path file, Path other) throws IOException {338return Files.isSameFile(unwrap(file), unwrap(other));339}340341@Override342public void checkAccess(Path file, AccessMode... modes)343throws IOException344{345// hack346if (modes.length == 0) {347if (Files.exists(unwrap(file)))348return;349else350throw new NoSuchFileException(file.toString());351}352throw new RuntimeException("not implemented yet");353}354}355356static class PassThroughPath implements Path {357private final FileSystem fs;358private final Path delegate;359360PassThroughPath(FileSystem fs, Path delegate) {361this.fs = fs;362this.delegate = delegate;363}364365private Path wrap(Path path) {366return (path != null) ? new PassThroughPath(fs, path) : null;367}368369@Override370public FileSystem getFileSystem() {371return fs;372}373374@Override375public boolean isAbsolute() {376return delegate.isAbsolute();377}378379@Override380public Path getRoot() {381return wrap(delegate.getRoot());382}383384@Override385public Path getParent() {386return wrap(delegate.getParent());387}388389@Override390public int getNameCount() {391return delegate.getNameCount();392}393394@Override395public Path getFileName() {396return wrap(delegate.getFileName());397}398399@Override400public Path getName(int index) {401return wrap(delegate.getName(index));402}403404@Override405public Path subpath(int beginIndex, int endIndex) {406return wrap(delegate.subpath(beginIndex, endIndex));407}408409@Override410public boolean startsWith(Path other) {411return delegate.startsWith(unwrap(other));412}413414@Override415public boolean startsWith(String other) {416return delegate.startsWith(other);417}418419@Override420public boolean endsWith(Path other) {421return delegate.endsWith(unwrap(other));422}423424@Override425public boolean endsWith(String other) {426return delegate.endsWith(other);427}428429@Override430public Path normalize() {431return wrap(delegate.normalize());432}433434@Override435public Path resolve(Path other) {436return wrap(delegate.resolve(unwrap(other)));437}438439@Override440public Path resolve(String other) {441return wrap(delegate.resolve(other));442}443444@Override445public Path resolveSibling(Path other) {446return wrap(delegate.resolveSibling(unwrap(other)));447}448449@Override450public Path resolveSibling(String other) {451return wrap(delegate.resolveSibling(other));452}453454@Override455public Path relativize(Path other) {456return wrap(delegate.relativize(unwrap(other)));457}458459@Override460public boolean equals(Object other) {461if (!(other instanceof PassThroughPath))462return false;463return delegate.equals(unwrap((PassThroughPath)other));464}465466@Override467public int hashCode() {468return delegate.hashCode();469}470471@Override472public String toString() {473return delegate.toString();474}475476@Override477public URI toUri() {478String ssp = delegate.toUri().getSchemeSpecificPart();479return URI.create(fs.provider().getScheme() + ":" + ssp);480}481482@Override483public Path toAbsolutePath() {484return wrap(delegate.toAbsolutePath());485}486487@Override488public Path toRealPath(LinkOption... options) throws IOException {489return wrap(delegate.toRealPath(options));490}491492@Override493public File toFile() {494return delegate.toFile();495}496497@Override498public Iterator<Path> iterator() {499final Iterator<Path> itr = delegate.iterator();500return new Iterator<Path>() {501@Override502public boolean hasNext() {503return itr.hasNext();504}505@Override506public Path next() {507return wrap(itr.next());508}509@Override510public void remove() {511itr.remove();512}513};514}515516@Override517public int compareTo(Path other) {518return delegate.compareTo(unwrap(other));519}520521@Override522public WatchKey register(WatchService watcher,523WatchEvent.Kind<?>[] events,524WatchEvent.Modifier... modifiers)525{526throw new UnsupportedOperationException();527}528529@Override530public WatchKey register(WatchService watcher,531WatchEvent.Kind<?>... events)532{533throw new UnsupportedOperationException();534}535}536}537538539