jdk/src/solaris/classes/sun/nio/fs/UnixFileSystemProvider.java
changeset 2057 3acf8e5e2ca0
child 3065 452aaa2899fc
equal deleted inserted replaced
2056:115e09b7a004 2057:3acf8e5e2ca0
       
     1 /*
       
     2  * Copyright 2008-2009 Sun Microsystems, Inc.  All Rights Reserved.
       
     3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
       
     4  *
       
     5  * This code is free software; you can redistribute it and/or modify it
       
     6  * under the terms of the GNU General Public License version 2 only, as
       
     7  * published by the Free Software Foundation.  Sun designates this
       
     8  * particular file as subject to the "Classpath" exception as provided
       
     9  * by Sun in the LICENSE file that accompanied this code.
       
    10  *
       
    11  * This code is distributed in the hope that it will be useful, but WITHOUT
       
    12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
       
    13  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
       
    14  * version 2 for more details (a copy is included in the LICENSE file that
       
    15  * accompanied this code).
       
    16  *
       
    17  * You should have received a copy of the GNU General Public License version
       
    18  * 2 along with this work; if not, write to the Free Software Foundation,
       
    19  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
       
    20  *
       
    21  * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
       
    22  * CA 95054 USA or visit www.sun.com if you need additional information or
       
    23  * have any questions.
       
    24  */
       
    25 
       
    26 package sun.nio.fs;
       
    27 
       
    28 import java.nio.file.*;
       
    29 import java.nio.file.attribute.*;
       
    30 import java.nio.file.spi.FileSystemProvider;
       
    31 import java.nio.channels.*;
       
    32 import java.net.URI;
       
    33 import java.util.concurrent.ExecutorService;
       
    34 import java.io.IOException;
       
    35 import java.util.*;
       
    36 
       
    37 import sun.nio.ch.ThreadPool;
       
    38 
       
    39 /**
       
    40  * Base implementation of FileSystemProvider
       
    41  */
       
    42 
       
    43 public abstract class UnixFileSystemProvider
       
    44     extends FileSystemProvider
       
    45 {
       
    46     private static final String USER_DIR = "user.dir";
       
    47     private final UnixFileSystem theFileSystem;
       
    48 
       
    49     public UnixFileSystemProvider() {
       
    50         String userDir = System.getProperty(USER_DIR);
       
    51         theFileSystem = newFileSystem(userDir);
       
    52     }
       
    53 
       
    54     /**
       
    55      * Constructs a new file system using the given default directory.
       
    56      */
       
    57     abstract UnixFileSystem newFileSystem(String dir);
       
    58 
       
    59     @Override
       
    60     public final String getScheme() {
       
    61         return "file";
       
    62     }
       
    63 
       
    64     private void checkUri(URI uri) {
       
    65         if (!uri.getScheme().equalsIgnoreCase(getScheme()))
       
    66             throw new IllegalArgumentException("URI does not match this provider");
       
    67         if (uri.getAuthority() != null)
       
    68             throw new IllegalArgumentException("Authority component present");
       
    69         if (uri.getPath() == null)
       
    70             throw new IllegalArgumentException("Path component is undefined");
       
    71         if (!uri.getPath().equals("/"))
       
    72             throw new IllegalArgumentException("Path component should be '/'");
       
    73         if (uri.getQuery() != null)
       
    74             throw new IllegalArgumentException("Query component present");
       
    75         if (uri.getFragment() != null)
       
    76             throw new IllegalArgumentException("Fragment component present");
       
    77     }
       
    78 
       
    79     @Override
       
    80     public final FileSystem newFileSystem(URI uri, Map<String,?> env) {
       
    81         checkUri(uri);
       
    82         throw new FileSystemAlreadyExistsException();
       
    83     }
       
    84 
       
    85     @Override
       
    86     public final FileSystem getFileSystem(URI uri) {
       
    87         checkUri(uri);
       
    88         return theFileSystem;
       
    89     }
       
    90 
       
    91     @Override
       
    92     public Path getPath(URI uri) {
       
    93         return UnixUriUtils.fromUri(theFileSystem, uri);
       
    94     }
       
    95 
       
    96     private UnixPath checkPath(Path obj) {
       
    97         if (obj == null)
       
    98             throw new NullPointerException();
       
    99         if (!(obj instanceof UnixPath))
       
   100             throw new ProviderMismatchException();
       
   101         return (UnixPath)obj;
       
   102     }
       
   103 
       
   104     @Override
       
   105     public final FileChannel newFileChannel(Path obj,
       
   106                                             Set<? extends OpenOption> options,
       
   107                                             FileAttribute<?>... attrs)
       
   108         throws IOException
       
   109     {
       
   110         UnixPath file = checkPath(obj);
       
   111         int mode = UnixFileModeAttribute
       
   112             .toUnixMode(UnixFileModeAttribute.ALL_READWRITE, attrs);
       
   113         try {
       
   114             return UnixChannelFactory.newFileChannel(file, options, mode);
       
   115         } catch (UnixException x) {
       
   116             x.rethrowAsIOException(file);
       
   117             return null;
       
   118         }
       
   119     }
       
   120 
       
   121     @Override
       
   122     public final AsynchronousFileChannel newAsynchronousFileChannel(Path obj,
       
   123                                                                     Set<? extends OpenOption> options,
       
   124                                                                     ExecutorService executor,
       
   125                                                                     FileAttribute<?>... attrs) throws IOException
       
   126     {
       
   127         UnixPath file = checkPath(obj);
       
   128         int mode = UnixFileModeAttribute
       
   129             .toUnixMode(UnixFileModeAttribute.ALL_READWRITE, attrs);
       
   130         ThreadPool pool = (executor == null) ? null : ThreadPool.wrap(executor, 0);
       
   131         try {
       
   132             return UnixChannelFactory
       
   133                 .newAsynchronousFileChannel(file, options, mode, pool);
       
   134         } catch (UnixException x) {
       
   135             x.rethrowAsIOException(file);
       
   136             return null;
       
   137         }
       
   138     }
       
   139 }