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-2009 Sun Microsystems, Inc.
|
---|
8 | *
|
---|
9 | * The following license applies to this file only:
|
---|
10 | *
|
---|
11 | * Permission is hereby granted, free of charge, to any person
|
---|
12 | * obtaining a copy of this software and associated documentation
|
---|
13 | * files (the "Software"), to deal in the Software without
|
---|
14 | * restriction, including without limitation the rights to use,
|
---|
15 | * copy, modify, merge, publish, distribute, sublicense, and/or
|
---|
16 | * sell copies of the Software, and to permit persons to whom the
|
---|
17 | * Software is furnished to do so, subject to the following conditions:
|
---|
18 | *
|
---|
19 | * The above copyright notice and this permission notice shall be
|
---|
20 | * included in all copies or substantial portions of the Software.
|
---|
21 | *
|
---|
22 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
---|
23 | * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
|
---|
24 | * OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
---|
25 | * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
|
---|
26 | * HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
|
---|
27 | * WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
|
---|
28 | * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
|
---|
29 | * OTHER DEALINGS IN THE SOFTWARE.
|
---|
30 | */
|
---|
31 | /* Somewhat ugly way to support versioning */
|
---|
32 | import com.sun.xml.ws.commons.virtualbox_2_2.*;
|
---|
33 | import org.virtualbox_2_2.*;
|
---|
34 |
|
---|
35 | import java.util.*;
|
---|
36 | import javax.xml.ws.Holder;
|
---|
37 |
|
---|
38 | public class clienttest
|
---|
39 | {
|
---|
40 | IWebsessionManager mgr;
|
---|
41 | IVirtualBox vbox;
|
---|
42 |
|
---|
43 | public clienttest()
|
---|
44 | {
|
---|
45 | mgr = new IWebsessionManager("http://localhost:18083/");
|
---|
46 | vbox = mgr.logon("test", "test");
|
---|
47 | System.out.println("Initialized connection to VirtualBox version " + vbox.getVersion());
|
---|
48 | }
|
---|
49 |
|
---|
50 | public void disconnect()
|
---|
51 | {
|
---|
52 | mgr.disconnect(vbox);
|
---|
53 | }
|
---|
54 |
|
---|
55 | class Desktop
|
---|
56 | {
|
---|
57 | String name;
|
---|
58 | String uuid;
|
---|
59 |
|
---|
60 | Desktop(int n)
|
---|
61 | {
|
---|
62 | name = "Mach"+n;
|
---|
63 | uuid = UUID.randomUUID().toString();
|
---|
64 | }
|
---|
65 | String getName()
|
---|
66 | {
|
---|
67 | return name;
|
---|
68 | }
|
---|
69 | String getId()
|
---|
70 | {
|
---|
71 | return uuid;
|
---|
72 | }
|
---|
73 | }
|
---|
74 |
|
---|
75 | public void test()
|
---|
76 | {
|
---|
77 | for (int i=0; i<100; i++)
|
---|
78 | {
|
---|
79 | String baseFolder =
|
---|
80 | vbox.getSystemProperties().getDefaultMachineFolder();
|
---|
81 | Desktop desktop = new Desktop(i);
|
---|
82 | IMachine machine = vbox.createMachine(baseFolder,
|
---|
83 | "linux",
|
---|
84 | desktop.getName(),
|
---|
85 | desktop.getId());
|
---|
86 | machine.saveSettings();
|
---|
87 | mgr.cleanupUnused();
|
---|
88 | }
|
---|
89 | }
|
---|
90 |
|
---|
91 | public void test2()
|
---|
92 | {
|
---|
93 | ISession session = mgr.getSessionObject(vbox);
|
---|
94 | String id = "bc8b6219-2775-42c4-f1b2-b48b3c177294";
|
---|
95 | vbox.openSession(session, id);
|
---|
96 | IMachine mach = session.getMachine();
|
---|
97 | IBIOSSettings bios = mach.getBIOSSettings();
|
---|
98 | bios.setIOAPICEnabled(true);
|
---|
99 | mach.saveSettings();
|
---|
100 | session.close();
|
---|
101 | }
|
---|
102 |
|
---|
103 |
|
---|
104 | public void test3()
|
---|
105 | {
|
---|
106 |
|
---|
107 | IWebsessionManager mgr1 = new IWebsessionManager("http://localhost:18082/");
|
---|
108 | IWebsessionManager mgr2 = new IWebsessionManager("http://localhost:18083/");
|
---|
109 | IVirtualBox vbox1 = mgr1.logon("test", "test");
|
---|
110 | IVirtualBox vbox2 = mgr2.logon("test", "test");
|
---|
111 |
|
---|
112 |
|
---|
113 | System.out.println("connection 1 to VirtualBox version " + vbox1.getVersion());
|
---|
114 | System.out.println("connection 2 to VirtualBox version " + vbox2.getVersion());
|
---|
115 | mgr1.disconnect(vbox1);
|
---|
116 | mgr2.disconnect(vbox2);
|
---|
117 |
|
---|
118 | mgr1 = new IWebsessionManager("http://localhost:18082/");
|
---|
119 | mgr2 = new IWebsessionManager("http://localhost:18083/");
|
---|
120 | vbox1 = mgr1.logon("test", "test");
|
---|
121 | vbox2 = mgr2.logon("test", "test");
|
---|
122 |
|
---|
123 | System.out.println("second connection 1 to VirtualBox version " + vbox1.getVersion());
|
---|
124 | System.out.println("second connection 2 to VirtualBox version " + vbox2.getVersion());
|
---|
125 |
|
---|
126 | mgr1.disconnect(vbox1);
|
---|
127 | mgr2.disconnect(vbox2);
|
---|
128 | }
|
---|
129 |
|
---|
130 | public void showVMs()
|
---|
131 | {
|
---|
132 | try
|
---|
133 | {
|
---|
134 | int i = 0;
|
---|
135 | for (IMachine m : vbox.getMachines())
|
---|
136 | {
|
---|
137 | System.out.println("Machine " + (i++) + ": " + " [" + m.getId() + "]" + " - " + m.getName());
|
---|
138 | }
|
---|
139 | }
|
---|
140 | catch (Exception e)
|
---|
141 | {
|
---|
142 | e.printStackTrace();
|
---|
143 | }
|
---|
144 | }
|
---|
145 |
|
---|
146 | public void listHostInfo()
|
---|
147 | {
|
---|
148 | try
|
---|
149 | {
|
---|
150 | IHost host = vbox.getHost();
|
---|
151 | long uProcCount = host.getProcessorCount();
|
---|
152 | System.out.println("Processor count: " + uProcCount);
|
---|
153 |
|
---|
154 | for (long i=0; i<uProcCount; i++)
|
---|
155 | {
|
---|
156 | System.out.println("Processor #" + i + " speed: " + host.getProcessorSpeed(i) + "MHz");
|
---|
157 | }
|
---|
158 |
|
---|
159 | IPerformanceCollector oCollector = vbox.getPerformanceCollector();
|
---|
160 |
|
---|
161 | List<IPerformanceMetric> aMetrics =
|
---|
162 | oCollector.getMetrics(Arrays.asList(new String[]{"*"}),
|
---|
163 | Arrays.asList(new IUnknown[]{host}));
|
---|
164 |
|
---|
165 | for (IPerformanceMetric m : aMetrics)
|
---|
166 | {
|
---|
167 | System.out.println("known metric = "+m.getMetricName());
|
---|
168 | }
|
---|
169 |
|
---|
170 | Holder<List<String>> names = new Holder<List<String>> ();
|
---|
171 | Holder<List<IUnknown>> objects = new Holder<List<IUnknown>>() ;
|
---|
172 | Holder<List<String>> units = new Holder<List<String>>();
|
---|
173 | Holder<List<Long>> scales = new Holder<List<Long>>();
|
---|
174 | Holder<List<Long>> sequenceNumbers = new Holder<List<Long>>();
|
---|
175 | Holder<List<Long>> indices = new Holder<List<Long>>();
|
---|
176 | Holder<List<Long>> lengths = new Holder<List<Long>>();
|
---|
177 |
|
---|
178 | List<Integer> vals =
|
---|
179 | oCollector.queryMetricsData(Arrays.asList(new String[]{"*"}),
|
---|
180 | Arrays.asList(new IUnknown[]{host}),
|
---|
181 | names, objects, units, scales,
|
---|
182 | sequenceNumbers, indices, lengths);
|
---|
183 |
|
---|
184 | for (int i=0; i < names.value.size(); i++)
|
---|
185 | System.out.println("name: "+names.value.get(i));
|
---|
186 | }
|
---|
187 | catch (Exception e)
|
---|
188 | {
|
---|
189 | e.printStackTrace();
|
---|
190 | }
|
---|
191 | }
|
---|
192 |
|
---|
193 | public void startVM(String strVM)
|
---|
194 | {
|
---|
195 | ISession oSession = null;
|
---|
196 | IMachine oMachine = null;
|
---|
197 |
|
---|
198 | try
|
---|
199 | {
|
---|
200 | oSession = mgr.getSessionObject(vbox);
|
---|
201 |
|
---|
202 | // first assume we were given a UUID
|
---|
203 | try
|
---|
204 | {
|
---|
205 | oMachine = vbox.getMachine(strVM);
|
---|
206 | }
|
---|
207 | catch (Exception e)
|
---|
208 | {
|
---|
209 | try
|
---|
210 | {
|
---|
211 | oMachine = vbox.findMachine(strVM);
|
---|
212 | }
|
---|
213 | catch (Exception e1)
|
---|
214 | {
|
---|
215 | }
|
---|
216 | }
|
---|
217 |
|
---|
218 | if (oMachine == null)
|
---|
219 | {
|
---|
220 | System.out.println("Error: can't find VM \"" + strVM + "\"");
|
---|
221 | }
|
---|
222 | else
|
---|
223 | {
|
---|
224 | String uuid = oMachine.getId();
|
---|
225 | String sessionType = "gui";
|
---|
226 | String env = "DISPLAY=:0.0";
|
---|
227 | IProgress oProgress =
|
---|
228 | vbox.openRemoteSession(oSession,
|
---|
229 | uuid,
|
---|
230 | sessionType,
|
---|
231 | env);
|
---|
232 | System.out.println("Session for VM " + uuid + " is opening...");
|
---|
233 | oProgress.waitForCompletion(10000);
|
---|
234 |
|
---|
235 | long rc = oProgress.getResultCode();
|
---|
236 | if (rc != 0)
|
---|
237 | System.out.println("Session failed!");
|
---|
238 | }
|
---|
239 | }
|
---|
240 | catch (Exception e)
|
---|
241 | {
|
---|
242 | e.printStackTrace();
|
---|
243 | }
|
---|
244 | finally
|
---|
245 | {
|
---|
246 | if (oSession != null)
|
---|
247 | {
|
---|
248 | oSession.close();
|
---|
249 | }
|
---|
250 | }
|
---|
251 | }
|
---|
252 |
|
---|
253 | public void cleanup()
|
---|
254 | {
|
---|
255 | try
|
---|
256 | {
|
---|
257 | if (vbox != null)
|
---|
258 | {
|
---|
259 | disconnect();
|
---|
260 | vbox = null;
|
---|
261 | System.out.println("Logged off.");
|
---|
262 | }
|
---|
263 | mgr.cleanupUnused();
|
---|
264 | mgr = null;
|
---|
265 | }
|
---|
266 | catch (Exception e)
|
---|
267 | {
|
---|
268 | e.printStackTrace();
|
---|
269 | }
|
---|
270 | }
|
---|
271 |
|
---|
272 | public static void printArgs()
|
---|
273 | {
|
---|
274 | System.out.println( "Usage: java clienttest <mode> ..." +
|
---|
275 | "\nwith <mode> being:" +
|
---|
276 | "\n show vms list installed virtual machines" +
|
---|
277 | "\n list hostinfo list host info" +
|
---|
278 | "\n startvm <vmname|uuid> start the given virtual machine");
|
---|
279 | }
|
---|
280 |
|
---|
281 | public static void main(String[] args)
|
---|
282 | {
|
---|
283 | if (args.length < 1)
|
---|
284 | {
|
---|
285 | System.out.println("Error: Must specify at least one argument.");
|
---|
286 | printArgs();
|
---|
287 | }
|
---|
288 | else
|
---|
289 | {
|
---|
290 | clienttest c = new clienttest();
|
---|
291 | if (args[0].equals("show"))
|
---|
292 | {
|
---|
293 | if (args.length == 2)
|
---|
294 | {
|
---|
295 | if (args[1].equals("vms"))
|
---|
296 | c.showVMs();
|
---|
297 | else
|
---|
298 | System.out.println("Error: Unknown argument to \"show\": \"" + args[1] + "\".");
|
---|
299 | }
|
---|
300 | else
|
---|
301 | System.out.println("Error: Missing argument to \"show\" command");
|
---|
302 | }
|
---|
303 | else if (args[0].equals("list"))
|
---|
304 | {
|
---|
305 | if (args.length == 2)
|
---|
306 | {
|
---|
307 | if (args[1].equals("hostinfo"))
|
---|
308 | c.listHostInfo();
|
---|
309 | else
|
---|
310 | System.out.println("Error: Unknown argument to \"show\": \"" + args[1] + "\".");
|
---|
311 | }
|
---|
312 | else
|
---|
313 | System.out.println("Error: Missing argument to \"list\" command");
|
---|
314 | }
|
---|
315 | else if (args[0].equals("startvm"))
|
---|
316 | {
|
---|
317 | if (args.length == 2)
|
---|
318 | {
|
---|
319 | c.startVM(args[1]);
|
---|
320 | }
|
---|
321 | else
|
---|
322 | System.out.println("Error: Missing argument to \"startvm\" command");
|
---|
323 | }
|
---|
324 | else if (args[0].equals("test"))
|
---|
325 | {
|
---|
326 | c.test3();
|
---|
327 | }
|
---|
328 | else
|
---|
329 | System.out.println("Error: Unknown command: \"" + args[0] + "\".");
|
---|
330 |
|
---|
331 | c.cleanup();
|
---|
332 | }
|
---|
333 | }
|
---|
334 | }
|
---|