src/java.base/share/classes/jdk/internal/jimage/ImageReaderFactory.java
author erikj
Wed, 07 Mar 2018 13:26:15 -0800
changeset 49357 aaedb8343784
parent 47216 71c04702a3d5
permissions -rw-r--r--
8198243: Add build time check for global operator new/delete in object files Reviewed-by: tbell, kbarrett, dholmes, ihse
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
31673
135283550686 8080511: Refresh of jimage support
jfdenise
parents:
diff changeset
     1
/*
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents: 31673
diff changeset
     2
 * Copyright (c) 2015, 2016, Oracle and/or its affiliates. All rights reserved.
31673
135283550686 8080511: Refresh of jimage support
jfdenise
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
135283550686 8080511: Refresh of jimage support
jfdenise
parents:
diff changeset
     4
 *
135283550686 8080511: Refresh of jimage support
jfdenise
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
135283550686 8080511: Refresh of jimage support
jfdenise
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
135283550686 8080511: Refresh of jimage support
jfdenise
parents:
diff changeset
     7
 * published by the Free Software Foundation.  Oracle designates this
135283550686 8080511: Refresh of jimage support
jfdenise
parents:
diff changeset
     8
 * particular file as subject to the "Classpath" exception as provided
135283550686 8080511: Refresh of jimage support
jfdenise
parents:
diff changeset
     9
 * by Oracle in the LICENSE file that accompanied this code.
135283550686 8080511: Refresh of jimage support
jfdenise
parents:
diff changeset
    10
 *
135283550686 8080511: Refresh of jimage support
jfdenise
parents:
diff changeset
    11
 * This code is distributed in the hope that it will be useful, but WITHOUT
135283550686 8080511: Refresh of jimage support
jfdenise
parents:
diff changeset
    12
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
135283550686 8080511: Refresh of jimage support
jfdenise
parents:
diff changeset
    13
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
135283550686 8080511: Refresh of jimage support
jfdenise
parents:
diff changeset
    14
 * version 2 for more details (a copy is included in the LICENSE file that
135283550686 8080511: Refresh of jimage support
jfdenise
parents:
diff changeset
    15
 * accompanied this code).
135283550686 8080511: Refresh of jimage support
jfdenise
parents:
diff changeset
    16
 *
135283550686 8080511: Refresh of jimage support
jfdenise
parents:
diff changeset
    17
 * You should have received a copy of the GNU General Public License version
135283550686 8080511: Refresh of jimage support
jfdenise
parents:
diff changeset
    18
 * 2 along with this work; if not, write to the Free Software Foundation,
135283550686 8080511: Refresh of jimage support
jfdenise
parents:
diff changeset
    19
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
135283550686 8080511: Refresh of jimage support
jfdenise
parents:
diff changeset
    20
 *
135283550686 8080511: Refresh of jimage support
jfdenise
parents:
diff changeset
    21
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
135283550686 8080511: Refresh of jimage support
jfdenise
parents:
diff changeset
    22
 * or visit www.oracle.com if you need additional information or have any
135283550686 8080511: Refresh of jimage support
jfdenise
parents:
diff changeset
    23
 * questions.
135283550686 8080511: Refresh of jimage support
jfdenise
parents:
diff changeset
    24
 */
135283550686 8080511: Refresh of jimage support
jfdenise
parents:
diff changeset
    25
135283550686 8080511: Refresh of jimage support
jfdenise
parents:
diff changeset
    26
package jdk.internal.jimage;
135283550686 8080511: Refresh of jimage support
jfdenise
parents:
diff changeset
    27
135283550686 8080511: Refresh of jimage support
jfdenise
parents:
diff changeset
    28
import java.io.IOException;
135283550686 8080511: Refresh of jimage support
jfdenise
parents:
diff changeset
    29
import java.io.UncheckedIOException;
135283550686 8080511: Refresh of jimage support
jfdenise
parents:
diff changeset
    30
import java.nio.file.Path;
135283550686 8080511: Refresh of jimage support
jfdenise
parents:
diff changeset
    31
import java.nio.file.Paths;
135283550686 8080511: Refresh of jimage support
jfdenise
parents:
diff changeset
    32
import java.util.concurrent.ConcurrentHashMap;
135283550686 8080511: Refresh of jimage support
jfdenise
parents:
diff changeset
    33
import java.util.Map;
38582
8618f4ccb220 8156209: Add argument checks to BasicImageReader calls
jlaskey
parents: 36511
diff changeset
    34
import java.util.Objects;
43804
96fd6bf9c69b 8175010: ImageReader is not thread-safe
redestad
parents: 38582
diff changeset
    35
import java.util.function.Function;
31673
135283550686 8080511: Refresh of jimage support
jfdenise
parents:
diff changeset
    36
135283550686 8080511: Refresh of jimage support
jfdenise
parents:
diff changeset
    37
/**
135283550686 8080511: Refresh of jimage support
jfdenise
parents:
diff changeset
    38
 * Factory to get ImageReader
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents: 31673
diff changeset
    39
 *
9d0388c6b336 8142968: Module System implementation
alanb
parents: 31673
diff changeset
    40
 * @implNote This class needs to maintain JDK 8 source compatibility.
9d0388c6b336 8142968: Module System implementation
alanb
parents: 31673
diff changeset
    41
 *
9d0388c6b336 8142968: Module System implementation
alanb
parents: 31673
diff changeset
    42
 * It is used internally in the JDK to implement jimage/jrtfs access,
9d0388c6b336 8142968: Module System implementation
alanb
parents: 31673
diff changeset
    43
 * but also compiled and delivered as part of the jrtfs.jar to support access
9d0388c6b336 8142968: Module System implementation
alanb
parents: 31673
diff changeset
    44
 * to the jimage file provided by the shipped JDK by tools running on JDK 8.
31673
135283550686 8080511: Refresh of jimage support
jfdenise
parents:
diff changeset
    45
 */
135283550686 8080511: Refresh of jimage support
jfdenise
parents:
diff changeset
    46
public class ImageReaderFactory {
135283550686 8080511: Refresh of jimage support
jfdenise
parents:
diff changeset
    47
    private ImageReaderFactory() {}
135283550686 8080511: Refresh of jimage support
jfdenise
parents:
diff changeset
    48
135283550686 8080511: Refresh of jimage support
jfdenise
parents:
diff changeset
    49
    private static final String JAVA_HOME = System.getProperty("java.home");
135283550686 8080511: Refresh of jimage support
jfdenise
parents:
diff changeset
    50
    private static final Path BOOT_MODULES_JIMAGE =
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents: 31673
diff changeset
    51
        Paths.get(JAVA_HOME, "lib", "modules");
31673
135283550686 8080511: Refresh of jimage support
jfdenise
parents:
diff changeset
    52
135283550686 8080511: Refresh of jimage support
jfdenise
parents:
diff changeset
    53
    private static final Map<Path, ImageReader> readers = new ConcurrentHashMap<>();
135283550686 8080511: Refresh of jimage support
jfdenise
parents:
diff changeset
    54
135283550686 8080511: Refresh of jimage support
jfdenise
parents:
diff changeset
    55
    /**
135283550686 8080511: Refresh of jimage support
jfdenise
parents:
diff changeset
    56
     * Returns an {@code ImageReader} to read from the given image file
135283550686 8080511: Refresh of jimage support
jfdenise
parents:
diff changeset
    57
     */
135283550686 8080511: Refresh of jimage support
jfdenise
parents:
diff changeset
    58
    public static ImageReader get(Path jimage) throws IOException {
38582
8618f4ccb220 8156209: Add argument checks to BasicImageReader calls
jlaskey
parents: 36511
diff changeset
    59
        Objects.requireNonNull(jimage);
43804
96fd6bf9c69b 8175010: ImageReader is not thread-safe
redestad
parents: 38582
diff changeset
    60
        try {
96fd6bf9c69b 8175010: ImageReader is not thread-safe
redestad
parents: 38582
diff changeset
    61
            return readers.computeIfAbsent(jimage, OPENER);
96fd6bf9c69b 8175010: ImageReader is not thread-safe
redestad
parents: 38582
diff changeset
    62
        } catch (UncheckedIOException io) {
96fd6bf9c69b 8175010: ImageReader is not thread-safe
redestad
parents: 38582
diff changeset
    63
            throw io.getCause();
31673
135283550686 8080511: Refresh of jimage support
jfdenise
parents:
diff changeset
    64
        }
135283550686 8080511: Refresh of jimage support
jfdenise
parents:
diff changeset
    65
    }
135283550686 8080511: Refresh of jimage support
jfdenise
parents:
diff changeset
    66
43804
96fd6bf9c69b 8175010: ImageReader is not thread-safe
redestad
parents: 38582
diff changeset
    67
    private static Function<Path, ImageReader> OPENER = new Function<Path, ImageReader>() {
96fd6bf9c69b 8175010: ImageReader is not thread-safe
redestad
parents: 38582
diff changeset
    68
        public ImageReader apply(Path path) {
96fd6bf9c69b 8175010: ImageReader is not thread-safe
redestad
parents: 38582
diff changeset
    69
            try {
96fd6bf9c69b 8175010: ImageReader is not thread-safe
redestad
parents: 38582
diff changeset
    70
                return ImageReader.open(path);
96fd6bf9c69b 8175010: ImageReader is not thread-safe
redestad
parents: 38582
diff changeset
    71
            } catch (IOException io) {
96fd6bf9c69b 8175010: ImageReader is not thread-safe
redestad
parents: 38582
diff changeset
    72
                throw new UncheckedIOException(io);
96fd6bf9c69b 8175010: ImageReader is not thread-safe
redestad
parents: 38582
diff changeset
    73
            }
96fd6bf9c69b 8175010: ImageReader is not thread-safe
redestad
parents: 38582
diff changeset
    74
        }
96fd6bf9c69b 8175010: ImageReader is not thread-safe
redestad
parents: 38582
diff changeset
    75
    };
96fd6bf9c69b 8175010: ImageReader is not thread-safe
redestad
parents: 38582
diff changeset
    76
31673
135283550686 8080511: Refresh of jimage support
jfdenise
parents:
diff changeset
    77
    /**
135283550686 8080511: Refresh of jimage support
jfdenise
parents:
diff changeset
    78
     * Returns the {@code ImageReader} to read the image file in this
135283550686 8080511: Refresh of jimage support
jfdenise
parents:
diff changeset
    79
     * run-time image.
135283550686 8080511: Refresh of jimage support
jfdenise
parents:
diff changeset
    80
     *
135283550686 8080511: Refresh of jimage support
jfdenise
parents:
diff changeset
    81
     * @throws UncheckedIOException if an I/O error occurs
135283550686 8080511: Refresh of jimage support
jfdenise
parents:
diff changeset
    82
     */
135283550686 8080511: Refresh of jimage support
jfdenise
parents:
diff changeset
    83
    public static ImageReader getImageReader() {
135283550686 8080511: Refresh of jimage support
jfdenise
parents:
diff changeset
    84
        try {
135283550686 8080511: Refresh of jimage support
jfdenise
parents:
diff changeset
    85
            return get(BOOT_MODULES_JIMAGE);
135283550686 8080511: Refresh of jimage support
jfdenise
parents:
diff changeset
    86
        } catch (IOException ioe) {
135283550686 8080511: Refresh of jimage support
jfdenise
parents:
diff changeset
    87
            throw new UncheckedIOException(ioe);
135283550686 8080511: Refresh of jimage support
jfdenise
parents:
diff changeset
    88
        }
135283550686 8080511: Refresh of jimage support
jfdenise
parents:
diff changeset
    89
    }
135283550686 8080511: Refresh of jimage support
jfdenise
parents:
diff changeset
    90
}