1 /* 2 * Copyright (c) 2005, 2013, 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 /* @test 25 * @bug 4457683 26 * @summary After all of the remote objects (including a registry, if 27 * applicable) that had been exported with a given 28 * RMIServerSocketFactory value (including null) have been unexported, 29 * the server socket created for the exports should be closed (so that 30 * the local port is released). 31 * @author Peter Jones 32 * 33 * @library ../../testlibrary 34 * @build TestLibrary 35 * @run main/othervm CloseServerSocket 36 */ 37 38 import java.io.IOException; 39 import java.net.BindException; 40 import java.net.ServerSocket; 41 import java.rmi.Remote; 42 import java.rmi.registry.LocateRegistry; 43 import java.rmi.registry.Registry; 44 import java.rmi.server.RMIServerSocketFactory; 45 import java.rmi.server.UnicastRemoteObject; 46 47 public class CloseServerSocket implements Remote { 48 private static final int PORT = TestLibrary.getUnusedRandomPort(); 49 50 private CloseServerSocket() { } 51 52 public static void main(String[] args) throws Exception { 53 System.err.println("\nRegression test for bug 4457683\n"); 54 55 verifyPortFree(PORT); 56 Registry registry = LocateRegistry.createRegistry(PORT); 57 System.err.println("- exported registry: " + registry); 58 verifyPortInUse(PORT); 59 UnicastRemoteObject.unexportObject(registry, true); 60 System.err.println("- unexported registry"); 61 Thread.sleep(1000); // work around BindException (bug?) 62 verifyPortFree(PORT); 63 64 /* 65 * The follow portion of this test is disabled temporarily 66 * because 4457683 was partially backed out because of 67 * 6269166; for now, only server sockets originally opened for 68 * exports on non-anonymous ports will be closed when all of 69 * the corresponding remote objects have been exported. A 70 * separate bug will be filed to represent the remainder of 71 * 4457683 for anonymous-port exports. 72 */ 73 74 // SSF ssf = new SSF(); 75 // Remote impl = new CloseServerSocket(); 76 // Remote stub = UnicastRemoteObject.exportObject(impl, 0, null, ssf); 77 // System.err.println("- exported object: " + stub); 78 // UnicastRemoteObject.unexportObject(impl, true); 79 // System.err.println("- unexported object"); 80 // synchronized (ssf) { 81 // if (!ssf.serverSocketClosed) { 82 // throw new RuntimeException("TEST FAILED: " + 83 // "server socket not closed"); 84 // } 85 // } 86 87 System.err.println("TEST PASSED"); 88 } 89 90 private static void verifyPortFree(int port) throws IOException { 91 ServerSocket ss = new ServerSocket(port); 92 ss.close(); 93 System.err.println("- port " + port + " is free"); 94 } 95 96 private static void verifyPortInUse(int port) throws IOException { 97 try { 98 verifyPortFree(port); 99 } catch (BindException e) { 100 System.err.println("- port " + port + " is in use"); 101 return; 102 } 103 } 104 105 private static class SSF implements RMIServerSocketFactory { 106 boolean serverSocketClosed = false; 107 SSF() { }; 108 109 public ServerSocket createServerSocket(int port) throws IOException { 110 return new SS(port); 111 } 112 113 private class SS extends ServerSocket { 114 SS(int port) throws IOException { 115 super(port); 116 System.err.println("- created server socket: " + this); 117 }; 118 119 public void close() throws IOException { 120 synchronized (SSF.this) { 121 serverSocketClosed = true; 122 SSF.this.notifyAll(); 123 } 124 System.err.println("- closing server socket: " + this); 125 super.close(); 126 } 127 } 128 } 129 }