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