clienttest.java revision fa833d3a9203f26e4cd8c944223706f90b06d8a9
/*
* 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 Sun Microsystems, Inc.
*
* This file is part of VirtualBox Open Source Edition (OSE), as
* available from http://www.virtualbox.org. This file is free software;
* General Public License (GPL) as published by the Free Software
* Foundation, in version 2 as it comes in the "COPYING" file of the
* VirtualBox OSE distribution. VirtualBox OSE is distributed in the
* hope that it will be useful, but WITHOUT ANY WARRANTY of any kind.
*
* Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa
* Clara, CA 95054 USA or visit http://www.sun.com if you need
* additional information or have any questions.
*/
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();
}
}
}