1 | /*
|
---|
2 | * Sample client for the VirtualBox webservice, written in Java.
|
---|
3 | *
|
---|
4 | * Don't forget to run VBOX webserver
|
---|
5 | * with 'vboxwebsrv -t 1000' command, to calm down watchdog thread.
|
---|
6 | *
|
---|
7 | * Copyright (C) 2008 Sun Microsystems, Inc.
|
---|
8 | *
|
---|
9 | * This file is part of VirtualBox Open Source Edition (OSE), as
|
---|
10 | * available from http://www.215389.xyz. This file is free software;
|
---|
11 | * you can redistribute it and/or modify it under the terms of the GNU
|
---|
12 | * General Public License (GPL) as published by the Free Software
|
---|
13 | * Foundation, in version 2 as it comes in the "COPYING" file of the
|
---|
14 | * VirtualBox OSE distribution. VirtualBox OSE is distributed in the
|
---|
15 | * hope that it will be useful, but WITHOUT ANY WARRANTY of any kind.
|
---|
16 | *
|
---|
17 | * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa
|
---|
18 | * Clara, CA 95054 USA or visit http://www.sun.com if you need
|
---|
19 | * additional information or have any questions.
|
---|
20 | */
|
---|
21 | import com.sun.xml.ws.commons.virtualbox_2_1.*;
|
---|
22 | import java.util.*;
|
---|
23 | import javax.xml.ws.Holder;
|
---|
24 | import org.virtualbox_2_1.*;
|
---|
25 |
|
---|
26 | public class clienttest
|
---|
27 | {
|
---|
28 | IWebsessionManager mgr;
|
---|
29 | IVirtualBox vbox;
|
---|
30 |
|
---|
31 | public clienttest()
|
---|
32 | {
|
---|
33 | mgr = new IWebsessionManager("http://localhost:18083/");
|
---|
34 | vbox = mgr.logon("test", "test");
|
---|
35 | System.out.println("Initialized connection to VirtualBox version " + vbox.getVersion());
|
---|
36 | }
|
---|
37 |
|
---|
38 | public void disconnect()
|
---|
39 | {
|
---|
40 | mgr.disconnect(vbox);
|
---|
41 | }
|
---|
42 |
|
---|
43 | class Desktop
|
---|
44 | {
|
---|
45 | String name;
|
---|
46 | UUID uuid;
|
---|
47 |
|
---|
48 | Desktop(int n)
|
---|
49 | {
|
---|
50 | name = "Mach"+n;
|
---|
51 | uuid = UUID.randomUUID();
|
---|
52 | }
|
---|
53 | String getName()
|
---|
54 | {
|
---|
55 | return name;
|
---|
56 | }
|
---|
57 | UUID getId()
|
---|
58 | {
|
---|
59 | return uuid;
|
---|
60 | }
|
---|
61 | }
|
---|
62 |
|
---|
63 | public void test()
|
---|
64 | {
|
---|
65 | for (int i=0; i<100; i++)
|
---|
66 | {
|
---|
67 | String baseFolder =
|
---|
68 | vbox.getSystemProperties().getDefaultMachineFolder();
|
---|
69 | Desktop desktop = new Desktop(i);
|
---|
70 | IMachine machine = vbox.createMachine(baseFolder,
|
---|
71 | "linux",
|
---|
72 | desktop.getName(),
|
---|
73 | desktop.getId());
|
---|
74 | machine.saveSettings();
|
---|
75 | mgr.cleanupUnused();
|
---|
76 | }
|
---|
77 | }
|
---|
78 |
|
---|
79 | public void test2()
|
---|
80 | {
|
---|
81 | ISession session = mgr.getSessionObject(vbox);
|
---|
82 | UUID id = UUID.fromString("bc8b6219-2775-42c4-f1b2-b48b3c177294");
|
---|
83 | vbox.openSession(session, id);
|
---|
84 | IMachine mach = session.getMachine();
|
---|
85 | IBIOSSettings bios = mach.getBIOSSettings();
|
---|
86 | bios.setIOAPICEnabled(true);
|
---|
87 | mach.saveSettings();
|
---|
88 | session.close();
|
---|
89 | }
|
---|
90 |
|
---|
91 |
|
---|
92 | public void test3()
|
---|
93 | {
|
---|
94 |
|
---|
95 | IWebsessionManager mgr1 = new IWebsessionManager("http://localhost:18082/");
|
---|
96 | IWebsessionManager mgr2 = new IWebsessionManager("http://localhost:18083/");
|
---|
97 | IVirtualBox vbox1 = mgr1.logon("test", "test");
|
---|
98 | IVirtualBox vbox2 = mgr2.logon("test", "test");
|
---|
99 |
|
---|
100 |
|
---|
101 | System.out.println("connection 1 to VirtualBox version " + vbox1.getVersion());
|
---|
102 | System.out.println("connection 2 to VirtualBox version " + vbox2.getVersion());
|
---|
103 | mgr1.disconnect(vbox1);
|
---|
104 | mgr2.disconnect(vbox2);
|
---|
105 |
|
---|
106 | mgr1 = new IWebsessionManager("http://localhost:18082/");
|
---|
107 | mgr2 = new IWebsessionManager("http://localhost:18083/");
|
---|
108 | vbox1 = mgr1.logon("test", "test");
|
---|
109 | vbox2 = mgr2.logon("test", "test");
|
---|
110 |
|
---|
111 | System.out.println("second connection 1 to VirtualBox version " + vbox1.getVersion());
|
---|
112 | System.out.println("second connection 2 to VirtualBox version " + vbox2.getVersion());
|
---|
113 |
|
---|
114 | mgr1.disconnect(vbox1);
|
---|
115 | mgr2.disconnect(vbox2);
|
---|
116 | }
|
---|
117 |
|
---|
118 | public void showVMs()
|
---|
119 | {
|
---|
120 | try
|
---|
121 | {
|
---|
122 | int i = 0;
|
---|
123 | for (IMachine m : vbox.getMachines())
|
---|
124 | {
|
---|
125 | System.out.println("Machine " + (i++) + ": " + " [" + m.getId() + "]" + " - " + m.getName());
|
---|
126 | }
|
---|
127 | }
|
---|
128 | catch (Exception e)
|
---|
129 | {
|
---|
130 | e.printStackTrace();
|
---|
131 | }
|
---|
132 | }
|
---|
133 |
|
---|
134 | public void listHostInfo()
|
---|
135 | {
|
---|
136 | try
|
---|
137 | {
|
---|
138 | IHost host = vbox.getHost();
|
---|
139 | long uProcCount = host.getProcessorCount();
|
---|
140 | System.out.println("Processor count: " + uProcCount);
|
---|
141 |
|
---|
142 | for (long i=0; i<uProcCount; i++)
|
---|
143 | {
|
---|
144 | System.out.println("Processor #" + i + " speed: " + host.getProcessorSpeed(i) + "MHz");
|
---|
145 | }
|
---|
146 |
|
---|
147 | IPerformanceCollector oCollector = vbox.getPerformanceCollector();
|
---|
148 |
|
---|
149 | List<IPerformanceMetric> aMetrics =
|
---|
150 | oCollector.getMetrics(Arrays.asList(new String[]{"*"}),
|
---|
151 | Arrays.asList(new IUnknown[]{host}));
|
---|
152 |
|
---|
153 | for (IPerformanceMetric m : aMetrics)
|
---|
154 | {
|
---|
155 | System.out.println("known metric = "+m.getMetricName());
|
---|
156 | }
|
---|
157 |
|
---|
158 | Holder<List<String>> names = new Holder<List<String>> ();
|
---|
159 | Holder<List<IUnknown>> objects = new Holder<List<IUnknown>>() ;
|
---|
160 | Holder<List<String>> units = new Holder<List<String>>();
|
---|
161 | Holder<List<Long>> scales = new Holder<List<Long>>();
|
---|
162 | Holder<List<Long>> sequenceNumbers = new Holder<List<Long>>();
|
---|
163 | Holder<List<Long>> indices = new Holder<List<Long>>();
|
---|
164 | Holder<List<Long>> lengths = new Holder<List<Long>>();
|
---|
165 |
|
---|
166 | List<Integer> vals =
|
---|
167 | oCollector.queryMetricsData(Arrays.asList(new String[]{"*"}),
|
---|
168 | Arrays.asList(new IUnknown[]{host}),
|
---|
169 | names, objects, units, scales,
|
---|
170 | sequenceNumbers, indices, lengths);
|
---|
171 |
|
---|
172 | for (int i=0; i < names.value.size(); i++)
|
---|
173 | System.out.println("name: "+names.value.get(i));
|
---|
174 | }
|
---|
175 | catch (Exception e)
|
---|
176 | {
|
---|
177 | e.printStackTrace();
|
---|
178 | }
|
---|
179 | }
|
---|
180 |
|
---|
181 | public void startVM(String strVM)
|
---|
182 | {
|
---|
183 | ISession oSession = null;
|
---|
184 | IMachine oMachine = null;
|
---|
185 |
|
---|
186 | try
|
---|
187 | {
|
---|
188 | oSession = mgr.getSessionObject(vbox);
|
---|
189 |
|
---|
190 | // first assume we were given a UUID
|
---|
191 | try
|
---|
192 | {
|
---|
193 | oMachine = vbox.getMachine(UUID.fromString(strVM));
|
---|
194 | }
|
---|
195 | catch (Exception e)
|
---|
196 | {
|
---|
197 | try
|
---|
198 | {
|
---|
199 | oMachine = vbox.findMachine(strVM);
|
---|
200 | }
|
---|
201 | catch (Exception e1)
|
---|
202 | {
|
---|
203 | }
|
---|
204 | }
|
---|
205 |
|
---|
206 | if (oMachine == null)
|
---|
207 | {
|
---|
208 | System.out.println("Error: can't find VM \"" + strVM + "\"");
|
---|
209 | }
|
---|
210 | else
|
---|
211 | {
|
---|
212 | UUID uuid = oMachine.getId();
|
---|
213 | String sessionType = "gui";
|
---|
214 | String env = "DISPLAY=:0.0";
|
---|
215 | IProgress oProgress =
|
---|
216 | vbox.openRemoteSession(oSession,
|
---|
217 | uuid,
|
---|
218 | sessionType,
|
---|
219 | env);
|
---|
220 | System.out.println("Session for VM " + uuid + " is opening...");
|
---|
221 | oProgress.waitForCompletion(10000);
|
---|
222 |
|
---|
223 | long rc = oProgress.getResultCode();
|
---|
224 | if (rc != 0)
|
---|
225 | System.out.println("Session failed!");
|
---|
226 | }
|
---|
227 | }
|
---|
228 | catch (Exception e)
|
---|
229 | {
|
---|
230 | e.printStackTrace();
|
---|
231 | }
|
---|
232 | finally
|
---|
233 | {
|
---|
234 | if (oSession != null)
|
---|
235 | {
|
---|
236 | oSession.close();
|
---|
237 | }
|
---|
238 | }
|
---|
239 | }
|
---|
240 |
|
---|
241 | public void cleanup()
|
---|
242 | {
|
---|
243 | try
|
---|
244 | {
|
---|
245 | if (vbox != null)
|
---|
246 | {
|
---|
247 | disconnect();
|
---|
248 | vbox = null;
|
---|
249 | System.out.println("Logged off.");
|
---|
250 | }
|
---|
251 | mgr.cleanupUnused();
|
---|
252 | mgr = null;
|
---|
253 | }
|
---|
254 | catch (Exception e)
|
---|
255 | {
|
---|
256 | e.printStackTrace();
|
---|
257 | }
|
---|
258 | }
|
---|
259 |
|
---|
260 | public static void printArgs()
|
---|
261 | {
|
---|
262 | System.out.println( "Usage: java clienttest <mode> ..." +
|
---|
263 | "\nwith <mode> being:" +
|
---|
264 | "\n show vms list installed virtual machines" +
|
---|
265 | "\n list hostinfo list host info" +
|
---|
266 | "\n startvm <vmname|uuid> start the given virtual machine");
|
---|
267 | }
|
---|
268 |
|
---|
269 | public static void main(String[] args)
|
---|
270 | {
|
---|
271 | if (args.length < 1)
|
---|
272 | {
|
---|
273 | System.out.println("Error: Must specify at least one argument.");
|
---|
274 | printArgs();
|
---|
275 | }
|
---|
276 | else
|
---|
277 | {
|
---|
278 | clienttest c = new clienttest();
|
---|
279 | if (args[0].equals("show"))
|
---|
280 | {
|
---|
281 | if (args.length == 2)
|
---|
282 | {
|
---|
283 | if (args[1].equals("vms"))
|
---|
284 | c.showVMs();
|
---|
285 | else
|
---|
286 | System.out.println("Error: Unknown argument to \"show\": \"" + args[1] + "\".");
|
---|
287 | }
|
---|
288 | else
|
---|
289 | System.out.println("Error: Missing argument to \"show\" command");
|
---|
290 | }
|
---|
291 | else if (args[0].equals("list"))
|
---|
292 | {
|
---|
293 | if (args.length == 2)
|
---|
294 | {
|
---|
295 | if (args[1].equals("hostinfo"))
|
---|
296 | c.listHostInfo();
|
---|
297 | else
|
---|
298 | System.out.println("Error: Unknown argument to \"show\": \"" + args[1] + "\".");
|
---|
299 | }
|
---|
300 | else
|
---|
301 | System.out.println("Error: Missing argument to \"list\" command");
|
---|
302 | }
|
---|
303 | else if (args[0].equals("startvm"))
|
---|
304 | {
|
---|
305 | if (args.length == 2)
|
---|
306 | {
|
---|
307 | c.startVM(args[1]);
|
---|
308 | }
|
---|
309 | else
|
---|
310 | System.out.println("Error: Missing argument to \"startvm\" command");
|
---|
311 | }
|
---|
312 | else if (args[0].equals("test"))
|
---|
313 | {
|
---|
314 | c.test3();
|
---|
315 | }
|
---|
316 | else
|
---|
317 | System.out.println("Error: Unknown command: \"" + args[0] + "\".");
|
---|
318 |
|
---|
319 | c.cleanup();
|
---|
320 | }
|
---|
321 | }
|
---|
322 | }
|
---|