VirtualBox

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

Last change on this file since 68155 was 32394, checked in by vboxsync, 14 years ago

more branding and header fixes

  • Property svn:eol-style set to native
File size: 10.5 KB
Line 
1/*
2 * Sample client for the VirtualBox web service, written in Java
3 * (object-oriented bindings).
4 *
5 * Run the VirtualBox web service server first; see the VirtualBox
6 * SDK reference for details.
7 *
8 * Copyright (C) 2008-2010 Oracle Corporation
9 *
10 * The following license applies to this file only:
11 *
12 * Permission is hereby granted, free of charge, to any person
13 * obtaining a copy of this software and associated documentation
14 * files (the "Software"), to deal in the Software without
15 * restriction, including without limitation the rights to use,
16 * copy, modify, merge, publish, distribute, sublicense, and/or
17 * sell copies of the Software, and to permit persons to whom the
18 * Software is furnished to do so, subject to the following conditions:
19 *
20 * The above copyright notice and this permission notice shall be
21 * included in all copies or substantial portions of the Software.
22 *
23 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
24 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
25 * OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
26 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
27 * HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
28 * WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
29 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
30 * OTHER DEALINGS IN THE SOFTWARE.
31 */
32/* Somewhat ugly way to support versioning */
33import com.sun.xml.ws.commons.virtualbox{VBOX_API_SUFFIX}.*;
34
35import java.util.*;
36import javax.xml.ws.Holder;
37
38public 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 true);
87 machine.saveSettings();
88 mgr.cleanupUnused();
89 }
90 }
91
92 public void test2()
93 {
94 ISession session = mgr.getSessionObject(vbox);
95 String id = "bc8b6219-2775-42c4-f1b2-b48b3c177294";
96 vbox.openSession(session, id);
97 IMachine mach = session.getMachine();
98 IBIOSSettings bios = mach.getBIOSSettings();
99 bios.setIOAPICEnabled(true);
100 mach.saveSettings();
101 session.close();
102 }
103
104
105 public void test3()
106 {
107
108 IWebsessionManager mgr1 = new IWebsessionManager("http://localhost:18082/");
109 IWebsessionManager mgr2 = new IWebsessionManager("http://localhost:18083/");
110 IVirtualBox vbox1 = mgr1.logon("test", "test");
111 IVirtualBox vbox2 = mgr2.logon("test", "test");
112
113
114 System.out.println("connection 1 to VirtualBox version " + vbox1.getVersion());
115 System.out.println("connection 2 to VirtualBox version " + vbox2.getVersion());
116 mgr1.disconnect(vbox1);
117 mgr2.disconnect(vbox2);
118
119 mgr1 = new IWebsessionManager("http://localhost:18082/");
120 mgr2 = new IWebsessionManager("http://localhost:18083/");
121 vbox1 = mgr1.logon("test", "test");
122 vbox2 = mgr2.logon("test", "test");
123
124 System.out.println("second connection 1 to VirtualBox version " + vbox1.getVersion());
125 System.out.println("second connection 2 to VirtualBox version " + vbox2.getVersion());
126
127 mgr1.disconnect(vbox1);
128 mgr2.disconnect(vbox2);
129 }
130
131 public void showVMs()
132 {
133 try
134 {
135 int i = 0;
136 for (IMachine m : vbox.getMachines())
137 {
138 System.out.println("Machine " + (i++) + ": " + " [" + m.getId() + "]" + " - " + m.getName());
139 }
140 }
141 catch (Exception e)
142 {
143 e.printStackTrace();
144 }
145 }
146
147 public void listHostInfo()
148 {
149 try
150 {
151 IHost host = vbox.getHost();
152 long uProcCount = host.getProcessorCount();
153 System.out.println("Processor count: " + uProcCount);
154
155 for (long i=0; i<uProcCount; i++)
156 {
157 System.out.println("Processor #" + i + " speed: " + host.getProcessorSpeed(i) + "MHz");
158 }
159
160 IPerformanceCollector oCollector = vbox.getPerformanceCollector();
161
162 List<IPerformanceMetric> aMetrics =
163 oCollector.getMetrics(Arrays.asList(new String[]{"*"}),
164 Arrays.asList(new IUnknown[]{host}));
165
166 for (IPerformanceMetric m : aMetrics)
167 {
168 System.out.println("known metric = "+m.getMetricName());
169 }
170
171 Holder<List<String>> names = new Holder<List<String>> ();
172 Holder<List<IUnknown>> objects = new Holder<List<IUnknown>>() ;
173 Holder<List<String>> units = new Holder<List<String>>();
174 Holder<List<Long>> scales = new Holder<List<Long>>();
175 Holder<List<Long>> sequenceNumbers = new Holder<List<Long>>();
176 Holder<List<Long>> indices = new Holder<List<Long>>();
177 Holder<List<Long>> lengths = new Holder<List<Long>>();
178
179 List<Integer> vals =
180 oCollector.queryMetricsData(Arrays.asList(new String[]{"*"}),
181 Arrays.asList(new IUnknown[]{host}),
182 names, objects, units, scales,
183 sequenceNumbers, indices, lengths);
184
185 for (int i=0; i < names.value.size(); i++)
186 System.out.println("name: "+names.value.get(i));
187 }
188 catch (Exception e)
189 {
190 e.printStackTrace();
191 }
192 }
193
194 public void startVM(String strVM)
195 {
196 ISession oSession = null;
197 IMachine oMachine = null;
198
199 try
200 {
201 oSession = mgr.getSessionObject(vbox);
202
203 // first assume we were given a UUID
204 try
205 {
206 oMachine = vbox.getMachine(strVM);
207 }
208 catch (Exception e)
209 {
210 try
211 {
212 oMachine = vbox.findMachine(strVM);
213 }
214 catch (Exception e1)
215 {
216 }
217 }
218
219 if (oMachine == null)
220 {
221 System.out.println("Error: can't find VM \"" + strVM + "\"");
222 }
223 else
224 {
225 String uuid = oMachine.getId();
226 String sessionType = "gui";
227 String env = "DISPLAY=:0.0";
228 IProgress oProgress =
229 oMachine.launchVMProcess(oSession,
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}
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