VirtualBox

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

Last change on this file since 21898 was 21898, checked in by vboxsync, 15 years ago

Java WS glue: better struct treatment, allow using struct fields in natural way

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