1 | /* $Id: VBoxTest.java 31883 2010-08-24 07:17:28Z vboxsync $ */
|
---|
2 | /*
|
---|
3 | * Copyright (C) 2010 Oracle Corporation
|
---|
4 | *
|
---|
5 | * This file is part of VirtualBox Open Source Edition (OSE), as
|
---|
6 | * available from http://www.virtualbox.org. This file is free software;
|
---|
7 | * you can redistribute it and/or modify it under the terms of the GNU
|
---|
8 | * General Public License (GPL) as published by the Free Software
|
---|
9 | * Foundation, in version 2 as it comes in the "COPYING" file of the
|
---|
10 | * VirtualBox OSE distribution. VirtualBox OSE is distributed in the
|
---|
11 | * hope that it will be useful, but WITHOUT ANY WARRANTY of any kind.
|
---|
12 | */
|
---|
13 | import com.jacob.activeX.*;
|
---|
14 | import com.jacob.com.ComThread;
|
---|
15 | import com.jacob.com.Dispatch;
|
---|
16 | import com.jacob.com.Variant;
|
---|
17 | import com.jacob.com.SafeArray;
|
---|
18 | import com.jacob.com.DispatchEvents;
|
---|
19 |
|
---|
20 |
|
---|
21 | public class VBoxTest {
|
---|
22 |
|
---|
23 | private ActiveXComponent vbox = null;
|
---|
24 |
|
---|
25 | public VBoxTest() {
|
---|
26 | setUp();
|
---|
27 | }
|
---|
28 |
|
---|
29 | public void setUp() {
|
---|
30 | if (vbox == null) {
|
---|
31 | ComThread.InitMTA();
|
---|
32 | vbox = new ActiveXComponent("VirtualBox.VirtualBox");
|
---|
33 | String v = Dispatch.get(vbox, "Version").getString();
|
---|
34 | System.out.println("vbox is "+v);
|
---|
35 | }
|
---|
36 | }
|
---|
37 |
|
---|
38 | public void startVm(String name)
|
---|
39 | {
|
---|
40 | Dispatch m = Dispatch.call(vbox, "findMachine", name).toDispatch();
|
---|
41 | ActiveXComponent session = new ActiveXComponent("VirtualBox.Session");
|
---|
42 | String id = Dispatch.get(m, "Id").getString();
|
---|
43 | String type= "gui";
|
---|
44 | Dispatch p = Dispatch.call(vbox, "openRemoteSession", session, id, type, "").toDispatch();
|
---|
45 | Dispatch.call(p, "waitForCompletion", 10000);
|
---|
46 | }
|
---|
47 |
|
---|
48 | public String list()
|
---|
49 | {
|
---|
50 | // returns first machine
|
---|
51 | SafeArray sa = Dispatch.get(vbox, "Machines").toSafeArray();
|
---|
52 | String rv = null;
|
---|
53 |
|
---|
54 | for (int i = sa.getLBound(); i <= sa.getUBound(); i++)
|
---|
55 | {
|
---|
56 | Dispatch m = sa.getVariant(i).getDispatch();
|
---|
57 | String name = Dispatch.get(m, "Name").getString();
|
---|
58 | if (rv == null)
|
---|
59 | rv = name;
|
---|
60 | String id = Dispatch.get(m, "Id").getString();
|
---|
61 | System.out.println("name="+name+" id="+id);
|
---|
62 | }
|
---|
63 | return rv;
|
---|
64 | }
|
---|
65 |
|
---|
66 | public void tearDown() {
|
---|
67 | ComThread.Release();
|
---|
68 | }
|
---|
69 |
|
---|
70 |
|
---|
71 | public void testCallbacks()
|
---|
72 | {
|
---|
73 | VBoxCallbacks cb = new VBoxCallbacks();
|
---|
74 | ActiveXDispatchEvents de = new ActiveXDispatchEvents(vbox, cb);
|
---|
75 | Dispatch.call(vbox, "registerCallback", de);
|
---|
76 | }
|
---|
77 |
|
---|
78 | public void testCallbacks1()
|
---|
79 | {
|
---|
80 | ActiveXComponent cbw = new ActiveXComponent("VirtualBox.CallbackWrapper");
|
---|
81 | VBoxCallbacks cb = new VBoxCallbacks();
|
---|
82 | //Dispatch.call(cbw, "setLocalObject", cb);
|
---|
83 | Dispatch.call(vbox, "registerCallback", cbw);
|
---|
84 | }
|
---|
85 |
|
---|
86 | public void testCallbacks2()
|
---|
87 | {
|
---|
88 | VBoxCallbacks cb = new VBoxCallbacks();
|
---|
89 | DispatchEvents de = new DispatchEvents(vbox, cb, "VirtualBox.VirtualBox");
|
---|
90 | //Dispatch.call(vbox, "registerCallback", );
|
---|
91 |
|
---|
92 | }
|
---|
93 |
|
---|
94 | public static void main(String[] args) {
|
---|
95 | VBoxTest vbox = new VBoxTest();
|
---|
96 | String first = vbox.list();
|
---|
97 | //vbox.startVm(first);
|
---|
98 | vbox.testCallbacks2();
|
---|
99 | try {
|
---|
100 | Thread.sleep(20000);
|
---|
101 | } catch (InterruptedException ie) {}
|
---|
102 | vbox.tearDown();
|
---|
103 | }
|
---|
104 | }
|
---|