diff options
author | Daniel Baumann <daniel.baumann@progress-linux.org> | 2024-04-07 16:49:04 +0000 |
---|---|---|
committer | Daniel Baumann <daniel.baumann@progress-linux.org> | 2024-04-07 16:49:04 +0000 |
commit | 16f504a9dca3fe3b70568f67b7d41241ae485288 (patch) | |
tree | c60f36ada0496ba928b7161059ba5ab1ab224f9d /src/VBox/Main/webservice/samples/java | |
parent | Initial commit. (diff) | |
download | virtualbox-upstream.tar.xz virtualbox-upstream.zip |
Adding upstream version 7.0.6-dfsg.upstream/7.0.6-dfsgupstream
Signed-off-by: Daniel Baumann <daniel.baumann@progress-linux.org>
Diffstat (limited to 'src/VBox/Main/webservice/samples/java')
5 files changed, 1035 insertions, 0 deletions
diff --git a/src/VBox/Main/webservice/samples/java/axis/clienttest.java b/src/VBox/Main/webservice/samples/java/axis/clienttest.java new file mode 100644 index 00000000..31043d3e --- /dev/null +++ b/src/VBox/Main/webservice/samples/java/axis/clienttest.java @@ -0,0 +1,310 @@ +/* $Id: clienttest.java $ */ +/*!file + * Sample client for the VirtualBox web service, written in Java (raw web service variant). + * + * Run the VirtualBox web service server first; see the VirtualBox + * SDK reference for details. + * + * The following license applies to this file only: + */ + +/* + * Copyright (C) 2008-2022 Oracle and/or its affiliates. + * + * 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, + * copy, modify, merge, publish, distribute, sublicense, and/or 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. + */ + +import org.virtualbox.www.Service.VboxService; +import org.virtualbox.www.Service.VboxServiceLocator; +import org.virtualbox.www.VboxPortType; + +public class clienttest +{ + private VboxService _service; + private VboxPortType _port; + private String _oVbox; + + 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(); + _port = _service.getvboxServicePort(); + + // 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. + _oVbox = _port.IWebsessionManager_logon("", ""); + + // Call IVirtualBox::getVersion and print out the result + String version = _port.IVirtualBox_getVersion(_oVbox); + System.out.println("Initialized connection to VirtualBox version " + version); + } + 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: + String[] aMachines = _port.IVirtualBox_getMachines2(_oVbox); + // Walk through this array and, for each machine, call + // IMachine::getName (accessor method to the "name" attribute) + for (int i = 0; i < aMachines.length; i++) + { + String oMachine = aMachines[i]; + String machinename = _port.IMachine_getName(oMachine); + System.out.println("Machine " + i + ": " + oMachine + " - " + machinename); + + // release managed object reference + _port.IManagedObjectRef_release(oMachine); + } + } + catch (Exception e) + { + e.printStackTrace(); + } + } + + public void listHostInfo() + { + try + { + String oHost = _port.IVirtualBox_getHost(_oVbox); + + org.apache.axis.types.UnsignedInt uProcCount = _port.IHost_getProcessorCount(oHost); + System.out.println("Processor count: " + uProcCount); + + String oCollector = _port.IVirtualBox_getPerformanceCollector(_oVbox); + + String aobj[] = {oHost}; + String astrMetrics[] = {"*"}; + String aMetrics[] = {}; + aMetrics = _port.IPerformanceCollector_getMetrics(oCollector, + astrMetrics, + 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(); + } + } + + public void startVM(String strVM) + { + String oSession = ""; + Boolean fSessionOpen = false; + + try + { + // this is pretty much what VBoxManage does to start a VM + String oMachine = ""; + Boolean fOK = false; + + oSession = _port.IWebsessionManager_getSessionObject(_oVbox); + + // first assume we were given a UUID + try + { + oMachine = _port.IVirtualBox_getMachine(_oVbox, strVM); + fOK = true; + } + catch (Exception e) + { + } + + if (!fOK) + { + try + { + // or try by name + oMachine = _port.IVirtualBox_findMachine(_oVbox, strVM); + fOK = true; + } + catch (Exception e) + { + } + } + + if (!fOK) + { + System.out.println("Error: can't find VM \"" + strVM + "\""); + } + else + { + String uuid = _port.IMachine_getId(oMachine); + String sessionType = "gui"; + String env = "DISPLAY=:0.0"; + String oProgress = _port.IVirtualBox_openRemoteSession(_oVbox, oSession, uuid, sessionType, env); + fSessionOpen = true; + + System.out.println("Session for VM " + uuid + " is opening..."); + _port.IProgress_waitForCompletion(oProgress, 10000); + + int rc = _port.IProgress_getResultCode(oProgress).intValue(); + if (rc != 0) + { + System.out.println("Session failed!"); + } + } + } + catch (Exception e) + { + e.printStackTrace(); + } + + if (fSessionOpen) + { + try + { + _port.ISession_close(oSession); + } + catch (Exception e) + { + } + } + } + + public void cleanup() + { + try + { + if (_oVbox.length() > 0) + { + // log off + _port.IWebsessionManager_logoff(_oVbox); + _oVbox = null; + System.out.println("Logged off."); + } + } + catch (Exception e) + { + e.printStackTrace(); + } + } + + public static void printArgs() + { + System.out.println( "Usage: java clienttest <mode> ..." + + "\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"); + } + + public static void main(String[] args) + { + if (args.length < 1) + { + System.out.println("Error: Must specify at least one argument."); + printArgs(); + } + else + { + clienttest c = new clienttest(); + if (args[0].equals("show")) + { + if (args.length == 2) + { + if (args[1].equals("vms")) + c.showVMs(); + else + System.out.println("Error: Unknown argument to \"show\": \"" + args[1] + "\"."); + } + else + System.out.println("Error: Missing argument to \"show\" command"); + } + else if (args[0].equals("list")) + { + if (args.length == 2) + { + if (args[1].equals("hostinfo")) + c.listHostInfo(); + else + System.out.println("Error: Unknown argument to \"show\": \"" + args[1] + "\"."); + } + else + System.out.println("Error: Missing argument to \"show\" command"); + } + else if (args[0].equals("startvm")) + { + if (args.length == 2) + { + c.startVM(args[1]); + } + else + System.out.println("Error: Missing argument to \"startvm\" command"); + } + else + System.out.println("Error: Unknown command: \"" + args[0] + "\"."); + + c.cleanup(); + } + } +} diff --git a/src/VBox/Main/webservice/samples/java/jax-ws/Makefile b/src/VBox/Main/webservice/samples/java/jax-ws/Makefile new file mode 100644 index 00000000..7bd53a61 --- /dev/null +++ b/src/VBox/Main/webservice/samples/java/jax-ws/Makefile @@ -0,0 +1,83 @@ +# $Id: Makefile $ +## @file +# Makefile for java samples. +# + + +# +# Copyright (C) 2008-2022 Oracle and/or its affiliates. +# +# 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, +# copy, modify, merge, publish, distribute, sublicense, and/or 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. +# + +JAVA16=java +JAVA15=/usr/lib/jvm/java-1.5.0-sun/bin/java +JAVAC = javac +JAVAC15 = javac -target 1.5 +JAVAC16 = javac -target 1.6 +MKDIR = mkdir +RM = rm + +DEST16 = ./gen16 +DEST15 = ./gen15 + +VBOXWS15 = ../lib/vboxws_java15.jar +VBOXWS16 = ../lib/vboxws_java16.jar + +JAXWS=./jaxws-ri +JAXWSLIBS=$(JAXWS)/lib/jaxws-api.jar:$(JAXWS)/lib/jaxb-api.jar:$(JAXWS)/lib/jsr181-api.jar:$(JAXWS)/lib/jaxws-rt.jar + +all: run16 + +metric: metric16 + +$(DEST16)/clienttest.class: clienttest.java + $(MKDIR) -p $(DEST16) + $(JAVAC16) -d $(DEST16) -cp $(VBOXWS16) $< + +$(DEST15)/clienttest.class: clienttest.java + $(MKDIR) -p $(DEST15) + $(JAVAC15) -d $(DEST15) -cp $(VBOXWS15):$(JAXWSLIBS) $< + +run16: $(DEST16)/clienttest.class + $(JAVA16) -cp $(VBOXWS16):$(DEST16) clienttest show vms + +run15: $(DEST15)/clienttest.class + $(JAVA15) -cp $(VBOXWS15):$(JAXWSLIBS):$(DEST15) clienttest show vms + +$(DEST16)/metrictest.class: metrictest.java + $(MKDIR) -p $(DEST16) + $(JAVAC16) -d $(DEST16) -cp $(VBOXWS16) $< + +$(DEST15)/metrictest.class: metrictest.java + $(MKDIR) -p $(DEST15) + $(JAVAC15) -d $(DEST15) -cp $(VBOXWS15):$(JAXWSLIBS) $< + +metric16: $(DEST16)/metrictest.class + -$(JAVA16) -cp $(VBOXWS16):$(DEST16) metrictest + +metric15: $(DEST15)/metrictest.class + -$(JAVA15) -cp $(VBOXWS15):$(JAXWSLIBS):$(DEST15) metrictest + +clean: + $(RM) -rf $(DEST15) $(DEST16) + diff --git a/src/VBox/Main/webservice/samples/java/jax-ws/Makefile.glue b/src/VBox/Main/webservice/samples/java/jax-ws/Makefile.glue new file mode 100644 index 00000000..295ea77c --- /dev/null +++ b/src/VBox/Main/webservice/samples/java/jax-ws/Makefile.glue @@ -0,0 +1,72 @@ +# $Id: Makefile.glue $ +## @file +# Makefile for java samples. +# + +# +# Copyright (C) 2008-2022 Oracle and/or its affiliates. +# +# 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, +# copy, modify, merge, publish, distribute, sublicense, and/or 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. +# + +JAXWS=./jaxws-ri +JAXWSLIBS=$(JAXWS)/lib/jaxws-api.jar:$(JAXWS)/lib/jaxb-api.jar:$(JAXWS)/lib/jsr181-api.jar:$(JAXWS)/lib/jaxws-rt.jar + + +JAVA16=java +JAVA15=/usr/lib/jvm/java-1.5.0-sun/bin/java +JAVAC15 = javac -target 1.5 +JAVAC16 = javac -target 1.6 +WSIMPORT15 = $(JAVA15) -cp $(JAXWS)/lib/jaxws-tools.jar com.sun.tools.ws.WsImport +WSIMPORT16 = wsimport +JAR = jar +CP = cp +MKDIR = mkdir +RM = rm + +DEST16 = ./gen16 +DEST15 = ./gen15 + +VBOXWS15 = ../lib/vboxws_java15.jar +VBOXWS16 = ../lib/vboxws_java16.jar + +all: $(VBOXWS15) $(VBOXWS16) + +$(VBOXWS15): ../../../vboxwebService.wsdl ../../../vboxweb.wsdl *.java + $(RM) -rf $(DEST15) + $(MKDIR) -p $(DEST15) + $(WSIMPORT15) -d $(DEST15) $< + $(JAVAC15) -cp $(DEST15) *.java -d $(DEST15) + $(CP) ../../../vboxwebService.wsdl ../../../vboxweb.wsdl $(DEST15) + $(JAR) cvf $(VBOXWS15) -C $(DEST15) . > /dev/null + +$(VBOXWS16): ../../../vboxwebService.wsdl ../../../vboxweb.wsdl *.java + $(RM) -rf $(DEST16) + $(MKDIR) -p $(DEST16) + $(WSIMPORT16) -d $(DEST16) $< + $(JAVAC16) -cp $(DEST16) *.java -d $(DEST16) + $(CP) ../../../vboxwebService.wsdl ../../../vboxweb.wsdl $(DEST16) + $(JAR) cvf $(VBOXWS16) -C $(DEST16) . > /dev/null + +clean: + $(RM) -rf $(DEST) + diff --git a/src/VBox/Main/webservice/samples/java/jax-ws/clienttest.java b/src/VBox/Main/webservice/samples/java/jax-ws/clienttest.java new file mode 100644 index 00000000..02d67ab0 --- /dev/null +++ b/src/VBox/Main/webservice/samples/java/jax-ws/clienttest.java @@ -0,0 +1,339 @@ +/* $Id: clienttest.java $ */ +/*!file + * Sample client for the VirtualBox web service, written in Java (object-oriented bindings). + * + * Run the VirtualBox web service server first; see the VirtualBox + * SDK reference for details. + * + * The following license applies to this file only: + */ + +/* + * Copyright (C) 2008-2022 Oracle and/or its affiliates. + * + * 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, + * copy, modify, merge, publish, distribute, sublicense, and/or 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. + */ + +/* Somewhat ugly way to support versioning */ +import com.sun.xml.ws.commons.virtualbox{VBOX_API_SUFFIX}.*; + +import java.util.*; +import javax.xml.ws.Holder; + +public class clienttest +{ + IWebsessionManager mgr; + IVirtualBox vbox; + + public clienttest() + { + mgr = new IWebsessionManager("http://localhost:18083/"); + vbox = mgr.logon("test", "test"); + System.out.println("Initialized connection to VirtualBox version " + vbox.getVersion()); + } + + public void disconnect() + { + mgr.disconnect(vbox); + } + + class Desktop + { + String name; + String uuid; + + Desktop(int n) + { + name = "Mach"+n; + uuid = UUID.randomUUID().toString(); + } + String getName() + { + return name; + } + String getId() + { + return uuid; + } + } + + public void test() + { + for (int i=0; i<100; i++) + { + String baseFolder = + vbox.getSystemProperties().getDefaultMachineFolder(); + Desktop desktop = new Desktop(i); + IMachine machine = vbox.createMachine(baseFolder, + "linux", + desktop.getName(), + desktop.getId(), + true); + machine.saveSettings(); + mgr.cleanupUnused(); + } + } + + public void test2() + { + ISession session = mgr.getSessionObject(vbox); + String id = "bc8b6219-2775-42c4-f1b2-b48b3c177294"; + vbox.openSession(session, id); + IMachine mach = session.getMachine(); + IBIOSSettings bios = mach.getBIOSSettings(); + bios.setIOAPICEnabled(true); + mach.saveSettings(); + session.close(); + } + + + public void test3() + { + + IWebsessionManager mgr1 = new IWebsessionManager("http://localhost:18082/"); + IWebsessionManager mgr2 = new IWebsessionManager("http://localhost:18083/"); + IVirtualBox vbox1 = mgr1.logon("test", "test"); + IVirtualBox vbox2 = mgr2.logon("test", "test"); + + + System.out.println("connection 1 to VirtualBox version " + vbox1.getVersion()); + System.out.println("connection 2 to VirtualBox version " + vbox2.getVersion()); + mgr1.disconnect(vbox1); + mgr2.disconnect(vbox2); + + mgr1 = new IWebsessionManager("http://localhost:18082/"); + mgr2 = new IWebsessionManager("http://localhost:18083/"); + vbox1 = mgr1.logon("test", "test"); + vbox2 = mgr2.logon("test", "test"); + + System.out.println("second connection 1 to VirtualBox version " + vbox1.getVersion()); + System.out.println("second connection 2 to VirtualBox version " + vbox2.getVersion()); + + mgr1.disconnect(vbox1); + mgr2.disconnect(vbox2); + } + + public void showVMs() + { + try + { + int i = 0; + for (IMachine m : vbox.getMachines()) + { + System.out.println("Machine " + (i++) + ": " + " [" + m.getId() + "]" + " - " + m.getName()); + } + } + catch (Exception e) + { + e.printStackTrace(); + } + } + + public void listHostInfo() + { + try + { + IHost host = vbox.getHost(); + long uProcCount = host.getProcessorCount(); + System.out.println("Processor count: " + uProcCount); + + for (long i=0; i<uProcCount; i++) + { + System.out.println("Processor #" + i + " speed: " + host.getProcessorSpeed(i) + "MHz"); + } + + IPerformanceCollector oCollector = vbox.getPerformanceCollector(); + + List<IPerformanceMetric> aMetrics = + oCollector.getMetrics(Arrays.asList(new String[]{"*"}), + Arrays.asList(new IUnknown[]{host})); + + for (IPerformanceMetric m : aMetrics) + { + System.out.println("known metric = "+m.getMetricName()); + } + + Holder<List<String>> names = new Holder<List<String>> (); + Holder<List<IUnknown>> objects = new Holder<List<IUnknown>>() ; + Holder<List<String>> units = new Holder<List<String>>(); + Holder<List<Long>> scales = new Holder<List<Long>>(); + Holder<List<Long>> sequenceNumbers = new Holder<List<Long>>(); + Holder<List<Long>> indices = new Holder<List<Long>>(); + Holder<List<Long>> lengths = new Holder<List<Long>>(); + + List<Integer> vals = + oCollector.queryMetricsData(Arrays.asList(new String[]{"*"}), + Arrays.asList(new IUnknown[]{host}), + names, objects, units, scales, + sequenceNumbers, indices, lengths); + + for (int i=0; i < names.value.size(); i++) + System.out.println("name: "+names.value.get(i)); + } + catch (Exception e) + { + e.printStackTrace(); + } + } + + public void startVM(String strVM) + { + ISession oSession = null; + IMachine oMachine = null; + + try + { + oSession = mgr.getSessionObject(vbox); + + // first assume we were given a UUID + try + { + oMachine = vbox.getMachine(strVM); + } + catch (Exception e) + { + try + { + oMachine = vbox.findMachine(strVM); + } + catch (Exception e1) + { + } + } + + if (oMachine == null) + { + System.out.println("Error: can't find VM \"" + strVM + "\""); + } + else + { + String uuid = oMachine.getId(); + String sessionType = "gui"; + ArrayList<String> env = new ArrayList<String>(); + env.add("DISPLAY=:0.0"); + IProgress oProgress = + oMachine.launchVMProcess(oSession, + sessionType, + env); + System.out.println("Session for VM " + uuid + " is opening..."); + oProgress.waitForCompletion(10000); + + long rc = oProgress.getResultCode(); + if (rc != 0) + System.out.println("Session failed!"); + } + } + catch (Exception e) + { + e.printStackTrace(); + } + finally + { + if (oSession != null) + { + oSession.close(); + } + } + } + + public void cleanup() + { + try + { + if (vbox != null) + { + disconnect(); + vbox = null; + System.out.println("Logged off."); + } + mgr.cleanupUnused(); + mgr = null; + } + catch (Exception e) + { + e.printStackTrace(); + } + } + + public static void printArgs() + { + System.out.println( "Usage: java clienttest <mode> ..." + + "\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"); + } + + public static void main(String[] args) + { + if (args.length < 1) + { + System.out.println("Error: Must specify at least one argument."); + printArgs(); + } + else + { + clienttest c = new clienttest(); + if (args[0].equals("show")) + { + if (args.length == 2) + { + if (args[1].equals("vms")) + c.showVMs(); + else + System.out.println("Error: Unknown argument to \"show\": \"" + args[1] + "\"."); + } + else + System.out.println("Error: Missing argument to \"show\" command"); + } + else if (args[0].equals("list")) + { + if (args.length == 2) + { + if (args[1].equals("hostinfo")) + c.listHostInfo(); + else + System.out.println("Error: Unknown argument to \"show\": \"" + args[1] + "\"."); + } + else + System.out.println("Error: Missing argument to \"list\" command"); + } + else if (args[0].equals("startvm")) + { + if (args.length == 2) + { + c.startVM(args[1]); + } + else + System.out.println("Error: Missing argument to \"startvm\" command"); + } + else if (args[0].equals("test")) + { + c.test3(); + } + else + System.out.println("Error: Unknown command: \"" + args[0] + "\"."); + + c.cleanup(); + } + } +} diff --git a/src/VBox/Main/webservice/samples/java/jax-ws/metrictest.java b/src/VBox/Main/webservice/samples/java/jax-ws/metrictest.java new file mode 100644 index 00000000..05cfabba --- /dev/null +++ b/src/VBox/Main/webservice/samples/java/jax-ws/metrictest.java @@ -0,0 +1,231 @@ +/* $Id: metrictest.java $ */ +/*!file + * Sample of performance API usage, written in Java. + * + * Don't forget to run VBOX webserver + * with 'vboxwebsrv -t 1000' command, to calm down watchdog thread. + * + * The following license applies to this file only: + */ + +/* + * Copyright (C) 2008-2022 Oracle and/or its affiliates. + * + * 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, + * copy, modify, merge, publish, distribute, sublicense, and/or 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. + */ + +import com.sun.xml.ws.commons.virtualbox{VBOX_API_SUFFIX}.*; + +import java.util.*; +import javax.xml.ws.Holder; + +class PerformanceData +{ + public String name; + public IUnknown object; + public String unit; + public Long scale; + public Long sequenceNumber; + public List<Long> samples; + + public String getFormattedSamples() + { + String out = "["; + String separator = ""; + + if (scale != 1) + { + for (Long sample : samples) + { + out += separator + (sample.doubleValue() / scale) + " " + unit; + separator = ", "; + } + } + else + { + for (Long sample : samples) + { + out += separator + sample.toString() + " " + unit; + separator = ", "; + } + } + out += "]"; + return out; + } +} + +class PerformanceCollector +{ + private IVirtualBox _vbox; + private IPerformanceCollector _collector; + + public PerformanceCollector(IVirtualBox vbox) + { + _vbox = vbox; + _collector = vbox.getPerformanceCollector(); + } + + public void cleanup() + { + _collector.releaseRemote(); + } + + public List<IPerformanceMetric> setup(List<String> metricNames, List<IUnknown> objects, Long period, Long samples) + { + return _collector.setupMetrics(metricNames, objects, period, samples); + } + + public List<IPerformanceMetric> enable(List<String> metricNames, List<IUnknown> objects) + { + return _collector.enableMetrics(metricNames, objects); + } + + public List<IPerformanceMetric> disable(List<String> metricNames, List<IUnknown> objects) + { + return _collector.disableMetrics(metricNames, objects); + } + + public List<PerformanceData> query(List<String> filterMetrics, List<IUnknown> filterObjects) + { + Holder<List<String>> names = new Holder<List<String>>(); + Holder<List<IUnknown>> objects = new Holder<List<IUnknown>>(); + Holder<List<String>> units = new Holder<List<String>>(); + Holder<List<Long>> scales = new Holder<List<Long>>(); + Holder<List<Long>> sequenceNumbers = new Holder<List<Long>>(); + Holder<List<Long>> indices = new Holder<List<Long>>(); + Holder<List<Long>> lengths = new Holder<List<Long>>(); + List<Integer> values = + _collector.queryMetricsData(filterMetrics, filterObjects, + names, objects, units, scales, sequenceNumbers, indices, lengths); + List<PerformanceData> data = new ArrayList<PerformanceData>(names.value.size()); + for (int i = 0; i < names.value.size(); i++) + { + PerformanceData singleMetricData = new PerformanceData(); + singleMetricData.name = names.value.get(i); + singleMetricData.object = objects.value.get(i); + singleMetricData.unit = units.value.get(i); + singleMetricData.scale = scales.value.get(i); + singleMetricData.sequenceNumber = sequenceNumbers.value.get(i); + List<Long> samples = new ArrayList<Long>(lengths.value.get(i).intValue()); + for (int j = 0; j < lengths.value.get(i); j++) + { + samples.add(values.get(indices.value.get(i).intValue() + j).longValue()); + } + singleMetricData.samples = samples; + data.add(singleMetricData); + } + + return data; + } +} + +public class metrictest implements Runnable +{ + IVirtualBox vbox; + IWebsessionManager mgr; + PerformanceCollector perf; + + public metrictest() + { + mgr = new IWebsessionManager("http://localhost:18083/"); + vbox = mgr.logon("test", "test"); + System.out.println("Initialized connection to VirtualBox version " + vbox.getVersion()); + perf = new PerformanceCollector(vbox); + } + + private String getObjectName(IUnknown object) + { + try + { + String machineName = object.getRemoteWSPort().iMachineGetName(object.getRef()); + return machineName; + } catch (Exception e) + { + } + return new String("host"); + } + + public void setup() + { + perf.setup(Arrays.asList(new String[]{"*"}), + new ArrayList<IUnknown>(), + new Long(1), new Long(5)); + } + + public void collect() + { + try + { + List<IUnknown> allObjects = new ArrayList<IUnknown>(); + List<PerformanceData> metricData = perf.query(Arrays.asList(new String[]{"*"}), + allObjects); + for (PerformanceData md : metricData) + { + System.out.println("(" + getObjectName(md.object) + ") " + + md.name + " " + md.getFormattedSamples()); + } + } + catch (Exception e) + { + e.printStackTrace(); + } + } + + public void run() + { + // Clean up + try + { + if (perf != null) + { + perf.cleanup(); + perf = null; + } + if (vbox != null) + { + mgr.logoff(vbox); + vbox = null; + mgr = null; + System.out.println("Logged off."); + } + } + catch (Exception e) + { + e.printStackTrace(); + } + } + + public static void main(String[] args) throws InterruptedException + { + metrictest c = new metrictest(); + // Add a shutdown handle to clean up + Runtime.getRuntime().addShutdownHook(new Thread(c)); + // Start metric collection + c.setup(); + // Obtain and print out stats continuously until ctrl-C is pressed + while (true) + { + Thread.sleep(1000); // Sleep for a second + c.collect(); + } + } +} |