1 | /* $Id: clienttest.java 101035 2023-09-07 08:59:15Z 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-2023 Oracle and/or its affiliates.
|
---|
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 */
|
---|
37 | import com.sun.xml.ws.commons.virtualbox{VBOX_API_SUFFIX}.*;
|
---|
38 |
|
---|
39 | import java.util.*;
|
---|
40 | import javax.xml.ws.Holder;
|
---|
41 |
|
---|
42 | public 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 | IFirmwareSettings firmware = mach.getFirmwareSettings();
|
---|
103 | firmware.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 | ArrayList<String> env = new ArrayList<String>();
|
---|
232 | env.add("DISPLAY=:0.0");
|
---|
233 | IProgress oProgress =
|
---|
234 | oMachine.launchVMProcess(oSession,
|
---|
235 | sessionType,
|
---|
236 | env);
|
---|
237 | System.out.println("Session for VM " + uuid + " is opening...");
|
---|
238 | oProgress.waitForCompletion(10000);
|
---|
239 |
|
---|
240 | long rc = oProgress.getResultCode();
|
---|
241 | if (rc != 0)
|
---|
242 | System.out.println("Session failed!");
|
---|
243 | }
|
---|
244 | }
|
---|
245 | catch (Exception e)
|
---|
246 | {
|
---|
247 | e.printStackTrace();
|
---|
248 | }
|
---|
249 | finally
|
---|
250 | {
|
---|
251 | if (oSession != null)
|
---|
252 | {
|
---|
253 | oSession.close();
|
---|
254 | }
|
---|
255 | }
|
---|
256 | }
|
---|
257 |
|
---|
258 | public void cleanup()
|
---|
259 | {
|
---|
260 | try
|
---|
261 | {
|
---|
262 | if (vbox != null)
|
---|
263 | {
|
---|
264 | disconnect();
|
---|
265 | vbox = null;
|
---|
266 | System.out.println("Logged off.");
|
---|
267 | }
|
---|
268 | mgr.cleanupUnused();
|
---|
269 | mgr = null;
|
---|
270 | }
|
---|
271 | catch (Exception e)
|
---|
272 | {
|
---|
273 | e.printStackTrace();
|
---|
274 | }
|
---|
275 | }
|
---|
276 |
|
---|
277 | public static void printArgs()
|
---|
278 | {
|
---|
279 | System.out.println( "Usage: java clienttest <mode> ..." +
|
---|
280 | "\nwith <mode> being:" +
|
---|
281 | "\n show vms list installed virtual machines" +
|
---|
282 | "\n list hostinfo list host info" +
|
---|
283 | "\n startvm <vmname|uuid> start the given virtual machine");
|
---|
284 | }
|
---|
285 |
|
---|
286 | public static void main(String[] args)
|
---|
287 | {
|
---|
288 | if (args.length < 1)
|
---|
289 | {
|
---|
290 | System.out.println("Error: Must specify at least one argument.");
|
---|
291 | printArgs();
|
---|
292 | }
|
---|
293 | else
|
---|
294 | {
|
---|
295 | clienttest c = new clienttest();
|
---|
296 | if (args[0].equals("show"))
|
---|
297 | {
|
---|
298 | if (args.length == 2)
|
---|
299 | {
|
---|
300 | if (args[1].equals("vms"))
|
---|
301 | c.showVMs();
|
---|
302 | else
|
---|
303 | System.out.println("Error: Unknown argument to \"show\": \"" + args[1] + "\".");
|
---|
304 | }
|
---|
305 | else
|
---|
306 | System.out.println("Error: Missing argument to \"show\" command");
|
---|
307 | }
|
---|
308 | else if (args[0].equals("list"))
|
---|
309 | {
|
---|
310 | if (args.length == 2)
|
---|
311 | {
|
---|
312 | if (args[1].equals("hostinfo"))
|
---|
313 | c.listHostInfo();
|
---|
314 | else
|
---|
315 | System.out.println("Error: Unknown argument to \"show\": \"" + args[1] + "\".");
|
---|
316 | }
|
---|
317 | else
|
---|
318 | System.out.println("Error: Missing argument to \"list\" command");
|
---|
319 | }
|
---|
320 | else if (args[0].equals("startvm"))
|
---|
321 | {
|
---|
322 | if (args.length == 2)
|
---|
323 | {
|
---|
324 | c.startVM(args[1]);
|
---|
325 | }
|
---|
326 | else
|
---|
327 | System.out.println("Error: Missing argument to \"startvm\" command");
|
---|
328 | }
|
---|
329 | else if (args[0].equals("test"))
|
---|
330 | {
|
---|
331 | c.test3();
|
---|
332 | }
|
---|
333 | else
|
---|
334 | System.out.println("Error: Unknown command: \"" + args[0] + "\".");
|
---|
335 |
|
---|
336 | c.cleanup();
|
---|
337 | }
|
---|
338 | }
|
---|
339 | }
|
---|