clienttest.java revision c1ca21ba335f4d6a24c34cb6fa07fa8e5ac55cc7
/*
* Sample client for the VirtualBox webservice, written in Java.
* Best consumed in conjunction with the explanations in the VirtualBox
* User Manual, which describe in detail how to get this code running.
*
* Copyright (C) 2008-2009 Sun Microsystems, Inc.
*
* The following license applies to this file only:
*
* Permission is hereby granted, free of charge, to any person
* obtaining a copy of this software and associated documentation
* files (the "Software"), to deal in the Software without
* restriction, including without limitation the rights to use,
* sell copies of the Software, and to permit persons to whom the
* Software is furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be
* included in all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
* OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
* NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
* HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
* WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
* OTHER DEALINGS IN THE SOFTWARE.
*/
public class clienttest
{
private VboxService _service;
private VboxPortType _port;
public clienttest()
{
try
{
// instantiate the webservice in instance data; the classes
// VboxServiceLocator and VboxPortType have been created
// by the WSDL2Java helper that you should have run prior
// to compiling this example, as described in the User Manual.
_service = new VboxServiceLocator();
// From now on, we can call any method in the webservice by
// prefixing it with "port."
// First step is always to log on to the webservice. This
// returns a managed object reference to the webservice's
// global instance of IVirtualBox, which in turn contains
// the most important methods provided by the Main API.
// Call IVirtualBox::getVersion and print out the result
}
catch (Exception e)
{
e.printStackTrace();
}
}
public void showVMs()
{
try
{
// Call IVirtualBox::getMachines, which yields an array
// of managed object references to all machines which have
// been registered:
// Walk through this array and, for each machine, call
// IMachine::getName (accessor method to the "name" attribute)
{
// release managed object reference
}
}
catch (Exception e)
{
e.printStackTrace();
}
}
public void listHostInfo()
{
try
{
aobj);
// String astrMetricNames[] = { "*" };
// String aObjects[];
// String aRetNames[];
// int aRetIndices[];
// int aRetLengths[];
// int aRetData[];
// int rc = _port.ICollector_queryMetricsData(oCollector,
// aObjects,
// aRetNames,
// aRetObjects,
// aRetIndices,
// aRetLengths,
// aRetData);
//
/*
Bstr metricNames[] = { L"*" };
com::SafeArray<BSTR> metrics (1);
metricNames[0].cloneTo (&metrics [0]);
com::SafeArray<BSTR> retNames;
com::SafeIfaceArray<IUnknown> retObjects;
com::SafeArray<ULONG> retIndices;
com::SafeArray<ULONG> retLengths;
com::SafeArray<LONG> retData;
CHECK_ERROR (collector, QueryMetricsData(ComSafeArrayAsInParam(metrics),
ComSafeArrayInArg(objects),
ComSafeArrayAsOutParam(retNames),
ComSafeArrayAsOutParam(retObjects),
ComSafeArrayAsOutParam(retIndices),
ComSafeArrayAsOutParam(retLengths),
ComSafeArrayAsOutParam(retData)) );
*/
}
catch (Exception e)
{
e.printStackTrace();
}
}
{
Boolean fSessionOpen = false;
try
{
// this is pretty much what VBoxManage does to start a VM
// first assume we were given a UUID
try
{
fOK = true;
}
catch (Exception e)
{
}
if (!fOK)
{
try
{
// or try by name
fOK = true;
}
catch (Exception e)
{
}
}
if (!fOK)
{
}
else
{
fSessionOpen = true;
if (rc != 0)
{
}
}
}
catch (Exception e)
{
e.printStackTrace();
}
if (fSessionOpen)
{
try
{
}
catch (Exception e)
{
}
}
}
public void cleanup()
{
try
{
{
// log off
}
}
catch (Exception e)
{
e.printStackTrace();
}
}
public static void printArgs()
{
"\nwith <mode> being:" +
"\n show vms list installed virtual machines" +
"\n list hostinfo list host info" +
"\n startvm <vmname|uuid> start the given virtual machine");
}
{
{
printArgs();
}
else
{
clienttest c = new clienttest();
{
{
c.showVMs();
else
}
else
}
{
{
c.listHostInfo();
else
}
else
}
{
{
}
else
}
else
c.cleanup();
}
}
}