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