169N/A/*
5266N/A * Copyright (c) 2005, 2012, Oracle and/or its affiliates. All rights reserved.
0N/A * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
0N/A *
0N/A * This code is free software; you can redistribute it and/or modify it
0N/A * under the terms of the GNU General Public License version 2 only, as
0N/A * published by the Free Software Foundation.
0N/A *
0N/A * This code is distributed in the hope that it will be useful, but WITHOUT
0N/A * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
0N/A * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
0N/A * version 2 for more details (a copy is included in the LICENSE file that
0N/A * accompanied this code).
0N/A *
0N/A * You should have received a copy of the GNU General Public License version
0N/A * 2 along with this work; if not, write to the Free Software Foundation,
0N/A * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
0N/A *
2362N/A * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
2362N/A * or visit www.oracle.com if you need additional information or have any
2362N/A * questions.
0N/A */
0N/A
0N/A/* @test
0N/A * @bug 6269166
0N/A * @summary After a remote object has been exported on an anonymous
0N/A * port, it should be possible to export another remote object on an
0N/A * explicit port (and with the same socket factories, if any) with the
0N/A * same value as the actual port to which the first export got bound.
0N/A * While the fact that this works (instead of failing with a
0N/A * BindException) might seem odd and not to be expected, it has
0N/A * historically worked with the J2SE RMI implementation, so it should
0N/A * continue to work because existing applications might depend on it.
0N/A * @author Peter Jones
0N/A *
5266N/A * @library ../../testlibrary
5551N/A * @build TestLibrary
0N/A * @run main/othervm ReuseDefaultPort
0N/A */
0N/A
0N/Aimport java.io.IOException;
0N/Aimport java.net.ServerSocket;
0N/Aimport java.net.Socket;
0N/Aimport java.rmi.Remote;
0N/Aimport java.rmi.registry.LocateRegistry;
0N/Aimport java.rmi.registry.Registry;
0N/Aimport java.rmi.server.RMISocketFactory;
0N/Aimport java.rmi.server.UnicastRemoteObject;
0N/A
0N/Apublic class ReuseDefaultPort implements Remote {
0N/A
5266N/A private static final int PORT = TestLibrary.getUnusedRandomPort();
0N/A
0N/A private ReuseDefaultPort() { }
0N/A
0N/A public static void main(String[] args) throws Exception {
169N/A System.err.println("\nRegression test for bug 6269166\n");
169N/A RMISocketFactory.setSocketFactory(new SF());
169N/A Remote impl = new ReuseDefaultPort();
169N/A Remote stub = UnicastRemoteObject.exportObject(impl, 0);
169N/A System.err.println("- exported object: " + stub);
169N/A try {
169N/A Registry registry = LocateRegistry.createRegistry(PORT);
169N/A System.err.println("- exported registry: " + registry);
169N/A System.err.println("TEST PASSED");
169N/A } finally {
169N/A UnicastRemoteObject.unexportObject(impl, true);
169N/A }
0N/A }
0N/A
0N/A private static class SF extends RMISocketFactory {
169N/A private static RMISocketFactory defaultFactory =
169N/A RMISocketFactory.getDefaultSocketFactory();
169N/A SF() { }
169N/A public Socket createSocket(String host, int port) throws IOException {
169N/A return defaultFactory.createSocket(host, port);
169N/A }
169N/A public ServerSocket createServerSocket(int port) throws IOException {
169N/A if (port == 0) {
169N/A port = PORT;
169N/A }
169N/A return defaultFactory.createServerSocket(port);
169N/A }
0N/A }
0N/A}