Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
PojavLauncherTeam
GitHub Repository: PojavLauncherTeam/mobile
Path: blob/master/src/java.base/share/classes/jdk/internal/jrtfs/SystemImage.java
41159 views
1
/*
2
* Copyright (c) 2014, 2021, 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")
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
image.getRootDirectory();
62
return new SystemImage() {
63
@Override
64
Node findNode(String path) throws IOException {
65
return image.findNode(path);
66
}
67
@Override
68
byte[] getResource(Node node) throws IOException {
69
return image.getResource(node);
70
}
71
@Override
72
void close() throws IOException {
73
image.close();
74
}
75
};
76
}
77
if (Files.notExists(explodedModulesDir))
78
throw new FileSystemNotFoundException(explodedModulesDir.toString());
79
return new ExplodedImage(explodedModulesDir);
80
}
81
82
static final String RUNTIME_HOME;
83
// "modules" jimage file Path
84
static final Path moduleImageFile;
85
// "modules" jimage exists or not?
86
static final boolean modulesImageExists;
87
// <JAVA_HOME>/modules directory Path
88
static final Path explodedModulesDir;
89
90
static {
91
PrivilegedAction<String> pa = SystemImage::findHome;
92
RUNTIME_HOME = AccessController.doPrivileged(pa);
93
94
FileSystem fs = FileSystems.getDefault();
95
moduleImageFile = fs.getPath(RUNTIME_HOME, "lib", "modules");
96
explodedModulesDir = fs.getPath(RUNTIME_HOME, "modules");
97
98
modulesImageExists = AccessController.doPrivileged(
99
new PrivilegedAction<Boolean>() {
100
@Override
101
public Boolean run() {
102
return Files.isRegularFile(moduleImageFile);
103
}
104
});
105
}
106
107
/**
108
* Returns the appropriate JDK home for this usage of the FileSystemProvider.
109
* When the CodeSource is null (null loader) then jrt:/ is the current runtime,
110
* otherwise the JDK home is located relative to jrt-fs.jar.
111
*/
112
private static String findHome() {
113
CodeSource cs = SystemImage.class.getProtectionDomain().getCodeSource();
114
if (cs == null)
115
return System.getProperty("java.home");
116
117
// assume loaded from $TARGETJDK/lib/jrt-fs.jar
118
URL url = cs.getLocation();
119
if (!url.getProtocol().equalsIgnoreCase("file"))
120
throw new InternalError(url + " loaded in unexpected way");
121
try {
122
Path lib = Paths.get(url.toURI()).getParent();
123
if (!lib.getFileName().toString().equals("lib"))
124
throw new InternalError(url + " unexpected path");
125
126
return lib.getParent().toString();
127
} catch (URISyntaxException e) {
128
throw new InternalError(e);
129
}
130
}
131
}
132
133