VirtualBox

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

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

export webservices to OSE

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