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