1 /* 2 * Copyright (c) 2016, 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 import java.io.*; 25 import java.net.BindException; 26 import java.net.InetSocketAddress; 27 import java.net.ProtocolFamily; 28 import java.nio.channels.Channel; 29 import java.nio.channels.DatagramChannel; 30 import java.nio.channels.Pipe; 31 import java.nio.channels.ServerSocketChannel; 32 import java.nio.channels.SocketChannel; 33 import java.nio.channels.spi.AbstractSelector; 34 import java.nio.channels.spi.SelectorProvider; 35 import java.time.LocalTime; 36 import static java.net.StandardSocketOptions.SO_REUSEADDR; 37 import static java.net.StandardSocketOptions.SO_REUSEPORT; 38 39 /** 40 * A SelectorProvider, that can be loaded by the child rmid process, whose 41 * inheritedChannel method will create a new server socket channel and report 42 * it back to the parent process, over stdout. 43 */ 44 public class RMIDSelectorProvider extends SelectorProvider { 45 46 private final SelectorProvider provider; 47 private ServerSocketChannel channel; 48 RMIDSelectorProvider()49 public RMIDSelectorProvider() { 50 provider = sun.nio.ch.DefaultSelectorProvider.create(); 51 } 52 openDatagramChannel()53 public DatagramChannel openDatagramChannel() 54 throws IOException 55 { 56 return provider.openDatagramChannel(); 57 } 58 openDatagramChannel(ProtocolFamily family)59 public DatagramChannel openDatagramChannel(ProtocolFamily family) 60 throws IOException 61 { 62 return provider.openDatagramChannel(family); 63 } 64 openPipe()65 public Pipe openPipe() 66 throws IOException 67 { 68 return provider.openPipe(); 69 } 70 openSelector()71 public AbstractSelector openSelector() 72 throws IOException 73 { 74 return provider.openSelector(); 75 } 76 openServerSocketChannel()77 public ServerSocketChannel openServerSocketChannel() 78 throws IOException 79 { 80 return provider.openServerSocketChannel(); 81 } 82 openSocketChannel()83 public SocketChannel openSocketChannel() 84 throws IOException 85 { 86 return provider.openSocketChannel(); 87 } 88 inheritedChannel()89 public synchronized Channel inheritedChannel() throws IOException { 90 System.out.println("RMIDSelectorProvider.inheritedChannel"); 91 if (channel == null) { 92 // Create and bind a new server socket channel 93 channel = ServerSocketChannel.open(); 94 95 // Enable SO_REUSEADDR before binding 96 channel.setOption(SO_REUSEADDR, true); 97 98 // Enable SO_REUSEPORT, if supported, before binding 99 if (channel.supportedOptions().contains(SO_REUSEPORT)) { 100 channel.setOption(SO_REUSEPORT, true); 101 } 102 103 // when it comes here, these properties should have been set with 104 // a valid value, but assign a default value anyway. 105 long timeout = Long.getLong( 106 "test.java.rmi.testlibrary.RMIDSelectorProvider.timeout", 107 200_000); 108 long deadline = System.currentTimeMillis() + timeout; 109 int port = Integer.getInteger( 110 "test.java.rmi.testlibrary.RMIDSelectorProvider.port", 0); 111 while (true) { 112 try { 113 channel.bind(new InetSocketAddress(port)); 114 break; 115 } catch (BindException ex) { 116 System.out.format("RMIDSelectorProvider: " 117 + "failed to bind to port %d due to \"%s\", at %s%n", 118 port, ex.getMessage(), LocalTime.now()); 119 } 120 if (System.currentTimeMillis() > deadline) { 121 System.out.format("RMIDSelectorProvider: " 122 + "fail to bind to port %d after trying for " 123 + "%d seconds, exiting rmid process, at %s%n", 124 port, timeout/1000, LocalTime.now()); 125 channel.close(); 126 // can not start rmid on specific port, 127 // there is no need to continue run rmid. 128 System.exit(1); 129 } 130 try { 131 Thread.sleep(1000); 132 } catch(InterruptedException ignore) { } 133 } 134 135 System.out.println(RMID.EPHEMERAL_MSG + channel.socket().getLocalPort()); 136 } 137 return channel; 138 } 139 } 140