1 | /* $Id: TestVBox.java 32404 2010-09-10 13:17:42Z vboxsync $ */
|
---|
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 | VBoxEventType type = ev.getType();
|
---|
24 | System.out.println("type = "+type);
|
---|
25 | switch (type)
|
---|
26 | {
|
---|
27 | case OnMachineStateChanged:
|
---|
28 | {
|
---|
29 | IMachineStateChangedEvent mcse = IMachineStateChangedEvent.queryInterface(ev);
|
---|
30 | if (mcse == null)
|
---|
31 | System.out.println("Cannot query an interface");
|
---|
32 | else
|
---|
33 | System.out.println("mid="+mcse.getMachineId());
|
---|
34 | break;
|
---|
35 | }
|
---|
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)
|
---|
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 = 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 | IEvent ev = es.getEvent(listener, 1000);
|
---|
66 | if (ev != null)
|
---|
67 | {
|
---|
68 | processEvent(ev);
|
---|
69 | es.eventProcessed(listener, ev);
|
---|
70 | }
|
---|
71 | }
|
---|
72 | } catch (Exception e) {
|
---|
73 | e.printStackTrace();
|
---|
74 | }
|
---|
75 |
|
---|
76 | es.unregisterListener(listener);
|
---|
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 | static void testMultiServer()
|
---|
99 | {
|
---|
100 | VirtualBoxManager mgr1 = VirtualBoxManager.createInstance(null);
|
---|
101 | VirtualBoxManager mgr2 = VirtualBoxManager.createInstance(null);
|
---|
102 |
|
---|
103 | try {
|
---|
104 | mgr1.connect("http://i7:18083", "", "");
|
---|
105 | mgr2.connect("http://main:18083", "", "");
|
---|
106 |
|
---|
107 | String mach1 = mgr1.getVBox().getMachines().get(0).getName();
|
---|
108 | String mach2 = mgr2.getVBox().getMachines().get(0).getName();
|
---|
109 |
|
---|
110 | mgr1.startVm(mach1, null, 7000);
|
---|
111 | mgr2.startVm(mach2, null, 7000);
|
---|
112 | } finally {
|
---|
113 | mgr1.cleanup();
|
---|
114 | mgr2.cleanup();
|
---|
115 | }
|
---|
116 | }
|
---|
117 |
|
---|
118 | static void testReadLog(VirtualBoxManager mgr, IVirtualBox vbox)
|
---|
119 | {
|
---|
120 | IMachine m = vbox.getMachines().get(0);
|
---|
121 | long logNo = 0;
|
---|
122 | long off = 0;
|
---|
123 | long size = 16 * 1024;
|
---|
124 | while (true)
|
---|
125 | {
|
---|
126 | byte[] buf = m.readLog(logNo, off, size);
|
---|
127 | if (buf.length == 0)
|
---|
128 | break;
|
---|
129 | System.out.print(new String(buf));
|
---|
130 | off += buf.length;
|
---|
131 | }
|
---|
132 | }
|
---|
133 |
|
---|
134 |
|
---|
135 | public static void main(String[] args)
|
---|
136 | {
|
---|
137 | VirtualBoxManager mgr = VirtualBoxManager.createInstance(null);
|
---|
138 |
|
---|
139 | boolean ws = false;
|
---|
140 | String url = null;
|
---|
141 | String user = null;
|
---|
142 | String passwd = null;
|
---|
143 |
|
---|
144 | for (int i = 0; i<args.length; i++)
|
---|
145 | {
|
---|
146 | if ("-w".equals(args[i]))
|
---|
147 | ws = true;
|
---|
148 | else if ("-url".equals(args[i]))
|
---|
149 | url = args[++i];
|
---|
150 | else if ("-user".equals(args[i]))
|
---|
151 | user = args[++i];
|
---|
152 | else if ("-passwd".equals(args[i]))
|
---|
153 | passwd = args[++i];
|
---|
154 | }
|
---|
155 |
|
---|
156 | if (ws)
|
---|
157 | {
|
---|
158 | try {
|
---|
159 | mgr.connect(url, user, passwd);
|
---|
160 | } catch (VBoxException e) {
|
---|
161 | e.printStackTrace();
|
---|
162 | System.out.println("Cannot connect, start webserver first!");
|
---|
163 | }
|
---|
164 | }
|
---|
165 |
|
---|
166 | try
|
---|
167 | {
|
---|
168 | IVirtualBox vbox = mgr.getVBox();
|
---|
169 | if (vbox != null)
|
---|
170 | {
|
---|
171 | System.out.println("VirtualBox version: " + vbox.getVersion() + "\n");
|
---|
172 | testEnumeration(mgr, vbox);
|
---|
173 | //testReadLog(mgr, vbox);
|
---|
174 | testStart(mgr, vbox);
|
---|
175 | testEvents(mgr, vbox.getEventSource());
|
---|
176 |
|
---|
177 | System.out.println("done, press Enter...");
|
---|
178 | int ch = System.in.read();
|
---|
179 | }
|
---|
180 | }
|
---|
181 | catch (VBoxException e)
|
---|
182 | {
|
---|
183 | System.out.println("VBox error: "+e.getMessage()+" original="+e.getWrapped());
|
---|
184 | e.printStackTrace();
|
---|
185 | }
|
---|
186 | catch (java.io.IOException e)
|
---|
187 | {
|
---|
188 | e.printStackTrace();
|
---|
189 | }
|
---|
190 |
|
---|
191 | if (ws)
|
---|
192 | {
|
---|
193 | try {
|
---|
194 | mgr.disconnect();
|
---|
195 | } catch (VBoxException e) {
|
---|
196 | e.printStackTrace();
|
---|
197 | }
|
---|
198 | }
|
---|
199 |
|
---|
200 | mgr.cleanup();
|
---|
201 |
|
---|
202 | }
|
---|
203 |
|
---|
204 | }
|
---|