VirtualBox

source: vbox/trunk/src/VBox/Main/glue/tests/TestVBox.java@ 30932

Last change on this file since 30932 was 30932, checked in by vboxsync, 14 years ago

Java bridge: uniform bridge is functional

  • Property svn:eol-style set to native
File size: 5.0 KB
Line 
1/* $Id:$ */
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 */
13import org.virtualbox_3_3.*;
14import java.util.List;
15import java.util.Arrays;
16import java.math.BigInteger;
17
18public class TestVBox
19{
20 static void processEvent(IEvent ev)
21 {
22 System.out.println("got event: " + ev);
23
24 VBoxEventType type = ev.getType();
25 System.out.println("type = "+type);
26
27 switch (type)
28 {
29 case OnMachineStateChanged:
30 IMachineStateChangedEvent mcse = IMachineStateChangedEvent.queryInterface(ev);
31 if (mcse == null)
32 System.out.println("Cannot query an interface");
33 else
34 System.out.println("mid="+mcse.getMachineId());
35 break;
36 }
37 }
38
39 static class EventHandler
40 {
41 EventHandler() {}
42 public void handleEvent(IEvent ev)
43 {
44 try {
45 processEvent(ev);
46 } catch (Throwable t) {
47 t.printStackTrace();
48 }
49 }
50 }
51
52 static void testEvents(VirtualBoxManager mgr, IEventSource es, boolean active)
53 {
54 // active mode for Java doesn't fully work yet, and using passive
55 // is more portable (the only mode for MSCOM and WS) and thus generally
56 // recommended
57 IEventListener listener = active ? mgr.createListener(new EventHandler()) : es.createListener();
58
59 es.registerListener(listener, Arrays.asList(VBoxEventType.Any), false);
60
61 try {
62 for (int i=0; i<100; i++)
63 {
64 System.out.print(".");
65 if (active)
66 {
67 mgr.waitForEvents(500);
68 }
69 else
70 {
71 IEvent ev = es.getEvent(listener, 1000);
72 if (ev != null)
73 {
74 processEvent(ev);
75 es.eventProcessed(listener, ev);
76 }
77 }
78 }
79 } catch (Exception e) {
80 e.printStackTrace();
81 }
82
83 es.unregisterListener(listener);
84 }
85
86 static void testEnumeration(VirtualBoxManager mgr, IVirtualBox vbox)
87 {
88 List<IMachine> machs = vbox.getMachines();
89 for (IMachine m : machs)
90 {
91 System.out.println("VM name: " + m.getName());// + ", RAM size: " + m.getMemorySize() + "MB");
92 System.out.println(" HWVirt: " + m.getHWVirtExProperty(HWVirtExPropertyType.Enabled)
93 + ", Nested Paging: " + m.getHWVirtExProperty(HWVirtExPropertyType.NestedPaging)
94 + ", PAE: " + m.getCPUProperty(CPUPropertyType.PAE) );
95 }
96 }
97
98 static void testStart(VirtualBoxManager mgr, IVirtualBox vbox)
99 {
100 String m = vbox.getMachines().get(0).getName();
101 System.out.println("\nAttempting to start VM '" + m + "'");
102 mgr.startVm(m, null, 7000);
103 }
104
105 public static void main(String[] args)
106 {
107 VirtualBoxManager mgr = VirtualBoxManager.getInstance(null);
108
109 boolean ws = false;
110 String url = null;
111 String user = null;
112 String passwd = null;
113
114 for (int i = 0; i<args.length; i++)
115 {
116 if ("-w".equals(args[i]))
117 ws = true;
118 else if ("-url".equals(args[i]))
119 url = args[++i];
120 else if ("-user".equals(args[i]))
121 user = args[++i];
122 else if ("-passwd".equals(args[i]))
123 passwd = args[++i];
124 }
125
126 if (ws)
127 {
128 try {
129 mgr.connect(url, user, passwd);
130 } catch (Exception e) {
131 e.printStackTrace();
132 System.out.println("Cannot connect, start webserver first!");
133 }
134 }
135
136 try
137 {
138 IVirtualBox vbox = mgr.getVBox();
139 if (vbox != null)
140 {
141 System.out.println("VirtualBox version: " + vbox.getVersion() + "\n");
142 testEnumeration(mgr, vbox);
143 testStart(mgr, vbox);
144 testEvents(mgr, vbox.getEventSource(), false);
145
146 System.out.println("done, press Enter...");
147 int ch = System.in.read();
148 }
149 }
150 catch (Throwable e)
151 {
152 e.printStackTrace();
153 }
154
155 if (ws)
156 {
157 try {
158 mgr.disconnect();
159 } catch (Exception e) {
160 e.printStackTrace();
161 }
162 }
163
164 mgr.cleanup();
165
166 }
167
168}
Note: See TracBrowser for help on using the repository browser.

© 2024 Oracle Support Privacy / Do Not Sell My Info Terms of Use Trademark Policy Automated Access Etiquette