VirtualBox

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

Last change on this file since 69498 was 69498, checked in by vboxsync, 8 years ago

backed out r118835 as it incorrectly updated the 'This file is based on' file headers.

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

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