Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
PojavLauncherTeam
GitHub Repository: PojavLauncherTeam/mobile
Path: blob/master/test/jdk/sun/misc/JarIndex/metaInfFilenames/Basic.java
41153 views
1
/*
2
* Copyright (c) 2011, 2015, 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
/*
25
* @test
26
* @bug 6887710
27
* @summary Verify the impact of sun.misc.JarIndex.metaInfFilenames on ServiceLoader
28
* @modules jdk.jartool/sun.tools.jar
29
* jdk.httpserver
30
* jdk.compiler
31
* jdk.zipfs
32
* @run main/othervm Basic
33
*/
34
35
import java.io.IOException;
36
import java.io.BufferedReader;
37
import java.io.File;
38
import java.io.FileInputStream;
39
import java.io.InputStream;
40
import java.io.InputStreamReader;
41
import java.io.OutputStream;
42
import java.net.InetSocketAddress;
43
import java.net.URI;
44
import java.net.URL;
45
import java.net.URLClassLoader;
46
import java.util.Arrays;
47
import java.util.Iterator;
48
import java.util.ServiceLoader;
49
import com.sun.net.httpserver.Headers;
50
import com.sun.net.httpserver.HttpExchange;
51
import com.sun.net.httpserver.HttpHandler;
52
import com.sun.net.httpserver.HttpServer;
53
54
/**
55
* Verifies the impact of sun.misc.JarIndex.metaInfFilenames on ServiceLoader
56
* and on finding resources via Class.getResource.
57
*
58
* 1) Compile the test sources:
59
* jarA:
60
* META-INF/services/my.happy.land
61
* com/message/spi/MessageService.java
62
* a/A.java
63
* jarB:
64
* META-INF/JAVA2.DS
65
* META-INF/services/no.name.service
66
* b/B.java
67
* jarC:
68
* META-INF/fonts.mf
69
* META-INF/fonts/Company-corporate.ttf
70
* META-INF/fonts/kidpr.ttf
71
* META-INF/services/com.message.spi.MessageService
72
* my/impl/StandardMessageService.java
73
*
74
* 2) Build three jar files a.jar, b.jar, c.jar
75
*
76
* 3) Create an index in a.jar (jar -i a.jar b.jar c.jar)
77
* with sun.misc.JarIndex.metaInfFilenames=true
78
*
79
* 4) Start a HTTP server serving out the three jars.
80
*
81
* The test then tries to locate services/resources within the jars using
82
* URLClassLoader. Each request to the HTTP server is recorded to ensure
83
* only the correct amount of requests are being made.
84
*
85
*/
86
87
public class Basic {
88
static final String slash = File.separator;
89
static final String[] testSources = {
90
"jarA" + slash + "a" + slash + "A.java",
91
"jarA" + slash + "com" + slash + "message" + slash + "spi" + slash + "MessageService.java",
92
"jarB" + slash + "b" + slash + "B.java",
93
"jarC" + slash + "my" + slash + "impl" + slash + "StandardMessageService.java"};
94
95
static final String testSrc = System.getProperty("test.src");
96
static final String testSrcDir = testSrc != null ? testSrc : ".";
97
static final String testClasses = System.getProperty("test.classes");
98
static final String testClassesDir = testClasses != null ? testClasses : ".";
99
100
static JarHttpServer httpServer;
101
102
public static void main(String[] args) throws Exception {
103
104
// Set global url cache to false so that we can track every jar request.
105
(new URL("http://localhost/")).openConnection().setDefaultUseCaches(false);
106
107
buildTest();
108
109
try {
110
httpServer = new JarHttpServer(testClassesDir);
111
httpServer.start();
112
113
doTest(httpServer.getAddress());
114
115
} catch (IOException ioe) {
116
ioe.printStackTrace();
117
} finally {
118
if (httpServer != null) { httpServer.stop(2); }
119
}
120
}
121
122
static void buildTest() {
123
/* compile the source that will be used to generate the jars */
124
for (int i=0; i<testSources.length; i++)
125
testSources[i] = testSrcDir + slash + testSources[i];
126
127
compile("-d" , testClassesDir,
128
"-sourcepath", testSrcDir,
129
testSources[0], testSources[1], testSources[2], testSources[3]);
130
131
/* build the 3 jar files */
132
jar("-cf", testClassesDir + slash + "a.jar",
133
"-C", testClassesDir, "a",
134
"-C", testClassesDir, "com",
135
"-C", testSrcDir + slash + "jarA", "META-INF");
136
jar("-cf", testClassesDir + slash + "b.jar",
137
"-C", testClassesDir, "b",
138
"-C", testSrcDir + slash + "jarB", "META-INF");
139
jar("-cf", testClassesDir + slash + "c.jar",
140
"-C", testClassesDir, "my",
141
"-C", testSrcDir + slash + "jarC", "META-INF");
142
143
/* Create an index in a.jar for b.jar and c.jar */
144
createIndex(testClassesDir);
145
}
146
147
/* run jar <args> */
148
static void jar(String... args) {
149
debug("Running: jar " + Arrays.toString(args));
150
sun.tools.jar.Main jar = new sun.tools.jar.Main(System.out, System.err, "jar");
151
if (!jar.run(args)) {
152
throw new RuntimeException("jar failed: args=" + Arrays.toString(args));
153
}
154
}
155
156
/* run javac <args> */
157
static void compile(String... args) {
158
debug("Running: javac " + Arrays.toString(args));
159
if (com.sun.tools.javac.Main.compile(args) != 0) {
160
throw new RuntimeException("javac failed: args=" + Arrays.toString(args));
161
}
162
}
163
164
static String jar;
165
static {
166
jar = System.getProperty("java.home") + slash+ "bin" + slash + "jar";
167
}
168
169
/* create the index */
170
static void createIndex(String workingDir) {
171
// ProcessBuilder is used so that the current directory can be set
172
// to the directory that directly contains the jars.
173
debug("Running jar to create the index");
174
ProcessBuilder pb = new ProcessBuilder(
175
jar, "-J-Dsun.misc.JarIndex.metaInfFilenames=true", "-i", "a.jar", "b.jar", "c.jar");
176
pb.directory(new File(workingDir));
177
//pd.inheritIO();
178
try {
179
Process p = pb.start();
180
if(p.waitFor() != 0)
181
throw new RuntimeException("jar indexing failed");
182
183
if(debug && p != null) {
184
String line = null;
185
BufferedReader reader =
186
new BufferedReader(new InputStreamReader(p.getInputStream()));
187
while((line = reader.readLine()) != null)
188
debug(line);
189
reader = new BufferedReader(new InputStreamReader(p.getErrorStream()));
190
while((line = reader.readLine()) != null)
191
debug(line);
192
}
193
} catch(InterruptedException ie) { throw new RuntimeException(ie);
194
} catch(IOException e) { throw new RuntimeException(e); }
195
}
196
197
static final boolean debug = true;
198
199
static void debug(Object message) { if (debug) System.out.println(message); }
200
201
/* service define in c.jar */
202
static final String messageService = "com.message.spi.MessageService";
203
204
/* a service that is not defined in any of the jars */
205
static final String unknownService = "java.lang.Object";
206
207
static void doTest(InetSocketAddress serverAddress) throws IOException {
208
URL baseURL = new URL("http://localhost:" + serverAddress.getPort() + "/");
209
210
int failed = 0;
211
212
// Tests using java.util.SerivceLoader
213
if (!javaUtilServiceLoaderTest(baseURL, messageService, true, false, true)) {
214
System.out.println("Test: ServiceLoader looking for " + messageService + ", failed");
215
failed++;
216
}
217
if (!javaUtilServiceLoaderTest(baseURL, unknownService, false, false, false)) {
218
System.out.println("Test: ServiceLoader looking for " + unknownService + " failed");
219
failed++;
220
}
221
222
// Tests using java.lang.Class (similar to the FontManager in javafx)
223
if (!klassLoader(baseURL, "/META-INF/fonts.mf", true, false, true)) {
224
System.out.println("Test: klassLoader looking for /META-INF/fonts.mf failed");
225
failed++;
226
}
227
if (!klassLoader(baseURL, "/META-INF/unknown.mf", false, false, false)) {
228
System.out.println("Test: klassLoader looking for /META-INF/unknown.mf failed");
229
failed++;
230
}
231
232
if (failed > 0)
233
throw new RuntimeException("Failed: " + failed + " tests");
234
}
235
236
static boolean javaUtilServiceLoaderTest(URL baseURL,
237
String serviceClass,
238
boolean expectToFind,
239
boolean expectbDotJar,
240
boolean expectcDotJar) throws IOException {
241
debug("----------------------------------");
242
debug("Running test with java.util.ServiceLoader looking for " + serviceClass);
243
URLClassLoader loader = getLoader(baseURL);
244
httpServer.reset();
245
246
Class<?> messageServiceClass = null;
247
try {
248
messageServiceClass = loader.loadClass(serviceClass);
249
} catch (ClassNotFoundException cnfe) {
250
System.err.println(cnfe);
251
throw new RuntimeException("Error in test: " + cnfe);
252
}
253
254
Iterator<?> iterator = (ServiceLoader.load(messageServiceClass, loader)).iterator();
255
if (expectToFind && !iterator.hasNext()) {
256
debug(messageServiceClass + " NOT found.");
257
return false;
258
}
259
260
while (iterator.hasNext()) {
261
debug("found " + iterator.next() + " " + messageService);
262
}
263
264
debug("HttpServer: " + httpServer);
265
266
if (!expectbDotJar && httpServer.bDotJar > 0) {
267
debug("Unexpeced request sent to the httpserver for b.jar");
268
return false;
269
}
270
if (!expectcDotJar && httpServer.cDotJar > 0) {
271
debug("Unexpeced request sent to the httpserver for c.jar");
272
return false;
273
}
274
275
return true;
276
}
277
278
/* Tries to find a resource in a similar way to the font manager in javafx
279
* com.sun.javafx.scene.text.FontManager */
280
static boolean klassLoader(URL baseURL,
281
String resource,
282
boolean expectToFind,
283
boolean expectbDotJar,
284
boolean expectcDotJar) throws IOException {
285
debug("----------------------------------");
286
debug("Running test looking for " + resource);
287
URLClassLoader loader = getLoader(baseURL);
288
httpServer.reset();
289
290
Class<?> ADotAKlass = null;
291
try {
292
ADotAKlass = loader.loadClass("a.A");
293
} catch (ClassNotFoundException cnfe) {
294
System.err.println(cnfe);
295
throw new RuntimeException("Error in test: " + cnfe);
296
}
297
298
URL u = ADotAKlass.getResource(resource);
299
if (expectToFind && u == null) {
300
System.out.println("Expected to find " + resource + " but didn't");
301
return false;
302
}
303
304
debug("HttpServer: " + httpServer);
305
306
if (!expectbDotJar && httpServer.bDotJar > 0) {
307
debug("Unexpeced request sent to the httpserver for b.jar");
308
return false;
309
}
310
if (!expectcDotJar && httpServer.cDotJar > 0) {
311
debug("Unexpeced request sent to the httpserver for c.jar");
312
return false;
313
}
314
315
return true;
316
}
317
318
static URLClassLoader getLoader(URL baseURL) throws IOException {
319
ClassLoader loader = Basic.class.getClassLoader();
320
321
while (loader.getParent() != null)
322
loader = loader.getParent();
323
324
return new URLClassLoader( new URL[]{
325
new URL(baseURL, "a.jar"),
326
new URL(baseURL, "b.jar"),
327
new URL(baseURL, "c.jar")}, loader );
328
}
329
330
/**
331
* HTTP Server to server the jar files.
332
*/
333
static class JarHttpServer implements HttpHandler {
334
final String docsDir;
335
final HttpServer httpServer;
336
int aDotJar, bDotJar, cDotJar;
337
338
JarHttpServer(String docsDir) throws IOException {
339
this.docsDir = docsDir;
340
341
httpServer = HttpServer.create(new InetSocketAddress(0), 0);
342
httpServer.createContext("/", this);
343
}
344
345
void start() throws IOException {
346
httpServer.start();
347
}
348
349
void stop(int delay) {
350
httpServer.stop(delay);
351
}
352
353
InetSocketAddress getAddress() {
354
return httpServer.getAddress();
355
}
356
357
void reset() {
358
aDotJar = bDotJar = cDotJar = 0;
359
}
360
361
@Override
362
public String toString() {
363
return "aDotJar=" + aDotJar + ", bDotJar=" + bDotJar + ", cDotJar=" + cDotJar;
364
}
365
366
public void handle(HttpExchange t) throws IOException {
367
InputStream is = t.getRequestBody();
368
Headers map = t.getRequestHeaders();
369
Headers rmap = t.getResponseHeaders();
370
URI uri = t.getRequestURI();
371
372
debug("Server: received request for " + uri);
373
String path = uri.getPath();
374
if (path.endsWith("a.jar"))
375
aDotJar++;
376
else if (path.endsWith("b.jar"))
377
bDotJar++;
378
else if (path.endsWith("c.jar"))
379
cDotJar++;
380
else
381
System.out.println("Unexpected resource request" + path);
382
383
while (is.read() != -1);
384
is.close();
385
386
File file = new File(docsDir, path);
387
if (!file.exists())
388
throw new RuntimeException("Error: request for " + file);
389
long clen = file.length();
390
t.sendResponseHeaders (200, clen);
391
OutputStream os = t.getResponseBody();
392
FileInputStream fis = new FileInputStream(file);
393
try {
394
byte[] buf = new byte [16 * 1024];
395
int len;
396
while ((len=fis.read(buf)) != -1) {
397
os.write (buf, 0, len);
398
}
399
} catch (IOException e) {
400
e.printStackTrace();
401
}
402
fis.close();
403
os.close();
404
}
405
}
406
}
407
408