VirtualBox

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

最後變更 在這個檔案從24910是 24910,由 vboxsync 提交於 15 年 前

Webservice: updated sample headers, add PHP sample

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

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