1 /*
2  * Copyright (c) 1999, 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 /**/
25 
26 import java.rmi.*;
27 import java.rmi.registry.*;
28 import java.rmi.server.*;
29 
30 /**
31  * Class to run a registry whos VM can be told to exit remotely; using
32  * the rmiregistry in this fashion makes tests more robust under
33  * windows where Process.destroy() seems not to be 100% reliable.
34  */
35 public class RegistryRunner extends UnicastRemoteObject
36     implements RemoteExiter
37 {
38     private static Registry registry = null;
39     private static RemoteExiter exiter = null;
40 
RegistryRunner()41     public RegistryRunner() throws RemoteException {
42     }
43 
44     /**
45      * Ask the registry to exit instead of forcing it do so; this
46      * works better on windows...
47      */
exit()48     public void exit() throws RemoteException {
49         // REMIND: create a thread to do this to avoid
50         // a remote exception?
51         System.err.println("received call to exit");
52         System.exit(0);
53     }
54 
55     /**
56      * Request that the registry process exit and handle
57      * related exceptions.
58      */
requestExit(int port)59     public static void requestExit(int port) {
60 
61         try {
62             RemoteExiter exiter =
63                 (RemoteExiter)
64                 Naming.lookup("rmi://localhost:" +
65                               port +
66                               "/RemoteExiter");
67             try {
68                 exiter.exit();
69             } catch (RemoteException re) {
70             }
71             exiter = null;
72         } catch (java.net.MalformedURLException mfue) {
73             // will not happen
74         } catch (NotBoundException nbe) {
75             TestLibrary.bomb("exiter not bound?", nbe);
76         } catch (RemoteException re) {
77             TestLibrary.bomb("remote exception trying to exit",
78                              re);
79         }
80     }
81 
main(String[] args)82     public static void main(String[] args) {
83         try {
84             if (args.length == 0) {
85                 System.err.println("Usage: <port>");
86                 System.exit(0);
87             }
88             int port = -1;
89             try {
90                 port = Integer.parseInt(args[0]);
91             } catch (NumberFormatException nfe) {
92             }
93 
94             // create a registry
95             registry = LocateRegistry.createRegistry(port);
96 
97             // create a remote object to tell this VM to exit
98             exiter = new RegistryRunner();
99             Naming.rebind("rmi://localhost:" + port +
100                           "/RemoteExiter", exiter);
101 
102         } catch (Exception e) {
103             System.err.println(e.getMessage());
104             e.printStackTrace();
105             System.exit(-1);
106         }
107     }
108 }
109