| 1 | import java.util.Arrays;
|
|---|
| 2 | import java.util.List;
|
|---|
| 3 |
|
|---|
| 4 | import org.virtualbox_4_2.CleanupMode;
|
|---|
| 5 | import org.virtualbox_4_2.IEvent;
|
|---|
| 6 | import org.virtualbox_4_2.IEventListener;
|
|---|
| 7 | import org.virtualbox_4_2.IMachine;
|
|---|
| 8 | import org.virtualbox_4_2.IMedium;
|
|---|
| 9 | import org.virtualbox_4_2.IProgress;
|
|---|
| 10 | import org.virtualbox_4_2.ISession;
|
|---|
| 11 | import org.virtualbox_4_2.ISnapshotTakenEvent;
|
|---|
| 12 | import org.virtualbox_4_2.IVirtualBox;
|
|---|
| 13 | import org.virtualbox_4_2.LockType;
|
|---|
| 14 | import org.virtualbox_4_2.VBoxEventType;
|
|---|
| 15 | import org.virtualbox_4_2.VirtualBoxManager;
|
|---|
| 16 |
|
|---|
| 17 | public class VBDebug_4_2 {
|
|---|
| 18 |
|
|---|
| 19 | public static void main(String[] args) {
|
|---|
| 20 | VirtualBoxManager vboxManager = VirtualBoxManager.createInstance(null);
|
|---|
| 21 | vboxManager.connect("http://localhost:18083", "", "");
|
|---|
| 22 | IVirtualBox vbox = vboxManager.getVBox();
|
|---|
| 23 | System.out.println("VBox Version : " + vbox.getVersion() + " r" + vbox.getRevision());
|
|---|
| 24 |
|
|---|
| 25 | IMachine newVm = vbox.createMachine(null, "Test", null, "Other", null);
|
|---|
| 26 | newVm.saveSettings();
|
|---|
| 27 | vbox.registerMachine(newVm);
|
|---|
| 28 |
|
|---|
| 29 | IMachine vm = vbox.findMachine("Test");
|
|---|
| 30 | ISession session = vboxManager.getSessionObject();
|
|---|
| 31 | vm.lockMachine(session, LockType.Shared);
|
|---|
| 32 |
|
|---|
| 33 | IEventListener listener = vbox.getEventSource().createListener();
|
|---|
| 34 | vbox.getEventSource().registerListener(listener, Arrays.asList(VBoxEventType.Any), false);
|
|---|
| 35 |
|
|---|
| 36 | IProgress progress = session.getConsole().takeSnapshot("test", "test");
|
|---|
| 37 | progress.waitForCompletion(-1);
|
|---|
| 38 | session.unlockMachine();
|
|---|
| 39 |
|
|---|
| 40 | boolean running = true;
|
|---|
| 41 | while (running) {
|
|---|
| 42 | IEvent rawEvent = vbox.getEventSource().getEvent(listener, 0);
|
|---|
| 43 | if (rawEvent != null) {
|
|---|
| 44 | if (rawEvent.getType().equals(VBoxEventType.OnSnapshotTaken)) {
|
|---|
| 45 | try {
|
|---|
| 46 | ISnapshotTakenEvent event = ISnapshotTakenEvent.queryInterface(rawEvent);
|
|---|
| 47 | String vmUuid = event.getMachineId();
|
|---|
| 48 | String snapUuid = event.getSnapshotId();
|
|---|
| 49 | System.out.println("Snapshot UUID " + snapUuid + " was taken on " + vmUuid);
|
|---|
| 50 | } catch (Throwable t) {
|
|---|
| 51 | t.printStackTrace();
|
|---|
| 52 | }
|
|---|
| 53 | } else {
|
|---|
| 54 | System.out.println("Skipping " + rawEvent.getType());
|
|---|
| 55 | }
|
|---|
| 56 | } else {
|
|---|
| 57 | running = false;
|
|---|
| 58 | }
|
|---|
| 59 | }
|
|---|
| 60 |
|
|---|
| 61 | List<IMedium> mediums = vm.unregister(CleanupMode.DetachAllReturnHardDisksOnly);
|
|---|
| 62 | progress = vm.delete(mediums);
|
|---|
| 63 | progress.waitForCompletion(-1);
|
|---|
| 64 | if (progress.getResultCode() == 0) {
|
|---|
| 65 | System.out.println("Deleted VM");
|
|---|
| 66 | }
|
|---|
| 67 |
|
|---|
| 68 | vbox.getEventSource().unregisterListener(listener);
|
|---|
| 69 | vboxManager.cleanup();
|
|---|
| 70 | }
|
|---|
| 71 |
|
|---|
| 72 | }
|
|---|