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