< prev index next >

src/java.base/share/classes/jdk/internal/jrtfs/SystemImage.java

Print this page

 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     private static final String RUNTIME_HOME;
 82     // "modules" jimage file Path
 83     private static final Path moduleImageFile;
 84     // "modules" jimage exists or not?
 85     private static final boolean modulesImageExists;
 86     // <JAVA_HOME>/modules directory Path
 87     private 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>() {

 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>() {
< prev index next >