1 /*
  2  * Copyright (c) 2014, 2025, Oracle and/or its affiliates. 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.  Oracle designates this
  8  * particular file as subject to the "Classpath" exception as provided
  9  * by Oracle 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 Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
 22  * or visit www.oracle.com if you need additional information or have any
 23  * questions.
 24  */
 25 package jdk.internal.jrtfs;
 26 
 27 import java.io.IOException;
 28 import java.net.URISyntaxException;
 29 import java.net.URL;
 30 import java.nio.file.Files;
 31 import java.nio.file.FileSystem;
 32 import java.nio.file.FileSystems;
 33 import java.nio.file.FileSystemNotFoundException;
 34 import java.nio.file.Path;
 35 import java.nio.file.Paths;
 36 import java.security.AccessController;
 37 import java.security.CodeSource;
 38 import java.security.PrivilegedAction;
 39 
 40 import jdk.internal.jimage.ImageReader;
 41 import jdk.internal.jimage.ImageReader.Node;
 42 
 43 /**
 44  * @implNote This class needs to maintain JDK 8 source compatibility.
 45  *
 46  * It is used internally in the JDK to implement jimage/jrtfs access,
 47  * but also compiled and delivered as part of the jrtfs.jar to support access
 48  * to the jimage file provided by the shipped JDK by tools running on JDK 8.
 49  */
 50 @SuppressWarnings({ "removal", "suppression"} )
 51 abstract class SystemImage {
 52 
 53     abstract Node findNode(String path) throws IOException;
 54     abstract byte[] getResource(Node node) throws IOException;
 55     abstract void close() throws IOException;
 56 
 57     static SystemImage open() throws IOException {
 58         if (modulesImageExists) {
 59             // open a .jimage and build directory structure
 60             final ImageReader image = ImageReader.open(moduleImageFile);
 61             return new SystemImage() {
 62                 @Override
 63                 Node findNode(String path) throws IOException {
 64                     return image.findNode(path);
 65                 }
 66                 @Override
 67                 byte[] getResource(Node node) throws IOException {
 68                     return image.getResource(node);
 69                 }
 70                 @Override
 71                 void close() throws IOException {
 72                     image.close();
 73                 }
 74             };
 75         }
 76         if (Files.notExists(explodedModulesDir))
 77             throw new FileSystemNotFoundException(explodedModulesDir.toString());
 78         return new ExplodedImage(explodedModulesDir);
 79     }
 80 
 81     static final String RUNTIME_HOME;
 82     // "modules" jimage file Path
 83     static final Path moduleImageFile;
 84     // "modules" jimage exists or not?
 85     static final boolean modulesImageExists;
 86     // <JAVA_HOME>/modules directory Path
 87     static final Path explodedModulesDir;
 88 
 89     static {
 90         PrivilegedAction<String> pa = SystemImage::findHome;
 91         RUNTIME_HOME = AccessController.doPrivileged(pa);
 92 
 93         FileSystem fs = FileSystems.getDefault();
 94         moduleImageFile = fs.getPath(RUNTIME_HOME, "lib", "modules");
 95         explodedModulesDir = fs.getPath(RUNTIME_HOME, "modules");
 96 
 97         modulesImageExists = AccessController.doPrivileged(
 98             new PrivilegedAction<Boolean>() {
 99                 @Override
100                 public Boolean run() {
101                     return Files.isRegularFile(moduleImageFile);
102                 }
103             });
104     }
105 
106     /**
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      */
111     private static String findHome() {
112         CodeSource cs = SystemImage.class.getProtectionDomain().getCodeSource();
113         if (cs == null)
114             return System.getProperty("java.home");
115 
116         // assume loaded from $TARGETJDK/lib/jrt-fs.jar
117         URL url = cs.getLocation();
118         if (!url.getProtocol().equalsIgnoreCase("file"))
119             throw new InternalError(url + " loaded in unexpected way");
120         try {
121             Path lib = Paths.get(url.toURI()).getParent();
122             if (!lib.getFileName().toString().equals("lib"))
123                 throw new InternalError(url + " unexpected path");
124 
125             return lib.getParent().toString();
126         } catch (URISyntaxException e) {
127             throw new InternalError(e);
128         }
129     }
130 }