VirtualBox

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

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