1 /*
   2  * Copyright (c) 2009, 2012, 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  * @library ../../../../com/sun/net/httpserver
  28  * @build FileServerHandler
  29  * @run shell build.sh
  30  * @run main/othervm CloseTest
  31  * @summary URL-downloaded jar files can consume all available file descriptors
  32  */
  33 
  34 import java.io.*;
  35 import java.net.*;
  36 import java.lang.reflect.*;
  37 import com.sun.net.httpserver.*;
  38 
  39 public class CloseTest extends Common {
  40 
  41 //
  42 // needs two jar files test1.jar and test2.jar with following structure
  43 //
  44 // com/foo/TestClass
  45 // com/foo/TestClass1
  46 // com/foo/Resource1
  47 // com/foo/Resource2
  48 //
  49 // and a directory hierarchy with the same structure/contents
  50 
  51     public static void main (String args[]) throws Exception {
  52 
  53         String workdir = System.getProperty("test.classes");
  54         if (workdir == null) {
  55             workdir = args[0];
  56         }
  57         if (!workdir.endsWith("/")) {
  58             workdir = workdir+"/";
  59         }
  60 
  61         startHttpServer (workdir+"serverRoot/");
  62 
  63         String testjar = workdir + "test.jar";
  64         copyFile (workdir+"test1.jar", testjar);
  65         test (testjar, 1);
  66 
  67         // repeat test with different implementation
  68         // of test.jar (whose TestClass.getValue() returns 2
  69 
  70         copyFile (workdir+"test2.jar", testjar);
  71         test (testjar, 2);
  72 
  73         // repeat test using a directory of files
  74         String testdir=workdir+"testdir/";
  75         rm_minus_rf (new File(testdir));
  76         copyDir (workdir+"test1/", testdir);
  77         test (testdir, 1);
  78 
  79         testdir=workdir+"testdir/";
  80         rm_minus_rf (new File(testdir));
  81         copyDir (workdir+"test2/", testdir);
  82         test (testdir, 2);
  83         getHttpServer().stop (3);
  84     }
  85 
  86     // create a loader on jarfile (or directory), plus a http loader
  87     // load a class , then look for a resource
  88     // also load a class from http loader
  89     // then close the loader
  90     // check further new classes/resources cannot be loaded
  91     // check jar (or dir) can be deleted
  92     // check existing classes can be loaded
  93     // check boot classes can be loaded
  94 
  95     static void test (String name, int expectedValue) throws Exception {
  96         URL url = new URL ("file", null, name);
  97         URL url2 = getServerURL();
  98         System.out.println ("Doing tests with URL: " + url + " and " + url2);
  99         URL[] urls = new URL[2];
 100         urls[0] =  url;
 101         urls[1] =  url2;
 102         URLClassLoader loader = new URLClassLoader (urls);
 103         Class testclass = loadClass ("com.foo.TestClass", loader, true);
 104         Class class2 = loadClass ("Test", loader, true); // from http
 105         class2.newInstance();
 106         Object test = testclass.newInstance();
 107         Method method = testclass.getDeclaredMethods()[0]; // int getValue();
 108         int res = (Integer) method.invoke (test);
 109 
 110         if (res != expectedValue) {
 111             throw new RuntimeException ("wrong value from getValue() ["+res+
 112                         "/"+expectedValue+"]");
 113         }
 114 
 115         // should find /resource1
 116         URL u1 = loader.findResource ("com/foo/Resource1");
 117         if (u1 == null) {
 118             throw new RuntimeException ("can't find com/foo/Resource1 in test1.jar");
 119         }
 120         loader.close ();
 121 
 122         // should NOT find /resource2 even though it is in jar
 123         URL u2 = loader.findResource ("com/foo/Resource2");
 124         if (u2 != null) {
 125             throw new RuntimeException ("com/foo/Resource2 unexpected in test1.jar");
 126         }
 127 
 128         // load tests
 129         loadClass ("com.foo.TestClass1", loader, false);
 130         loadClass ("com.foo.TestClass", loader, true);
 131         loadClass ("java.sql.Array", loader, true);
 132 
 133         // now check we can delete the path
 134         rm_minus_rf (new File(name));
 135         System.out.println (" ... OK");
 136     }
 137 
 138     static HttpServer httpServer;
 139 
 140     static HttpServer getHttpServer() {
 141         return httpServer;
 142     }
 143 
 144     static URL getServerURL () throws Exception {
 145         int port = httpServer.getAddress().getPort();
 146         String s = "http://127.0.0.1:"+port+"/";
 147         return new URL(s);
 148     }
 149 
 150     static void startHttpServer (String docroot) throws Exception {
 151         httpServer = HttpServer.create (new InetSocketAddress(0), 10);
 152         HttpContext ctx = httpServer.createContext (
 153                 "/", new FileServerHandler(docroot)
 154         );
 155         httpServer.start();
 156     }
 157 }