1 /* 2 * Copyright (c) 2003, 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 4295885 6824141 26 * @summary rmid should be startable from inetd 27 * @author Ann Wollrath 28 * 29 * @library ../../testlibrary 30 * @build RMID ActivationLibrary 31 * @build RmidViaInheritedChannel 32 * @run main/othervm/timeout=240 -Djava.rmi.activation.port=5398 RmidViaInheritedChannel 33 */ 34 35 import java.io.IOException; 36 import java.net.InetAddress; 37 import java.net.InetSocketAddress; 38 import java.net.ServerSocket; 39 import java.net.ProtocolFamily; 40 import java.nio.channels.*; 41 import java.nio.channels.spi.*; 42 import java.rmi.Remote; 43 import java.rmi.NotBoundException; 44 import java.rmi.activation.ActivationGroup; 45 import java.rmi.activation.ActivationSystem; 46 import java.rmi.registry.LocateRegistry; 47 import java.rmi.registry.Registry; 48 import java.rmi.server.UnicastRemoteObject; 49 50 public class RmidViaInheritedChannel implements Callback { 51 52 private static final int PORT = 5398; 53 private static final Object lock = new Object(); 54 private static boolean notified = false; 55 56 private RmidViaInheritedChannel() {} 57 58 public void notifyTest() { 59 synchronized (lock) { 60 notified = true; 61 System.err.println("notification received."); 62 lock.notifyAll(); 63 } 64 } 65 66 public static void main(String[] args) throws Exception { 67 68 RMID rmid = null; 69 Callback obj = null; 70 71 try { 72 /* 73 * Export callback object and bind in registry. 74 */ 75 System.err.println("export callback object and bind in registry"); 76 obj = new RmidViaInheritedChannel(); 77 Callback proxy = (Callback) 78 UnicastRemoteObject.exportObject(obj, 0); 79 Registry registry = 80 LocateRegistry.createRegistry(TestLibrary.REGISTRY_PORT); 81 registry.bind("Callback", proxy); 82 83 /* 84 * Start rmid. 85 */ 86 System.err.println("start rmid with inherited channel"); 87 RMID.removeLog(); 88 rmid = RMID.createRMID(System.out, System.err, true, false, PORT); 89 rmid.addOptions(new String[]{ 90 "-Djava.nio.channels.spi.SelectorProvider=RmidViaInheritedChannel$RmidSelectorProvider"}); 91 rmid.start(); 92 93 /* 94 * Get activation system and wait to be notified via callback 95 * from rmid's selector provider. 96 */ 97 System.err.println("get activation system"); 98 ActivationSystem system = ActivationGroup.getSystem(); 99 System.err.println("ActivationSystem = " + system); 100 synchronized (lock) { 101 while (!notified) { 102 lock.wait(); 103 } 104 } 105 System.err.println("TEST PASSED"); 106 107 } finally { 108 if (obj != null) { 109 UnicastRemoteObject.unexportObject(obj, true); 110 } 111 ActivationLibrary.rmidCleanup(rmid, PORT); 112 } 113 } 114 115 public static class RmidSelectorProvider extends SelectorProvider { 116 117 private final SelectorProvider provider; 118 private ServerSocketChannel channel = null; 119 120 public RmidSelectorProvider() { 121 provider = sun.nio.ch.DefaultSelectorProvider.create(); 122 } 123 124 public DatagramChannel openDatagramChannel() 125 throws IOException 126 { 127 return provider.openDatagramChannel(); 128 } 129 130 public DatagramChannel openDatagramChannel(ProtocolFamily family) 131 throws IOException 132 { 133 return provider.openDatagramChannel(family); 134 } 135 136 public Pipe openPipe() 137 throws IOException 138 { 139 return provider.openPipe(); 140 } 141 142 public AbstractSelector openSelector() 143 throws IOException 144 { 145 return provider.openSelector(); 146 } 147 148 public ServerSocketChannel openServerSocketChannel() 149 throws IOException 150 { 151 return provider.openServerSocketChannel(); 152 } 153 154 public SocketChannel openSocketChannel() 155 throws IOException 156 { 157 return provider.openSocketChannel(); 158 } 159 160 public synchronized Channel inheritedChannel() throws IOException { 161 System.err.println("RmidSelectorProvider.inheritedChannel"); 162 if (channel == null) { 163 /* 164 * Create server socket channel and bind server socket. 165 */ 166 channel = ServerSocketChannel.open(); 167 ServerSocket serverSocket = channel.socket(); 168 serverSocket.bind( 169 new InetSocketAddress(InetAddress.getLocalHost(), PORT)); 170 System.err.println("serverSocket = " + serverSocket); 171 172 /* 173 * Notify test that inherited channel was created. 174 */ 175 try { 176 System.err.println("notify test..."); 177 Registry registry = 178 LocateRegistry.getRegistry(TestLibrary.REGISTRY_PORT); 179 Callback obj = (Callback) registry.lookup("Callback"); 180 obj.notifyTest(); 181 } catch (NotBoundException nbe) { 182 throw (IOException) 183 new IOException("callback object not bound"). 184 initCause(nbe); 185 } 186 } 187 return channel; 188 } 189 } 190 } 191 192 interface Callback extends Remote { 193 void notifyTest() throws IOException; 194 }