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