jdk/test/java/rmi/activation/rmidViaInheritedChannel/RmidViaInheritedChannel.java
author duke
Sat, 01 Dec 2007 00:00:00 +0000
changeset 2 90ce3da70b43
child 2443 4bcc75ed04c0
permissions -rw-r--r--
Initial load
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
90ce3da70b43 Initial load
duke
parents:
diff changeset
     2
 * Copyright 2003 Sun Microsystems, Inc.  All Rights Reserved.
90ce3da70b43 Initial load
duke
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
90ce3da70b43 Initial load
duke
parents:
diff changeset
     4
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
90ce3da70b43 Initial load
duke
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
90ce3da70b43 Initial load
duke
parents:
diff changeset
     7
 * published by the Free Software Foundation.
90ce3da70b43 Initial load
duke
parents:
diff changeset
     8
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
     9
 * This code is distributed in the hope that it will be useful, but WITHOUT
90ce3da70b43 Initial load
duke
parents:
diff changeset
    10
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
90ce3da70b43 Initial load
duke
parents:
diff changeset
    11
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
90ce3da70b43 Initial load
duke
parents:
diff changeset
    12
 * version 2 for more details (a copy is included in the LICENSE file that
90ce3da70b43 Initial load
duke
parents:
diff changeset
    13
 * accompanied this code).
90ce3da70b43 Initial load
duke
parents:
diff changeset
    14
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    15
 * You should have received a copy of the GNU General Public License version
90ce3da70b43 Initial load
duke
parents:
diff changeset
    16
 * 2 along with this work; if not, write to the Free Software Foundation,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    17
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    18
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    19
 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    20
 * CA 95054 USA or visit www.sun.com if you need additional information or
90ce3da70b43 Initial load
duke
parents:
diff changeset
    21
 * have any questions.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    22
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    23
90ce3da70b43 Initial load
duke
parents:
diff changeset
    24
/* @test
90ce3da70b43 Initial load
duke
parents:
diff changeset
    25
 * @bug 4295885
90ce3da70b43 Initial load
duke
parents:
diff changeset
    26
 * @summary rmid should be startable from inetd
90ce3da70b43 Initial load
duke
parents:
diff changeset
    27
 * @author Ann Wollrath
90ce3da70b43 Initial load
duke
parents:
diff changeset
    28
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
 * @library ../../testlibrary
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
 * @build RMID ActivationLibrary
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
 * @build RmidViaInheritedChannel
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
 * @run main/othervm/timeout=240 -Djava.rmi.activation.port=5398 RmidViaInheritedChannel
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
import java.io.IOException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
import java.net.InetAddress;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
import java.net.InetSocketAddress;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
import java.net.ServerSocket;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
import java.nio.channels.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
import java.nio.channels.spi.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
import java.rmi.Remote;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
import java.rmi.NotBoundException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
import java.rmi.activation.ActivationGroup;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
import java.rmi.activation.ActivationSystem;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
import java.rmi.registry.LocateRegistry;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
import java.rmi.registry.Registry;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
import java.rmi.server.UnicastRemoteObject;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
public class RmidViaInheritedChannel implements Callback {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
    private static final int PORT = 5398;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
    private static final Object lock = new Object();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
    private static boolean notified = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
    private RmidViaInheritedChannel() {}
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
    public void notifyTest() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
        synchronized (lock) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
            notified = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
            System.err.println("notification received.");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
            lock.notifyAll();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
    public static void main(String[] args) throws Exception {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
        RMID rmid = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
        Callback obj = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
            /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
             * Export callback object and bind in registry.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
             */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
            System.err.println("export callback object and bind in registry");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
            obj = new RmidViaInheritedChannel();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
            Callback proxy = (Callback)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
                UnicastRemoteObject.exportObject(obj, 0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
            Registry registry =
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
                LocateRegistry.createRegistry(TestLibrary.REGISTRY_PORT);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
            registry.bind("Callback", proxy);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
            /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
             * Start rmid.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
             */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
            System.err.println("start rmid with inherited channel");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
            RMID.removeLog();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
            rmid = RMID.createRMID(System.out, System.err, true, false, PORT);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
            rmid.addOptions(new String[]{
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
                "-Djava.nio.channels.spi.SelectorProvider=RmidViaInheritedChannel$RmidSelectorProvider"});
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
            rmid.start();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
            /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
             * Get activation system and wait to be notified via callback
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
             * from rmid's selector provider.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
             */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
            System.err.println("get activation system");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
            ActivationSystem system = ActivationGroup.getSystem();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
            System.err.println("ActivationSystem = " + system);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
            synchronized (lock) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
                while (!notified) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
                    lock.wait();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
            System.err.println("TEST PASSED");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
        } finally {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
            if (obj != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
                UnicastRemoteObject.unexportObject(obj, true);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
            ActivationLibrary.rmidCleanup(rmid, PORT);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
    public static class RmidSelectorProvider extends SelectorProvider {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
        private final SelectorProvider provider;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
        private ServerSocketChannel channel = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
        public RmidSelectorProvider() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
            provider =  sun.nio.ch.DefaultSelectorProvider.create();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
        public DatagramChannel openDatagramChannel()
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
            throws IOException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
        {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
            return provider.openDatagramChannel();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
        public Pipe openPipe()
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
            throws IOException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
        {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
            return provider.openPipe();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
        public AbstractSelector openSelector()
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
            throws IOException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
        {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
            return provider.openSelector();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
        public ServerSocketChannel openServerSocketChannel()
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
            throws IOException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
        {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
            return provider.openServerSocketChannel();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
        public SocketChannel openSocketChannel()
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
             throws IOException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
        {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
            return provider.openSocketChannel();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
        public synchronized Channel inheritedChannel() throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
            System.err.println("RmidSelectorProvider.inheritedChannel");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
            if (channel == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
                /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
                 * Create server socket channel and bind server socket.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
                 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
                channel = ServerSocketChannel.open();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
                ServerSocket serverSocket = channel.socket();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
                serverSocket.bind(
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
                     new InetSocketAddress(InetAddress.getLocalHost(), PORT));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
                System.err.println("serverSocket = " + serverSocket);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
                /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
                 * Notify test that inherited channel was created.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
                 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
                try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
                    System.err.println("notify test...");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
                    Registry registry =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
                        LocateRegistry.getRegistry(TestLibrary.REGISTRY_PORT);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
                    Callback obj = (Callback) registry.lookup("Callback");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
                    obj.notifyTest();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
                } catch (NotBoundException nbe) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
                    throw (IOException)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
                        new IOException("callback object not bound").
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
                            initCause(nbe);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
            return channel;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   184
90ce3da70b43 Initial load
duke
parents:
diff changeset
   185
interface Callback extends Remote {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   186
    void notifyTest() throws IOException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   187
}