1 | /* $Id:$ */
|
---|
2 | /*
|
---|
3 | * Copyright (C) 2010 Oracle Corporation
|
---|
4 | *
|
---|
5 | * This file is part of VirtualBox Open Source Edition (OSE), as
|
---|
6 | * available from http://www.virtualbox.org. This file is free software;
|
---|
7 | * you can redistribute it and/or modify it under the terms of the GNU
|
---|
8 | * General Public License (GPL) as published by the Free Software
|
---|
9 | * Foundation, in version 2 as it comes in the "COPYING" file of the
|
---|
10 | * VirtualBox OSE distribution. VirtualBox OSE is distributed in the
|
---|
11 | * hope that it will be useful, but WITHOUT ANY WARRANTY of any kind.
|
---|
12 | */
|
---|
13 | import org.virtualbox_3_3.*;
|
---|
14 | import java.util.List;
|
---|
15 | import java.util.Arrays;
|
---|
16 | import java.math.BigInteger;
|
---|
17 |
|
---|
18 | class VBoxCallbacks extends VBoxObjectBase implements IVirtualBoxCallback
|
---|
19 | {
|
---|
20 | public void onGuestPropertyChange(String machineId, String name, String value, String flags)
|
---|
21 | {
|
---|
22 | System.out.println("onGuestPropertyChange -- VM: " + machineId + ", " + name + "->" + value);
|
---|
23 | }
|
---|
24 | public void onSnapshotChange(String machineId, String snapshotId)
|
---|
25 | {
|
---|
26 | System.out.println("onSnapshotChange -- VM: " + machineId + ", snap: " + snapshotId);
|
---|
27 |
|
---|
28 | }
|
---|
29 | public void onSnapshotDeleted(String machineId, String snapshotId)
|
---|
30 | {
|
---|
31 | System.out.println("onSnapshotDeleted -- VM: " + machineId + ", snap: " + snapshotId);
|
---|
32 | }
|
---|
33 | public void onSnapshotTaken(String machineId, String snapshotId)
|
---|
34 | {
|
---|
35 | System.out.println("onSnapshotTaken -- VM: " + machineId + ", snap: " + snapshotId);
|
---|
36 | }
|
---|
37 | public void onSessionStateChange(String machineId, SessionState state)
|
---|
38 | {
|
---|
39 | System.out.println("onSessionStateChange -- VM: " + machineId + ", state: " + state);
|
---|
40 | }
|
---|
41 | public void onMachineRegistered(String machineId, Boolean registered)
|
---|
42 | {
|
---|
43 | System.out.println("onMachineRegistered -- VM: " + machineId + ", registered: " + registered);
|
---|
44 | }
|
---|
45 | public void onMediumRegistered(String mediumId, DeviceType mediumType, Boolean registered)
|
---|
46 | {
|
---|
47 | System.out.println("onMediumRegistered -- ID: " + mediumId + ", type=" + mediumType + ", registered: " + registered);
|
---|
48 | }
|
---|
49 | public void onExtraDataChange(String machineId, String key, String value)
|
---|
50 | {
|
---|
51 | System.out.println("onExtraDataChange -- VM: " + machineId + ": " + key+"->"+value);
|
---|
52 | }
|
---|
53 | public Boolean onExtraDataCanChange(String machineId, String key, String value, Holder<String> error)
|
---|
54 | {
|
---|
55 | return true;
|
---|
56 | }
|
---|
57 | public void onMachineDataChange(String machineId)
|
---|
58 | {
|
---|
59 | System.out.println("onMachineDataChange -- VM: " + machineId);
|
---|
60 | }
|
---|
61 | public void onMachineStateChange(String machineId, MachineState state)
|
---|
62 | {
|
---|
63 | System.out.println("onMachineStateChange -- VM: " + machineId + ", state: " + state);
|
---|
64 | }
|
---|
65 | }
|
---|
66 |
|
---|
67 | class ConsoleCallbacks extends VBoxObjectBase implements IConsoleCallback
|
---|
68 | {
|
---|
69 | String mach;
|
---|
70 | ConsoleCallbacks(String mach)
|
---|
71 | {
|
---|
72 | this.mach = mach;
|
---|
73 | }
|
---|
74 | public void onMousePointerShapeChange(Boolean visible, Boolean alpha, Long xHot, Long yHot, Long width, Long height, List<Short> shape)
|
---|
75 | {
|
---|
76 | System.out.println("onMousePointerShapeChange -- VM: " + mach);
|
---|
77 | }
|
---|
78 | public void onMouseCapabilityChange(Boolean supportsAbsolute, Boolean supportsRelative, Boolean needsHostCursor)
|
---|
79 | {
|
---|
80 | System.out.println("onMouseCapabilityChange -- VM: " + mach+" abs="+supportsAbsolute+ " rel="+supportsRelative+" need host="+needsHostCursor);
|
---|
81 | }
|
---|
82 | public void onKeyboardLedsChange(Boolean numLock, Boolean capsLock, Boolean scrollLock)
|
---|
83 | {
|
---|
84 | System.out.println("onKeyboardLedsChange -- VM: " + mach);
|
---|
85 | }
|
---|
86 | public void onStateChange(org.virtualbox_3_3.MachineState state)
|
---|
87 | {
|
---|
88 | System.out.println("onStateChange -- VM: " + mach);
|
---|
89 | }
|
---|
90 | public void onAdditionsStateChange()
|
---|
91 | {
|
---|
92 | System.out.println("onAdditionsStateChange -- VM: " + mach);
|
---|
93 | }
|
---|
94 | public void onNetworkAdapterChange(org.virtualbox_3_3.INetworkAdapter networkAdapter)
|
---|
95 | {
|
---|
96 | System.out.println("onNetworkAdapterChange -- VM: " + mach);
|
---|
97 | }
|
---|
98 | public void onSerialPortChange(org.virtualbox_3_3.ISerialPort serialPort)
|
---|
99 | {
|
---|
100 | System.out.println("onSerialPortChange -- VM: " + mach);
|
---|
101 | }
|
---|
102 | public void onParallelPortChange(org.virtualbox_3_3.IParallelPort parallelPort)
|
---|
103 | {
|
---|
104 | System.out.println("onParallelPortChange -- VM: " + mach);
|
---|
105 | }
|
---|
106 | public void onStorageControllerChange()
|
---|
107 | {
|
---|
108 | System.out.println("onStorageControllerChange -- VM: " + mach);
|
---|
109 | }
|
---|
110 | public void onMediumChange(org.virtualbox_3_3.IMediumAttachment mediumAttachment)
|
---|
111 | {
|
---|
112 | System.out.println("onMediumChange -- VM: " + mach);
|
---|
113 | }
|
---|
114 | public void onCPUChange(Long cpu, Boolean add)
|
---|
115 | {
|
---|
116 | System.out.println("onCPUChange -- VM: " + mach);
|
---|
117 | }
|
---|
118 | public void onVRDPServerChange()
|
---|
119 | {
|
---|
120 | System.out.println("onVRDPServerChange -- VM: " + mach);
|
---|
121 | }
|
---|
122 | public void onRemoteDisplayInfoChange()
|
---|
123 | {
|
---|
124 | System.out.println("onRemoteDisplayInfoChange -- VM: " + mach);
|
---|
125 | }
|
---|
126 | public void onUSBControllerChange()
|
---|
127 | {
|
---|
128 | System.out.println("onUSBControllerChange -- VM: " + mach);
|
---|
129 | }
|
---|
130 | public void onUSBDeviceStateChange(org.virtualbox_3_3.IUSBDevice device, Boolean attached, org.virtualbox_3_3.IVirtualBoxErrorInfo error)
|
---|
131 | {
|
---|
132 | System.out.println("onUSBDeviceStateChange -- VM: " + mach);
|
---|
133 | }
|
---|
134 | public void onSharedFolderChange(org.virtualbox_3_3.Scope scope)
|
---|
135 | {
|
---|
136 | System.out.println("onSharedFolderChange -- VM: " + mach);
|
---|
137 | }
|
---|
138 |
|
---|
139 | public void onRuntimeError(Boolean fatal, String id, String message)
|
---|
140 | {
|
---|
141 | System.out.println("onRuntimeError -- VM: " + mach);
|
---|
142 | }
|
---|
143 |
|
---|
144 | public Boolean onCanShowWindow()
|
---|
145 | {
|
---|
146 | System.out.println("onCanShowWindow -- VM: " + mach);
|
---|
147 | return true;
|
---|
148 | }
|
---|
149 |
|
---|
150 | public BigInteger onShowWindow()
|
---|
151 | {
|
---|
152 | System.out.println("onShowWindow -- VM: " + mach);
|
---|
153 | return BigInteger.ZERO;
|
---|
154 | }
|
---|
155 | }
|
---|
156 |
|
---|
157 | public class TestVBox
|
---|
158 | {
|
---|
159 | static void testCallbacks(VirtualBoxManager mgr, IVirtualBox vbox)
|
---|
160 | {
|
---|
161 |
|
---|
162 | IVirtualBoxCallback cbs = new VBoxCallbacks();
|
---|
163 | mgr.registerGlobalCallback(vbox, cbs);
|
---|
164 |
|
---|
165 | IMachine mach = vbox.getMachines().get(0);
|
---|
166 | IConsoleCallback mcbs = new ConsoleCallbacks(mach.getName());
|
---|
167 |
|
---|
168 | ISession session = null;
|
---|
169 | try {
|
---|
170 | session = mgr.openMachineSession(mach);
|
---|
171 | mgr.registerMachineCallback(session, mcbs);
|
---|
172 |
|
---|
173 | for (int i=0; i<100; i++)
|
---|
174 | {
|
---|
175 | mgr.waitForEvents(500);
|
---|
176 | }
|
---|
177 |
|
---|
178 | System.out.println("done waiting");
|
---|
179 |
|
---|
180 | mgr.unregisterMachineCallback(session, mcbs);
|
---|
181 | } catch (Exception e) {
|
---|
182 | e.printStackTrace();
|
---|
183 | } finally {
|
---|
184 | mgr.closeMachineSession(session);
|
---|
185 | }
|
---|
186 | mgr.unregisterGlobalCallback(vbox, cbs);
|
---|
187 | }
|
---|
188 |
|
---|
189 |
|
---|
190 | static void processEvent(IEvent ev)
|
---|
191 | {
|
---|
192 | System.out.println("got event: " + ev);
|
---|
193 |
|
---|
194 | VBoxEventType type = ev.getType();
|
---|
195 | System.out.println("type = "+type);
|
---|
196 |
|
---|
197 | switch (type)
|
---|
198 | {
|
---|
199 | case OnMachineStateChange:
|
---|
200 | IMachineStateChangeEvent mcse = IMachineStateChangeEvent.queryInterface(ev);
|
---|
201 | if (mcse == null)
|
---|
202 | System.out.println("Cannot query an interface");
|
---|
203 | else
|
---|
204 | System.out.println("mid="+mcse.getMachineId());
|
---|
205 | break;
|
---|
206 | }
|
---|
207 | }
|
---|
208 |
|
---|
209 | static class EventHandler
|
---|
210 | {
|
---|
211 | EventHandler() {}
|
---|
212 | public void handleEvent(IEvent ev)
|
---|
213 | {
|
---|
214 | try {
|
---|
215 | processEvent(ev);
|
---|
216 | } catch (Throwable t) {
|
---|
217 | t.printStackTrace();
|
---|
218 | }
|
---|
219 | }
|
---|
220 | }
|
---|
221 |
|
---|
222 | static void testEvents(VirtualBoxManager mgr, IEventSource es, boolean active)
|
---|
223 | {
|
---|
224 | // active mode for Java doesn't fully work yet, and using passive
|
---|
225 | // is more portable (the only mode for MSCOM and WS) and thus generally
|
---|
226 | // recommended
|
---|
227 | IEventListener listener = active ? mgr.createListener(new EventHandler()) : es.createListener();
|
---|
228 |
|
---|
229 | es.registerListener(listener, Arrays.asList(VBoxEventType.Any), false);
|
---|
230 |
|
---|
231 | try {
|
---|
232 | for (int i=0; i<100; i++)
|
---|
233 | {
|
---|
234 | System.out.print(".");
|
---|
235 | if (active)
|
---|
236 | {
|
---|
237 | mgr.waitForEvents(500);
|
---|
238 | }
|
---|
239 | else
|
---|
240 | {
|
---|
241 | IEvent ev = es.getEvent(listener, 1000);
|
---|
242 | if (ev != null)
|
---|
243 | {
|
---|
244 | processEvent(ev);
|
---|
245 | es.eventProcessed(listener, ev);
|
---|
246 | }
|
---|
247 | }
|
---|
248 | }
|
---|
249 | } catch (Exception e) {
|
---|
250 | e.printStackTrace();
|
---|
251 | }
|
---|
252 |
|
---|
253 | es.unregisterListener(listener);
|
---|
254 | }
|
---|
255 |
|
---|
256 | static void testEnumeration(VirtualBoxManager mgr, IVirtualBox vbox)
|
---|
257 | {
|
---|
258 | List<IMachine> machs = vbox.getMachines();
|
---|
259 | for (IMachine m : machs)
|
---|
260 | {
|
---|
261 | System.out.println("VM name: " + m.getName());// + ", RAM size: " + m.getMemorySize() + "MB");
|
---|
262 | System.out.println(" HWVirt: " + m.getHWVirtExProperty(HWVirtExPropertyType.Enabled)
|
---|
263 | + ", Nested Paging: " + m.getHWVirtExProperty(HWVirtExPropertyType.NestedPaging)
|
---|
264 | + ", PAE: " + m.getCPUProperty(CPUPropertyType.PAE) );
|
---|
265 | }
|
---|
266 | }
|
---|
267 |
|
---|
268 | static void testStart(VirtualBoxManager mgr, IVirtualBox vbox)
|
---|
269 | {
|
---|
270 | String m = vbox.getMachines().get(0).getName();
|
---|
271 | System.out.println("\nAttempting to start VM '" + m + "'");
|
---|
272 | mgr.startVm(m, null, 7000);
|
---|
273 | }
|
---|
274 |
|
---|
275 | public static void main(String[] args)
|
---|
276 | {
|
---|
277 | VirtualBoxManager mgr = VirtualBoxManager.getInstance(null);
|
---|
278 |
|
---|
279 | System.out.println("\n--> initialized\n");
|
---|
280 |
|
---|
281 | try
|
---|
282 | {
|
---|
283 | IVirtualBox vbox = mgr.getVBox();
|
---|
284 | System.out.println("VirtualBox version: " + vbox.getVersion() + "\n");
|
---|
285 | testEnumeration(mgr, vbox);
|
---|
286 | testStart(mgr, vbox);
|
---|
287 | //testCallbacks(mgr, vbox);
|
---|
288 | testEvents(mgr, vbox.getEventSource(), false);
|
---|
289 |
|
---|
290 | System.out.println("done, press Enter...");
|
---|
291 | int ch = System.in.read();
|
---|
292 | }
|
---|
293 | catch (Throwable e)
|
---|
294 | {
|
---|
295 | e.printStackTrace();
|
---|
296 | }
|
---|
297 |
|
---|
298 | mgr.cleanup();
|
---|
299 |
|
---|
300 | System.out.println("\n--< done\n");
|
---|
301 | }
|
---|
302 |
|
---|
303 | }
|
---|