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 |
|
---|
14 | import org.mozilla.interfaces.*;
|
---|
15 | import org.virtualbox.*;
|
---|
16 |
|
---|
17 | class VBoxCallbacks extends VBoxObjectBase implements IVirtualBoxCallback
|
---|
18 | {
|
---|
19 | public void onGuestPropertyChange(String machineId, String name, String value, String flags)
|
---|
20 | {
|
---|
21 | System.out.println("onGuestPropertyChange -- VM: " + machineId + ", " + name + "->" + value);
|
---|
22 | }
|
---|
23 | public void onSnapshotChange(String machineId, String snapshotId)
|
---|
24 | {
|
---|
25 | }
|
---|
26 | public void onSnapshotDeleted(String machineId, String snapshotId)
|
---|
27 | {
|
---|
28 | }
|
---|
29 | public void onSnapshotTaken(String machineId, String snapshotId) {}
|
---|
30 | public void onSessionStateChange(String machineId, long state)
|
---|
31 | {
|
---|
32 | System.out.println("onSessionStateChange -- VM: " + machineId + ", state: " + state);
|
---|
33 | }
|
---|
34 | public void onMachineRegistered(String machineId, boolean registered) {}
|
---|
35 | public void onMediumRegistered(String mediumId, long mediumType, boolean registered) {}
|
---|
36 | public void onExtraDataChange(String machineId, String key, String value)
|
---|
37 | {
|
---|
38 | System.out.println("onExtraDataChange -- VM: " + machineId + ": " + key+"->"+value);
|
---|
39 | }
|
---|
40 | public boolean onExtraDataCanChange(String machineId, String key, String value, String[] error) { return true; }
|
---|
41 | public void onMachineDataChange(String machineId)
|
---|
42 | {}
|
---|
43 | public void onMachineStateChange(String machineId, long state)
|
---|
44 | {
|
---|
45 | System.out.println("onMachineStateChange -- VM: " + machineId + ", state: " + state);
|
---|
46 | }
|
---|
47 | }
|
---|
48 |
|
---|
49 | public class TestVBox
|
---|
50 | {
|
---|
51 | static void testCallbacks(VirtualBoxManager mgr, IVirtualBox vbox)
|
---|
52 | {
|
---|
53 | IVirtualBoxCallback cbs = new VBoxCallbacks();
|
---|
54 | vbox.registerCallback(cbs);
|
---|
55 | for (int i=0; i<100; i++)
|
---|
56 | {
|
---|
57 | mgr.waitForEvents(500);
|
---|
58 | }
|
---|
59 | vbox.unregisterCallback(cbs);
|
---|
60 | }
|
---|
61 |
|
---|
62 | static void testEnumeration(VirtualBoxManager mgr, IVirtualBox vbox)
|
---|
63 | {
|
---|
64 | IMachine[] machs = vbox.getMachines(null);
|
---|
65 | for (IMachine m : machs)
|
---|
66 | {
|
---|
67 | System.out.println("VM name: " + m.getName() + ", RAM size: " + m.getMemorySize() + "MB");
|
---|
68 | System.out.println(" HWVirt: " + m.getHWVirtExProperty(HWVirtExPropertyType.Enabled)
|
---|
69 | + ", Nested Paging: " + m.getHWVirtExProperty(HWVirtExPropertyType.NestedPaging)
|
---|
70 | + ", PAE: " + m.getCPUProperty(CPUPropertyType.PAE) );
|
---|
71 | }
|
---|
72 | }
|
---|
73 |
|
---|
74 | static void testStart(VirtualBoxManager mgr, IVirtualBox vbox)
|
---|
75 | {
|
---|
76 | String m = vbox.getMachines(null)[0].getName();
|
---|
77 | System.out.println("\nAttempting to start VM '" + m + "'");
|
---|
78 | mgr.startVm(m, null, 7000);
|
---|
79 | }
|
---|
80 |
|
---|
81 | public static void main(String[] args)
|
---|
82 | {
|
---|
83 | VirtualBoxManager mgr = VirtualBoxManager.getInstance(null);
|
---|
84 |
|
---|
85 | System.out.println("\n--> initialized\n");
|
---|
86 |
|
---|
87 | try
|
---|
88 | {
|
---|
89 | IVirtualBox vbox = mgr.getVBox();
|
---|
90 | System.out.println("VirtualBox version: " + vbox.getVersion() + "\n");
|
---|
91 | testEnumeration(mgr, vbox);
|
---|
92 | testCallbacks(mgr, vbox);
|
---|
93 |
|
---|
94 | System.out.println("done, press Enter...");
|
---|
95 | int ch = System.in.read();
|
---|
96 | }
|
---|
97 | catch (Throwable e)
|
---|
98 | {
|
---|
99 | e.printStackTrace();
|
---|
100 | }
|
---|
101 |
|
---|
102 | mgr.cleanup();
|
---|
103 |
|
---|
104 | System.out.println("\n--< done\n");
|
---|
105 | }
|
---|
106 |
|
---|
107 | }
|
---|