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_2.*;
|
---|
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 | }
|
---|
25 | public void onSnapshotDeleted(String machineId, String snapshotId)
|
---|
26 | {
|
---|
27 | }
|
---|
28 | public void onSnapshotTaken(String machineId, String snapshotId) {}
|
---|
29 | public void onSessionStateChange(String machineId, SessionState state)
|
---|
30 | {
|
---|
31 | System.out.println("onSessionStateChange -- VM: " + machineId + ", state: " + state);
|
---|
32 | }
|
---|
33 | public void onMachineRegistered(String machineId, Boolean registered) {}
|
---|
34 | public void onMediumRegistered(String mediumId, DeviceType mediumType, Boolean registered) {}
|
---|
35 | public void onExtraDataChange(String machineId, String key, String value)
|
---|
36 | {
|
---|
37 | System.out.println("onExtraDataChange -- VM: " + machineId + ": " + key+"->"+value);
|
---|
38 | }
|
---|
39 | public Boolean onExtraDataCanChange(String machineId, String key, String value, Holder<String> error) { return true; }
|
---|
40 | public void onMachineDataChange(String machineId)
|
---|
41 | {}
|
---|
42 | public void onMachineStateChange(String machineId, MachineState state)
|
---|
43 | {
|
---|
44 | System.out.println("onMachineStateChange -- VM: " + machineId + ", state: " + state);
|
---|
45 | }
|
---|
46 | }
|
---|
47 |
|
---|
48 | public class TestVBox
|
---|
49 | {
|
---|
50 | static void testCallbacks(VirtualBoxManager mgr, IVirtualBox vbox)
|
---|
51 | {
|
---|
52 | IVirtualBoxCallback cbs = mgr.createIVirtualBoxCallback(new VBoxCallbacks());
|
---|
53 | vbox.registerCallback(cbs);
|
---|
54 | for (int i=0; i<100; i++)
|
---|
55 | {
|
---|
56 | mgr.waitForEvents(500);
|
---|
57 | }
|
---|
58 | vbox.unregisterCallback(cbs);
|
---|
59 | }
|
---|
60 |
|
---|
61 | static void testEnumeration(VirtualBoxManager mgr, IVirtualBox vbox)
|
---|
62 | {
|
---|
63 | List<IMachine> machs = vbox.getMachines();
|
---|
64 | for (IMachine m : machs)
|
---|
65 | {
|
---|
66 | System.out.println("VM name: " + m.getName());// + ", RAM size: " + m.getMemorySize() + "MB");
|
---|
67 | System.out.println(" HWVirt: " + m.getHWVirtExProperty(HWVirtExPropertyType.Enabled)
|
---|
68 | + ", Nested Paging: " + m.getHWVirtExProperty(HWVirtExPropertyType.NestedPaging)
|
---|
69 | + ", PAE: " + m.getCPUProperty(CPUPropertyType.PAE) );
|
---|
70 | }
|
---|
71 | }
|
---|
72 |
|
---|
73 | static void testStart(VirtualBoxManager mgr, IVirtualBox vbox)
|
---|
74 | {
|
---|
75 | String m = vbox.getMachines().get(0).getName();
|
---|
76 | System.out.println("\nAttempting to start VM '" + m + "'");
|
---|
77 | mgr.startVm(m, null, 7000);
|
---|
78 | }
|
---|
79 |
|
---|
80 | public static void main(String[] args)
|
---|
81 | {
|
---|
82 | VirtualBoxManager mgr = VirtualBoxManager.getInstance(null);
|
---|
83 |
|
---|
84 | System.out.println("\n--> initialized\n");
|
---|
85 |
|
---|
86 | try
|
---|
87 | {
|
---|
88 | IVirtualBox vbox = mgr.getVBox();
|
---|
89 | System.out.println("VirtualBox version: " + vbox.getVersion() + "\n");
|
---|
90 | testEnumeration(mgr, vbox);
|
---|
91 | testStart(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 | }
|
---|