1 /* 2 * Copyright (c) 2009, 2017, 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 4167874 27 * @modules java.logging 28 * jdk.httpserver 29 * jdk.compiler 30 * @library ../../../../com/sun/net/httpserver 31 * /lib/testlibrary 32 * /test/lib 33 * @build FileServerHandler JarUtils CompilerUtils 34 * @run main/othervm CloseTest 35 * @summary URL-downloaded jar files can consume all available file descriptors 36 */ 37 38 import java.io.File; 39 import java.io.IOException; 40 import java.lang.reflect.Method; 41 import java.net.URLClassLoader; 42 import java.net.InetSocketAddress; 43 import java.net.URL; 44 import java.nio.file.Files; 45 import java.nio.file.Path; 46 import java.nio.file.Paths; 47 48 import com.sun.net.httpserver.HttpContext; 49 import com.sun.net.httpserver.HttpServer; 50 51 import static java.nio.file.StandardCopyOption.REPLACE_EXISTING; 52 53 public class CloseTest extends Common { 54 private static final String WORK_DIR = System.getProperty("user.dir") 55 + "/"; 56 // 57 // needs two jar files test1.jar and test2.jar with following structure 58 // 59 // com/foo/TestClass 60 // com/foo/TestClass1 61 // com/foo/Resource1 62 // com/foo/Resource2 63 // 64 // and a directory hierarchy with the same structure/contents 65 66 public static void main(String args[]) throws Exception { 67 setup(); 68 69 startHttpServer(WORK_DIR + "serverRoot/"); 70 71 String testjar = WORK_DIR + "test.jar"; 72 copyFile(WORK_DIR + "test1.jar", testjar); 73 test(testjar, 1); 74 75 // repeat test with different implementation 76 // of test.jar (whose TestClass.getValue() returns 2 77 copyFile(WORK_DIR + "test2.jar", testjar); 78 test(testjar, 2); 79 80 // repeat test using a directory of files 81 String testdir = WORK_DIR + "testdir/"; 82 rm_minus_rf(new File(testdir)); 83 copyDir(WORK_DIR + "test1/", testdir); 84 test(testdir, 1); 85 86 testdir = WORK_DIR + "testdir/"; 87 rm_minus_rf(new File(testdir)); 88 copyDir(WORK_DIR + "test2/", testdir); 89 test(testdir, 2); 90 getHttpServer().stop(3); 91 } 92 93 // create a loader on jarfile (or directory), plus a http loader 94 // load a class , then look for a resource 95 // also load a class from http loader 96 // then close the loader 97 // check further new classes/resources cannot be loaded 98 // check jar (or dir) can be deleted 99 // check existing classes can be loaded 100 // check boot classes can be loaded 101 102 static void test(String name, int expectedValue) throws Exception { 103 104 URL url = new URL("file", null, name); 105 URL url2 = getServerURL(); 106 System.out.println("Doing tests with URL: " + url + " and " + url2); 107 URL[] urls = new URL[2]; 108 urls[0] = url; 109 urls[1] = url2; 110 URLClassLoader loader = new URLClassLoader(urls); 111 Class testclass = loadClass("com.foo.TestClass", loader, true); 112 Class class2 = loadClass("Test", loader, true); // from http 113 class2.newInstance(); 114 Object test = testclass.newInstance(); 115 Method method = testclass.getDeclaredMethods()[0]; // int getValue(); 116 int res = (Integer) method.invoke(test); 117 118 if (res != expectedValue) { 119 throw new RuntimeException("wrong value from getValue() [" + res + 120 "/" + expectedValue + "]"); 121 } 122 123 // should find /resource1 124 URL u1 = loader.findResource("com/foo/Resource1"); 125 if (u1 == null) { 126 throw new RuntimeException("can't find com/foo/Resource1 in test1.jar"); 127 } 128 loader.close(); 129 130 // should NOT find /resource2 even though it is in jar 131 URL u2 = loader.findResource("com/foo/Resource2"); 132 if (u2 != null) { 133 throw new RuntimeException("com/foo/Resource2 unexpected in test1.jar"); 134 } 135 136 // load tests 137 loadClass("com.foo.TestClass1", loader, false); 138 loadClass("com.foo.TestClass", loader, true); 139 loadClass("java.util.ArrayList", loader, true); 140 141 // now check we can delete the path 142 rm_minus_rf(new File(name)); 143 System.out.println(" ... OK"); 144 } 145 146 static HttpServer httpServer; 147 148 static HttpServer getHttpServer() { 149 return httpServer; 150 } 151 152 static URL getServerURL() throws Exception { 153 int port = httpServer.getAddress().getPort(); 154 String s = "http://127.0.0.1:" + port + "/"; 155 return new URL(s); 156 } 157 158 static void startHttpServer(String docroot) throws Exception { 159 httpServer = HttpServer.create(new InetSocketAddress(0), 10); 160 HttpContext ctx = httpServer.createContext( 161 "/", new FileServerHandler(docroot) 162 ); 163 httpServer.start(); 164 } 165 166 /** 167 * Prepare jars files for the tests 168 */ 169 private static void setup () throws IOException { 170 String[] tests = new String[]{"test1", "test2"}; 171 Path workDir = Paths.get(WORK_DIR); 172 Path testSrc = Paths.get(System.getProperty("test.src")); 173 for (String test : tests) { 174 Path testSrcDir = testSrc.resolve(test); 175 Path testTargetDir = workDir.resolve(test); 176 // Compile sources for corresponding test 177 CompilerUtils.compile(testSrcDir, testTargetDir); 178 // Copy all resources 179 Path packages = Paths.get("com", "foo"); 180 Path copySrcDir = testSrcDir.resolve(packages); 181 Path copyTargetDir = testTargetDir.resolve(packages); 182 Files.createDirectories(copyTargetDir); 183 Path res1 = Paths.get("Resource1"); 184 Path res2 = Paths.get("Resource2"); 185 Files.copy(copySrcDir.resolve(res1), copyTargetDir.resolve(res1), 186 REPLACE_EXISTING); 187 Files.copy(copySrcDir.resolve(res2), copyTargetDir.resolve(res2), 188 REPLACE_EXISTING); 189 // Create jar 190 JarUtils.createJarFile(workDir.resolve(test + ".jar"), testTargetDir); 191 } 192 193 // Copy and compile server test class 194 Path serverRoot = Paths.get("serverRoot"); 195 Path targetDir = workDir.resolve(serverRoot); 196 Path file = Paths.get("Test.java"); 197 Files.createDirectories(targetDir); 198 Files.copy(testSrc.resolve(serverRoot).resolve(file), 199 targetDir.resolve(file), REPLACE_EXISTING); 200 CompilerUtils.compile(targetDir, targetDir); 201 } 202 }