src/java.xml/share/classes/com/sun/org/apache/bcel/internal/util/ModularRuntimeImage.java
author joehw
Wed, 26 Jun 2019 05:49:59 +0000
changeset 55496 8e0ae3830fca
permissions -rw-r--r--
8224157: BCEL: update to version 6.3.1 Reviewed-by: dfuchs, lancea
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
55496
8e0ae3830fca 8224157: BCEL: update to version 6.3.1
joehw
parents:
diff changeset
     1
/*
8e0ae3830fca 8224157: BCEL: update to version 6.3.1
joehw
parents:
diff changeset
     2
 * reserved comment block
8e0ae3830fca 8224157: BCEL: update to version 6.3.1
joehw
parents:
diff changeset
     3
 * DO NOT REMOVE OR ALTER!
8e0ae3830fca 8224157: BCEL: update to version 6.3.1
joehw
parents:
diff changeset
     4
 */
8e0ae3830fca 8224157: BCEL: update to version 6.3.1
joehw
parents:
diff changeset
     5
/*
8e0ae3830fca 8224157: BCEL: update to version 6.3.1
joehw
parents:
diff changeset
     6
 * Licensed to the Apache Software Foundation (ASF) under one or more
8e0ae3830fca 8224157: BCEL: update to version 6.3.1
joehw
parents:
diff changeset
     7
 * contributor license agreements.  See the NOTICE file distributed with
8e0ae3830fca 8224157: BCEL: update to version 6.3.1
joehw
parents:
diff changeset
     8
 * this work for additional information regarding copyright ownership.
8e0ae3830fca 8224157: BCEL: update to version 6.3.1
joehw
parents:
diff changeset
     9
 * The ASF licenses this file to You under the Apache License, Version 2.0
8e0ae3830fca 8224157: BCEL: update to version 6.3.1
joehw
parents:
diff changeset
    10
 * (the "License"); you may not use this file except in compliance with
8e0ae3830fca 8224157: BCEL: update to version 6.3.1
joehw
parents:
diff changeset
    11
 * the License.  You may obtain a copy of the License at
8e0ae3830fca 8224157: BCEL: update to version 6.3.1
joehw
parents:
diff changeset
    12
 *
8e0ae3830fca 8224157: BCEL: update to version 6.3.1
joehw
parents:
diff changeset
    13
 *      http://www.apache.org/licenses/LICENSE-2.0
8e0ae3830fca 8224157: BCEL: update to version 6.3.1
joehw
parents:
diff changeset
    14
 *
8e0ae3830fca 8224157: BCEL: update to version 6.3.1
joehw
parents:
diff changeset
    15
 *  Unless required by applicable law or agreed to in writing, software
8e0ae3830fca 8224157: BCEL: update to version 6.3.1
joehw
parents:
diff changeset
    16
 *  distributed under the License is distributed on an "AS IS" BASIS,
8e0ae3830fca 8224157: BCEL: update to version 6.3.1
joehw
parents:
diff changeset
    17
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
8e0ae3830fca 8224157: BCEL: update to version 6.3.1
joehw
parents:
diff changeset
    18
 *  See the License for the specific language governing permissions and
8e0ae3830fca 8224157: BCEL: update to version 6.3.1
joehw
parents:
diff changeset
    19
 *  limitations under the License.
8e0ae3830fca 8224157: BCEL: update to version 6.3.1
joehw
parents:
diff changeset
    20
 *
8e0ae3830fca 8224157: BCEL: update to version 6.3.1
joehw
parents:
diff changeset
    21
 */
8e0ae3830fca 8224157: BCEL: update to version 6.3.1
joehw
parents:
diff changeset
    22
package com.sun.org.apache.bcel.internal.util;
8e0ae3830fca 8224157: BCEL: update to version 6.3.1
joehw
parents:
diff changeset
    23
8e0ae3830fca 8224157: BCEL: update to version 6.3.1
joehw
parents:
diff changeset
    24
import java.io.Closeable;
8e0ae3830fca 8224157: BCEL: update to version 6.3.1
joehw
parents:
diff changeset
    25
import java.io.File;
8e0ae3830fca 8224157: BCEL: update to version 6.3.1
joehw
parents:
diff changeset
    26
import java.io.IOException;
8e0ae3830fca 8224157: BCEL: update to version 6.3.1
joehw
parents:
diff changeset
    27
import java.net.URI;
8e0ae3830fca 8224157: BCEL: update to version 6.3.1
joehw
parents:
diff changeset
    28
import java.net.URL;
8e0ae3830fca 8224157: BCEL: update to version 6.3.1
joehw
parents:
diff changeset
    29
import java.net.URLClassLoader;
8e0ae3830fca 8224157: BCEL: update to version 6.3.1
joehw
parents:
diff changeset
    30
import java.nio.file.DirectoryStream;
8e0ae3830fca 8224157: BCEL: update to version 6.3.1
joehw
parents:
diff changeset
    31
import java.nio.file.FileSystem;
8e0ae3830fca 8224157: BCEL: update to version 6.3.1
joehw
parents:
diff changeset
    32
import java.nio.file.FileSystems;
8e0ae3830fca 8224157: BCEL: update to version 6.3.1
joehw
parents:
diff changeset
    33
import java.nio.file.Files;
8e0ae3830fca 8224157: BCEL: update to version 6.3.1
joehw
parents:
diff changeset
    34
import java.nio.file.Path;
8e0ae3830fca 8224157: BCEL: update to version 6.3.1
joehw
parents:
diff changeset
    35
import java.nio.file.Paths;
8e0ae3830fca 8224157: BCEL: update to version 6.3.1
joehw
parents:
diff changeset
    36
import java.util.ArrayList;
8e0ae3830fca 8224157: BCEL: update to version 6.3.1
joehw
parents:
diff changeset
    37
import java.util.Collections;
8e0ae3830fca 8224157: BCEL: update to version 6.3.1
joehw
parents:
diff changeset
    38
import java.util.Iterator;
8e0ae3830fca 8224157: BCEL: update to version 6.3.1
joehw
parents:
diff changeset
    39
import java.util.List;
8e0ae3830fca 8224157: BCEL: update to version 6.3.1
joehw
parents:
diff changeset
    40
import java.util.Map;
8e0ae3830fca 8224157: BCEL: update to version 6.3.1
joehw
parents:
diff changeset
    41
8e0ae3830fca 8224157: BCEL: update to version 6.3.1
joehw
parents:
diff changeset
    42
/**
8e0ae3830fca 8224157: BCEL: update to version 6.3.1
joehw
parents:
diff changeset
    43
 * Wraps a Java 9 JEP 220 modular runtime image. Requires the JRT NIO file system.
8e0ae3830fca 8224157: BCEL: update to version 6.3.1
joehw
parents:
diff changeset
    44
 *
8e0ae3830fca 8224157: BCEL: update to version 6.3.1
joehw
parents:
diff changeset
    45
 * @since 6.3
8e0ae3830fca 8224157: BCEL: update to version 6.3.1
joehw
parents:
diff changeset
    46
 */
8e0ae3830fca 8224157: BCEL: update to version 6.3.1
joehw
parents:
diff changeset
    47
public class ModularRuntimeImage implements Closeable {
8e0ae3830fca 8224157: BCEL: update to version 6.3.1
joehw
parents:
diff changeset
    48
8e0ae3830fca 8224157: BCEL: update to version 6.3.1
joehw
parents:
diff changeset
    49
    static final String MODULES_PATH = File.separator + "modules";
8e0ae3830fca 8224157: BCEL: update to version 6.3.1
joehw
parents:
diff changeset
    50
    static final String PACKAGES_PATH = File.separator + "packages";
8e0ae3830fca 8224157: BCEL: update to version 6.3.1
joehw
parents:
diff changeset
    51
8e0ae3830fca 8224157: BCEL: update to version 6.3.1
joehw
parents:
diff changeset
    52
    private final URLClassLoader classLoader;
8e0ae3830fca 8224157: BCEL: update to version 6.3.1
joehw
parents:
diff changeset
    53
    private final FileSystem fileSystem;
8e0ae3830fca 8224157: BCEL: update to version 6.3.1
joehw
parents:
diff changeset
    54
8e0ae3830fca 8224157: BCEL: update to version 6.3.1
joehw
parents:
diff changeset
    55
    /**
8e0ae3830fca 8224157: BCEL: update to version 6.3.1
joehw
parents:
diff changeset
    56
     * Constructs a default instance.
8e0ae3830fca 8224157: BCEL: update to version 6.3.1
joehw
parents:
diff changeset
    57
     *
8e0ae3830fca 8224157: BCEL: update to version 6.3.1
joehw
parents:
diff changeset
    58
     * @throws IOException
8e0ae3830fca 8224157: BCEL: update to version 6.3.1
joehw
parents:
diff changeset
    59
     *             an I/O error occurs accessing the file system
8e0ae3830fca 8224157: BCEL: update to version 6.3.1
joehw
parents:
diff changeset
    60
     */
8e0ae3830fca 8224157: BCEL: update to version 6.3.1
joehw
parents:
diff changeset
    61
    public ModularRuntimeImage() throws IOException {
8e0ae3830fca 8224157: BCEL: update to version 6.3.1
joehw
parents:
diff changeset
    62
        this(null, FileSystems.getFileSystem(URI.create("jrt:/")));
8e0ae3830fca 8224157: BCEL: update to version 6.3.1
joehw
parents:
diff changeset
    63
    }
8e0ae3830fca 8224157: BCEL: update to version 6.3.1
joehw
parents:
diff changeset
    64
8e0ae3830fca 8224157: BCEL: update to version 6.3.1
joehw
parents:
diff changeset
    65
    /**
8e0ae3830fca 8224157: BCEL: update to version 6.3.1
joehw
parents:
diff changeset
    66
     * Constructs an instance using the JRT file system implementation from a specific Java Home.
8e0ae3830fca 8224157: BCEL: update to version 6.3.1
joehw
parents:
diff changeset
    67
     *
8e0ae3830fca 8224157: BCEL: update to version 6.3.1
joehw
parents:
diff changeset
    68
     * @param javaHome
8e0ae3830fca 8224157: BCEL: update to version 6.3.1
joehw
parents:
diff changeset
    69
     *            Path to a Java 9 or greater home.
8e0ae3830fca 8224157: BCEL: update to version 6.3.1
joehw
parents:
diff changeset
    70
     *
8e0ae3830fca 8224157: BCEL: update to version 6.3.1
joehw
parents:
diff changeset
    71
     * @throws IOException
8e0ae3830fca 8224157: BCEL: update to version 6.3.1
joehw
parents:
diff changeset
    72
     *             an I/O error occurs accessing the file system
8e0ae3830fca 8224157: BCEL: update to version 6.3.1
joehw
parents:
diff changeset
    73
     */
8e0ae3830fca 8224157: BCEL: update to version 6.3.1
joehw
parents:
diff changeset
    74
    public ModularRuntimeImage(final String javaHome) throws IOException {
8e0ae3830fca 8224157: BCEL: update to version 6.3.1
joehw
parents:
diff changeset
    75
        final Map<String, ?> emptyMap = Collections.emptyMap();
8e0ae3830fca 8224157: BCEL: update to version 6.3.1
joehw
parents:
diff changeset
    76
        final Path jrePath = Paths.get(javaHome);
8e0ae3830fca 8224157: BCEL: update to version 6.3.1
joehw
parents:
diff changeset
    77
        final Path jrtFsPath = jrePath.resolve("lib").resolve("jrt-fs.jar");
8e0ae3830fca 8224157: BCEL: update to version 6.3.1
joehw
parents:
diff changeset
    78
        this.classLoader = new URLClassLoader(new URL[] {jrtFsPath.toUri().toURL() });
8e0ae3830fca 8224157: BCEL: update to version 6.3.1
joehw
parents:
diff changeset
    79
        this.fileSystem = FileSystems.newFileSystem(URI.create("jrt:/"), emptyMap, classLoader);
8e0ae3830fca 8224157: BCEL: update to version 6.3.1
joehw
parents:
diff changeset
    80
    }
8e0ae3830fca 8224157: BCEL: update to version 6.3.1
joehw
parents:
diff changeset
    81
8e0ae3830fca 8224157: BCEL: update to version 6.3.1
joehw
parents:
diff changeset
    82
    private ModularRuntimeImage(final URLClassLoader cl, final FileSystem fs) {
8e0ae3830fca 8224157: BCEL: update to version 6.3.1
joehw
parents:
diff changeset
    83
        this.classLoader = cl;
8e0ae3830fca 8224157: BCEL: update to version 6.3.1
joehw
parents:
diff changeset
    84
        this.fileSystem = fs;
8e0ae3830fca 8224157: BCEL: update to version 6.3.1
joehw
parents:
diff changeset
    85
    }
8e0ae3830fca 8224157: BCEL: update to version 6.3.1
joehw
parents:
diff changeset
    86
8e0ae3830fca 8224157: BCEL: update to version 6.3.1
joehw
parents:
diff changeset
    87
    @Override
8e0ae3830fca 8224157: BCEL: update to version 6.3.1
joehw
parents:
diff changeset
    88
    public void close() throws IOException {
8e0ae3830fca 8224157: BCEL: update to version 6.3.1
joehw
parents:
diff changeset
    89
        if (classLoader != null) {
8e0ae3830fca 8224157: BCEL: update to version 6.3.1
joehw
parents:
diff changeset
    90
            if (classLoader != null) {
8e0ae3830fca 8224157: BCEL: update to version 6.3.1
joehw
parents:
diff changeset
    91
                classLoader.close();
8e0ae3830fca 8224157: BCEL: update to version 6.3.1
joehw
parents:
diff changeset
    92
            }
8e0ae3830fca 8224157: BCEL: update to version 6.3.1
joehw
parents:
diff changeset
    93
            if (fileSystem != null) {
8e0ae3830fca 8224157: BCEL: update to version 6.3.1
joehw
parents:
diff changeset
    94
                fileSystem.close();
8e0ae3830fca 8224157: BCEL: update to version 6.3.1
joehw
parents:
diff changeset
    95
            }
8e0ae3830fca 8224157: BCEL: update to version 6.3.1
joehw
parents:
diff changeset
    96
        }
8e0ae3830fca 8224157: BCEL: update to version 6.3.1
joehw
parents:
diff changeset
    97
    }
8e0ae3830fca 8224157: BCEL: update to version 6.3.1
joehw
parents:
diff changeset
    98
8e0ae3830fca 8224157: BCEL: update to version 6.3.1
joehw
parents:
diff changeset
    99
    /**
8e0ae3830fca 8224157: BCEL: update to version 6.3.1
joehw
parents:
diff changeset
   100
     * Lists all entries in the given directory.
8e0ae3830fca 8224157: BCEL: update to version 6.3.1
joehw
parents:
diff changeset
   101
     *
8e0ae3830fca 8224157: BCEL: update to version 6.3.1
joehw
parents:
diff changeset
   102
     * @param dirPath
8e0ae3830fca 8224157: BCEL: update to version 6.3.1
joehw
parents:
diff changeset
   103
     *            directory path.
8e0ae3830fca 8224157: BCEL: update to version 6.3.1
joehw
parents:
diff changeset
   104
     * @return a list of dir entries if an I/O error occurs
8e0ae3830fca 8224157: BCEL: update to version 6.3.1
joehw
parents:
diff changeset
   105
     * @throws IOException
8e0ae3830fca 8224157: BCEL: update to version 6.3.1
joehw
parents:
diff changeset
   106
     *             an I/O error occurs accessing the file system
8e0ae3830fca 8224157: BCEL: update to version 6.3.1
joehw
parents:
diff changeset
   107
     */
8e0ae3830fca 8224157: BCEL: update to version 6.3.1
joehw
parents:
diff changeset
   108
    public List<Path> list(final Path dirPath) throws IOException {
8e0ae3830fca 8224157: BCEL: update to version 6.3.1
joehw
parents:
diff changeset
   109
        final List<Path> list = new ArrayList<>();
8e0ae3830fca 8224157: BCEL: update to version 6.3.1
joehw
parents:
diff changeset
   110
        try (DirectoryStream<Path> ds = Files.newDirectoryStream(dirPath)) {
8e0ae3830fca 8224157: BCEL: update to version 6.3.1
joehw
parents:
diff changeset
   111
            final Iterator<Path> iterator = ds.iterator();
8e0ae3830fca 8224157: BCEL: update to version 6.3.1
joehw
parents:
diff changeset
   112
            while (iterator.hasNext()) {
8e0ae3830fca 8224157: BCEL: update to version 6.3.1
joehw
parents:
diff changeset
   113
                list.add(iterator.next());
8e0ae3830fca 8224157: BCEL: update to version 6.3.1
joehw
parents:
diff changeset
   114
            }
8e0ae3830fca 8224157: BCEL: update to version 6.3.1
joehw
parents:
diff changeset
   115
        }
8e0ae3830fca 8224157: BCEL: update to version 6.3.1
joehw
parents:
diff changeset
   116
        return list;
8e0ae3830fca 8224157: BCEL: update to version 6.3.1
joehw
parents:
diff changeset
   117
    }
8e0ae3830fca 8224157: BCEL: update to version 6.3.1
joehw
parents:
diff changeset
   118
8e0ae3830fca 8224157: BCEL: update to version 6.3.1
joehw
parents:
diff changeset
   119
    /**
8e0ae3830fca 8224157: BCEL: update to version 6.3.1
joehw
parents:
diff changeset
   120
     * Lists all entries in the given directory.
8e0ae3830fca 8224157: BCEL: update to version 6.3.1
joehw
parents:
diff changeset
   121
     *
8e0ae3830fca 8224157: BCEL: update to version 6.3.1
joehw
parents:
diff changeset
   122
     * @param dirName
8e0ae3830fca 8224157: BCEL: update to version 6.3.1
joehw
parents:
diff changeset
   123
     *            directory path.
8e0ae3830fca 8224157: BCEL: update to version 6.3.1
joehw
parents:
diff changeset
   124
     * @return a list of dir entries if an I/O error occurs
8e0ae3830fca 8224157: BCEL: update to version 6.3.1
joehw
parents:
diff changeset
   125
     * @throws IOException
8e0ae3830fca 8224157: BCEL: update to version 6.3.1
joehw
parents:
diff changeset
   126
     *             an I/O error occurs accessing the file system
8e0ae3830fca 8224157: BCEL: update to version 6.3.1
joehw
parents:
diff changeset
   127
     */
8e0ae3830fca 8224157: BCEL: update to version 6.3.1
joehw
parents:
diff changeset
   128
    public List<Path> list(final String dirName) throws IOException {
8e0ae3830fca 8224157: BCEL: update to version 6.3.1
joehw
parents:
diff changeset
   129
        return list(fileSystem.getPath(dirName));
8e0ae3830fca 8224157: BCEL: update to version 6.3.1
joehw
parents:
diff changeset
   130
    }
8e0ae3830fca 8224157: BCEL: update to version 6.3.1
joehw
parents:
diff changeset
   131
8e0ae3830fca 8224157: BCEL: update to version 6.3.1
joehw
parents:
diff changeset
   132
    /**
8e0ae3830fca 8224157: BCEL: update to version 6.3.1
joehw
parents:
diff changeset
   133
     * Lists all modules.
8e0ae3830fca 8224157: BCEL: update to version 6.3.1
joehw
parents:
diff changeset
   134
     *
8e0ae3830fca 8224157: BCEL: update to version 6.3.1
joehw
parents:
diff changeset
   135
     * @return a list of modules
8e0ae3830fca 8224157: BCEL: update to version 6.3.1
joehw
parents:
diff changeset
   136
     * @throws IOException
8e0ae3830fca 8224157: BCEL: update to version 6.3.1
joehw
parents:
diff changeset
   137
     *             an I/O error occurs accessing the file system
8e0ae3830fca 8224157: BCEL: update to version 6.3.1
joehw
parents:
diff changeset
   138
     */
8e0ae3830fca 8224157: BCEL: update to version 6.3.1
joehw
parents:
diff changeset
   139
    public List<Path> modules() throws IOException {
8e0ae3830fca 8224157: BCEL: update to version 6.3.1
joehw
parents:
diff changeset
   140
        return list(MODULES_PATH);
8e0ae3830fca 8224157: BCEL: update to version 6.3.1
joehw
parents:
diff changeset
   141
    }
8e0ae3830fca 8224157: BCEL: update to version 6.3.1
joehw
parents:
diff changeset
   142
8e0ae3830fca 8224157: BCEL: update to version 6.3.1
joehw
parents:
diff changeset
   143
    /**
8e0ae3830fca 8224157: BCEL: update to version 6.3.1
joehw
parents:
diff changeset
   144
     * Lists all packages.
8e0ae3830fca 8224157: BCEL: update to version 6.3.1
joehw
parents:
diff changeset
   145
     *
8e0ae3830fca 8224157: BCEL: update to version 6.3.1
joehw
parents:
diff changeset
   146
     * @return a list of modules
8e0ae3830fca 8224157: BCEL: update to version 6.3.1
joehw
parents:
diff changeset
   147
     * @throws IOException
8e0ae3830fca 8224157: BCEL: update to version 6.3.1
joehw
parents:
diff changeset
   148
     *             an I/O error occurs accessing the file system
8e0ae3830fca 8224157: BCEL: update to version 6.3.1
joehw
parents:
diff changeset
   149
     */
8e0ae3830fca 8224157: BCEL: update to version 6.3.1
joehw
parents:
diff changeset
   150
    public List<Path> packages() throws IOException {
8e0ae3830fca 8224157: BCEL: update to version 6.3.1
joehw
parents:
diff changeset
   151
        return list(PACKAGES_PATH);
8e0ae3830fca 8224157: BCEL: update to version 6.3.1
joehw
parents:
diff changeset
   152
    }
8e0ae3830fca 8224157: BCEL: update to version 6.3.1
joehw
parents:
diff changeset
   153
8e0ae3830fca 8224157: BCEL: update to version 6.3.1
joehw
parents:
diff changeset
   154
    public FileSystem getFileSystem() {
8e0ae3830fca 8224157: BCEL: update to version 6.3.1
joehw
parents:
diff changeset
   155
        return fileSystem;
8e0ae3830fca 8224157: BCEL: update to version 6.3.1
joehw
parents:
diff changeset
   156
    }
8e0ae3830fca 8224157: BCEL: update to version 6.3.1
joehw
parents:
diff changeset
   157
8e0ae3830fca 8224157: BCEL: update to version 6.3.1
joehw
parents:
diff changeset
   158
}