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