1 | import java.util.Arrays;
|
---|
2 | import java.util.List;
|
---|
3 |
|
---|
4 | import org.virtualbox_4_3.CleanupMode;
|
---|
5 | import org.virtualbox_4_3.IEvent;
|
---|
6 | import org.virtualbox_4_3.IEventListener;
|
---|
7 | import org.virtualbox_4_3.IMachine;
|
---|
8 | import org.virtualbox_4_3.IMedium;
|
---|
9 | import org.virtualbox_4_3.IProgress;
|
---|
10 | import org.virtualbox_4_3.ISession;
|
---|
11 | import org.virtualbox_4_3.IVirtualBox;
|
---|
12 | import org.virtualbox_4_3.SessionState;
|
---|
13 | import org.virtualbox_4_3.VBoxEventType;
|
---|
14 | import org.virtualbox_4_3.VBoxException;
|
---|
15 | import org.virtualbox_4_3.VirtualBoxManager;
|
---|
16 |
|
---|
17 | /*
|
---|
18 | * Hyperbox - Enterprise Virtualization Manager
|
---|
19 | * Copyright (C) 2014 Maxime Dor
|
---|
20 | *
|
---|
21 | * http://hyperbox.altherian.org
|
---|
22 | *
|
---|
23 | * This program is free software: you can redistribute it and/or modify
|
---|
24 | * it under the terms of the GNU General Public License as published by
|
---|
25 | * the Free Software Foundation, either version 3 of the License, or
|
---|
26 | * (at your option) any later version.
|
---|
27 | *
|
---|
28 | * This program is distributed in the hope that it will be useful,
|
---|
29 | * but WITHOUT ANY WARRANTY; without even the implied warranty of
|
---|
30 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
---|
31 | * GNU General Public License for more details.
|
---|
32 | *
|
---|
33 | * You should have received a copy of the GNU General Public License
|
---|
34 | * along with this program. If not, see <http://www.gnu.org/licenses/>.
|
---|
35 | *
|
---|
36 | */
|
---|
37 |
|
---|
38 | public class OutOfMemory {
|
---|
39 |
|
---|
40 | private final int waitingCoef = 500;
|
---|
41 | private Thread worker;
|
---|
42 |
|
---|
43 | @SuppressWarnings("unused")
|
---|
44 | public static void main(String[] args) {
|
---|
45 | new OutOfMemory();
|
---|
46 | }
|
---|
47 |
|
---|
48 | public OutOfMemory() {
|
---|
49 | System.out.println("Starting");
|
---|
50 | VirtualBoxManager vboxManager = VirtualBoxManager.createInstance(null);
|
---|
51 | if (System.getProperty("vbox.ws") != null) {
|
---|
52 | vboxManager.connect("http://" + System.getProperty("vbox.ws.host") + ":18083", "", "");
|
---|
53 | }
|
---|
54 | IVirtualBox vbox = vboxManager.getVBox();
|
---|
55 |
|
---|
56 | worker = new Thread(new EventWorker(vbox));
|
---|
57 | worker.start();
|
---|
58 |
|
---|
59 | while (worker.isAlive()) {
|
---|
60 | try {
|
---|
61 | IMachine machine = vbox.createMachine(null, Long.toString(System.currentTimeMillis()), null, "Other", null);
|
---|
62 | vbox.registerMachine(machine);
|
---|
63 | System.out.println("Machine registered");
|
---|
64 |
|
---|
65 | ISession session = vboxManager.getSessionObject();
|
---|
66 | IProgress pOn = machine.launchVMProcess(session, "headless", null);
|
---|
67 | while (!pOn.getCompleted() || pOn.getCanceled()) {
|
---|
68 | try {
|
---|
69 | Thread.sleep(Math.abs(pOn.getTimeRemaining()) * waitingCoef);
|
---|
70 | } catch (InterruptedException e) {
|
---|
71 | e.printStackTrace();
|
---|
72 | }
|
---|
73 | }
|
---|
74 | if (pOn.getResultCode() != 0) {
|
---|
75 | System.out.println("Error when starting machine: " + pOn.getErrorInfo().getText());
|
---|
76 | } else {
|
---|
77 | System.out.println("Maching started");
|
---|
78 | }
|
---|
79 |
|
---|
80 | IProgress pOff = session.getConsole().powerDown();
|
---|
81 | while (!pOff.getCompleted() || pOff.getCanceled()) {
|
---|
82 | try {
|
---|
83 | Thread.sleep(Math.abs(pOn.getTimeRemaining()) * waitingCoef);
|
---|
84 | } catch (InterruptedException e) {
|
---|
85 | e.printStackTrace();
|
---|
86 | }
|
---|
87 | }
|
---|
88 | if (pOff.getResultCode() != 0) {
|
---|
89 | System.out.println("Error when stopping machine: " + pOff.getErrorInfo().getText());
|
---|
90 | } else {
|
---|
91 | System.out.println("Machine stopped");
|
---|
92 | }
|
---|
93 |
|
---|
94 | if (session.getState().equals(SessionState.Locked)) {
|
---|
95 | try {
|
---|
96 | session.unlockMachine();
|
---|
97 | } catch (VBoxException e) {
|
---|
98 | if (!e.getMessage().toUpperCase().contains("8000FFFF")) {
|
---|
99 | e.printStackTrace();
|
---|
100 | }
|
---|
101 |
|
---|
102 | }
|
---|
103 | }
|
---|
104 |
|
---|
105 | machine = vbox.findMachine(machine.getId());
|
---|
106 | while (!machine.getSessionState().equals(SessionState.Unlocked)) {
|
---|
107 | Thread.sleep(10);
|
---|
108 | }
|
---|
109 | try {
|
---|
110 | List<IMedium> hdds = machine.unregister(CleanupMode.DetachAllReturnHardDisksOnly);
|
---|
111 | IProgress pDel = machine.deleteConfig(hdds);
|
---|
112 | while (!pDel.getCompleted() || pDel.getCanceled()) {
|
---|
113 | try {
|
---|
114 | Thread.sleep(Math.abs(pDel.getTimeRemaining()) * waitingCoef);
|
---|
115 | } catch (InterruptedException e) {
|
---|
116 | e.printStackTrace();
|
---|
117 | }
|
---|
118 | }
|
---|
119 | if (pDel.getResultCode() != 0) {
|
---|
120 | System.out.println("Error when deleting machine: " + pDel.getErrorInfo().getText());
|
---|
121 | } else {
|
---|
122 | System.out.println("Machine deleted");
|
---|
123 | }
|
---|
124 | } catch (Throwable t) {
|
---|
125 | t.printStackTrace();
|
---|
126 | }
|
---|
127 |
|
---|
128 | System.out.println("------------------------------");
|
---|
129 | } catch (Throwable t) {
|
---|
130 | t.printStackTrace();
|
---|
131 | worker.interrupt();
|
---|
132 | break;
|
---|
133 | }
|
---|
134 | }
|
---|
135 |
|
---|
136 | vboxManager.cleanup();
|
---|
137 |
|
---|
138 | System.out.println("Stopping");
|
---|
139 | }
|
---|
140 |
|
---|
141 | private class EventWorker implements Runnable {
|
---|
142 |
|
---|
143 | private IVirtualBox vbox;
|
---|
144 |
|
---|
145 | public EventWorker(IVirtualBox vbox) {
|
---|
146 | this.vbox = vbox;
|
---|
147 | }
|
---|
148 |
|
---|
149 | @Override
|
---|
150 | public void run() {
|
---|
151 | System.out.println("Starting worker");
|
---|
152 | IEventListener el = vbox.getEventSource().createListener();
|
---|
153 | vbox.getEventSource().registerListener(el, Arrays.asList(VBoxEventType.Any), false);
|
---|
154 |
|
---|
155 | while (!Thread.currentThread().isInterrupted()) {
|
---|
156 | try {
|
---|
157 | IEvent rawEvent = vbox.getEventSource().getEvent(el, 1000);
|
---|
158 | if (rawEvent != null) {
|
---|
159 | System.out.println("VBox Event: " + rawEvent.getClass().getName() + " - " + rawEvent.getType() + " - " + rawEvent);
|
---|
160 | vbox.getEventSource().eventProcessed(el, rawEvent);
|
---|
161 | }
|
---|
162 | } catch (Throwable t) {
|
---|
163 | t.printStackTrace();
|
---|
164 | break;
|
---|
165 | }
|
---|
166 | }
|
---|
167 |
|
---|
168 | vbox.getEventSource().unregisterListener(el);
|
---|
169 | System.out.println("Stopping worker");
|
---|
170 | }
|
---|
171 |
|
---|
172 | }
|
---|
173 |
|
---|
174 | }
|
---|