VirtualBox

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

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

doc/SDKRef: Document how to run the system event queue when using the Java binding, and make more use of it in the sample code (which already used it, and the method was already there since day 1), plus a fix in the webservice case.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 9.1 KB
Line 
1/* $Id: TestVBox.java 56625 2015-06-24 17:18:45Z 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 mgr.waitForEvents(0);
76 }
77 } catch (Exception e) {
78 e.printStackTrace();
79 }
80
81 es.unregisterListener(listener);
82 }
83
84 static void testEnumeration(VirtualBoxManager mgr, IVirtualBox vbox)
85 {
86 List<IMachine> machs = vbox.getMachines();
87 for (IMachine m : machs)
88 {
89 String name;
90 Long ram = 0L;
91 boolean hwvirtEnabled = false, hwvirtNestedPaging = false;
92 boolean paeEnabled = false;
93 boolean inaccessible = false;
94 try
95 {
96 name = m.getName();
97 ram = m.getMemorySize();
98 hwvirtEnabled = m.getHWVirtExProperty(HWVirtExPropertyType.Enabled);
99 hwvirtNestedPaging = m.getHWVirtExProperty(HWVirtExPropertyType.NestedPaging);
100 paeEnabled = m.getCPUProperty(CPUPropertyType.PAE);
101 String osType = m.getOSTypeId();
102 IGuestOSType foo = vbox.getGuestOSType(osType);
103 }
104 catch (VBoxException e)
105 {
106 name = "<inaccessible>";
107 inaccessible = true;
108 }
109 System.out.println("VM name: " + name);
110 if (!inaccessible)
111 {
112 System.out.println(" RAM size: " + ram + "MB"
113 + ", HWVirt: " + hwvirtEnabled
114 + ", Nested Paging: " + hwvirtNestedPaging
115 + ", PAE: " + paeEnabled);
116 }
117 }
118 mgr.waitForEvents(0);
119 }
120
121 static boolean progressBar(VirtualBoxManager mgr, IProgress p, long waitMillis)
122 {
123 long end = System.currentTimeMillis() + waitMillis;
124 while (!p.getCompleted())
125 {
126 mgr.waitForEvents(0);
127 p.waitForCompletion(200);
128 if (System.currentTimeMillis() >= end)
129 return false;
130 }
131 return true;
132 }
133
134 static void testStart(VirtualBoxManager mgr, IVirtualBox vbox)
135 {
136 IMachine m = vbox.getMachines().get(0);
137 String name = m.getName();
138 System.out.println("\nAttempting to start VM '" + name + "'");
139
140 ISession session = mgr.getSessionObject();
141 IProgress p = m.launchVMProcess(session, "gui", "");
142 progressBar(mgr, p, 10000);
143 session.unlockMachine();
144 mgr.waitForEvents(0);
145 }
146
147 static void testMultiServer()
148 {
149 VirtualBoxManager mgr1 = VirtualBoxManager.createInstance(null);
150 VirtualBoxManager mgr2 = VirtualBoxManager.createInstance(null);
151
152 try {
153 mgr1.connect("http://i7:18083", "", "");
154 mgr2.connect("http://main:18083", "", "");
155
156 IMachine m1 = mgr1.getVBox().getMachines().get(0);
157 IMachine m2 = mgr2.getVBox().getMachines().get(0);
158 String name1 = m1.getName();
159 String name2 = m2.getName();
160 ISession session1 = mgr1.getSessionObject();
161 ISession session2 = mgr2.getSessionObject();
162 IProgress p1 = m1.launchVMProcess(session1, "gui", "");
163 IProgress p2 = m2.launchVMProcess(session2, "gui", "");
164 progressBar(mgr1, p1, 10000);
165 progressBar(mgr2, p2, 10000);
166 session1.unlockMachine();
167 session2.unlockMachine();
168 mgr1.waitForEvents(0);
169 mgr2.waitForEvents(0);
170 } finally {
171 mgr1.cleanup();
172 mgr2.cleanup();
173 }
174 }
175
176 static void testReadLog(VirtualBoxManager mgr, IVirtualBox vbox)
177 {
178 IMachine m = vbox.getMachines().get(0);
179 long logNo = 0;
180 long off = 0;
181 long size = 16 * 1024;
182 while (true)
183 {
184 byte[] buf = m.readLog(logNo, off, size);
185 if (buf.length == 0)
186 break;
187 System.out.print(new String(buf));
188 off += buf.length;
189 }
190 mgr.waitForEvents(0);
191 }
192
193 static void printErrorInfo(VBoxException e)
194 {
195 System.out.println("VBox error: " + e.getMessage());
196 System.out.println("Error cause message: " + e.getCause());
197 System.out.println("Overall result code: " + Integer.toHexString(e.getResultCode()));
198 int i = 1;
199 for (IVirtualBoxErrorInfo ei = e.getVirtualBoxErrorInfo(); ei != null; ei = ei.getNext(), i++)
200 {
201 System.out.println("Detail information #" + i);
202 System.out.println("Error mesage: " + ei.getText());
203 System.out.println("Result code: " + Integer.toHexString(ei.getResultCode()));
204 // optional, usually provides little additional information:
205 System.out.println("Component: " + ei.getComponent());
206 System.out.println("Interface ID: " + ei.getInterfaceID());
207 }
208 }
209
210
211 public static void main(String[] args)
212 {
213 VirtualBoxManager mgr = VirtualBoxManager.createInstance(null);
214
215 boolean ws = false;
216 String url = null;
217 String user = null;
218 String passwd = null;
219
220 for (int i = 0; i < args.length; i++)
221 {
222 if (args[i].equals("-w"))
223 ws = true;
224 else if (args[i].equals("-url"))
225 url = args[++i];
226 else if (args[i].equals("-user"))
227 user = args[++i];
228 else if (args[i].equals("-passwd"))
229 passwd = args[++i];
230 }
231
232 if (ws)
233 {
234 try {
235 mgr.connect(url, user, passwd);
236 } catch (VBoxException e) {
237 e.printStackTrace();
238 System.out.println("Cannot connect, start webserver first!");
239 }
240 }
241
242 try
243 {
244 IVirtualBox vbox = mgr.getVBox();
245 if (vbox != null)
246 {
247 System.out.println("VirtualBox version: " + vbox.getVersion() + "\n");
248 testEnumeration(mgr, vbox);
249 testReadLog(mgr, vbox);
250 testStart(mgr, vbox);
251 testEvents(mgr, vbox.getEventSource());
252
253 System.out.println("done, press Enter...");
254 int ch = System.in.read();
255 }
256 }
257 catch (VBoxException e)
258 {
259 printErrorInfo(e);
260 System.out.println("Java stack trace:");
261 e.printStackTrace();
262 }
263 catch (RuntimeException e)
264 {
265 System.out.println("Runtime error: " + e.getMessage());
266 e.printStackTrace();
267 }
268 catch (java.io.IOException e)
269 {
270 e.printStackTrace();
271 }
272
273 mgr.waitForEvents(0);
274 if (ws)
275 {
276 try {
277 mgr.disconnect();
278 } catch (VBoxException e) {
279 e.printStackTrace();
280 }
281 }
282
283 mgr.cleanup();
284
285 }
286
287}
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