VirtualBox

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

Last change on this file since 31694 was 31694, checked in by vboxsync, 15 years ago

Java XPCOM bridge: octet arrays are byte[] in XPCOM backend, proper null treatment

  • Property svn:eol-style set to native
File size: 6.6 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 VBoxEventType type = ev.getType();
24 System.out.println("type = "+type);
25 switch (type)
26 {
27 case OnMachineStateChanged:
28 {
29 IMachineStateChangedEvent mcse = IMachineStateChangedEvent.queryInterface(ev);
30 if (mcse == null)
31 System.out.println("Cannot query an interface");
32 else
33 System.out.println("mid="+mcse.getMachineId());
34 break;
35 }
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)
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 = 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 IEvent ev = es.getEvent(listener, 1000);
66 if (ev != null)
67 {
68 processEvent(ev);
69 es.eventProcessed(listener, ev);
70 }
71 }
72 } catch (Exception e) {
73 e.printStackTrace();
74 }
75
76 es.unregisterListener(listener);
77 }
78
79 static void testEnumeration(VirtualBoxManager mgr, IVirtualBox vbox)
80 {
81 List<IMachine> machs = vbox.getMachines();
82 for (IMachine m : machs)
83 {
84 System.out.println("VM name: " + m.getName());// + ", RAM size: " + m.getMemorySize() + "MB");
85 System.out.println(" HWVirt: " + m.getHWVirtExProperty(HWVirtExPropertyType.Enabled)
86 + ", Nested Paging: " + m.getHWVirtExProperty(HWVirtExPropertyType.NestedPaging)
87 + ", PAE: " + m.getCPUProperty(CPUPropertyType.PAE) );
88 }
89 }
90
91 static void testStart(VirtualBoxManager mgr, IVirtualBox vbox)
92 {
93 String m = vbox.getMachines().get(0).getName();
94 System.out.println("\nAttempting to start VM '" + m + "'");
95 mgr.startVm(m, null, 7000);
96 }
97
98 static void testMultiServer()
99 {
100 VirtualBoxManager mgr1 = VirtualBoxManager.createInstance(null);
101 VirtualBoxManager mgr2 = VirtualBoxManager.createInstance(null);
102
103 try {
104 mgr1.connect("http://i7:18083", "", "");
105 mgr2.connect("http://main:18083", "", "");
106
107 String mach1 = mgr1.getVBox().getMachines().get(0).getName();
108 String mach2 = mgr2.getVBox().getMachines().get(0).getName();
109
110 mgr1.startVm(mach1, null, 7000);
111 mgr2.startVm(mach2, null, 7000);
112 } finally {
113 mgr1.cleanup();
114 mgr2.cleanup();
115 }
116 }
117
118 /*
119 static void testReadLogBI(VirtualBoxManager mgr, IVirtualBox vbox)
120 {
121 IMachine m = vbox.getMachines().get(0);
122 long logNo = 0;
123 BigInteger off = BigInteger.valueOf(0);
124 BigInteger size = BigInteger.valueOf(16 * 1024);
125 while (true)
126 {
127 byte[] buf = m.readLog(logNo, off, size);
128 if (buf.length == 0)
129 break;
130 System.out.print(new String(buf));
131 off.add(BigInteger.valueOf(buf.length));
132 }
133 }
134
135 static void testReadLog(VirtualBoxManager mgr, IVirtualBox vbox)
136 {
137 IMachine m = vbox.getMachines().get(0);
138 long logNo = 0;
139 long off = 0;
140 long size = 16 * 1024;
141 while (true)
142 {
143 byte[] buf = m.readLog(logNo, off, size);
144 if (buf.length == 0)
145 break;
146 System.out.print(new String(buf));
147 off += buf.length;
148 }
149 }
150 */
151
152
153 public static void main(String[] args)
154 {
155 VirtualBoxManager mgr = VirtualBoxManager.createInstance(null);
156
157 boolean ws = false;
158 String url = null;
159 String user = null;
160 String passwd = null;
161
162 for (int i = 0; i<args.length; i++)
163 {
164 if ("-w".equals(args[i]))
165 ws = true;
166 else if ("-url".equals(args[i]))
167 url = args[++i];
168 else if ("-user".equals(args[i]))
169 user = args[++i];
170 else if ("-passwd".equals(args[i]))
171 passwd = args[++i];
172 }
173
174 if (ws)
175 {
176 try {
177 mgr.connect(url, user, passwd);
178 } catch (VBoxException e) {
179 e.printStackTrace();
180 System.out.println("Cannot connect, start webserver first!");
181 }
182 }
183
184 try
185 {
186 IVirtualBox vbox = mgr.getVBox();
187 if (vbox != null)
188 {
189 System.out.println("VirtualBox version: " + vbox.getVersion() + "\n");
190 testEnumeration(mgr, vbox);
191 //testReadLog(mgr, vbox);
192 testStart(mgr, vbox);
193 testEvents(mgr, vbox.getEventSource());
194
195 System.out.println("done, press Enter...");
196 int ch = System.in.read();
197 }
198 }
199 catch (VBoxException e)
200 {
201 System.out.println("VBox error: "+e.getMessage()+" original="+e.getWrapped());
202 e.printStackTrace();
203 }
204 catch (java.io.IOException e)
205 {
206 e.printStackTrace();
207 }
208
209 if (ws)
210 {
211 try {
212 mgr.disconnect();
213 } catch (VBoxException e) {
214 e.printStackTrace();
215 }
216 }
217
218 mgr.cleanup();
219
220 }
221
222}
Note: See TracBrowser for help on using the repository browser.

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