1 | /* $Id: TestVBox.java 69500 2017-10-28 15:14:05Z vboxsync $ */
|
---|
2 | /*! file
|
---|
3 | * Small sample/testcase which demonstrates that the same source code can
|
---|
4 | * be used to connect to the webservice and (XP)COM APIs.
|
---|
5 | */
|
---|
6 |
|
---|
7 | /*
|
---|
8 | * Copyright (C) 2010-2017 Oracle Corporation
|
---|
9 | *
|
---|
10 | * This file is part of VirtualBox Open Source Edition (OSE), as
|
---|
11 | * available from http://www.virtualbox.org. This file is free software;
|
---|
12 | * you can redistribute it and/or modify it under the terms of the GNU
|
---|
13 | * General Public License (GPL) as published by the Free Software
|
---|
14 | * Foundation, in version 2 as it comes in the "COPYING" file of the
|
---|
15 | * VirtualBox OSE distribution. VirtualBox OSE is distributed in the
|
---|
16 | * hope that it will be useful, but WITHOUT ANY WARRANTY of any kind.
|
---|
17 | */
|
---|
18 |
|
---|
19 | import org.virtualbox_5_0.*;
|
---|
20 | import java.util.List;
|
---|
21 | import java.util.Arrays;
|
---|
22 | import java.math.BigInteger;
|
---|
23 |
|
---|
24 | public class TestVBox
|
---|
25 | {
|
---|
26 | static void processEvent(IEvent ev)
|
---|
27 | {
|
---|
28 | System.out.println("got event: " + ev);
|
---|
29 | VBoxEventType type = ev.getType();
|
---|
30 | System.out.println("type = " + type);
|
---|
31 | switch (type)
|
---|
32 | {
|
---|
33 | case OnMachineStateChanged:
|
---|
34 | {
|
---|
35 | IMachineStateChangedEvent mcse = IMachineStateChangedEvent.queryInterface(ev);
|
---|
36 | if (mcse == null)
|
---|
37 | System.out.println("Cannot query an interface");
|
---|
38 | else
|
---|
39 | System.out.println("mid=" + mcse.getMachineId());
|
---|
40 | break;
|
---|
41 | }
|
---|
42 | }
|
---|
43 | }
|
---|
44 |
|
---|
45 | static class EventHandler
|
---|
46 | {
|
---|
47 | EventHandler() {}
|
---|
48 | public void handleEvent(IEvent ev)
|
---|
49 | {
|
---|
50 | try {
|
---|
51 | processEvent(ev);
|
---|
52 | } catch (Throwable t) {
|
---|
53 | t.printStackTrace();
|
---|
54 | }
|
---|
55 | }
|
---|
56 | }
|
---|
57 |
|
---|
58 | static void testEvents(VirtualBoxManager mgr, IEventSource es)
|
---|
59 | {
|
---|
60 | // active mode for Java doesn't fully work yet, and using passive
|
---|
61 | // is more portable (the only mode for MSCOM and WS) and thus generally
|
---|
62 | // recommended
|
---|
63 | IEventListener listener = es.createListener();
|
---|
64 |
|
---|
65 | es.registerListener(listener, Arrays.asList(VBoxEventType.Any), false);
|
---|
66 |
|
---|
67 | try {
|
---|
68 | for (int i = 0; i < 50; i++)
|
---|
69 | {
|
---|
70 | System.out.print(".");
|
---|
71 | IEvent ev = es.getEvent(listener, 500);
|
---|
72 | if (ev != null)
|
---|
73 | {
|
---|
74 | processEvent(ev);
|
---|
75 | es.eventProcessed(listener, ev);
|
---|
76 | }
|
---|
77 | // process system event queue
|
---|
78 | mgr.waitForEvents(0);
|
---|
79 | }
|
---|
80 | } catch (Exception e) {
|
---|
81 | e.printStackTrace();
|
---|
82 | }
|
---|
83 |
|
---|
84 | es.unregisterListener(listener);
|
---|
85 | }
|
---|
86 |
|
---|
87 | static void testEnumeration(VirtualBoxManager mgr, IVirtualBox vbox)
|
---|
88 | {
|
---|
89 | List<IMachine> machs = vbox.getMachines();
|
---|
90 | for (IMachine m : machs)
|
---|
91 | {
|
---|
92 | String name;
|
---|
93 | Long ram = 0L;
|
---|
94 | boolean hwvirtEnabled = false, hwvirtNestedPaging = false;
|
---|
95 | boolean paeEnabled = false;
|
---|
96 | boolean inaccessible = false;
|
---|
97 | try
|
---|
98 | {
|
---|
99 | name = m.getName();
|
---|
100 | ram = m.getMemorySize();
|
---|
101 | hwvirtEnabled = m.getHWVirtExProperty(HWVirtExPropertyType.Enabled);
|
---|
102 | hwvirtNestedPaging = m.getHWVirtExProperty(HWVirtExPropertyType.NestedPaging);
|
---|
103 | paeEnabled = m.getCPUProperty(CPUPropertyType.PAE);
|
---|
104 | String osType = m.getOSTypeId();
|
---|
105 | IGuestOSType foo = vbox.getGuestOSType(osType);
|
---|
106 | }
|
---|
107 | catch (VBoxException e)
|
---|
108 | {
|
---|
109 | name = "<inaccessible>";
|
---|
110 | inaccessible = true;
|
---|
111 | }
|
---|
112 | System.out.println("VM name: " + name);
|
---|
113 | if (!inaccessible)
|
---|
114 | {
|
---|
115 | System.out.println(" RAM size: " + ram + "MB"
|
---|
116 | + ", HWVirt: " + hwvirtEnabled
|
---|
117 | + ", Nested Paging: " + hwvirtNestedPaging
|
---|
118 | + ", PAE: " + paeEnabled);
|
---|
119 | }
|
---|
120 | }
|
---|
121 | // process system event queue
|
---|
122 | mgr.waitForEvents(0);
|
---|
123 | }
|
---|
124 |
|
---|
125 | static boolean progressBar(VirtualBoxManager mgr, IProgress p, long waitMillis)
|
---|
126 | {
|
---|
127 | long end = System.currentTimeMillis() + waitMillis;
|
---|
128 | while (!p.getCompleted())
|
---|
129 | {
|
---|
130 | // process system event queue
|
---|
131 | mgr.waitForEvents(0);
|
---|
132 | // wait for completion of the task, but at most 200 msecs
|
---|
133 | p.waitForCompletion(200);
|
---|
134 | if (System.currentTimeMillis() >= end)
|
---|
135 | return false;
|
---|
136 | }
|
---|
137 | return true;
|
---|
138 | }
|
---|
139 |
|
---|
140 | static void testStart(VirtualBoxManager mgr, IVirtualBox vbox)
|
---|
141 | {
|
---|
142 | IMachine m = vbox.getMachines().get(0);
|
---|
143 | String name = m.getName();
|
---|
144 | System.out.println("\nAttempting to start VM '" + name + "'");
|
---|
145 |
|
---|
146 | ISession session = mgr.getSessionObject();
|
---|
147 | IProgress p = m.launchVMProcess(session, "gui", "");
|
---|
148 | progressBar(mgr, p, 10000);
|
---|
149 | session.unlockMachine();
|
---|
150 | // process system event queue
|
---|
151 | mgr.waitForEvents(0);
|
---|
152 | }
|
---|
153 |
|
---|
154 | static void testMultiServer()
|
---|
155 | {
|
---|
156 | VirtualBoxManager mgr1 = VirtualBoxManager.createInstance(null);
|
---|
157 | VirtualBoxManager mgr2 = VirtualBoxManager.createInstance(null);
|
---|
158 |
|
---|
159 | try {
|
---|
160 | mgr1.connect("http://i7:18083", "", "");
|
---|
161 | mgr2.connect("http://main:18083", "", "");
|
---|
162 |
|
---|
163 | IMachine m1 = mgr1.getVBox().getMachines().get(0);
|
---|
164 | IMachine m2 = mgr2.getVBox().getMachines().get(0);
|
---|
165 | String name1 = m1.getName();
|
---|
166 | String name2 = m2.getName();
|
---|
167 | ISession session1 = mgr1.getSessionObject();
|
---|
168 | ISession session2 = mgr2.getSessionObject();
|
---|
169 | IProgress p1 = m1.launchVMProcess(session1, "gui", "");
|
---|
170 | IProgress p2 = m2.launchVMProcess(session2, "gui", "");
|
---|
171 | progressBar(mgr1, p1, 10000);
|
---|
172 | progressBar(mgr2, p2, 10000);
|
---|
173 | session1.unlockMachine();
|
---|
174 | session2.unlockMachine();
|
---|
175 | // process system event queue
|
---|
176 | mgr1.waitForEvents(0);
|
---|
177 | mgr2.waitForEvents(0);
|
---|
178 | } finally {
|
---|
179 | mgr1.cleanup();
|
---|
180 | mgr2.cleanup();
|
---|
181 | }
|
---|
182 | }
|
---|
183 |
|
---|
184 | static void testReadLog(VirtualBoxManager mgr, IVirtualBox vbox)
|
---|
185 | {
|
---|
186 | IMachine m = vbox.getMachines().get(0);
|
---|
187 | long logNo = 0;
|
---|
188 | long off = 0;
|
---|
189 | long size = 16 * 1024;
|
---|
190 | while (true)
|
---|
191 | {
|
---|
192 | byte[] buf = m.readLog(logNo, off, size);
|
---|
193 | if (buf.length == 0)
|
---|
194 | break;
|
---|
195 | System.out.print(new String(buf));
|
---|
196 | off += buf.length;
|
---|
197 | }
|
---|
198 | // process system event queue
|
---|
199 | mgr.waitForEvents(0);
|
---|
200 | }
|
---|
201 |
|
---|
202 | static void printErrorInfo(VBoxException e)
|
---|
203 | {
|
---|
204 | System.out.println("VBox error: " + e.getMessage());
|
---|
205 | System.out.println("Error cause message: " + e.getCause());
|
---|
206 | System.out.println("Overall result code: " + Integer.toHexString(e.getResultCode()));
|
---|
207 | int i = 1;
|
---|
208 | for (IVirtualBoxErrorInfo ei = e.getVirtualBoxErrorInfo(); ei != null; ei = ei.getNext(), i++)
|
---|
209 | {
|
---|
210 | System.out.println("Detail information #" + i);
|
---|
211 | System.out.println("Error mesage: " + ei.getText());
|
---|
212 | System.out.println("Result code: " + Integer.toHexString(ei.getResultCode()));
|
---|
213 | // optional, usually provides little additional information:
|
---|
214 | System.out.println("Component: " + ei.getComponent());
|
---|
215 | System.out.println("Interface ID: " + ei.getInterfaceID());
|
---|
216 | }
|
---|
217 | }
|
---|
218 |
|
---|
219 |
|
---|
220 | public static void main(String[] args)
|
---|
221 | {
|
---|
222 | VirtualBoxManager mgr = VirtualBoxManager.createInstance(null);
|
---|
223 |
|
---|
224 | boolean ws = false;
|
---|
225 | String url = null;
|
---|
226 | String user = null;
|
---|
227 | String passwd = null;
|
---|
228 |
|
---|
229 | for (int i = 0; i < args.length; i++)
|
---|
230 | {
|
---|
231 | if (args[i].equals("-w"))
|
---|
232 | ws = true;
|
---|
233 | else if (args[i].equals("-url"))
|
---|
234 | url = args[++i];
|
---|
235 | else if (args[i].equals("-user"))
|
---|
236 | user = args[++i];
|
---|
237 | else if (args[i].equals("-passwd"))
|
---|
238 | passwd = args[++i];
|
---|
239 | }
|
---|
240 |
|
---|
241 | if (ws)
|
---|
242 | {
|
---|
243 | try {
|
---|
244 | mgr.connect(url, user, passwd);
|
---|
245 | } catch (VBoxException e) {
|
---|
246 | e.printStackTrace();
|
---|
247 | System.out.println("Cannot connect, start webserver first!");
|
---|
248 | }
|
---|
249 | }
|
---|
250 |
|
---|
251 | try
|
---|
252 | {
|
---|
253 | IVirtualBox vbox = mgr.getVBox();
|
---|
254 | if (vbox != null)
|
---|
255 | {
|
---|
256 | System.out.println("VirtualBox version: " + vbox.getVersion() + "\n");
|
---|
257 | testEnumeration(mgr, vbox);
|
---|
258 | testReadLog(mgr, vbox);
|
---|
259 | testStart(mgr, vbox);
|
---|
260 | testEvents(mgr, vbox.getEventSource());
|
---|
261 |
|
---|
262 | System.out.println("done, press Enter...");
|
---|
263 | int ch = System.in.read();
|
---|
264 | }
|
---|
265 | }
|
---|
266 | catch (VBoxException e)
|
---|
267 | {
|
---|
268 | printErrorInfo(e);
|
---|
269 | System.out.println("Java stack trace:");
|
---|
270 | e.printStackTrace();
|
---|
271 | }
|
---|
272 | catch (RuntimeException e)
|
---|
273 | {
|
---|
274 | System.out.println("Runtime error: " + e.getMessage());
|
---|
275 | e.printStackTrace();
|
---|
276 | }
|
---|
277 | catch (java.io.IOException e)
|
---|
278 | {
|
---|
279 | e.printStackTrace();
|
---|
280 | }
|
---|
281 |
|
---|
282 | // process system event queue
|
---|
283 | mgr.waitForEvents(0);
|
---|
284 | if (ws)
|
---|
285 | {
|
---|
286 | try {
|
---|
287 | mgr.disconnect();
|
---|
288 | } catch (VBoxException e) {
|
---|
289 | e.printStackTrace();
|
---|
290 | }
|
---|
291 | }
|
---|
292 |
|
---|
293 | mgr.cleanup();
|
---|
294 |
|
---|
295 | }
|
---|
296 |
|
---|
297 | }
|
---|