Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
PojavLauncherTeam
GitHub Repository: PojavLauncherTeam/mobile
Path: blob/master/test/jdk/lib/testlibrary/java/util/jar/CreateMultiReleaseTestJars.java
41159 views
1
/*
2
* Copyright (c) 2015, 2020, 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.
8
*
9
* This code is distributed in the hope that it will be useful, but WITHOUT
10
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
12
* version 2 for more details (a copy is included in the LICENSE file that
13
* accompanied this code).
14
*
15
* You should have received a copy of the GNU General Public License version
16
* 2 along with this work; if not, write to the Free Software Foundation,
17
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
18
*
19
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
20
* or visit www.oracle.com if you need additional information or have any
21
* questions.
22
*/
23
24
import java.io.File;
25
import java.io.FileOutputStream;
26
import java.io.IOException;
27
import java.nio.file.Files;
28
import java.nio.file.Path;
29
import java.nio.file.Paths;
30
import java.security.KeyStore;
31
import java.security.PrivateKey;
32
import java.security.cert.CertPath;
33
import java.security.cert.CertificateFactory;
34
import java.util.Arrays;
35
import java.util.HashMap;
36
import java.util.Map;
37
import java.util.zip.ZipFile;
38
import java.io.File;
39
40
import jdk.security.jarsigner.JarSigner;
41
import jdk.test.lib.compiler.Compiler;
42
import jdk.test.lib.util.JarBuilder;
43
44
public class CreateMultiReleaseTestJars {
45
final private String main =
46
"package version;\n\n"
47
+ "public class Main {\n"
48
+ " public static void main(String[] args) {\n"
49
+ " Version v = new Version();\n"
50
+ " System.out.println(\"I am running on version \" + v.getVersion());\n"
51
+ " }\n"
52
+ "}\n";
53
final private String java8 =
54
"package version;\n\n"
55
+ "public class Version {\n"
56
+ " public int getVersion() {\n"
57
+ " return 8;\n"
58
+ " }\n"
59
+ "}\n";
60
final private String java9 =
61
"package version;\n\n"
62
+ "public class Version {\n"
63
+ " public int getVersion() {\n"
64
+ " int version = (new PackagePrivate()).getVersion();\n"
65
+ " if (version == 9) return 9;\n" // strange I know, but easy to test
66
+ " return version;\n"
67
+ " }\n"
68
+ "}\n";
69
final private String ppjava9 =
70
"package version;\n\n"
71
+ "class PackagePrivate {\n"
72
+ " int getVersion() {\n"
73
+ " return 9;\n"
74
+ " }\n"
75
+ "}\n";
76
final int currentVersion = Runtime.version().feature();
77
final String currentVersionStr = Integer.toString(currentVersion);
78
final private String javaCurrent = java8.replace("8", currentVersionStr);
79
final String readme8 = "This is the root readme file";
80
final String readme9 = "This is the version nine readme file";
81
final String readmeCurrent = "This is the current version readme file";
82
private Map<String,byte[]> rootClasses;
83
private Map<String,byte[]> version9Classes;
84
private Map<String,byte[]> versionCurrentClasses;
85
86
public void buildUnversionedJar() throws IOException {
87
JarBuilder jb = new JarBuilder("unversioned.jar");
88
jb.addEntry("README", readme8.getBytes());
89
jb.addEntry("version/Main.java", main.getBytes());
90
jb.addEntry("version/Main.class", rootClasses.get("version.Main"));
91
jb.addEntry("version/Version.java", java8.getBytes());
92
jb.addEntry("version/Version.class", rootClasses.get("version.Version"));
93
jb.build();
94
}
95
96
public void buildMultiReleaseJar() throws IOException {
97
JarBuilder jb = customMultiReleaseJar("multi-release.jar", "true");
98
addEntries(jb);
99
jb.addEntry("META-INF/versions/9/version/Version.class", version9Classes.get("version.Version"));
100
jb.build();
101
}
102
103
public void buildShortMultiReleaseJar() throws IOException {
104
JarBuilder jb = customMultiReleaseJar("short-multi-release.jar", "true");
105
addEntries(jb);
106
jb.build();
107
}
108
109
private JarBuilder customMultiReleaseJar(String filename, String multiReleaseValue)
110
throws IOException {
111
JarBuilder jb = new JarBuilder(filename);
112
jb.addAttribute("Multi-Release", multiReleaseValue);
113
return jb;
114
}
115
116
public void buildCustomMultiReleaseJar(String filename, String multiReleaseValue,
117
Map<String, String> extraAttributes) throws IOException {
118
buildCustomMultiReleaseJar(filename, multiReleaseValue, extraAttributes, false);
119
}
120
121
public void buildCustomMultiReleaseJar(String filename, String multiReleaseValue,
122
Map<String, String> extraAttributes, boolean addEntries) throws IOException {
123
JarBuilder jb = new JarBuilder(filename);
124
extraAttributes.entrySet()
125
.forEach(entry -> jb.addAttribute(entry.getKey(), entry.getValue()));
126
if (addEntries) {
127
addEntries(jb);
128
}
129
jb.addAttribute("Multi-Release", multiReleaseValue);
130
jb.build();
131
}
132
133
private void addEntries(JarBuilder jb) {
134
jb.addEntry("README", readme8.getBytes());
135
jb.addEntry("version/Main.java", main.getBytes());
136
jb.addEntry("version/Main.class", rootClasses.get("version.Main"));
137
jb.addEntry("version/Version.java", java8.getBytes());
138
jb.addEntry("version/Version.class", rootClasses.get("version.Version"));
139
jb.addEntry("META-INF/versions/9/README", readme9.getBytes());
140
jb.addEntry("META-INF/versions/9/version/Version.java", java9.getBytes());
141
jb.addEntry("META-INF/versions/9/version/PackagePrivate.java", ppjava9.getBytes());
142
jb.addEntry("META-INF/versions/9/version/PackagePrivate.class", version9Classes.get("version.PackagePrivate"));
143
jb.addEntry("META-INF/versions/" + currentVersionStr + "/README", readmeCurrent.getBytes());
144
jb.addEntry("META-INF/versions/" + currentVersionStr + "/version/Version.java", javaCurrent.getBytes());
145
jb.addEntry("META-INF/versions/" + currentVersionStr + "/version/Version.class", versionCurrentClasses.get("version.Version"));
146
}
147
148
public void buildSignedMultiReleaseJar() throws Exception {
149
buildSignedMultiReleaseJar("multi-release.jar", "signed-multi-release.jar");
150
}
151
152
public void buildSignedMultiReleaseJar(String multiReleaseJar,
153
String signedMultiReleaseJar) throws Exception
154
{
155
String testsrc = System.getProperty("test.src",".");
156
String testdir = findTestDir(testsrc);
157
String keystore = testdir + "/sun/security/tools/jarsigner/JarSigning.keystore";
158
159
// jarsigner -keystore keystore -storepass "bbbbbb"
160
// -signedJar signed-multi-release.jar multi-release.jar b
161
162
char[] password = "bbbbbb".toCharArray();
163
KeyStore ks = KeyStore.getInstance(new File(keystore), password);
164
PrivateKey pkb = (PrivateKey)ks.getKey("b", password);
165
CertPath cp = CertificateFactory.getInstance("X.509")
166
.generateCertPath(Arrays.asList(ks.getCertificateChain("b")));
167
JarSigner js = new JarSigner.Builder(pkb, cp).build();
168
try (ZipFile in = new ZipFile(multiReleaseJar);
169
FileOutputStream os = new FileOutputStream(signedMultiReleaseJar))
170
{
171
js.sign(in, os);
172
}
173
}
174
175
String findTestDir(String dir) throws IOException {
176
Path path = Paths.get(dir).toAbsolutePath();
177
Path child = null;
178
while (path != null && !path.endsWith("test")) {
179
child = path;
180
path = path.getParent();
181
}
182
if (child == null) {
183
throw new IllegalArgumentException(dir + " is not in a test directory");
184
}
185
if (!Files.isDirectory(child)) {
186
throw new IOException(child.toString() + " is not a directory");
187
}
188
return child.toString();
189
}
190
191
void compileEntries() {
192
Map<String,String> input = new HashMap<>();
193
input.put("version.Main", main);
194
input.put("version.Version", java8);
195
rootClasses = (new Compiler(input)).setRelease(8).compile();
196
input.clear();
197
input.put("version.Version", java9);
198
input.put("version.PackagePrivate", ppjava9);
199
version9Classes = (new Compiler(input)).setRelease(9).compile();
200
input.clear();
201
input.put("version.Version", javaCurrent);
202
versionCurrentClasses = (new Compiler(input)).compile(); // Use default release
203
}
204
}
205
206