Path: blob/master/src/java.base/share/classes/jdk/internal/jrtfs/SystemImage.java
41159 views
/*1* Copyright (c) 2014, 2021, 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. Oracle designates this7* particular file as subject to the "Classpath" exception as provided8* by Oracle in the LICENSE file that accompanied this code.9*10* This code is distributed in the hope that it will be useful, but WITHOUT11* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or12* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License13* version 2 for more details (a copy is included in the LICENSE file that14* accompanied this code).15*16* You should have received a copy of the GNU General Public License version17* 2 along with this work; if not, write to the Free Software Foundation,18* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.19*20* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA21* or visit www.oracle.com if you need additional information or have any22* questions.23*/24package jdk.internal.jrtfs;2526import java.io.IOException;27import java.net.URISyntaxException;28import java.net.URL;29import java.nio.file.Files;30import java.nio.file.FileSystem;31import java.nio.file.FileSystems;32import java.nio.file.FileSystemNotFoundException;33import java.nio.file.Path;34import java.nio.file.Paths;35import java.security.AccessController;36import java.security.CodeSource;37import java.security.PrivilegedAction;3839import jdk.internal.jimage.ImageReader;40import jdk.internal.jimage.ImageReader.Node;4142/**43* @implNote This class needs to maintain JDK 8 source compatibility.44*45* It is used internally in the JDK to implement jimage/jrtfs access,46* but also compiled and delivered as part of the jrtfs.jar to support access47* to the jimage file provided by the shipped JDK by tools running on JDK 8.48*/49@SuppressWarnings("removal")50abstract class SystemImage {5152abstract Node findNode(String path) throws IOException;53abstract byte[] getResource(Node node) throws IOException;54abstract void close() throws IOException;5556static SystemImage open() throws IOException {57if (modulesImageExists) {58// open a .jimage and build directory structure59final ImageReader image = ImageReader.open(moduleImageFile);60image.getRootDirectory();61return new SystemImage() {62@Override63Node findNode(String path) throws IOException {64return image.findNode(path);65}66@Override67byte[] getResource(Node node) throws IOException {68return image.getResource(node);69}70@Override71void close() throws IOException {72image.close();73}74};75}76if (Files.notExists(explodedModulesDir))77throw new FileSystemNotFoundException(explodedModulesDir.toString());78return new ExplodedImage(explodedModulesDir);79}8081static final String RUNTIME_HOME;82// "modules" jimage file Path83static final Path moduleImageFile;84// "modules" jimage exists or not?85static final boolean modulesImageExists;86// <JAVA_HOME>/modules directory Path87static final Path explodedModulesDir;8889static {90PrivilegedAction<String> pa = SystemImage::findHome;91RUNTIME_HOME = AccessController.doPrivileged(pa);9293FileSystem fs = FileSystems.getDefault();94moduleImageFile = fs.getPath(RUNTIME_HOME, "lib", "modules");95explodedModulesDir = fs.getPath(RUNTIME_HOME, "modules");9697modulesImageExists = AccessController.doPrivileged(98new PrivilegedAction<Boolean>() {99@Override100public Boolean run() {101return Files.isRegularFile(moduleImageFile);102}103});104}105106/**107* Returns the appropriate JDK home for this usage of the FileSystemProvider.108* When the CodeSource is null (null loader) then jrt:/ is the current runtime,109* otherwise the JDK home is located relative to jrt-fs.jar.110*/111private static String findHome() {112CodeSource cs = SystemImage.class.getProtectionDomain().getCodeSource();113if (cs == null)114return System.getProperty("java.home");115116// assume loaded from $TARGETJDK/lib/jrt-fs.jar117URL url = cs.getLocation();118if (!url.getProtocol().equalsIgnoreCase("file"))119throw new InternalError(url + " loaded in unexpected way");120try {121Path lib = Paths.get(url.toURI()).getParent();122if (!lib.getFileName().toString().equals("lib"))123throw new InternalError(url + " unexpected path");124125return lib.getParent().toString();126} catch (URISyntaxException e) {127throw new InternalError(e);128}129}130}131132133