jdk/test/java/rmi/transport/closeServerSocket/CloseServerSocket.java
author duke
Sat, 01 Dec 2007 00:00:00 +0000
changeset 2 90ce3da70b43
child 309 bda219d843f6
permissions -rw-r--r--
Initial load
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/* 
90ce3da70b43 Initial load
duke
parents:
diff changeset
     2
 * Copyright 2005 Sun Microsystems, Inc.  All Rights Reserved.
90ce3da70b43 Initial load
duke
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
90ce3da70b43 Initial load
duke
parents:
diff changeset
     4
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
90ce3da70b43 Initial load
duke
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
90ce3da70b43 Initial load
duke
parents:
diff changeset
     7
 * published by the Free Software Foundation.
90ce3da70b43 Initial load
duke
parents:
diff changeset
     8
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
     9
 * This code is distributed in the hope that it will be useful, but WITHOUT
90ce3da70b43 Initial load
duke
parents:
diff changeset
    10
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
90ce3da70b43 Initial load
duke
parents:
diff changeset
    11
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
90ce3da70b43 Initial load
duke
parents:
diff changeset
    12
 * version 2 for more details (a copy is included in the LICENSE file that
90ce3da70b43 Initial load
duke
parents:
diff changeset
    13
 * accompanied this code).
90ce3da70b43 Initial load
duke
parents:
diff changeset
    14
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    15
 * You should have received a copy of the GNU General Public License version
90ce3da70b43 Initial load
duke
parents:
diff changeset
    16
 * 2 along with this work; if not, write to the Free Software Foundation,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    17
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    18
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    19
 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    20
 * CA 95054 USA or visit www.sun.com if you need additional information or
90ce3da70b43 Initial load
duke
parents:
diff changeset
    21
 * have any questions.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    22
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    23
90ce3da70b43 Initial load
duke
parents:
diff changeset
    24
/* @test
90ce3da70b43 Initial load
duke
parents:
diff changeset
    25
 * @bug 4457683
90ce3da70b43 Initial load
duke
parents:
diff changeset
    26
 * @summary After all of the remote objects (including a registry, if
90ce3da70b43 Initial load
duke
parents:
diff changeset
    27
 * applicable) that had been exported with a given
90ce3da70b43 Initial load
duke
parents:
diff changeset
    28
 * RMIServerSocketFactory value (including null) have been unexported,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
 * the server socket created for the exports should be closed (so that
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
 * the local port is released).
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
 * @author Peter Jones
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
 * @build CloseServerSocket
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
 * @run main/othervm CloseServerSocket
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
import java.io.IOException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
import java.net.BindException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
import java.net.ServerSocket;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
import java.rmi.Remote;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
import java.rmi.registry.LocateRegistry;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
import java.rmi.registry.Registry;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
import java.rmi.server.RMIServerSocketFactory;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
import java.rmi.server.UnicastRemoteObject;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
public class CloseServerSocket implements Remote {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
    private static final int PORT = 2020;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
    private CloseServerSocket() { }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
    public static void main(String[] args) throws Exception {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
	System.err.println("\nRegression test for bug 4457683\n");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
	verifyPortFree(PORT);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
	Registry registry = LocateRegistry.createRegistry(PORT);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
	System.err.println("- exported registry: " + registry);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
	verifyPortInUse(PORT);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
	UnicastRemoteObject.unexportObject(registry, true);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
	System.err.println("- unexported registry");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
	Thread.sleep(1);	// work around BindException (bug?)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
	verifyPortFree(PORT);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
	/*
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
	 * The follow portion of this test is disabled temporarily
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
	 * because 4457683 was partially backed out because of
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
	 * 6269166; for now, only server sockets originally opened for
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
	 * exports on non-anonymous ports will be closed when all of
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
	 * the corresponding remote objects have been exported.  A
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
	 * separate bug will be filed to represent the remainder of
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
	 * 4457683 for anonymous-port exports.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
	 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
//	SSF ssf = new SSF();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
//	Remote impl = new CloseServerSocket();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
//	Remote stub = UnicastRemoteObject.exportObject(impl, 0, null, ssf);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
//	System.err.println("- exported object: " + stub);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
//	UnicastRemoteObject.unexportObject(impl, true);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
//	System.err.println("- unexported object");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
//	synchronized (ssf) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
//	    if (!ssf.serverSocketClosed) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
//		throw new RuntimeException("TEST FAILED: " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
//					   "server socket not closed");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
//	    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
//	}
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
	System.err.println("TEST PASSED");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
    private static void verifyPortFree(int port) throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
	ServerSocket ss = new ServerSocket(PORT);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
	ss.close();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
	System.err.println("- port " + port + " is free");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
    private static void verifyPortInUse(int port) throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
	try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
	    verifyPortFree(port);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
	} catch (BindException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
	    System.err.println("- port " + port + " is in use");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
	    return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
	}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
    private static class SSF implements RMIServerSocketFactory {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
	boolean serverSocketClosed = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
	SSF() { };
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
	public ServerSocket createServerSocket(int port) throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
	    return new SS(port);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
	}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
	private class SS extends ServerSocket {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
	    SS(int port) throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
		super(port);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
		System.err.println("- created server socket: " + this);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
	    };
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
	    public void close() throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
		synchronized (SSF.this) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
		    serverSocketClosed = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
		    SSF.this.notifyAll();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
		}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
		System.err.println("- closing server socket: " + this);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
		super.close();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
	    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
	}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
}