1 |
|
---|
2 | /*
|
---|
3 | * Sample client for the VirtualBox webservice, written in Java.
|
---|
4 | * Best consumed in conjunction with the explanations in the VirtualBox
|
---|
5 | * User Manual, which describe in detail how to get this code running.
|
---|
6 | *
|
---|
7 | * Copyright (C) 2008 Sun Microsystems, Inc.
|
---|
8 | *
|
---|
9 | * This file is part of VirtualBox Open Source Edition (OSE), as
|
---|
10 | * available from http://www.215389.xyz. This file is free software;
|
---|
11 | * you can redistribute it and/or modify it under the terms of the GNU
|
---|
12 | * General Public License (GPL) as published by the Free Software
|
---|
13 | * Foundation, in version 2 as it comes in the "COPYING" file of the
|
---|
14 | * VirtualBox OSE distribution. VirtualBox OSE is distributed in the
|
---|
15 | * hope that it will be useful, but WITHOUT ANY WARRANTY of any kind.
|
---|
16 | *
|
---|
17 | * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa
|
---|
18 | * Clara, CA 95054 USA or visit http://www.sun.com if you need
|
---|
19 | * additional information or have any questions.
|
---|
20 | */
|
---|
21 |
|
---|
22 | import org.virtualbox.www.Service.VboxService;
|
---|
23 | import org.virtualbox.www.Service.VboxServiceLocator;
|
---|
24 | import org.virtualbox.www.VboxPortType;
|
---|
25 |
|
---|
26 | public class clienttest
|
---|
27 | {
|
---|
28 | private VboxService _service;
|
---|
29 | private VboxPortType _port;
|
---|
30 | private String _oVbox;
|
---|
31 |
|
---|
32 | public clienttest()
|
---|
33 | {
|
---|
34 | try
|
---|
35 | {
|
---|
36 | // instantiate the webservice in instance data; the classes
|
---|
37 | // VboxServiceLocator and VboxPortType have been created
|
---|
38 | // by the WSDL2Java helper that you should have run prior
|
---|
39 | // to compiling this example, as described in the User Manual.
|
---|
40 | _service = new VboxServiceLocator();
|
---|
41 | _port = _service.getvboxServicePort();
|
---|
42 |
|
---|
43 | // From now on, we can call any method in the webservice by
|
---|
44 | // prefixing it with "port."
|
---|
45 |
|
---|
46 | // First step is always to log on to the webservice. This
|
---|
47 | // returns a managed object reference to the webservice's
|
---|
48 | // global instance of IVirtualBox, which in turn contains
|
---|
49 | // the most important methods provided by the Main API.
|
---|
50 | _oVbox = _port.IWebsessionManager_logon("", "");
|
---|
51 |
|
---|
52 | // Call IVirtualBox::getVersion and print out the result
|
---|
53 | String version = _port.IVirtualBox_getVersion(_oVbox);
|
---|
54 | System.out.println("Initialized connection to VirtualBox version " + version);
|
---|
55 | }
|
---|
56 | catch (Exception e)
|
---|
57 | {
|
---|
58 | e.printStackTrace();
|
---|
59 | }
|
---|
60 | }
|
---|
61 |
|
---|
62 | public void showVMs()
|
---|
63 | {
|
---|
64 | try
|
---|
65 | {
|
---|
66 | // Call IVirtualBox::getMachines, which yields an array
|
---|
67 | // of managed object references to all machines which have
|
---|
68 | // been registered:
|
---|
69 | String[] aMachines = _port.IVirtualBox_getMachines2(_oVbox);
|
---|
70 | // Walk through this array and, for each machine, call
|
---|
71 | // IMachine::getName (accessor method to the "name" attribute)
|
---|
72 | for (int i = 0; i < aMachines.length; i++)
|
---|
73 | {
|
---|
74 | String oMachine = aMachines[i];
|
---|
75 | String machinename = _port.IMachine_getName(oMachine);
|
---|
76 | System.out.println("Machine " + i + ": " + oMachine + " - " + machinename);
|
---|
77 |
|
---|
78 | // release managed object reference
|
---|
79 | _port.IManagedObjectRef_release(oMachine);
|
---|
80 | }
|
---|
81 | }
|
---|
82 | catch (Exception e)
|
---|
83 | {
|
---|
84 | e.printStackTrace();
|
---|
85 | }
|
---|
86 | }
|
---|
87 |
|
---|
88 | public void listHostInfo()
|
---|
89 | {
|
---|
90 | try
|
---|
91 | {
|
---|
92 | String oHost = _port.IVirtualBox_getHost(_oVbox);
|
---|
93 |
|
---|
94 | org.apache.axis.types.UnsignedInt uProcCount = _port.IHost_getProcessorCount(oHost);
|
---|
95 | System.out.println("Processor count: " + uProcCount);
|
---|
96 |
|
---|
97 | String oCollector = _port.IVirtualBox_getPerformanceCollector(_oVbox);
|
---|
98 |
|
---|
99 | String aobj[] = {oHost};
|
---|
100 | String astrMetrics[] = {"*"};
|
---|
101 | String aMetrics[] = {};
|
---|
102 | aMetrics = _port.IPerformanceCollector_getMetrics(oCollector,
|
---|
103 | astrMetrics,
|
---|
104 | aobj);
|
---|
105 |
|
---|
106 | // String astrMetricNames[] = { "*" };
|
---|
107 | // String aObjects[];
|
---|
108 | // String aRetNames[];
|
---|
109 | // int aRetIndices[];
|
---|
110 | // int aRetLengths[];
|
---|
111 | // int aRetData[];
|
---|
112 | // int rc = _port.ICollector_queryMetricsData(oCollector,
|
---|
113 | // aObjects,
|
---|
114 | // aRetNames,
|
---|
115 | // aRetObjects,
|
---|
116 | // aRetIndices,
|
---|
117 | // aRetLengths,
|
---|
118 | // aRetData);
|
---|
119 | //
|
---|
120 | /*
|
---|
121 | Bstr metricNames[] = { L"*" };
|
---|
122 | com::SafeArray<BSTR> metrics (1);
|
---|
123 | metricNames[0].cloneTo (&metrics [0]);
|
---|
124 | com::SafeArray<BSTR> retNames;
|
---|
125 | com::SafeIfaceArray<IUnknown> retObjects;
|
---|
126 | com::SafeArray<ULONG> retIndices;
|
---|
127 | com::SafeArray<ULONG> retLengths;
|
---|
128 | com::SafeArray<LONG> retData;
|
---|
129 | CHECK_ERROR (collector, QueryMetricsData(ComSafeArrayAsInParam(metrics),
|
---|
130 | ComSafeArrayInArg(objects),
|
---|
131 | ComSafeArrayAsOutParam(retNames),
|
---|
132 | ComSafeArrayAsOutParam(retObjects),
|
---|
133 | ComSafeArrayAsOutParam(retIndices),
|
---|
134 | ComSafeArrayAsOutParam(retLengths),
|
---|
135 | ComSafeArrayAsOutParam(retData)) );
|
---|
136 | */
|
---|
137 |
|
---|
138 | }
|
---|
139 | catch (Exception e)
|
---|
140 | {
|
---|
141 | e.printStackTrace();
|
---|
142 | }
|
---|
143 | }
|
---|
144 |
|
---|
145 | public void startVM(String strVM)
|
---|
146 | {
|
---|
147 | String oSession = "";
|
---|
148 | Boolean fSessionOpen = false;
|
---|
149 |
|
---|
150 | try
|
---|
151 | {
|
---|
152 | // this is pretty much what VBoxManage does to start a VM
|
---|
153 | String oMachine = "";
|
---|
154 | Boolean fOK = false;
|
---|
155 |
|
---|
156 | oSession = _port.IWebsessionManager_getSessionObject(_oVbox);
|
---|
157 |
|
---|
158 | // first assume we were given a UUID
|
---|
159 | try
|
---|
160 | {
|
---|
161 | oMachine = _port.IVirtualBox_getMachine(_oVbox, strVM);
|
---|
162 | fOK = true;
|
---|
163 | }
|
---|
164 | catch (Exception e)
|
---|
165 | {
|
---|
166 | }
|
---|
167 |
|
---|
168 | if (!fOK)
|
---|
169 | {
|
---|
170 | try
|
---|
171 | {
|
---|
172 | // or try by name
|
---|
173 | oMachine = _port.IVirtualBox_findMachine(_oVbox, strVM);
|
---|
174 | fOK = true;
|
---|
175 | }
|
---|
176 | catch (Exception e)
|
---|
177 | {
|
---|
178 | }
|
---|
179 | }
|
---|
180 |
|
---|
181 | if (!fOK)
|
---|
182 | {
|
---|
183 | System.out.println("Error: can't find VM \"" + strVM + "\"");
|
---|
184 | }
|
---|
185 | else
|
---|
186 | {
|
---|
187 | String uuid = _port.IMachine_getId(oMachine);
|
---|
188 | String sessionType = "gui";
|
---|
189 | String env = "DISPLAY=:0.0";
|
---|
190 | String oProgress = _port.IVirtualBox_openRemoteSession(_oVbox, oSession, uuid, sessionType, env);
|
---|
191 | fSessionOpen = true;
|
---|
192 |
|
---|
193 | System.out.println("Session for VM " + uuid + " is opening...");
|
---|
194 | _port.IProgress_waitForCompletion(oProgress, 10000);
|
---|
195 |
|
---|
196 | int rc = _port.IProgress_getResultCode(oProgress).intValue();
|
---|
197 | if (rc != 0)
|
---|
198 | {
|
---|
199 | System.out.println("Session failed!");
|
---|
200 | }
|
---|
201 | }
|
---|
202 | }
|
---|
203 | catch (Exception e)
|
---|
204 | {
|
---|
205 | e.printStackTrace();
|
---|
206 | }
|
---|
207 |
|
---|
208 | if (fSessionOpen)
|
---|
209 | {
|
---|
210 | try
|
---|
211 | {
|
---|
212 | _port.ISession_close(oSession);
|
---|
213 | }
|
---|
214 | catch (Exception e)
|
---|
215 | {
|
---|
216 | }
|
---|
217 | }
|
---|
218 | }
|
---|
219 |
|
---|
220 | public void cleanup()
|
---|
221 | {
|
---|
222 | try
|
---|
223 | {
|
---|
224 | if (_oVbox.length() > 0)
|
---|
225 | {
|
---|
226 | // log off
|
---|
227 | _port.IWebsessionManager_logoff(_oVbox);
|
---|
228 | _oVbox = null;
|
---|
229 | System.out.println("Logged off.");
|
---|
230 | }
|
---|
231 | }
|
---|
232 | catch (Exception e)
|
---|
233 | {
|
---|
234 | e.printStackTrace();
|
---|
235 | }
|
---|
236 | }
|
---|
237 |
|
---|
238 | public static void printArgs()
|
---|
239 | {
|
---|
240 | System.out.println( "Usage: java clienttest <mode> ..." +
|
---|
241 | "\nwith <mode> being:" +
|
---|
242 | "\n show vms list installed virtual machines" +
|
---|
243 | "\n list hostinfo list host info" +
|
---|
244 | "\n startvm <vmname|uuid> start the given virtual machine");
|
---|
245 | }
|
---|
246 |
|
---|
247 | public static void main(String[] args)
|
---|
248 | {
|
---|
249 | if (args.length < 1)
|
---|
250 | {
|
---|
251 | System.out.println("Error: Must specify at least one argument.");
|
---|
252 | printArgs();
|
---|
253 | }
|
---|
254 | else
|
---|
255 | {
|
---|
256 | clienttest c = new clienttest();
|
---|
257 | if (args[0].equals("show"))
|
---|
258 | {
|
---|
259 | if (args.length == 2)
|
---|
260 | {
|
---|
261 | if (args[1].equals("vms"))
|
---|
262 | c.showVMs();
|
---|
263 | else
|
---|
264 | System.out.println("Error: Unknown argument to \"show\": \"" + args[1] + "\".");
|
---|
265 | }
|
---|
266 | else
|
---|
267 | System.out.println("Error: Missing argument to \"show\" command");
|
---|
268 | }
|
---|
269 | else if (args[0].equals("list"))
|
---|
270 | {
|
---|
271 | if (args.length == 2)
|
---|
272 | {
|
---|
273 | if (args[1].equals("hostinfo"))
|
---|
274 | c.listHostInfo();
|
---|
275 | else
|
---|
276 | System.out.println("Error: Unknown argument to \"show\": \"" + args[1] + "\".");
|
---|
277 | }
|
---|
278 | else
|
---|
279 | System.out.println("Error: Missing argument to \"show\" command");
|
---|
280 | }
|
---|
281 | else if (args[0].equals("startvm"))
|
---|
282 | {
|
---|
283 | if (args.length == 2)
|
---|
284 | {
|
---|
285 | c.startVM(args[1]);
|
---|
286 | }
|
---|
287 | else
|
---|
288 | System.out.println("Error: Missing argument to \"startvm\" command");
|
---|
289 | }
|
---|
290 | else
|
---|
291 | System.out.println("Error: Unknown command: \"" + args[0] + "\".");
|
---|
292 |
|
---|
293 | c.cleanup();
|
---|
294 | }
|
---|
295 | }
|
---|
296 | }
|
---|