VirtualBox

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

最後變更 在這個檔案是 106061,由 vboxsync 提交於 4 月 前

Copyright year updates by scm.

  • 屬性 svn:eol-style 設為 native
  • 屬性 svn:keywords 設為 Id Revision
檔案大小: 10.5 KB
 
1/* $Id: clienttest.java 106061 2024-09-16 14:03:52Z vboxsync $ */
2/*!file
3 * Sample client for the VirtualBox web service, written in Java (raw web service variant).
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-2024 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
36import org.virtualbox.www.Service.VboxService;
37import org.virtualbox.www.Service.VboxServiceLocator;
38import org.virtualbox.www.VboxPortType;
39
40public class clienttest
41{
42 private VboxService _service;
43 private VboxPortType _port;
44 private String _oVbox;
45
46 public clienttest()
47 {
48 try
49 {
50 // instantiate the webservice in instance data; the classes
51 // VboxServiceLocator and VboxPortType have been created
52 // by the WSDL2Java helper that you should have run prior
53 // to compiling this example, as described in the User Manual.
54 _service = new VboxServiceLocator();
55 _port = _service.getvboxServicePort();
56
57 // From now on, we can call any method in the webservice by
58 // prefixing it with "port."
59
60 // First step is always to log on to the webservice. This
61 // returns a managed object reference to the webservice's
62 // global instance of IVirtualBox, which in turn contains
63 // the most important methods provided by the Main API.
64 _oVbox = _port.IWebsessionManager_logon("", "");
65
66 // Call IVirtualBox::getVersion and print out the result
67 String version = _port.IVirtualBox_getVersion(_oVbox);
68 System.out.println("Initialized connection to VirtualBox version " + version);
69 }
70 catch (Exception e)
71 {
72 e.printStackTrace();
73 }
74 }
75
76 public void showVMs()
77 {
78 try
79 {
80 // Call IVirtualBox::getMachines, which yields an array
81 // of managed object references to all machines which have
82 // been registered:
83 String[] aMachines = _port.IVirtualBox_getMachines2(_oVbox);
84 // Walk through this array and, for each machine, call
85 // IMachine::getName (accessor method to the "name" attribute)
86 for (int i = 0; i < aMachines.length; i++)
87 {
88 String oMachine = aMachines[i];
89 String machinename = _port.IMachine_getName(oMachine);
90 System.out.println("Machine " + i + ": " + oMachine + " - " + machinename);
91
92 // release managed object reference
93 _port.IManagedObjectRef_release(oMachine);
94 }
95 }
96 catch (Exception e)
97 {
98 e.printStackTrace();
99 }
100 }
101
102 public void listHostInfo()
103 {
104 try
105 {
106 String oHost = _port.IVirtualBox_getHost(_oVbox);
107
108 org.apache.axis.types.UnsignedInt uProcCount = _port.IHost_getProcessorCount(oHost);
109 System.out.println("Processor count: " + uProcCount);
110
111 String oCollector = _port.IVirtualBox_getPerformanceCollector(_oVbox);
112
113 String aobj[] = {oHost};
114 String astrMetrics[] = {"*"};
115 String aMetrics[] = {};
116 aMetrics = _port.IPerformanceCollector_getMetrics(oCollector,
117 astrMetrics,
118 aobj);
119
120// String astrMetricNames[] = { "*" };
121// String aObjects[];
122// String aRetNames[];
123// int aRetIndices[];
124// int aRetLengths[];
125// int aRetData[];
126// int rc = _port.ICollector_queryMetricsData(oCollector,
127// aObjects,
128// aRetNames,
129// aRetObjects,
130// aRetIndices,
131// aRetLengths,
132// aRetData);
133//
134/*
135 Bstr metricNames[] = { L"*" };
136 com::SafeArray<BSTR> metrics (1);
137 metricNames[0].cloneTo (&metrics [0]);
138 com::SafeArray<BSTR> retNames;
139 com::SafeIfaceArray<IUnknown> retObjects;
140 com::SafeArray<ULONG> retIndices;
141 com::SafeArray<ULONG> retLengths;
142 com::SafeArray<LONG> retData;
143 CHECK_ERROR (collector, QueryMetricsData(ComSafeArrayAsInParam(metrics),
144 ComSafeArrayInArg(objects),
145 ComSafeArrayAsOutParam(retNames),
146 ComSafeArrayAsOutParam(retObjects),
147 ComSafeArrayAsOutParam(retIndices),
148 ComSafeArrayAsOutParam(retLengths),
149 ComSafeArrayAsOutParam(retData)) );
150*/
151
152 }
153 catch (Exception e)
154 {
155 e.printStackTrace();
156 }
157 }
158
159 public void startVM(String strVM)
160 {
161 String oSession = "";
162 Boolean fSessionOpen = false;
163
164 try
165 {
166 // this is pretty much what VBoxManage does to start a VM
167 String oMachine = "";
168 Boolean fOK = false;
169
170 oSession = _port.IWebsessionManager_getSessionObject(_oVbox);
171
172 // first assume we were given a UUID
173 try
174 {
175 oMachine = _port.IVirtualBox_getMachine(_oVbox, strVM);
176 fOK = true;
177 }
178 catch (Exception e)
179 {
180 }
181
182 if (!fOK)
183 {
184 try
185 {
186 // or try by name
187 oMachine = _port.IVirtualBox_findMachine(_oVbox, strVM);
188 fOK = true;
189 }
190 catch (Exception e)
191 {
192 }
193 }
194
195 if (!fOK)
196 {
197 System.out.println("Error: can't find VM \"" + strVM + "\"");
198 }
199 else
200 {
201 String uuid = _port.IMachine_getId(oMachine);
202 String sessionType = "gui";
203 String env = "DISPLAY=:0.0";
204 String oProgress = _port.IVirtualBox_openRemoteSession(_oVbox, oSession, uuid, sessionType, env);
205 fSessionOpen = true;
206
207 System.out.println("Session for VM " + uuid + " is opening...");
208 _port.IProgress_waitForCompletion(oProgress, 10000);
209
210 int rc = _port.IProgress_getResultCode(oProgress).intValue();
211 if (rc != 0)
212 {
213 System.out.println("Session failed!");
214 }
215 }
216 }
217 catch (Exception e)
218 {
219 e.printStackTrace();
220 }
221
222 if (fSessionOpen)
223 {
224 try
225 {
226 _port.ISession_close(oSession);
227 }
228 catch (Exception e)
229 {
230 }
231 }
232 }
233
234 public void cleanup()
235 {
236 try
237 {
238 if (_oVbox.length() > 0)
239 {
240 // log off
241 _port.IWebsessionManager_logoff(_oVbox);
242 _oVbox = null;
243 System.out.println("Logged off.");
244 }
245 }
246 catch (Exception e)
247 {
248 e.printStackTrace();
249 }
250 }
251
252 public static void printArgs()
253 {
254 System.out.println( "Usage: java clienttest <mode> ..." +
255 "\nwith <mode> being:" +
256 "\n show vms list installed virtual machines" +
257 "\n list hostinfo list host info" +
258 "\n startvm <vmname|uuid> start the given virtual machine");
259 }
260
261 public static void main(String[] args)
262 {
263 if (args.length < 1)
264 {
265 System.out.println("Error: Must specify at least one argument.");
266 printArgs();
267 }
268 else
269 {
270 clienttest c = new clienttest();
271 if (args[0].equals("show"))
272 {
273 if (args.length == 2)
274 {
275 if (args[1].equals("vms"))
276 c.showVMs();
277 else
278 System.out.println("Error: Unknown argument to \"show\": \"" + args[1] + "\".");
279 }
280 else
281 System.out.println("Error: Missing argument to \"show\" command");
282 }
283 else if (args[0].equals("list"))
284 {
285 if (args.length == 2)
286 {
287 if (args[1].equals("hostinfo"))
288 c.listHostInfo();
289 else
290 System.out.println("Error: Unknown argument to \"show\": \"" + args[1] + "\".");
291 }
292 else
293 System.out.println("Error: Missing argument to \"show\" command");
294 }
295 else if (args[0].equals("startvm"))
296 {
297 if (args.length == 2)
298 {
299 c.startVM(args[1]);
300 }
301 else
302 System.out.println("Error: Missing argument to \"startvm\" command");
303 }
304 else
305 System.out.println("Error: Unknown command: \"" + args[0] + "\".");
306
307 c.cleanup();
308 }
309 }
310}
注意: 瀏覽 TracBrowser 來幫助您使用儲存庫瀏覽器

© 2024 Oracle Support Privacy / Do Not Sell My Info Terms of Use Trademark Policy Automated Access Etiquette