VirtualBox

source: vbox/trunk/src/VBox/Main/webservice/samples/java/axis/clienttest.java@ 23009

Last change on this file since 23009 was 18694, checked in by vboxsync, 16 years ago

SDK licensing

  • Property svn:eol-style set to native
File size: 10.5 KB
Line 
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-2009 Sun Microsystems, Inc.
8 *
9 * The following license applies to this file only:
10 *
11 * Permission is hereby granted, free of charge, to any person
12 * obtaining a copy of this software and associated documentation
13 * files (the "Software"), to deal in the Software without
14 * restriction, including without limitation the rights to use,
15 * copy, modify, merge, publish, distribute, sublicense, and/or
16 * sell copies of the Software, and to permit persons to whom the
17 * Software is furnished to do so, subject to the following conditions:
18 *
19 * The above copyright notice and this permission notice shall be
20 * included in all copies or substantial portions of the Software.
21 *
22 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
23 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
24 * OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
25 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
26 * HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
27 * WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
28 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
29 * OTHER DEALINGS IN THE SOFTWARE.
30 */
31
32import org.virtualbox.www.Service.VboxService;
33import org.virtualbox.www.Service.VboxServiceLocator;
34import org.virtualbox.www.VboxPortType;
35
36public class clienttest
37{
38 private VboxService _service;
39 private VboxPortType _port;
40 private String _oVbox;
41
42 public clienttest()
43 {
44 try
45 {
46 // instantiate the webservice in instance data; the classes
47 // VboxServiceLocator and VboxPortType have been created
48 // by the WSDL2Java helper that you should have run prior
49 // to compiling this example, as described in the User Manual.
50 _service = new VboxServiceLocator();
51 _port = _service.getvboxServicePort();
52
53 // From now on, we can call any method in the webservice by
54 // prefixing it with "port."
55
56 // First step is always to log on to the webservice. This
57 // returns a managed object reference to the webservice's
58 // global instance of IVirtualBox, which in turn contains
59 // the most important methods provided by the Main API.
60 _oVbox = _port.IWebsessionManager_logon("", "");
61
62 // Call IVirtualBox::getVersion and print out the result
63 String version = _port.IVirtualBox_getVersion(_oVbox);
64 System.out.println("Initialized connection to VirtualBox version " + version);
65 }
66 catch (Exception e)
67 {
68 e.printStackTrace();
69 }
70 }
71
72 public void showVMs()
73 {
74 try
75 {
76 // Call IVirtualBox::getMachines, which yields an array
77 // of managed object references to all machines which have
78 // been registered:
79 String[] aMachines = _port.IVirtualBox_getMachines2(_oVbox);
80 // Walk through this array and, for each machine, call
81 // IMachine::getName (accessor method to the "name" attribute)
82 for (int i = 0; i < aMachines.length; i++)
83 {
84 String oMachine = aMachines[i];
85 String machinename = _port.IMachine_getName(oMachine);
86 System.out.println("Machine " + i + ": " + oMachine + " - " + machinename);
87
88 // release managed object reference
89 _port.IManagedObjectRef_release(oMachine);
90 }
91 }
92 catch (Exception e)
93 {
94 e.printStackTrace();
95 }
96 }
97
98 public void listHostInfo()
99 {
100 try
101 {
102 String oHost = _port.IVirtualBox_getHost(_oVbox);
103
104 org.apache.axis.types.UnsignedInt uProcCount = _port.IHost_getProcessorCount(oHost);
105 System.out.println("Processor count: " + uProcCount);
106
107 String oCollector = _port.IVirtualBox_getPerformanceCollector(_oVbox);
108
109 String aobj[] = {oHost};
110 String astrMetrics[] = {"*"};
111 String aMetrics[] = {};
112 aMetrics = _port.IPerformanceCollector_getMetrics(oCollector,
113 astrMetrics,
114 aobj);
115
116// String astrMetricNames[] = { "*" };
117// String aObjects[];
118// String aRetNames[];
119// int aRetIndices[];
120// int aRetLengths[];
121// int aRetData[];
122// int rc = _port.ICollector_queryMetricsData(oCollector,
123// aObjects,
124// aRetNames,
125// aRetObjects,
126// aRetIndices,
127// aRetLengths,
128// aRetData);
129//
130/*
131 Bstr metricNames[] = { L"*" };
132 com::SafeArray<BSTR> metrics (1);
133 metricNames[0].cloneTo (&metrics [0]);
134 com::SafeArray<BSTR> retNames;
135 com::SafeIfaceArray<IUnknown> retObjects;
136 com::SafeArray<ULONG> retIndices;
137 com::SafeArray<ULONG> retLengths;
138 com::SafeArray<LONG> retData;
139 CHECK_ERROR (collector, QueryMetricsData(ComSafeArrayAsInParam(metrics),
140 ComSafeArrayInArg(objects),
141 ComSafeArrayAsOutParam(retNames),
142 ComSafeArrayAsOutParam(retObjects),
143 ComSafeArrayAsOutParam(retIndices),
144 ComSafeArrayAsOutParam(retLengths),
145 ComSafeArrayAsOutParam(retData)) );
146*/
147
148 }
149 catch (Exception e)
150 {
151 e.printStackTrace();
152 }
153 }
154
155 public void startVM(String strVM)
156 {
157 String oSession = "";
158 Boolean fSessionOpen = false;
159
160 try
161 {
162 // this is pretty much what VBoxManage does to start a VM
163 String oMachine = "";
164 Boolean fOK = false;
165
166 oSession = _port.IWebsessionManager_getSessionObject(_oVbox);
167
168 // first assume we were given a UUID
169 try
170 {
171 oMachine = _port.IVirtualBox_getMachine(_oVbox, strVM);
172 fOK = true;
173 }
174 catch (Exception e)
175 {
176 }
177
178 if (!fOK)
179 {
180 try
181 {
182 // or try by name
183 oMachine = _port.IVirtualBox_findMachine(_oVbox, strVM);
184 fOK = true;
185 }
186 catch (Exception e)
187 {
188 }
189 }
190
191 if (!fOK)
192 {
193 System.out.println("Error: can't find VM \"" + strVM + "\"");
194 }
195 else
196 {
197 String uuid = _port.IMachine_getId(oMachine);
198 String sessionType = "gui";
199 String env = "DISPLAY=:0.0";
200 String oProgress = _port.IVirtualBox_openRemoteSession(_oVbox, oSession, uuid, sessionType, env);
201 fSessionOpen = true;
202
203 System.out.println("Session for VM " + uuid + " is opening...");
204 _port.IProgress_waitForCompletion(oProgress, 10000);
205
206 int rc = _port.IProgress_getResultCode(oProgress).intValue();
207 if (rc != 0)
208 {
209 System.out.println("Session failed!");
210 }
211 }
212 }
213 catch (Exception e)
214 {
215 e.printStackTrace();
216 }
217
218 if (fSessionOpen)
219 {
220 try
221 {
222 _port.ISession_close(oSession);
223 }
224 catch (Exception e)
225 {
226 }
227 }
228 }
229
230 public void cleanup()
231 {
232 try
233 {
234 if (_oVbox.length() > 0)
235 {
236 // log off
237 _port.IWebsessionManager_logoff(_oVbox);
238 _oVbox = null;
239 System.out.println("Logged off.");
240 }
241 }
242 catch (Exception e)
243 {
244 e.printStackTrace();
245 }
246 }
247
248 public static void printArgs()
249 {
250 System.out.println( "Usage: java clienttest <mode> ..." +
251 "\nwith <mode> being:" +
252 "\n show vms list installed virtual machines" +
253 "\n list hostinfo list host info" +
254 "\n startvm <vmname|uuid> start the given virtual machine");
255 }
256
257 public static void main(String[] args)
258 {
259 if (args.length < 1)
260 {
261 System.out.println("Error: Must specify at least one argument.");
262 printArgs();
263 }
264 else
265 {
266 clienttest c = new clienttest();
267 if (args[0].equals("show"))
268 {
269 if (args.length == 2)
270 {
271 if (args[1].equals("vms"))
272 c.showVMs();
273 else
274 System.out.println("Error: Unknown argument to \"show\": \"" + args[1] + "\".");
275 }
276 else
277 System.out.println("Error: Missing argument to \"show\" command");
278 }
279 else if (args[0].equals("list"))
280 {
281 if (args.length == 2)
282 {
283 if (args[1].equals("hostinfo"))
284 c.listHostInfo();
285 else
286 System.out.println("Error: Unknown argument to \"show\": \"" + args[1] + "\".");
287 }
288 else
289 System.out.println("Error: Missing argument to \"show\" command");
290 }
291 else if (args[0].equals("startvm"))
292 {
293 if (args.length == 2)
294 {
295 c.startVM(args[1]);
296 }
297 else
298 System.out.println("Error: Missing argument to \"startvm\" command");
299 }
300 else
301 System.out.println("Error: Unknown command: \"" + args[0] + "\".");
302
303 c.cleanup();
304 }
305 }
306}
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