jdk/src/java.base/share/classes/jdk/internal/jrtfs/JrtFileSystemProvider.java
author sherman
Mon, 18 Apr 2016 10:57:11 -0700
changeset 37516 66a0579e606c
parent 37365 9cc4eb4d7491
child 37782 ad8fe7507ecc
permissions -rw-r--r--
8154403: JRT filesystem loaded by JDK8 with URLClassLoader is not closable since JDK-8147460 Reviewed-by: alanb
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
27565
729f9700483a 8049367: Modular Run-Time Images
chegar
parents:
diff changeset
     1
/*
37365
9cc4eb4d7491 8147460: Clean-up jrtfs implementation
sherman
parents: 36511
diff changeset
     2
 * Copyright (c) 2016, Oracle and/or its affiliates. All rights reserved.
27565
729f9700483a 8049367: Modular Run-Time Images
chegar
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
729f9700483a 8049367: Modular Run-Time Images
chegar
parents:
diff changeset
     4
 *
729f9700483a 8049367: Modular Run-Time Images
chegar
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
729f9700483a 8049367: Modular Run-Time Images
chegar
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
729f9700483a 8049367: Modular Run-Time Images
chegar
parents:
diff changeset
     7
 * published by the Free Software Foundation.  Oracle designates this
729f9700483a 8049367: Modular Run-Time Images
chegar
parents:
diff changeset
     8
 * particular file as subject to the "Classpath" exception as provided
729f9700483a 8049367: Modular Run-Time Images
chegar
parents:
diff changeset
     9
 * by Oracle in the LICENSE file that accompanied this code.
729f9700483a 8049367: Modular Run-Time Images
chegar
parents:
diff changeset
    10
 *
729f9700483a 8049367: Modular Run-Time Images
chegar
parents:
diff changeset
    11
 * This code is distributed in the hope that it will be useful, but WITHOUT
729f9700483a 8049367: Modular Run-Time Images
chegar
parents:
diff changeset
    12
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
729f9700483a 8049367: Modular Run-Time Images
chegar
parents:
diff changeset
    13
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
729f9700483a 8049367: Modular Run-Time Images
chegar
parents:
diff changeset
    14
 * version 2 for more details (a copy is included in the LICENSE file that
729f9700483a 8049367: Modular Run-Time Images
chegar
parents:
diff changeset
    15
 * accompanied this code).
729f9700483a 8049367: Modular Run-Time Images
chegar
parents:
diff changeset
    16
 *
729f9700483a 8049367: Modular Run-Time Images
chegar
parents:
diff changeset
    17
 * You should have received a copy of the GNU General Public License version
729f9700483a 8049367: Modular Run-Time Images
chegar
parents:
diff changeset
    18
 * 2 along with this work; if not, write to the Free Software Foundation,
729f9700483a 8049367: Modular Run-Time Images
chegar
parents:
diff changeset
    19
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
729f9700483a 8049367: Modular Run-Time Images
chegar
parents:
diff changeset
    20
 *
729f9700483a 8049367: Modular Run-Time Images
chegar
parents:
diff changeset
    21
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
729f9700483a 8049367: Modular Run-Time Images
chegar
parents:
diff changeset
    22
 * or visit www.oracle.com if you need additional information or have any
729f9700483a 8049367: Modular Run-Time Images
chegar
parents:
diff changeset
    23
 * questions.
729f9700483a 8049367: Modular Run-Time Images
chegar
parents:
diff changeset
    24
 */
729f9700483a 8049367: Modular Run-Time Images
chegar
parents:
diff changeset
    25
package jdk.internal.jrtfs;
729f9700483a 8049367: Modular Run-Time Images
chegar
parents:
diff changeset
    26
729f9700483a 8049367: Modular Run-Time Images
chegar
parents:
diff changeset
    27
import java.io.*;
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents: 31673
diff changeset
    28
import java.net.MalformedURLException;
9d0388c6b336 8142968: Module System implementation
alanb
parents: 31673
diff changeset
    29
import java.net.URL;
9d0388c6b336 8142968: Module System implementation
alanb
parents: 31673
diff changeset
    30
import java.net.URLClassLoader;
27565
729f9700483a 8049367: Modular Run-Time Images
chegar
parents:
diff changeset
    31
import java.nio.channels.*;
729f9700483a 8049367: Modular Run-Time Images
chegar
parents:
diff changeset
    32
import java.nio.file.*;
729f9700483a 8049367: Modular Run-Time Images
chegar
parents:
diff changeset
    33
import java.nio.file.DirectoryStream.Filter;
729f9700483a 8049367: Modular Run-Time Images
chegar
parents:
diff changeset
    34
import java.nio.file.attribute.*;
729f9700483a 8049367: Modular Run-Time Images
chegar
parents:
diff changeset
    35
import java.nio.file.spi.FileSystemProvider;
729f9700483a 8049367: Modular Run-Time Images
chegar
parents:
diff changeset
    36
import java.net.URI;
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents: 31673
diff changeset
    37
import java.security.AccessController;
9d0388c6b336 8142968: Module System implementation
alanb
parents: 31673
diff changeset
    38
import java.security.PrivilegedAction;
27565
729f9700483a 8049367: Modular Run-Time Images
chegar
parents:
diff changeset
    39
import java.util.HashMap;
729f9700483a 8049367: Modular Run-Time Images
chegar
parents:
diff changeset
    40
import java.util.Map;
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents: 31673
diff changeset
    41
import java.util.Objects;
27565
729f9700483a 8049367: Modular Run-Time Images
chegar
parents:
diff changeset
    42
import java.util.Set;
729f9700483a 8049367: Modular Run-Time Images
chegar
parents:
diff changeset
    43
import java.util.concurrent.ExecutorService;
729f9700483a 8049367: Modular Run-Time Images
chegar
parents:
diff changeset
    44
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents: 31673
diff changeset
    45
/**
9d0388c6b336 8142968: Module System implementation
alanb
parents: 31673
diff changeset
    46
 * File system provider for jrt file systems. Conditionally creates jrt fs on
9d0388c6b336 8142968: Module System implementation
alanb
parents: 31673
diff changeset
    47
 * .jimage file or exploded modules directory of underlying JDK.
9d0388c6b336 8142968: Module System implementation
alanb
parents: 31673
diff changeset
    48
 *
9d0388c6b336 8142968: Module System implementation
alanb
parents: 31673
diff changeset
    49
 * @implNote This class needs to maintain JDK 8 source compatibility.
9d0388c6b336 8142968: Module System implementation
alanb
parents: 31673
diff changeset
    50
 *
9d0388c6b336 8142968: Module System implementation
alanb
parents: 31673
diff changeset
    51
 * It is used internally in the JDK to implement jimage/jrtfs access,
9d0388c6b336 8142968: Module System implementation
alanb
parents: 31673
diff changeset
    52
 * but also compiled and delivered as part of the jrtfs.jar to support access
9d0388c6b336 8142968: Module System implementation
alanb
parents: 31673
diff changeset
    53
 * to the jimage file provided by the shipped JDK by tools running on JDK 8.
9d0388c6b336 8142968: Module System implementation
alanb
parents: 31673
diff changeset
    54
 */
27565
729f9700483a 8049367: Modular Run-Time Images
chegar
parents:
diff changeset
    55
public final class JrtFileSystemProvider extends FileSystemProvider {
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents: 31673
diff changeset
    56
27565
729f9700483a 8049367: Modular Run-Time Images
chegar
parents:
diff changeset
    57
    private volatile FileSystem theFileSystem;
729f9700483a 8049367: Modular Run-Time Images
chegar
parents:
diff changeset
    58
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents: 31673
diff changeset
    59
    public JrtFileSystemProvider() {
9d0388c6b336 8142968: Module System implementation
alanb
parents: 31673
diff changeset
    60
    }
27565
729f9700483a 8049367: Modular Run-Time Images
chegar
parents:
diff changeset
    61
729f9700483a 8049367: Modular Run-Time Images
chegar
parents:
diff changeset
    62
    @Override
729f9700483a 8049367: Modular Run-Time Images
chegar
parents:
diff changeset
    63
    public String getScheme() {
729f9700483a 8049367: Modular Run-Time Images
chegar
parents:
diff changeset
    64
        return "jrt";
729f9700483a 8049367: Modular Run-Time Images
chegar
parents:
diff changeset
    65
    }
729f9700483a 8049367: Modular Run-Time Images
chegar
parents:
diff changeset
    66
729f9700483a 8049367: Modular Run-Time Images
chegar
parents:
diff changeset
    67
    /**
729f9700483a 8049367: Modular Run-Time Images
chegar
parents:
diff changeset
    68
     * Need FilePermission ${java.home}/-", "read" to create or get jrt:/
729f9700483a 8049367: Modular Run-Time Images
chegar
parents:
diff changeset
    69
     */
729f9700483a 8049367: Modular Run-Time Images
chegar
parents:
diff changeset
    70
    private void checkPermission() {
729f9700483a 8049367: Modular Run-Time Images
chegar
parents:
diff changeset
    71
        SecurityManager sm = System.getSecurityManager();
729f9700483a 8049367: Modular Run-Time Images
chegar
parents:
diff changeset
    72
        if (sm != null) {
37365
9cc4eb4d7491 8147460: Clean-up jrtfs implementation
sherman
parents: 36511
diff changeset
    73
            String home = SystemImage.RUNTIME_HOME;
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents: 31673
diff changeset
    74
            FilePermission perm
9d0388c6b336 8142968: Module System implementation
alanb
parents: 31673
diff changeset
    75
                    = new FilePermission(home + File.separator + "-", "read");
27565
729f9700483a 8049367: Modular Run-Time Images
chegar
parents:
diff changeset
    76
            sm.checkPermission(perm);
729f9700483a 8049367: Modular Run-Time Images
chegar
parents:
diff changeset
    77
        }
729f9700483a 8049367: Modular Run-Time Images
chegar
parents:
diff changeset
    78
    }
729f9700483a 8049367: Modular Run-Time Images
chegar
parents:
diff changeset
    79
729f9700483a 8049367: Modular Run-Time Images
chegar
parents:
diff changeset
    80
    private void checkUri(URI uri) {
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents: 31673
diff changeset
    81
        if (!uri.getScheme().equalsIgnoreCase(getScheme())) {
27565
729f9700483a 8049367: Modular Run-Time Images
chegar
parents:
diff changeset
    82
            throw new IllegalArgumentException("URI does not match this provider");
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents: 31673
diff changeset
    83
        }
9d0388c6b336 8142968: Module System implementation
alanb
parents: 31673
diff changeset
    84
        if (uri.getAuthority() != null) {
27565
729f9700483a 8049367: Modular Run-Time Images
chegar
parents:
diff changeset
    85
            throw new IllegalArgumentException("Authority component present");
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents: 31673
diff changeset
    86
        }
9d0388c6b336 8142968: Module System implementation
alanb
parents: 31673
diff changeset
    87
        if (uri.getPath() == null) {
27565
729f9700483a 8049367: Modular Run-Time Images
chegar
parents:
diff changeset
    88
            throw new IllegalArgumentException("Path component is undefined");
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents: 31673
diff changeset
    89
        }
9d0388c6b336 8142968: Module System implementation
alanb
parents: 31673
diff changeset
    90
        if (!uri.getPath().equals("/")) {
27565
729f9700483a 8049367: Modular Run-Time Images
chegar
parents:
diff changeset
    91
            throw new IllegalArgumentException("Path component should be '/'");
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents: 31673
diff changeset
    92
        }
9d0388c6b336 8142968: Module System implementation
alanb
parents: 31673
diff changeset
    93
        if (uri.getQuery() != null) {
27565
729f9700483a 8049367: Modular Run-Time Images
chegar
parents:
diff changeset
    94
            throw new IllegalArgumentException("Query component present");
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents: 31673
diff changeset
    95
        }
9d0388c6b336 8142968: Module System implementation
alanb
parents: 31673
diff changeset
    96
        if (uri.getFragment() != null) {
27565
729f9700483a 8049367: Modular Run-Time Images
chegar
parents:
diff changeset
    97
            throw new IllegalArgumentException("Fragment component present");
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents: 31673
diff changeset
    98
        }
27565
729f9700483a 8049367: Modular Run-Time Images
chegar
parents:
diff changeset
    99
    }
729f9700483a 8049367: Modular Run-Time Images
chegar
parents:
diff changeset
   100
729f9700483a 8049367: Modular Run-Time Images
chegar
parents:
diff changeset
   101
    @Override
729f9700483a 8049367: Modular Run-Time Images
chegar
parents:
diff changeset
   102
    public FileSystem newFileSystem(URI uri, Map<String, ?> env)
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents: 31673
diff changeset
   103
            throws IOException {
37516
66a0579e606c 8154403: JRT filesystem loaded by JDK8 with URLClassLoader is not closable since JDK-8147460
sherman
parents: 37365
diff changeset
   104
        Objects.requireNonNull(env);
27565
729f9700483a 8049367: Modular Run-Time Images
chegar
parents:
diff changeset
   105
        checkPermission();
729f9700483a 8049367: Modular Run-Time Images
chegar
parents:
diff changeset
   106
        checkUri(uri);
37516
66a0579e606c 8154403: JRT filesystem loaded by JDK8 with URLClassLoader is not closable since JDK-8147460
sherman
parents: 37365
diff changeset
   107
        if (env.containsKey("java.home")) {
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents: 31673
diff changeset
   108
            return newFileSystem((String)env.get("java.home"), uri, env);
9d0388c6b336 8142968: Module System implementation
alanb
parents: 31673
diff changeset
   109
        } else {
37365
9cc4eb4d7491 8147460: Clean-up jrtfs implementation
sherman
parents: 36511
diff changeset
   110
            return new JrtFileSystem(this, env);
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents: 31673
diff changeset
   111
        }
9d0388c6b336 8142968: Module System implementation
alanb
parents: 31673
diff changeset
   112
    }
9d0388c6b336 8142968: Module System implementation
alanb
parents: 31673
diff changeset
   113
9d0388c6b336 8142968: Module System implementation
alanb
parents: 31673
diff changeset
   114
    private static final String JRT_FS_JAR = "jrt-fs.jar";
9d0388c6b336 8142968: Module System implementation
alanb
parents: 31673
diff changeset
   115
    private FileSystem newFileSystem(String targetHome, URI uri, Map<String, ?> env)
9d0388c6b336 8142968: Module System implementation
alanb
parents: 31673
diff changeset
   116
            throws IOException {
9d0388c6b336 8142968: Module System implementation
alanb
parents: 31673
diff changeset
   117
        Objects.requireNonNull(targetHome);
9d0388c6b336 8142968: Module System implementation
alanb
parents: 31673
diff changeset
   118
        Path jrtfs = FileSystems.getDefault().getPath(targetHome, JRT_FS_JAR);
9d0388c6b336 8142968: Module System implementation
alanb
parents: 31673
diff changeset
   119
        if (Files.notExists(jrtfs)) {
9d0388c6b336 8142968: Module System implementation
alanb
parents: 31673
diff changeset
   120
            throw new IOException(jrtfs.toString() + " not exist");
9d0388c6b336 8142968: Module System implementation
alanb
parents: 31673
diff changeset
   121
        }
9d0388c6b336 8142968: Module System implementation
alanb
parents: 31673
diff changeset
   122
        Map<String,?> newEnv = new HashMap<>(env);
9d0388c6b336 8142968: Module System implementation
alanb
parents: 31673
diff changeset
   123
        newEnv.remove("java.home");
9d0388c6b336 8142968: Module System implementation
alanb
parents: 31673
diff changeset
   124
        ClassLoader cl = newJrtFsLoader(jrtfs);
9d0388c6b336 8142968: Module System implementation
alanb
parents: 31673
diff changeset
   125
        try {
9d0388c6b336 8142968: Module System implementation
alanb
parents: 31673
diff changeset
   126
            Class<?> c = Class.forName(JrtFileSystemProvider.class.getName(), false, cl);
9d0388c6b336 8142968: Module System implementation
alanb
parents: 31673
diff changeset
   127
            return ((FileSystemProvider)c.newInstance()).newFileSystem(uri, newEnv);
9d0388c6b336 8142968: Module System implementation
alanb
parents: 31673
diff changeset
   128
        } catch (ClassNotFoundException |
9d0388c6b336 8142968: Module System implementation
alanb
parents: 31673
diff changeset
   129
                 IllegalAccessException |
9d0388c6b336 8142968: Module System implementation
alanb
parents: 31673
diff changeset
   130
                 InstantiationException e) {
9d0388c6b336 8142968: Module System implementation
alanb
parents: 31673
diff changeset
   131
            throw new IOException(e);
9d0388c6b336 8142968: Module System implementation
alanb
parents: 31673
diff changeset
   132
        }
9d0388c6b336 8142968: Module System implementation
alanb
parents: 31673
diff changeset
   133
    }
9d0388c6b336 8142968: Module System implementation
alanb
parents: 31673
diff changeset
   134
9d0388c6b336 8142968: Module System implementation
alanb
parents: 31673
diff changeset
   135
    private static class JrtFsLoader extends URLClassLoader {
9d0388c6b336 8142968: Module System implementation
alanb
parents: 31673
diff changeset
   136
        JrtFsLoader(URL[] urls) {
9d0388c6b336 8142968: Module System implementation
alanb
parents: 31673
diff changeset
   137
            super(urls);
9d0388c6b336 8142968: Module System implementation
alanb
parents: 31673
diff changeset
   138
        }
9d0388c6b336 8142968: Module System implementation
alanb
parents: 31673
diff changeset
   139
        @Override
9d0388c6b336 8142968: Module System implementation
alanb
parents: 31673
diff changeset
   140
        protected Class<?> loadClass(String cn, boolean resolve)
9d0388c6b336 8142968: Module System implementation
alanb
parents: 31673
diff changeset
   141
                throws ClassNotFoundException
9d0388c6b336 8142968: Module System implementation
alanb
parents: 31673
diff changeset
   142
        {
9d0388c6b336 8142968: Module System implementation
alanb
parents: 31673
diff changeset
   143
            Class<?> c = findLoadedClass(cn);
9d0388c6b336 8142968: Module System implementation
alanb
parents: 31673
diff changeset
   144
            if (c == null) {
9d0388c6b336 8142968: Module System implementation
alanb
parents: 31673
diff changeset
   145
                URL u = findResource(cn.replace('.', '/') + ".class");
9d0388c6b336 8142968: Module System implementation
alanb
parents: 31673
diff changeset
   146
                if (u != null) {
9d0388c6b336 8142968: Module System implementation
alanb
parents: 31673
diff changeset
   147
                    c = findClass(cn);
9d0388c6b336 8142968: Module System implementation
alanb
parents: 31673
diff changeset
   148
                } else {
9d0388c6b336 8142968: Module System implementation
alanb
parents: 31673
diff changeset
   149
                    return super.loadClass(cn, resolve);
9d0388c6b336 8142968: Module System implementation
alanb
parents: 31673
diff changeset
   150
                }
9d0388c6b336 8142968: Module System implementation
alanb
parents: 31673
diff changeset
   151
            }
9d0388c6b336 8142968: Module System implementation
alanb
parents: 31673
diff changeset
   152
            if (resolve)
9d0388c6b336 8142968: Module System implementation
alanb
parents: 31673
diff changeset
   153
                resolveClass(c);
9d0388c6b336 8142968: Module System implementation
alanb
parents: 31673
diff changeset
   154
            return c;
9d0388c6b336 8142968: Module System implementation
alanb
parents: 31673
diff changeset
   155
        }
9d0388c6b336 8142968: Module System implementation
alanb
parents: 31673
diff changeset
   156
    }
9d0388c6b336 8142968: Module System implementation
alanb
parents: 31673
diff changeset
   157
9d0388c6b336 8142968: Module System implementation
alanb
parents: 31673
diff changeset
   158
    private static URLClassLoader newJrtFsLoader(Path jrtfs) {
9d0388c6b336 8142968: Module System implementation
alanb
parents: 31673
diff changeset
   159
        final URL url;
9d0388c6b336 8142968: Module System implementation
alanb
parents: 31673
diff changeset
   160
        try {
9d0388c6b336 8142968: Module System implementation
alanb
parents: 31673
diff changeset
   161
            url = jrtfs.toUri().toURL();
9d0388c6b336 8142968: Module System implementation
alanb
parents: 31673
diff changeset
   162
        } catch (MalformedURLException mue) {
9d0388c6b336 8142968: Module System implementation
alanb
parents: 31673
diff changeset
   163
            throw new IllegalArgumentException(mue);
9d0388c6b336 8142968: Module System implementation
alanb
parents: 31673
diff changeset
   164
        }
9d0388c6b336 8142968: Module System implementation
alanb
parents: 31673
diff changeset
   165
9d0388c6b336 8142968: Module System implementation
alanb
parents: 31673
diff changeset
   166
        final URL[] urls = new URL[] { url };
9d0388c6b336 8142968: Module System implementation
alanb
parents: 31673
diff changeset
   167
        return AccessController.doPrivileged(
9d0388c6b336 8142968: Module System implementation
alanb
parents: 31673
diff changeset
   168
                new PrivilegedAction<URLClassLoader>() {
9d0388c6b336 8142968: Module System implementation
alanb
parents: 31673
diff changeset
   169
                    @Override
9d0388c6b336 8142968: Module System implementation
alanb
parents: 31673
diff changeset
   170
                    public URLClassLoader run() {
9d0388c6b336 8142968: Module System implementation
alanb
parents: 31673
diff changeset
   171
                        return new JrtFsLoader(urls);
9d0388c6b336 8142968: Module System implementation
alanb
parents: 31673
diff changeset
   172
                    }
9d0388c6b336 8142968: Module System implementation
alanb
parents: 31673
diff changeset
   173
                }
9d0388c6b336 8142968: Module System implementation
alanb
parents: 31673
diff changeset
   174
        );
27565
729f9700483a 8049367: Modular Run-Time Images
chegar
parents:
diff changeset
   175
    }
729f9700483a 8049367: Modular Run-Time Images
chegar
parents:
diff changeset
   176
729f9700483a 8049367: Modular Run-Time Images
chegar
parents:
diff changeset
   177
    @Override
729f9700483a 8049367: Modular Run-Time Images
chegar
parents:
diff changeset
   178
    public Path getPath(URI uri) {
729f9700483a 8049367: Modular Run-Time Images
chegar
parents:
diff changeset
   179
        checkPermission();
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents: 31673
diff changeset
   180
        if (!uri.getScheme().equalsIgnoreCase(getScheme())) {
27565
729f9700483a 8049367: Modular Run-Time Images
chegar
parents:
diff changeset
   181
            throw new IllegalArgumentException("URI does not match this provider");
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents: 31673
diff changeset
   182
        }
9d0388c6b336 8142968: Module System implementation
alanb
parents: 31673
diff changeset
   183
        if (uri.getAuthority() != null) {
27565
729f9700483a 8049367: Modular Run-Time Images
chegar
parents:
diff changeset
   184
            throw new IllegalArgumentException("Authority component present");
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents: 31673
diff changeset
   185
        }
9d0388c6b336 8142968: Module System implementation
alanb
parents: 31673
diff changeset
   186
        if (uri.getQuery() != null) {
27565
729f9700483a 8049367: Modular Run-Time Images
chegar
parents:
diff changeset
   187
            throw new IllegalArgumentException("Query component present");
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents: 31673
diff changeset
   188
        }
9d0388c6b336 8142968: Module System implementation
alanb
parents: 31673
diff changeset
   189
        if (uri.getFragment() != null) {
27565
729f9700483a 8049367: Modular Run-Time Images
chegar
parents:
diff changeset
   190
            throw new IllegalArgumentException("Fragment component present");
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents: 31673
diff changeset
   191
        }
27565
729f9700483a 8049367: Modular Run-Time Images
chegar
parents:
diff changeset
   192
        String path = uri.getPath();
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents: 31673
diff changeset
   193
        if (path == null || path.charAt(0) != '/') {
27565
729f9700483a 8049367: Modular Run-Time Images
chegar
parents:
diff changeset
   194
            throw new IllegalArgumentException("Invalid path component");
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents: 31673
diff changeset
   195
        }
27565
729f9700483a 8049367: Modular Run-Time Images
chegar
parents:
diff changeset
   196
        return getTheFileSystem().getPath(path);
729f9700483a 8049367: Modular Run-Time Images
chegar
parents:
diff changeset
   197
    }
729f9700483a 8049367: Modular Run-Time Images
chegar
parents:
diff changeset
   198
729f9700483a 8049367: Modular Run-Time Images
chegar
parents:
diff changeset
   199
    private FileSystem getTheFileSystem() {
729f9700483a 8049367: Modular Run-Time Images
chegar
parents:
diff changeset
   200
        checkPermission();
729f9700483a 8049367: Modular Run-Time Images
chegar
parents:
diff changeset
   201
        FileSystem fs = this.theFileSystem;
729f9700483a 8049367: Modular Run-Time Images
chegar
parents:
diff changeset
   202
        if (fs == null) {
729f9700483a 8049367: Modular Run-Time Images
chegar
parents:
diff changeset
   203
            synchronized (this) {
729f9700483a 8049367: Modular Run-Time Images
chegar
parents:
diff changeset
   204
                fs = this.theFileSystem;
729f9700483a 8049367: Modular Run-Time Images
chegar
parents:
diff changeset
   205
                if (fs == null) {
729f9700483a 8049367: Modular Run-Time Images
chegar
parents:
diff changeset
   206
                    try {
37365
9cc4eb4d7491 8147460: Clean-up jrtfs implementation
sherman
parents: 36511
diff changeset
   207
                        this.theFileSystem = fs = new JrtFileSystem(this, null);
27565
729f9700483a 8049367: Modular Run-Time Images
chegar
parents:
diff changeset
   208
                    } catch (IOException ioe) {
729f9700483a 8049367: Modular Run-Time Images
chegar
parents:
diff changeset
   209
                        throw new InternalError(ioe);
729f9700483a 8049367: Modular Run-Time Images
chegar
parents:
diff changeset
   210
                    }
729f9700483a 8049367: Modular Run-Time Images
chegar
parents:
diff changeset
   211
                }
729f9700483a 8049367: Modular Run-Time Images
chegar
parents:
diff changeset
   212
            }
729f9700483a 8049367: Modular Run-Time Images
chegar
parents:
diff changeset
   213
        }
729f9700483a 8049367: Modular Run-Time Images
chegar
parents:
diff changeset
   214
        return fs;
729f9700483a 8049367: Modular Run-Time Images
chegar
parents:
diff changeset
   215
    }
729f9700483a 8049367: Modular Run-Time Images
chegar
parents:
diff changeset
   216
729f9700483a 8049367: Modular Run-Time Images
chegar
parents:
diff changeset
   217
    @Override
729f9700483a 8049367: Modular Run-Time Images
chegar
parents:
diff changeset
   218
    public FileSystem getFileSystem(URI uri) {
729f9700483a 8049367: Modular Run-Time Images
chegar
parents:
diff changeset
   219
        checkPermission();
729f9700483a 8049367: Modular Run-Time Images
chegar
parents:
diff changeset
   220
        checkUri(uri);
729f9700483a 8049367: Modular Run-Time Images
chegar
parents:
diff changeset
   221
        return getTheFileSystem();
729f9700483a 8049367: Modular Run-Time Images
chegar
parents:
diff changeset
   222
    }
729f9700483a 8049367: Modular Run-Time Images
chegar
parents:
diff changeset
   223
729f9700483a 8049367: Modular Run-Time Images
chegar
parents:
diff changeset
   224
    // Checks that the given file is a JrtPath
37365
9cc4eb4d7491 8147460: Clean-up jrtfs implementation
sherman
parents: 36511
diff changeset
   225
    static final JrtPath toJrtPath(Path path) {
9cc4eb4d7491 8147460: Clean-up jrtfs implementation
sherman
parents: 36511
diff changeset
   226
        Objects.requireNonNull(path, "path");
9cc4eb4d7491 8147460: Clean-up jrtfs implementation
sherman
parents: 36511
diff changeset
   227
        if (!(path instanceof JrtPath)) {
27565
729f9700483a 8049367: Modular Run-Time Images
chegar
parents:
diff changeset
   228
            throw new ProviderMismatchException();
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents: 31673
diff changeset
   229
        }
37365
9cc4eb4d7491 8147460: Clean-up jrtfs implementation
sherman
parents: 36511
diff changeset
   230
        return (JrtPath) path;
27565
729f9700483a 8049367: Modular Run-Time Images
chegar
parents:
diff changeset
   231
    }
729f9700483a 8049367: Modular Run-Time Images
chegar
parents:
diff changeset
   232
729f9700483a 8049367: Modular Run-Time Images
chegar
parents:
diff changeset
   233
    @Override
729f9700483a 8049367: Modular Run-Time Images
chegar
parents:
diff changeset
   234
    public void checkAccess(Path path, AccessMode... modes) throws IOException {
37365
9cc4eb4d7491 8147460: Clean-up jrtfs implementation
sherman
parents: 36511
diff changeset
   235
        toJrtPath(path).checkAccess(modes);
27565
729f9700483a 8049367: Modular Run-Time Images
chegar
parents:
diff changeset
   236
    }
729f9700483a 8049367: Modular Run-Time Images
chegar
parents:
diff changeset
   237
729f9700483a 8049367: Modular Run-Time Images
chegar
parents:
diff changeset
   238
    @Override
31673
135283550686 8080511: Refresh of jimage support
jfdenise
parents: 27565
diff changeset
   239
    public Path readSymbolicLink(Path link) throws IOException {
37365
9cc4eb4d7491 8147460: Clean-up jrtfs implementation
sherman
parents: 36511
diff changeset
   240
        return toJrtPath(link).readSymbolicLink();
31673
135283550686 8080511: Refresh of jimage support
jfdenise
parents: 27565
diff changeset
   241
    }
135283550686 8080511: Refresh of jimage support
jfdenise
parents: 27565
diff changeset
   242
135283550686 8080511: Refresh of jimage support
jfdenise
parents: 27565
diff changeset
   243
    @Override
27565
729f9700483a 8049367: Modular Run-Time Images
chegar
parents:
diff changeset
   244
    public void copy(Path src, Path target, CopyOption... options)
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents: 31673
diff changeset
   245
            throws IOException {
37365
9cc4eb4d7491 8147460: Clean-up jrtfs implementation
sherman
parents: 36511
diff changeset
   246
        toJrtPath(src).copy(toJrtPath(target), options);
27565
729f9700483a 8049367: Modular Run-Time Images
chegar
parents:
diff changeset
   247
    }
729f9700483a 8049367: Modular Run-Time Images
chegar
parents:
diff changeset
   248
729f9700483a 8049367: Modular Run-Time Images
chegar
parents:
diff changeset
   249
    @Override
729f9700483a 8049367: Modular Run-Time Images
chegar
parents:
diff changeset
   250
    public void createDirectory(Path path, FileAttribute<?>... attrs)
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents: 31673
diff changeset
   251
            throws IOException {
37365
9cc4eb4d7491 8147460: Clean-up jrtfs implementation
sherman
parents: 36511
diff changeset
   252
        toJrtPath(path).createDirectory(attrs);
27565
729f9700483a 8049367: Modular Run-Time Images
chegar
parents:
diff changeset
   253
    }
729f9700483a 8049367: Modular Run-Time Images
chegar
parents:
diff changeset
   254
729f9700483a 8049367: Modular Run-Time Images
chegar
parents:
diff changeset
   255
    @Override
729f9700483a 8049367: Modular Run-Time Images
chegar
parents:
diff changeset
   256
    public final void delete(Path path) throws IOException {
37365
9cc4eb4d7491 8147460: Clean-up jrtfs implementation
sherman
parents: 36511
diff changeset
   257
        toJrtPath(path).delete();
27565
729f9700483a 8049367: Modular Run-Time Images
chegar
parents:
diff changeset
   258
    }
729f9700483a 8049367: Modular Run-Time Images
chegar
parents:
diff changeset
   259
729f9700483a 8049367: Modular Run-Time Images
chegar
parents:
diff changeset
   260
    @Override
729f9700483a 8049367: Modular Run-Time Images
chegar
parents:
diff changeset
   261
    @SuppressWarnings("unchecked")
729f9700483a 8049367: Modular Run-Time Images
chegar
parents:
diff changeset
   262
    public <V extends FileAttributeView> V
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents: 31673
diff changeset
   263
            getFileAttributeView(Path path, Class<V> type, LinkOption... options) {
37365
9cc4eb4d7491 8147460: Clean-up jrtfs implementation
sherman
parents: 36511
diff changeset
   264
        return JrtFileAttributeView.get(toJrtPath(path), type, options);
27565
729f9700483a 8049367: Modular Run-Time Images
chegar
parents:
diff changeset
   265
    }
729f9700483a 8049367: Modular Run-Time Images
chegar
parents:
diff changeset
   266
729f9700483a 8049367: Modular Run-Time Images
chegar
parents:
diff changeset
   267
    @Override
729f9700483a 8049367: Modular Run-Time Images
chegar
parents:
diff changeset
   268
    public FileStore getFileStore(Path path) throws IOException {
37365
9cc4eb4d7491 8147460: Clean-up jrtfs implementation
sherman
parents: 36511
diff changeset
   269
        return toJrtPath(path).getFileStore();
27565
729f9700483a 8049367: Modular Run-Time Images
chegar
parents:
diff changeset
   270
    }
729f9700483a 8049367: Modular Run-Time Images
chegar
parents:
diff changeset
   271
729f9700483a 8049367: Modular Run-Time Images
chegar
parents:
diff changeset
   272
    @Override
729f9700483a 8049367: Modular Run-Time Images
chegar
parents:
diff changeset
   273
    public boolean isHidden(Path path) {
37365
9cc4eb4d7491 8147460: Clean-up jrtfs implementation
sherman
parents: 36511
diff changeset
   274
        return toJrtPath(path).isHidden();
27565
729f9700483a 8049367: Modular Run-Time Images
chegar
parents:
diff changeset
   275
    }
729f9700483a 8049367: Modular Run-Time Images
chegar
parents:
diff changeset
   276
729f9700483a 8049367: Modular Run-Time Images
chegar
parents:
diff changeset
   277
    @Override
729f9700483a 8049367: Modular Run-Time Images
chegar
parents:
diff changeset
   278
    public boolean isSameFile(Path path, Path other) throws IOException {
37365
9cc4eb4d7491 8147460: Clean-up jrtfs implementation
sherman
parents: 36511
diff changeset
   279
        return toJrtPath(path).isSameFile(other);
27565
729f9700483a 8049367: Modular Run-Time Images
chegar
parents:
diff changeset
   280
    }
729f9700483a 8049367: Modular Run-Time Images
chegar
parents:
diff changeset
   281
729f9700483a 8049367: Modular Run-Time Images
chegar
parents:
diff changeset
   282
    @Override
729f9700483a 8049367: Modular Run-Time Images
chegar
parents:
diff changeset
   283
    public void move(Path src, Path target, CopyOption... options)
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents: 31673
diff changeset
   284
            throws IOException {
37365
9cc4eb4d7491 8147460: Clean-up jrtfs implementation
sherman
parents: 36511
diff changeset
   285
        toJrtPath(src).move(toJrtPath(target), options);
27565
729f9700483a 8049367: Modular Run-Time Images
chegar
parents:
diff changeset
   286
    }
729f9700483a 8049367: Modular Run-Time Images
chegar
parents:
diff changeset
   287
729f9700483a 8049367: Modular Run-Time Images
chegar
parents:
diff changeset
   288
    @Override
729f9700483a 8049367: Modular Run-Time Images
chegar
parents:
diff changeset
   289
    public AsynchronousFileChannel newAsynchronousFileChannel(Path path,
729f9700483a 8049367: Modular Run-Time Images
chegar
parents:
diff changeset
   290
            Set<? extends OpenOption> options,
729f9700483a 8049367: Modular Run-Time Images
chegar
parents:
diff changeset
   291
            ExecutorService exec,
729f9700483a 8049367: Modular Run-Time Images
chegar
parents:
diff changeset
   292
            FileAttribute<?>... attrs)
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents: 31673
diff changeset
   293
            throws IOException {
27565
729f9700483a 8049367: Modular Run-Time Images
chegar
parents:
diff changeset
   294
        throw new UnsupportedOperationException();
729f9700483a 8049367: Modular Run-Time Images
chegar
parents:
diff changeset
   295
    }
729f9700483a 8049367: Modular Run-Time Images
chegar
parents:
diff changeset
   296
729f9700483a 8049367: Modular Run-Time Images
chegar
parents:
diff changeset
   297
    @Override
729f9700483a 8049367: Modular Run-Time Images
chegar
parents:
diff changeset
   298
    public SeekableByteChannel newByteChannel(Path path,
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents: 31673
diff changeset
   299
            Set<? extends OpenOption> options,
9d0388c6b336 8142968: Module System implementation
alanb
parents: 31673
diff changeset
   300
            FileAttribute<?>... attrs)
9d0388c6b336 8142968: Module System implementation
alanb
parents: 31673
diff changeset
   301
            throws IOException {
37365
9cc4eb4d7491 8147460: Clean-up jrtfs implementation
sherman
parents: 36511
diff changeset
   302
        return toJrtPath(path).newByteChannel(options, attrs);
27565
729f9700483a 8049367: Modular Run-Time Images
chegar
parents:
diff changeset
   303
    }
729f9700483a 8049367: Modular Run-Time Images
chegar
parents:
diff changeset
   304
729f9700483a 8049367: Modular Run-Time Images
chegar
parents:
diff changeset
   305
    @Override
729f9700483a 8049367: Modular Run-Time Images
chegar
parents:
diff changeset
   306
    public DirectoryStream<Path> newDirectoryStream(
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents: 31673
diff changeset
   307
            Path path, Filter<? super Path> filter) throws IOException {
37365
9cc4eb4d7491 8147460: Clean-up jrtfs implementation
sherman
parents: 36511
diff changeset
   308
        return toJrtPath(path).newDirectoryStream(filter);
27565
729f9700483a 8049367: Modular Run-Time Images
chegar
parents:
diff changeset
   309
    }
729f9700483a 8049367: Modular Run-Time Images
chegar
parents:
diff changeset
   310
729f9700483a 8049367: Modular Run-Time Images
chegar
parents:
diff changeset
   311
    @Override
729f9700483a 8049367: Modular Run-Time Images
chegar
parents:
diff changeset
   312
    public FileChannel newFileChannel(Path path,
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents: 31673
diff changeset
   313
            Set<? extends OpenOption> options,
9d0388c6b336 8142968: Module System implementation
alanb
parents: 31673
diff changeset
   314
            FileAttribute<?>... attrs)
9d0388c6b336 8142968: Module System implementation
alanb
parents: 31673
diff changeset
   315
            throws IOException {
37365
9cc4eb4d7491 8147460: Clean-up jrtfs implementation
sherman
parents: 36511
diff changeset
   316
        return toJrtPath(path).newFileChannel(options, attrs);
27565
729f9700483a 8049367: Modular Run-Time Images
chegar
parents:
diff changeset
   317
    }
729f9700483a 8049367: Modular Run-Time Images
chegar
parents:
diff changeset
   318
729f9700483a 8049367: Modular Run-Time Images
chegar
parents:
diff changeset
   319
    @Override
729f9700483a 8049367: Modular Run-Time Images
chegar
parents:
diff changeset
   320
    public InputStream newInputStream(Path path, OpenOption... options)
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents: 31673
diff changeset
   321
            throws IOException {
37365
9cc4eb4d7491 8147460: Clean-up jrtfs implementation
sherman
parents: 36511
diff changeset
   322
        return toJrtPath(path).newInputStream(options);
27565
729f9700483a 8049367: Modular Run-Time Images
chegar
parents:
diff changeset
   323
    }
729f9700483a 8049367: Modular Run-Time Images
chegar
parents:
diff changeset
   324
729f9700483a 8049367: Modular Run-Time Images
chegar
parents:
diff changeset
   325
    @Override
729f9700483a 8049367: Modular Run-Time Images
chegar
parents:
diff changeset
   326
    public OutputStream newOutputStream(Path path, OpenOption... options)
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents: 31673
diff changeset
   327
            throws IOException {
37365
9cc4eb4d7491 8147460: Clean-up jrtfs implementation
sherman
parents: 36511
diff changeset
   328
        return toJrtPath(path).newOutputStream(options);
27565
729f9700483a 8049367: Modular Run-Time Images
chegar
parents:
diff changeset
   329
    }
729f9700483a 8049367: Modular Run-Time Images
chegar
parents:
diff changeset
   330
729f9700483a 8049367: Modular Run-Time Images
chegar
parents:
diff changeset
   331
    @Override
729f9700483a 8049367: Modular Run-Time Images
chegar
parents:
diff changeset
   332
    @SuppressWarnings("unchecked") // Cast to A
729f9700483a 8049367: Modular Run-Time Images
chegar
parents:
diff changeset
   333
    public <A extends BasicFileAttributes> A
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents: 31673
diff changeset
   334
            readAttributes(Path path, Class<A> type, LinkOption... options)
9d0388c6b336 8142968: Module System implementation
alanb
parents: 31673
diff changeset
   335
            throws IOException {
9d0388c6b336 8142968: Module System implementation
alanb
parents: 31673
diff changeset
   336
        if (type == BasicFileAttributes.class || type == JrtFileAttributes.class) {
37365
9cc4eb4d7491 8147460: Clean-up jrtfs implementation
sherman
parents: 36511
diff changeset
   337
            return (A) toJrtPath(path).getAttributes(options);
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents: 31673
diff changeset
   338
        }
27565
729f9700483a 8049367: Modular Run-Time Images
chegar
parents:
diff changeset
   339
        return null;
729f9700483a 8049367: Modular Run-Time Images
chegar
parents:
diff changeset
   340
    }
729f9700483a 8049367: Modular Run-Time Images
chegar
parents:
diff changeset
   341
729f9700483a 8049367: Modular Run-Time Images
chegar
parents:
diff changeset
   342
    @Override
729f9700483a 8049367: Modular Run-Time Images
chegar
parents:
diff changeset
   343
    public Map<String, Object>
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents: 31673
diff changeset
   344
            readAttributes(Path path, String attribute, LinkOption... options)
9d0388c6b336 8142968: Module System implementation
alanb
parents: 31673
diff changeset
   345
            throws IOException {
37365
9cc4eb4d7491 8147460: Clean-up jrtfs implementation
sherman
parents: 36511
diff changeset
   346
        return toJrtPath(path).readAttributes(attribute, options);
27565
729f9700483a 8049367: Modular Run-Time Images
chegar
parents:
diff changeset
   347
    }
729f9700483a 8049367: Modular Run-Time Images
chegar
parents:
diff changeset
   348
729f9700483a 8049367: Modular Run-Time Images
chegar
parents:
diff changeset
   349
    @Override
729f9700483a 8049367: Modular Run-Time Images
chegar
parents:
diff changeset
   350
    public void setAttribute(Path path, String attribute,
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents: 31673
diff changeset
   351
            Object value, LinkOption... options)
9d0388c6b336 8142968: Module System implementation
alanb
parents: 31673
diff changeset
   352
            throws IOException {
37365
9cc4eb4d7491 8147460: Clean-up jrtfs implementation
sherman
parents: 36511
diff changeset
   353
        toJrtPath(path).setAttribute(attribute, value, options);
27565
729f9700483a 8049367: Modular Run-Time Images
chegar
parents:
diff changeset
   354
    }
729f9700483a 8049367: Modular Run-Time Images
chegar
parents:
diff changeset
   355
}