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 import jdk.internal.jimage.PreviewMode;
 43 
 44 /**
 45  * @implNote This class needs to maintain JDK 8 source compatibility.
 46  *
 47  * It is used internally in the JDK to implement jimage/jrtfs access,
 48  * but also compiled and delivered as part of the jrtfs.jar to support access
 49  * to the jimage file provided by the shipped JDK by tools running on JDK 8.
 50  */
 51 @SuppressWarnings({"removal", "suppression"})
 52 public abstract class SystemImage implements AutoCloseable {
 53 
 54     public abstract Node findNode(String path) throws IOException;
 55     public abstract byte[] getResource(Node node) throws IOException;
 56     public abstract void close() throws IOException;
 57 
 58     /**
 59      * Opens the system image for the current runtime.
 60      *
 61      * @param mode determines whether preview mode should be enabled.
 62      * @return a new system image based on either the jimage file or an "exploded"
 63      *     modules directory, according to the build state.
 64      */
 65     public static SystemImage open(PreviewMode mode) throws IOException {
 66         return modulesImageExists ? fromJimage(moduleImageFile, mode) : fromDirectory(explodedModulesDir, mode);
 67     }
 68 
 69     /** Internal factory method for testing only, use {@link SystemImage#open(PreviewMode)}. */
 70     public static SystemImage fromJimage(Path path, PreviewMode mode) throws IOException {
 71         final ImageReader image = ImageReader.open(path, mode);
 72         return new SystemImage() {
 73             @Override
 74             public Node findNode(String path) throws IOException {
 75                 return image.findNode(path);
 76             }
 77             @Override
 78             public byte[] getResource(Node node) throws IOException {
 79                 return image.getResource(node);
 80             }
 81             @Override
 82             public void close() throws IOException {
 83                 image.close();
 84             }
 85         };
 86     }
 87 
 88     /** Internal factory method for testing only, use {@link SystemImage#open(PreviewMode)}. */
 89     public static SystemImage fromDirectory(Path modulesDir, PreviewMode mode) throws IOException {
 90         if (!Files.isDirectory(modulesDir)) {
 91             throw new FileSystemNotFoundException(modulesDir.toString());
 92         }
 93         return new ExplodedImage(modulesDir, mode.isPreviewModeEnabled());
 94     }
 95 
 96     private static final String RUNTIME_HOME;
 97     // "modules" jimage file Path
 98     private static final Path moduleImageFile;
 99     // "modules" jimage exists or not?
100     private static final boolean modulesImageExists;
101     // <JAVA_HOME>/modules directory Path
102     private static final Path explodedModulesDir;
103 
104     static {
105         PrivilegedAction<String> pa = SystemImage::findHome;
106         RUNTIME_HOME = AccessController.doPrivileged(pa);
107 
108         FileSystem fs = FileSystems.getDefault();
109         moduleImageFile = fs.getPath(RUNTIME_HOME, "lib", "modules");
110         explodedModulesDir = fs.getPath(RUNTIME_HOME, "modules");
111 
112         modulesImageExists = AccessController.doPrivileged(
113             new PrivilegedAction<Boolean>() {
114                 @Override
115                 public Boolean run() {
116                     return Files.isRegularFile(moduleImageFile);
117                 }
118             });
119     }
120 
121     /**
122      * Returns the appropriate JDK home for this usage of the FileSystemProvider.
123      * When the CodeSource is null (null loader) then jrt:/ is the current runtime,
124      * otherwise the JDK home is located relative to jrt-fs.jar.
125      */
126     private static String findHome() {
127         CodeSource cs = SystemImage.class.getProtectionDomain().getCodeSource();
128         if (cs == null)
129             return System.getProperty("java.home");
130 
131         // assume loaded from $TARGETJDK/lib/jrt-fs.jar
132         URL url = cs.getLocation();
133         if (!url.getProtocol().equalsIgnoreCase("file"))
134             throw new InternalError(url + " loaded in unexpected way");
135         try {
136             Path lib = Paths.get(url.toURI()).getParent();
137             if (!lib.getFileName().toString().equals("lib"))
138                 throw new InternalError(url + " unexpected path");
139 
140             return lib.getParent().toString();
141         } catch (URISyntaxException e) {
142             throw new InternalError(e);
143         }
144     }
145 }