VirtualBox

source: vbox/trunk/src/VBox/Main/glue/tests/TestVBox.java@ 102157

Last change on this file since 102157 was 98103, checked in by vboxsync, 22 months ago

Copyright year updates by scm.

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

© 2024 Oracle Support Privacy / Do Not Sell My Info Terms of Use Trademark Policy Automated Access Etiquette