VirtualBox

source: vbox/trunk/src/VBox/Main/webservice/samples/java/jax-ws/clienttest.java@ 18609

Last change on this file since 18609 was 18609, checked in by vboxsync, 16 years ago

SDK: versioning in samples, cleanup

  • Property svn:eol-style set to native
File size: 10.1 KB
Line 
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.virtualbox.org. 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/* Somewhat ugly way to support versioning */
22import com.sun.xml.ws.commons.virtualbox{VBOX_API_SUFFIX}.*;
23import org.virtualbox{VBOX_API_SUFFIX}.*;
24
25import java.util.*;
26import javax.xml.ws.Holder;
27
28public class clienttest
29{
30 IWebsessionManager mgr;
31 IVirtualBox vbox;
32
33 public clienttest()
34 {
35 mgr = new IWebsessionManager("http://localhost:18083/");
36 vbox = mgr.logon("test", "test");
37 System.out.println("Initialized connection to VirtualBox version " + vbox.getVersion());
38 }
39
40 public void disconnect()
41 {
42 mgr.disconnect(vbox);
43 }
44
45 class Desktop
46 {
47 String name;
48 UUID uuid;
49
50 Desktop(int n)
51 {
52 name = "Mach"+n;
53 uuid = UUID.randomUUID();
54 }
55 String getName()
56 {
57 return name;
58 }
59 UUID getId()
60 {
61 return uuid;
62 }
63 }
64
65 public void test()
66 {
67 for (int i=0; i<100; i++)
68 {
69 String baseFolder =
70 vbox.getSystemProperties().getDefaultMachineFolder();
71 Desktop desktop = new Desktop(i);
72 IMachine machine = vbox.createMachine(baseFolder,
73 "linux",
74 desktop.getName(),
75 desktop.getId());
76 machine.saveSettings();
77 mgr.cleanupUnused();
78 }
79 }
80
81 public void test2()
82 {
83 ISession session = mgr.getSessionObject(vbox);
84 UUID id = UUID.fromString("bc8b6219-2775-42c4-f1b2-b48b3c177294");
85 vbox.openSession(session, id);
86 IMachine mach = session.getMachine();
87 IBIOSSettings bios = mach.getBIOSSettings();
88 bios.setIOAPICEnabled(true);
89 mach.saveSettings();
90 session.close();
91 }
92
93
94 public void test3()
95 {
96
97 IWebsessionManager mgr1 = new IWebsessionManager("http://localhost:18082/");
98 IWebsessionManager mgr2 = new IWebsessionManager("http://localhost:18083/");
99 IVirtualBox vbox1 = mgr1.logon("test", "test");
100 IVirtualBox vbox2 = mgr2.logon("test", "test");
101
102
103 System.out.println("connection 1 to VirtualBox version " + vbox1.getVersion());
104 System.out.println("connection 2 to VirtualBox version " + vbox2.getVersion());
105 mgr1.disconnect(vbox1);
106 mgr2.disconnect(vbox2);
107
108 mgr1 = new IWebsessionManager("http://localhost:18082/");
109 mgr2 = new IWebsessionManager("http://localhost:18083/");
110 vbox1 = mgr1.logon("test", "test");
111 vbox2 = mgr2.logon("test", "test");
112
113 System.out.println("second connection 1 to VirtualBox version " + vbox1.getVersion());
114 System.out.println("second connection 2 to VirtualBox version " + vbox2.getVersion());
115
116 mgr1.disconnect(vbox1);
117 mgr2.disconnect(vbox2);
118 }
119
120 public void showVMs()
121 {
122 try
123 {
124 int i = 0;
125 for (IMachine m : vbox.getMachines())
126 {
127 System.out.println("Machine " + (i++) + ": " + " [" + m.getId() + "]" + " - " + m.getName());
128 }
129 }
130 catch (Exception e)
131 {
132 e.printStackTrace();
133 }
134 }
135
136 public void listHostInfo()
137 {
138 try
139 {
140 IHost host = vbox.getHost();
141 long uProcCount = host.getProcessorCount();
142 System.out.println("Processor count: " + uProcCount);
143
144 for (long i=0; i<uProcCount; i++)
145 {
146 System.out.println("Processor #" + i + " speed: " + host.getProcessorSpeed(i) + "MHz");
147 }
148
149 IPerformanceCollector oCollector = vbox.getPerformanceCollector();
150
151 List<IPerformanceMetric> aMetrics =
152 oCollector.getMetrics(Arrays.asList(new String[]{"*"}),
153 Arrays.asList(new IUnknown[]{host}));
154
155 for (IPerformanceMetric m : aMetrics)
156 {
157 System.out.println("known metric = "+m.getMetricName());
158 }
159
160 Holder<List<String>> names = new Holder<List<String>> ();
161 Holder<List<IUnknown>> objects = new Holder<List<IUnknown>>() ;
162 Holder<List<String>> units = new Holder<List<String>>();
163 Holder<List<Long>> scales = new Holder<List<Long>>();
164 Holder<List<Long>> sequenceNumbers = new Holder<List<Long>>();
165 Holder<List<Long>> indices = new Holder<List<Long>>();
166 Holder<List<Long>> lengths = new Holder<List<Long>>();
167
168 List<Integer> vals =
169 oCollector.queryMetricsData(Arrays.asList(new String[]{"*"}),
170 Arrays.asList(new IUnknown[]{host}),
171 names, objects, units, scales,
172 sequenceNumbers, indices, lengths);
173
174 for (int i=0; i < names.value.size(); i++)
175 System.out.println("name: "+names.value.get(i));
176 }
177 catch (Exception e)
178 {
179 e.printStackTrace();
180 }
181 }
182
183 public void startVM(String strVM)
184 {
185 ISession oSession = null;
186 IMachine oMachine = null;
187
188 try
189 {
190 oSession = mgr.getSessionObject(vbox);
191
192 // first assume we were given a UUID
193 try
194 {
195 oMachine = vbox.getMachine(UUID.fromString(strVM));
196 }
197 catch (Exception e)
198 {
199 try
200 {
201 oMachine = vbox.findMachine(strVM);
202 }
203 catch (Exception e1)
204 {
205 }
206 }
207
208 if (oMachine == null)
209 {
210 System.out.println("Error: can't find VM \"" + strVM + "\"");
211 }
212 else
213 {
214 UUID uuid = oMachine.getId();
215 String sessionType = "gui";
216 String env = "DISPLAY=:0.0";
217 IProgress oProgress =
218 vbox.openRemoteSession(oSession,
219 uuid,
220 sessionType,
221 env);
222 System.out.println("Session for VM " + uuid + " is opening...");
223 oProgress.waitForCompletion(10000);
224
225 long rc = oProgress.getResultCode();
226 if (rc != 0)
227 System.out.println("Session failed!");
228 }
229 }
230 catch (Exception e)
231 {
232 e.printStackTrace();
233 }
234 finally
235 {
236 if (oSession != null)
237 {
238 oSession.close();
239 }
240 }
241 }
242
243 public void cleanup()
244 {
245 try
246 {
247 if (vbox != null)
248 {
249 disconnect();
250 vbox = null;
251 System.out.println("Logged off.");
252 }
253 mgr.cleanupUnused();
254 mgr = null;
255 }
256 catch (Exception e)
257 {
258 e.printStackTrace();
259 }
260 }
261
262 public static void printArgs()
263 {
264 System.out.println( "Usage: java clienttest <mode> ..." +
265 "\nwith <mode> being:" +
266 "\n show vms list installed virtual machines" +
267 "\n list hostinfo list host info" +
268 "\n startvm <vmname|uuid> start the given virtual machine");
269 }
270
271 public static void main(String[] args)
272 {
273 if (args.length < 1)
274 {
275 System.out.println("Error: Must specify at least one argument.");
276 printArgs();
277 }
278 else
279 {
280 clienttest c = new clienttest();
281 if (args[0].equals("show"))
282 {
283 if (args.length == 2)
284 {
285 if (args[1].equals("vms"))
286 c.showVMs();
287 else
288 System.out.println("Error: Unknown argument to \"show\": \"" + args[1] + "\".");
289 }
290 else
291 System.out.println("Error: Missing argument to \"show\" command");
292 }
293 else if (args[0].equals("list"))
294 {
295 if (args.length == 2)
296 {
297 if (args[1].equals("hostinfo"))
298 c.listHostInfo();
299 else
300 System.out.println("Error: Unknown argument to \"show\": \"" + args[1] + "\".");
301 }
302 else
303 System.out.println("Error: Missing argument to \"list\" command");
304 }
305 else if (args[0].equals("startvm"))
306 {
307 if (args.length == 2)
308 {
309 c.startVM(args[1]);
310 }
311 else
312 System.out.println("Error: Missing argument to \"startvm\" command");
313 }
314 else if (args[0].equals("test"))
315 {
316 c.test3();
317 }
318 else
319 System.out.println("Error: Unknown command: \"" + args[0] + "\".");
320
321 c.cleanup();
322 }
323 }
324}
Note: See TracBrowser for help on using the repository browser.

© 2024 Oracle Support Privacy / Do Not Sell My Info Terms of Use Trademark Policy Automated Access Etiquette