Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- * DO WHAT THE FUCK YOU WANT TO PUBLIC LICENSE
- * Version 2, December 2004
- *
- * Everyone is permitted to copy and distribute verbatim or modified
- * copies of this license document, and changing it is allowed as long
- * as the name is changed.
- *
- * DO WHAT THE FUCK YOU WANT TO PUBLIC LICENSE
- * TERMS AND CONDITIONS FOR COPYING, DISTRIBUTION AND MODIFICATION
- *
- * 0. You just DO WHAT THE FUCK YOU WANT TO.
- */
- import java.util.Arrays;
- import org.virtualbox_4_3.GuestSessionStatus;
- import org.virtualbox_4_3.IEvent;
- import org.virtualbox_4_3.IEventListener;
- import org.virtualbox_4_3.IEventSource;
- import org.virtualbox_4_3.IGuestProcess;
- import org.virtualbox_4_3.IGuestSession;
- import org.virtualbox_4_3.IMachine;
- import org.virtualbox_4_3.ISession;
- import org.virtualbox_4_3.LockType;
- import org.virtualbox_4_3.ProcessCreateFlag;
- import org.virtualbox_4_3.ProcessWaitForFlag;
- import org.virtualbox_4_3.ProcessWaitResult;
- import org.virtualbox_4_3.VBoxEventType;
- import org.virtualbox_4_3.VirtualBoxManager;
- public class GuestSessionProcess {
- private static String errorIfNull(String systemProperty) {
- String value = System.getProperty(systemProperty);
- if (value != null) {
- return value;
- } else {
- throw new RuntimeException(systemProperty + " is not set");
- }
- }
- public static void main(String[] args) {
- String vmId = errorIfNull("vbox.vm");
- String guestUser = errorIfNull("vbox.guest.user");
- String guestPass = errorIfNull("vbox.guest.pass");
- String exec = errorIfNull("vbox.guest.exec");
- System.out.println("Starting");
- VirtualBoxManager vboxManager = VirtualBoxManager.createInstance(null);
- try {
- if (System.getProperty("vbox.ws") != null) {
- String host = System.getProperty("vbox.ws.host", "http://localhost:18083");
- String user = System.getProperty("vbox.ws.user", "");
- String pass = System.getProperty("vbox.ws.pass", "");
- vboxManager.connect(host, user, pass);
- }
- System.out.println("Connected");
- try {
- ISession session = vboxManager.getSessionObject();
- IMachine vm = vboxManager.getVBox().findMachine(vmId);
- vm.lockMachine(session, LockType.Shared);
- try {
- System.out.println("Machine locked");
- IGuestSession guestSess = session.getConsole().getGuest().createSession(guestUser, guestPass, null, null);
- try {
- System.out.println("Session created");
- guestSess.waitFor(1L, 30 * 1000L);
- if (!guestSess.getStatus().equals(GuestSessionStatus.Started)) {
- throw new RuntimeException("Guest session did not start after 30 sec");
- }
- IGuestProcess process = guestSess.processCreate(exec, null, null, Arrays.asList(ProcessCreateFlag.WaitForStdOut), 0L);
- IEventSource es = process.getEventSource();
- IEventListener el = es.createListener();
- es.registerListener(el, Arrays.asList(VBoxEventType.Any), false);
- try {
- System.out.println("Guest process created");
- ProcessWaitResult pwr = process.waitFor((long) ProcessWaitForFlag.Start.value(), 30 * 1000L);
- System.out.println("Process wait result: " + pwr);
- boolean keepLooping = true;
- do {
- IEvent ev = es.getEvent(el, 200);
- if (ev != null) {
- es.eventProcessed(el, ev);
- }
- /* This is how you should normally do it, but waiting for Stdout is not currently implemented - see http://hyperbox.altherian.org/kb/guessProcessHandling.txt
- ProcessWaitResult wr = process.waitForArray(Arrays.asList(ProcessWaitForFlag.StdOut, ProcessWaitForFlag.Terminate), 200L);
- System.out.println("Process wait result: " + wr);
- */
- byte[] stdOut = process.read(1L, 64L, 0L);
- System.out.print(new String(stdOut));
- keepLooping = !process.getStatus().toString().contains("Terminated");
- } while (keepLooping);
- System.out.println("Process exit code: " + process.getExitCode());
- } finally {
- es.unregisterListener(el);
- if (!process.getStatus().toString().contains("Terminated")) {
- process.terminate();
- }
- }
- } finally {
- System.out.println("Session close");
- guestSess.close();
- }
- } catch (Throwable t) {
- t.printStackTrace();
- } finally {
- System.out.println("Machine unlock");
- session.unlockMachine();
- }
- } finally {
- if (System.getProperty("vbox.ws") != null) {
- vboxManager.disconnect();
- }
- System.out.println("Disconnected");
- }
- } finally {
- vboxManager.cleanup();
- System.out.println("Closing");
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement