4ef2057dbc4d1b09cc3533ea444f77cf933c91f3vboxsync * Sample client for the VirtualBox web service, written in Java
4ef2057dbc4d1b09cc3533ea444f77cf933c91f3vboxsync * (raw web service variant).
3081cf472b2203dc98508778f2a390ba07a99e1avboxsync * Run the VirtualBox web service server first; see the VirtualBox
4ef2057dbc4d1b09cc3533ea444f77cf933c91f3vboxsync * SDK reference for details.
5c65eaa08f2ec993a19c9bef6e5463918e40e0ebvboxsync * Copyright (C) 2008-2010 Oracle Corporation
c1ca21ba335f4d6a24c34cb6fa07fa8e5ac55cc7vboxsync * The following license applies to this file only:
c1ca21ba335f4d6a24c34cb6fa07fa8e5ac55cc7vboxsync * Permission is hereby granted, free of charge, to any person
c1ca21ba335f4d6a24c34cb6fa07fa8e5ac55cc7vboxsync * obtaining a copy of this software and associated documentation
c1ca21ba335f4d6a24c34cb6fa07fa8e5ac55cc7vboxsync * files (the "Software"), to deal in the Software without
c1ca21ba335f4d6a24c34cb6fa07fa8e5ac55cc7vboxsync * restriction, including without limitation the rights to use,
c1ca21ba335f4d6a24c34cb6fa07fa8e5ac55cc7vboxsync * copy, modify, merge, publish, distribute, sublicense, and/or
c1ca21ba335f4d6a24c34cb6fa07fa8e5ac55cc7vboxsync * sell copies of the Software, and to permit persons to whom the
c1ca21ba335f4d6a24c34cb6fa07fa8e5ac55cc7vboxsync * Software is furnished to do so, subject to the following conditions:
c1ca21ba335f4d6a24c34cb6fa07fa8e5ac55cc7vboxsync * The above copyright notice and this permission notice shall be
c1ca21ba335f4d6a24c34cb6fa07fa8e5ac55cc7vboxsync * included in all copies or substantial portions of the Software.
c1ca21ba335f4d6a24c34cb6fa07fa8e5ac55cc7vboxsync * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
c1ca21ba335f4d6a24c34cb6fa07fa8e5ac55cc7vboxsync * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
c1ca21ba335f4d6a24c34cb6fa07fa8e5ac55cc7vboxsync * OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
c1ca21ba335f4d6a24c34cb6fa07fa8e5ac55cc7vboxsync * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
c1ca21ba335f4d6a24c34cb6fa07fa8e5ac55cc7vboxsync * HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
c1ca21ba335f4d6a24c34cb6fa07fa8e5ac55cc7vboxsync * WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
c1ca21ba335f4d6a24c34cb6fa07fa8e5ac55cc7vboxsync * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
c1ca21ba335f4d6a24c34cb6fa07fa8e5ac55cc7vboxsync * OTHER DEALINGS IN THE SOFTWARE.
db3dbd0ed7eb69f804a8921fa23a1267ea01f46evboxsyncimport org.virtualbox.www.Service.VboxServiceLocator;
db3dbd0ed7eb69f804a8921fa23a1267ea01f46evboxsync // instantiate the webservice in instance data; the classes
db3dbd0ed7eb69f804a8921fa23a1267ea01f46evboxsync // VboxServiceLocator and VboxPortType have been created
db3dbd0ed7eb69f804a8921fa23a1267ea01f46evboxsync // by the WSDL2Java helper that you should have run prior
db3dbd0ed7eb69f804a8921fa23a1267ea01f46evboxsync // to compiling this example, as described in the User Manual.
db3dbd0ed7eb69f804a8921fa23a1267ea01f46evboxsync // From now on, we can call any method in the webservice by
db3dbd0ed7eb69f804a8921fa23a1267ea01f46evboxsync // prefixing it with "port."
db3dbd0ed7eb69f804a8921fa23a1267ea01f46evboxsync // First step is always to log on to the webservice. This
db3dbd0ed7eb69f804a8921fa23a1267ea01f46evboxsync // returns a managed object reference to the webservice's
db3dbd0ed7eb69f804a8921fa23a1267ea01f46evboxsync // global instance of IVirtualBox, which in turn contains
db3dbd0ed7eb69f804a8921fa23a1267ea01f46evboxsync // the most important methods provided by the Main API.
db3dbd0ed7eb69f804a8921fa23a1267ea01f46evboxsync // Call IVirtualBox::getVersion and print out the result
db3dbd0ed7eb69f804a8921fa23a1267ea01f46evboxsync String version = _port.IVirtualBox_getVersion(_oVbox);
db3dbd0ed7eb69f804a8921fa23a1267ea01f46evboxsync System.out.println("Initialized connection to VirtualBox version " + version);
db3dbd0ed7eb69f804a8921fa23a1267ea01f46evboxsync // Call IVirtualBox::getMachines, which yields an array
db3dbd0ed7eb69f804a8921fa23a1267ea01f46evboxsync // of managed object references to all machines which have
db3dbd0ed7eb69f804a8921fa23a1267ea01f46evboxsync // been registered:
db3dbd0ed7eb69f804a8921fa23a1267ea01f46evboxsync String[] aMachines = _port.IVirtualBox_getMachines2(_oVbox);
db3dbd0ed7eb69f804a8921fa23a1267ea01f46evboxsync // Walk through this array and, for each machine, call
db3dbd0ed7eb69f804a8921fa23a1267ea01f46evboxsync // IMachine::getName (accessor method to the "name" attribute)
db3dbd0ed7eb69f804a8921fa23a1267ea01f46evboxsync String machinename = _port.IMachine_getName(oMachine);
db3dbd0ed7eb69f804a8921fa23a1267ea01f46evboxsync System.out.println("Machine " + i + ": " + oMachine + " - " + machinename);
db3dbd0ed7eb69f804a8921fa23a1267ea01f46evboxsync // release managed object reference
db3dbd0ed7eb69f804a8921fa23a1267ea01f46evboxsync org.apache.axis.types.UnsignedInt uProcCount = _port.IHost_getProcessorCount(oHost);
db3dbd0ed7eb69f804a8921fa23a1267ea01f46evboxsync System.out.println("Processor count: " + uProcCount);
db3dbd0ed7eb69f804a8921fa23a1267ea01f46evboxsync String oCollector = _port.IVirtualBox_getPerformanceCollector(_oVbox);
db3dbd0ed7eb69f804a8921fa23a1267ea01f46evboxsync aMetrics = _port.IPerformanceCollector_getMetrics(oCollector,
db3dbd0ed7eb69f804a8921fa23a1267ea01f46evboxsync// String astrMetricNames[] = { "*" };
db3dbd0ed7eb69f804a8921fa23a1267ea01f46evboxsync// String aObjects[];
db3dbd0ed7eb69f804a8921fa23a1267ea01f46evboxsync// String aRetNames[];
db3dbd0ed7eb69f804a8921fa23a1267ea01f46evboxsync// int aRetIndices[];
db3dbd0ed7eb69f804a8921fa23a1267ea01f46evboxsync// int aRetLengths[];
db3dbd0ed7eb69f804a8921fa23a1267ea01f46evboxsync// int aRetData[];
db3dbd0ed7eb69f804a8921fa23a1267ea01f46evboxsync// int rc = _port.ICollector_queryMetricsData(oCollector,
db3dbd0ed7eb69f804a8921fa23a1267ea01f46evboxsync// aObjects,
db3dbd0ed7eb69f804a8921fa23a1267ea01f46evboxsync// aRetNames,
db3dbd0ed7eb69f804a8921fa23a1267ea01f46evboxsync// aRetObjects,
db3dbd0ed7eb69f804a8921fa23a1267ea01f46evboxsync// aRetIndices,
db3dbd0ed7eb69f804a8921fa23a1267ea01f46evboxsync// aRetLengths,
db3dbd0ed7eb69f804a8921fa23a1267ea01f46evboxsync// aRetData);
db3dbd0ed7eb69f804a8921fa23a1267ea01f46evboxsync Bstr metricNames[] = { L"*" };
db3dbd0ed7eb69f804a8921fa23a1267ea01f46evboxsync com::SafeArray<BSTR> metrics (1);
db3dbd0ed7eb69f804a8921fa23a1267ea01f46evboxsync metricNames[0].cloneTo (&metrics [0]);
db3dbd0ed7eb69f804a8921fa23a1267ea01f46evboxsync com::SafeArray<BSTR> retNames;
db3dbd0ed7eb69f804a8921fa23a1267ea01f46evboxsync com::SafeIfaceArray<IUnknown> retObjects;
db3dbd0ed7eb69f804a8921fa23a1267ea01f46evboxsync com::SafeArray<ULONG> retIndices;
db3dbd0ed7eb69f804a8921fa23a1267ea01f46evboxsync com::SafeArray<ULONG> retLengths;
db3dbd0ed7eb69f804a8921fa23a1267ea01f46evboxsync com::SafeArray<LONG> retData;
db3dbd0ed7eb69f804a8921fa23a1267ea01f46evboxsync CHECK_ERROR (collector, QueryMetricsData(ComSafeArrayAsInParam(metrics),
db3dbd0ed7eb69f804a8921fa23a1267ea01f46evboxsync ComSafeArrayInArg(objects),
db3dbd0ed7eb69f804a8921fa23a1267ea01f46evboxsync ComSafeArrayAsOutParam(retNames),
db3dbd0ed7eb69f804a8921fa23a1267ea01f46evboxsync ComSafeArrayAsOutParam(retObjects),
db3dbd0ed7eb69f804a8921fa23a1267ea01f46evboxsync ComSafeArrayAsOutParam(retIndices),
db3dbd0ed7eb69f804a8921fa23a1267ea01f46evboxsync ComSafeArrayAsOutParam(retLengths),
db3dbd0ed7eb69f804a8921fa23a1267ea01f46evboxsync ComSafeArrayAsOutParam(retData)) );
db3dbd0ed7eb69f804a8921fa23a1267ea01f46evboxsync // this is pretty much what VBoxManage does to start a VM
db3dbd0ed7eb69f804a8921fa23a1267ea01f46evboxsync oSession = _port.IWebsessionManager_getSessionObject(_oVbox);
db3dbd0ed7eb69f804a8921fa23a1267ea01f46evboxsync // first assume we were given a UUID
db3dbd0ed7eb69f804a8921fa23a1267ea01f46evboxsync oMachine = _port.IVirtualBox_getMachine(_oVbox, strVM);
db3dbd0ed7eb69f804a8921fa23a1267ea01f46evboxsync // or try by name
db3dbd0ed7eb69f804a8921fa23a1267ea01f46evboxsync oMachine = _port.IVirtualBox_findMachine(_oVbox, strVM);
db3dbd0ed7eb69f804a8921fa23a1267ea01f46evboxsync System.out.println("Error: can't find VM \"" + strVM + "\"");
db3dbd0ed7eb69f804a8921fa23a1267ea01f46evboxsync String oProgress = _port.IVirtualBox_openRemoteSession(_oVbox, oSession, uuid, sessionType, env);
db3dbd0ed7eb69f804a8921fa23a1267ea01f46evboxsync System.out.println("Session for VM " + uuid + " is opening...");
db3dbd0ed7eb69f804a8921fa23a1267ea01f46evboxsync _port.IProgress_waitForCompletion(oProgress, 10000);
db3dbd0ed7eb69f804a8921fa23a1267ea01f46evboxsync int rc = _port.IProgress_getResultCode(oProgress).intValue();
db3dbd0ed7eb69f804a8921fa23a1267ea01f46evboxsync System.out.println( "Usage: java clienttest <mode> ..." +
db3dbd0ed7eb69f804a8921fa23a1267ea01f46evboxsync "\nwith <mode> being:" +
db3dbd0ed7eb69f804a8921fa23a1267ea01f46evboxsync "\n show vms list installed virtual machines" +
db3dbd0ed7eb69f804a8921fa23a1267ea01f46evboxsync "\n list hostinfo list host info" +
db3dbd0ed7eb69f804a8921fa23a1267ea01f46evboxsync "\n startvm <vmname|uuid> start the given virtual machine");
db3dbd0ed7eb69f804a8921fa23a1267ea01f46evboxsync System.out.println("Error: Must specify at least one argument.");
db3dbd0ed7eb69f804a8921fa23a1267ea01f46evboxsync System.out.println("Error: Unknown argument to \"show\": \"" + args[1] + "\".");
db3dbd0ed7eb69f804a8921fa23a1267ea01f46evboxsync System.out.println("Error: Missing argument to \"show\" command");
db3dbd0ed7eb69f804a8921fa23a1267ea01f46evboxsync System.out.println("Error: Unknown argument to \"show\": \"" + args[1] + "\".");
db3dbd0ed7eb69f804a8921fa23a1267ea01f46evboxsync System.out.println("Error: Missing argument to \"show\" command");
db3dbd0ed7eb69f804a8921fa23a1267ea01f46evboxsync System.out.println("Error: Missing argument to \"startvm\" command");