summaryrefslogtreecommitdiffstats
path: root/src/VBox/Main/webservice/samples/java/axis/clienttest.java
blob: 31043d3ef6da05b9a87dfe56395e68f01dfedb22 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
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();
        }
    }
}