VirtualBox

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

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

glue/tests/TestVBox.java: fix API version in source code

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