1 /* |
|
2 * Copyright (c) 2016, 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 import java.io.OutputStream; |
|
25 import java.io.IOException; |
|
26 |
|
27 /** |
|
28 * Class to run and control rmiregistry in a sub-process. |
|
29 * |
|
30 * We can't kill a registry if we have too-close control |
|
31 * over it. We must make it in a subprocess, and then kill the |
|
32 * subprocess when it has served our needs. |
|
33 */ |
|
34 public class REGISTRY extends JavaVM { |
|
35 |
|
36 private static final double START_TIMEOUT = |
|
37 20_000 * TestLibrary.getTimeoutFactor(); |
|
38 private static final String DEFAULT_RUNNER = "RegistryRunner"; |
|
39 |
|
40 private int port = -1; |
|
41 |
|
42 private REGISTRY(String runner, OutputStream out, OutputStream err, |
|
43 String options, int port) { |
|
44 super(runner, options, Integer.toString(port), out, err); |
|
45 try { |
|
46 Class runnerClass = Class.forName(runner); |
|
47 if (!RegistryRunner.class.isAssignableFrom(runnerClass)) { |
|
48 throw new RuntimeException("runner class must be RegistryRunner" |
|
49 + " or its sub class"); |
|
50 } |
|
51 } catch (ClassNotFoundException ex) { |
|
52 throw new RuntimeException(ex); |
|
53 } |
|
54 this.port = port; |
|
55 } |
|
56 |
|
57 public static REGISTRY createREGISTRY() { |
|
58 return createREGISTRYWithRunner(DEFAULT_RUNNER, System.out, System.err, "", 0); |
|
59 } |
|
60 |
|
61 public static REGISTRY createREGISTRY(OutputStream out, OutputStream err, |
|
62 String options, int port) { |
|
63 return createREGISTRYWithRunner(DEFAULT_RUNNER, out, err, options, port); |
|
64 } |
|
65 |
|
66 public static REGISTRY createREGISTRYWithRunner(String runner, String options) { |
|
67 return createREGISTRYWithRunner(runner, System.out, System.err, options, 0); |
|
68 } |
|
69 |
|
70 public static REGISTRY createREGISTRYWithRunner(String runner, OutputStream out, |
|
71 OutputStream err, String options, int port) { |
|
72 options += " --add-exports=java.rmi/sun.rmi.registry=ALL-UNNAMED" |
|
73 + " --add-exports=java.rmi/sun.rmi.server=ALL-UNNAMED" |
|
74 + " --add-exports=java.rmi/sun.rmi.transport=ALL-UNNAMED" |
|
75 + " --add-exports=java.rmi/sun.rmi.transport.tcp=ALL-UNNAMED"; |
|
76 REGISTRY reg = new REGISTRY(runner, out, err, options, port); |
|
77 return reg; |
|
78 } |
|
79 |
|
80 /** |
|
81 * Starts the registry in a sub-process and waits up to |
|
82 * the given timeout period to confirm that it's running, |
|
83 * and get the port where it's running. |
|
84 */ |
|
85 public void start() throws IOException { |
|
86 super.start(); |
|
87 long startTime = System.currentTimeMillis(); |
|
88 long deadline = TestLibrary.computeDeadline(startTime, (long)START_TIMEOUT); |
|
89 while (true) { |
|
90 try { |
|
91 Thread.sleep(1000); |
|
92 } catch (InterruptedException ignore) { } |
|
93 |
|
94 String output = outputStream.ba.toString(); |
|
95 port = RegistryRunner.getRegistryPort(output); |
|
96 if (port != -1) { |
|
97 break; |
|
98 } |
|
99 if (System.currentTimeMillis() > deadline) { |
|
100 TestLibrary.bomb("Failed to start registry, giving up after " + |
|
101 (System.currentTimeMillis() - startTime) + "ms.", null); |
|
102 } |
|
103 } |
|
104 } |
|
105 |
|
106 /** |
|
107 * Shuts down the registry. |
|
108 */ |
|
109 public void shutdown() { |
|
110 RegistryRunner.requestExit(port); |
|
111 } |
|
112 |
|
113 /** |
|
114 * Gets the port where the registry is serving. |
|
115 */ |
|
116 public int getPort() { |
|
117 return port; |
|
118 } |
|
119 } |
|