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 | public class TestVBox
|
---|
19 | {
|
---|
20 | static void processEvent(IEvent ev)
|
---|
21 | {
|
---|
22 | System.out.println("got event: " + ev);
|
---|
23 |
|
---|
24 | VBoxEventType type = ev.getType();
|
---|
25 | System.out.println("type = "+type);
|
---|
26 |
|
---|
27 | switch (type)
|
---|
28 | {
|
---|
29 | case OnMachineStateChange:
|
---|
30 | IMachineStateChangedEvent mcse = IMachineStateChangedEvent.queryInterface(ev);
|
---|
31 | if (mcse == null)
|
---|
32 | System.out.println("Cannot query an interface");
|
---|
33 | else
|
---|
34 | System.out.println("mid="+mcse.getMachineId());
|
---|
35 | break;
|
---|
36 | }
|
---|
37 | }
|
---|
38 |
|
---|
39 | static class EventHandler
|
---|
40 | {
|
---|
41 | EventHandler() {}
|
---|
42 | public void handleEvent(IEvent ev)
|
---|
43 | {
|
---|
44 | try {
|
---|
45 | processEvent(ev);
|
---|
46 | } catch (Throwable t) {
|
---|
47 | t.printStackTrace();
|
---|
48 | }
|
---|
49 | }
|
---|
50 | }
|
---|
51 |
|
---|
52 | static void testEvents(VirtualBoxManager mgr, IEventSource es, boolean active)
|
---|
53 | {
|
---|
54 | // active mode for Java doesn't fully work yet, and using passive
|
---|
55 | // is more portable (the only mode for MSCOM and WS) and thus generally
|
---|
56 | // recommended
|
---|
57 | IEventListener listener = active ? mgr.createListener(new EventHandler()) : es.createListener();
|
---|
58 |
|
---|
59 | es.registerListener(listener, Arrays.asList(VBoxEventType.Any), false);
|
---|
60 |
|
---|
61 | try {
|
---|
62 | for (int i=0; i<100; i++)
|
---|
63 | {
|
---|
64 | System.out.print(".");
|
---|
65 | if (active)
|
---|
66 | {
|
---|
67 | mgr.waitForEvents(500);
|
---|
68 | }
|
---|
69 | else
|
---|
70 | {
|
---|
71 | IEvent ev = es.getEvent(listener, 1000);
|
---|
72 | if (ev != null)
|
---|
73 | {
|
---|
74 | processEvent(ev);
|
---|
75 | es.eventProcessed(listener, ev);
|
---|
76 | }
|
---|
77 | }
|
---|
78 | }
|
---|
79 | } catch (Exception e) {
|
---|
80 | e.printStackTrace();
|
---|
81 | }
|
---|
82 |
|
---|
83 | es.unregisterListener(listener);
|
---|
84 | }
|
---|
85 |
|
---|
86 | static void testEnumeration(VirtualBoxManager mgr, IVirtualBox vbox)
|
---|
87 | {
|
---|
88 | List<IMachine> machs = vbox.getMachines();
|
---|
89 | for (IMachine m : machs)
|
---|
90 | {
|
---|
91 | System.out.println("VM name: " + m.getName());// + ", RAM size: " + m.getMemorySize() + "MB");
|
---|
92 | System.out.println(" HWVirt: " + m.getHWVirtExProperty(HWVirtExPropertyType.Enabled)
|
---|
93 | + ", Nested Paging: " + m.getHWVirtExProperty(HWVirtExPropertyType.NestedPaging)
|
---|
94 | + ", PAE: " + m.getCPUProperty(CPUPropertyType.PAE) );
|
---|
95 | }
|
---|
96 | }
|
---|
97 |
|
---|
98 | static void testStart(VirtualBoxManager mgr, IVirtualBox vbox)
|
---|
99 | {
|
---|
100 | String m = vbox.getMachines().get(0).getName();
|
---|
101 | System.out.println("\nAttempting to start VM '" + m + "'");
|
---|
102 | mgr.startVm(m, null, 7000);
|
---|
103 | }
|
---|
104 |
|
---|
105 | public static void main(String[] args)
|
---|
106 | {
|
---|
107 | VirtualBoxManager mgr = VirtualBoxManager.getInstance(null);
|
---|
108 |
|
---|
109 | System.out.println("\n--> initialized\n");
|
---|
110 |
|
---|
111 | try
|
---|
112 | {
|
---|
113 | IVirtualBox vbox = mgr.getVBox();
|
---|
114 | System.out.println("VirtualBox version: " + vbox.getVersion() + "\n");
|
---|
115 | testEnumeration(mgr, vbox);
|
---|
116 | testStart(mgr, vbox);
|
---|
117 | testEvents(mgr, vbox.getEventSource(), false);
|
---|
118 |
|
---|
119 | System.out.println("done, press Enter...");
|
---|
120 | int ch = System.in.read();
|
---|
121 | }
|
---|
122 | catch (Throwable e)
|
---|
123 | {
|
---|
124 | e.printStackTrace();
|
---|
125 | }
|
---|
126 |
|
---|
127 | mgr.cleanup();
|
---|
128 |
|
---|
129 | System.out.println("\n--< done\n");
|
---|
130 | }
|
---|
131 |
|
---|
132 | }
|
---|