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