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.InetSocketAddress; 26 import java.net.ProtocolFamily; 27 import java.nio.channels.Channel; 28 import java.nio.channels.DatagramChannel; 29 import java.nio.channels.Pipe; 30 import java.nio.channels.ServerSocketChannel; 31 import java.nio.channels.SocketChannel; 32 import java.nio.channels.spi.AbstractSelector; 33 import java.nio.channels.spi.SelectorProvider; 34 import static java.net.StandardSocketOptions.SO_REUSEADDR; 35 import static java.net.StandardSocketOptions.SO_REUSEPORT; 36 37 /** 38 * A SelectorProvider, that can be loaded by the child rmid process, whose 39 * inheritedChannel method will create a new server socket channel and report 40 * it back to the parent process, over stdout. 41 */ 42 public class RMIDSelectorProvider extends SelectorProvider { 43 44 private final SelectorProvider provider; 45 private ServerSocketChannel channel; 46 47 public RMIDSelectorProvider() { 48 provider = sun.nio.ch.DefaultSelectorProvider.create(); 49 } 50 51 public DatagramChannel openDatagramChannel() 52 throws IOException 53 { 54 return provider.openDatagramChannel(); 55 } 56 57 public DatagramChannel openDatagramChannel(ProtocolFamily family) 58 throws IOException 59 { 60 return provider.openDatagramChannel(family); 61 } 62 63 public Pipe openPipe() 64 throws IOException 65 { 66 return provider.openPipe(); 67 } 68 69 public AbstractSelector openSelector() 70 throws IOException 71 { 72 return provider.openSelector(); 73 } 74 75 public ServerSocketChannel openServerSocketChannel() 76 throws IOException 77 { 78 return provider.openServerSocketChannel(); 79 } 80 81 public SocketChannel openSocketChannel() 82 throws IOException 83 { 84 return provider.openSocketChannel(); 85 } 86 87 public synchronized Channel inheritedChannel() throws IOException { 88 System.out.println("RMIDSelectorProvider.inheritedChannel"); 89 if (channel == null) { 90 // Create and bind a new server socket channel 91 channel = ServerSocketChannel.open(); 92 93 // Enable SO_REUSEADDR before binding 94 channel.setOption(SO_REUSEADDR, true); 95 96 // Enable SO_REUSEPORT, if supported, before binding 97 if (channel.supportedOptions().contains(SO_REUSEPORT)) { 98 channel.setOption(SO_REUSEPORT, true); 99 } 100 101 channel.bind(new InetSocketAddress(0)); 102 103 System.out.println(RMID.EPHEMERAL_MSG + channel.socket().getLocalPort()); 104 } 105 return channel; 106 } 107 }