VirtualBox

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

Last change on this file since 63470 was 56636, checked in by vboxsync, 9 years ago

Main/glue/tests: add some comments to the java sample code, and clean up the code using the NAT engine (not part of the SDK)

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