static boolean progressBar(VirtualBoxManager mgr, IProgress p, long waitMillis) { long end = System.currentTimeMillis() + waitMillis; while (!p.getCompleted()) { mgr.waitForEvents(0); p.waitForCompletion(200); if (System.currentTimeMillis() >= end) return false; } return true; }
static void testStart(VirtualBoxManager mgr, IVirtualBox vbox) { IMachine m = vbox.getMachines().get(0); String name = m.getName(); System.out.println("\nAttempting to start VM '" + name + "'"); ISession session = mgr.getSessionObject(); IProgress p = m.launchVMProcess(session, "gui", ""); progressBar(mgr, p, 10000); session.unlockMachine(); }
public static void main(String[] args) { VirtualBoxManager mgr = VirtualBoxManager.createInstance(null); boolean ws = false; String url = null; String user = null; String passwd = null; for (int i = 0; i < args.length; i++) { if (args[i].equals("-w")) ws = true; else if (args[i].equals("-url")) url = args[++i]; else if (args[i].equals("-user")) user = args[++i]; else if (args[i].equals("-passwd")) passwd = args[++i]; } if (ws) { try { mgr.connect(url, user, passwd); } catch (VBoxException e) { e.printStackTrace(); System.out.println("Cannot connect, start webserver first!"); } } try { IVirtualBox vbox = mgr.getVBox(); if (vbox != null) { System.out.println("VirtualBox version: " + vbox.getVersion() + "\n"); testEnumeration(mgr, vbox); testReadLog(mgr, vbox); testStart(mgr, vbox); testEvents(mgr, vbox.getEventSource()); System.out.println("done, press Enter..."); int ch = System.in.read(); } } catch (VBoxException e) { printErrorInfo(e); System.out.println("Java stack trace:"); e.printStackTrace(); } catch (RuntimeException e) { System.out.println("Runtime error: " + e.getMessage()); e.printStackTrace(); } catch (java.io.IOException e) { e.printStackTrace(); } if (ws) { try { mgr.disconnect(); } catch (VBoxException e) { e.printStackTrace(); } } mgr.cleanup(); }
static void testMultiServer() { VirtualBoxManager mgr1 = VirtualBoxManager.createInstance(null); VirtualBoxManager mgr2 = VirtualBoxManager.createInstance(null); try { mgr1.connect("http://i7:18083", "", ""); mgr2.connect("http://main:18083", "", ""); IMachine m1 = mgr1.getVBox().getMachines().get(0); IMachine m2 = mgr2.getVBox().getMachines().get(0); String name1 = m1.getName(); String name2 = m2.getName(); ISession session1 = mgr1.getSessionObject(); ISession session2 = mgr2.getSessionObject(); IProgress p1 = m1.launchVMProcess(session1, "gui", ""); IProgress p2 = m2.launchVMProcess(session2, "gui", ""); progressBar(mgr1, p1, 10000); progressBar(mgr2, p2, 10000); session1.unlockMachine(); session2.unlockMachine(); } finally { mgr1.cleanup(); mgr2.cleanup(); } }