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