/** * Copyright (C) 2010 Oracle Corporation * * This file is part of VirtualBox Open Source Edition (OSE), as * available from http://www.virtualbox.org. This file is free software; * you can redistribute it and/or modify it under the terms of the GNU * General Public License (GPL) as published by the Free Software * Foundation, in version 2 as it comes in the "COPYING" file of the * VirtualBox OSE distribution. VirtualBox OSE is distributed in the * hope that it will be useful, but WITHOUT ANY WARRANTY of any kind. * * * DO NOT EDIT! This is a generated file. * Generated from: src/VBox/Main/idl/VirtualBox.xidl (VirtualBox's interface definitions in XML) * Generator: src/VBox/Main/glue/glue-java.xsl */ , ; private final int value; value = v; } public int value() { return value; } if (c.value == (int)v) { return c; } } throw new IllegalArgumentException(Long.toString(v)); } } IUnknown void void , void , import java.util.List; import java.util.List; import java.util.List; { public T value; public Holder() { } public Holder(T value) { this.value = value; } } ]]> { public T value; public Holder() { } public Holder(T value) { this.value = value; } } ]]> wrap(byte[] vals) { if (vals==null) return null; List ret = new ArrayList(vals.length); for (short v : vals) { ret.add(v); } return ret; } public static List wrap(int[] vals) { if (vals==null) return null; List ret = new ArrayList(vals.length); for (int v : vals) { ret.add(v); } return ret; } public static List wrap(long[] vals) { if (vals==null) return null; List ret = new ArrayList(vals.length); for (long v : vals) { ret.add(v); } return ret; } public static List wrap(String[] vals) { if (vals==null) return null; List ret = new ArrayList(vals.length); for (String v : vals) { ret.add(v); } return ret; } public static List wrap(Class wrapperClass, T[] thisPtrs) { if (thisPtrs==null) return null; List ret = new ArrayList(thisPtrs.length); for (T thisPtr : thisPtrs) { ret.add(thisPtr); } return ret; } public static List wrapEnum(Class wrapperClass, long values[]) { try { if (values==null) return null; Constructor c = wrapperClass.getConstructor(int.class); List ret = new ArrayList(values.length); for (long v : values) { ret.add(c.newInstance(v)); } return ret; } catch (NoSuchMethodException e) { throw new AssertionError(e); } catch (InstantiationException e) { throw new AssertionError(e); } catch (IllegalAccessException e) { throw new AssertionError(e); } catch (InvocationTargetException e) { throw new AssertionError(e); } } public static short[] unwrap(List vals) { if (vals==null) return null; short[] ret = new short[vals.size()]; int i = 0; for (short l : vals) { ret[i++] = l; } return ret; } public static int[] unwrap(List vals) { if (vals == null) return null; int[] ret = new int[vals.size()]; int i = 0; for (int l : vals) { ret[i++] = l; } return ret; } public static long[] unwrap(List vals) { if (vals == null) return null; long[] ret = new long[vals.size()]; int i = 0; for (long l : vals) { ret[i++] = l; } return ret; } public static boolean[] unwrap(List vals) { if (vals==null) return null; boolean[] ret = new boolean[vals.size()]; int i = 0; for (boolean l : vals) { ret[i++] = l; } return ret; } public static String[] unwrap(List vals) { if (vals==null) return null; String[] ret = new String[vals.size()]; int i = 0; for (String l : vals) { ret[i++] = l; } return ret; } public static > long[] unwrapEnum(Class enumClass, List values) { if (values == null) return null; long result[] = new long[values.size()]; try { java.lang.reflect.Method valueM = enumClass.getMethod("value"); int i = 0; for (T v : values) { result[i++] = (Integer)valueM.invoke(v); } return result; } catch (NoSuchMethodException e) { throw new AssertionError(e); } catch(SecurityException e) { throw new AssertionError(e); } catch (IllegalAccessException e) { throw new AssertionError(e); } catch (IllegalArgumentException e) { throw new AssertionError(e); } catch (InvocationTargetException e) { throw new AssertionError(e); } } public static List wrap2(Class wrapperClass1, Class wrapperClass2, T2[] thisPtrs) { try { if (thisPtrs==null) return null; Constructor c = wrapperClass1.getConstructor(wrapperClass2); List ret = new ArrayList(thisPtrs.length); for (T2 thisPtr : thisPtrs) { ret.add(c.newInstance(thisPtr)); } return ret; } catch (NoSuchMethodException e) { throw new AssertionError(e); } catch (InstantiationException e) { throw new AssertionError(e); } catch (IllegalAccessException e) { throw new AssertionError(e); } catch (InvocationTargetException e) { throw new AssertionError(e); } } @SuppressWarnings( "unchecked") public static T[] unwrap(Class wrapperClass, List thisPtrs) { if (thisPtrs==null) return null; if (thisPtrs.size() == 0) return null; return (T[])thisPtrs.toArray((T[])Array.newInstance(wrapperClass, thisPtrs.size())); } @SuppressWarnings( "unchecked" ) public static T queryInterface(Object obj, String uuid, Class iface) { return (T)queryInterface(obj, uuid); } public static Object queryInterface(Object obj, String uuid) { try { /* Kind of ugly, but does the job of casting */ org.mozilla.xpcom.Mozilla moz = org.mozilla.xpcom.Mozilla.getInstance(); long xpobj = moz.wrapJavaObject(obj, uuid); return moz.wrapXPCOMObject(xpobj, uuid); } catch (Exception e) { return null; } } @SuppressWarnings("unchecked") public static T2[] unwrap2(Class wrapperClass1, Class wrapperClass2, List thisPtrs) { if (thisPtrs==null) return null; T2 ret[] = (T2[])Array.newInstance(wrapperClass2, thisPtrs.size()); int i = 0; for (T1 obj : thisPtrs) { ret[i++] = (T2)obj.getWrapped(); } return ret; } } ]]> = end) return false; } return true; } public boolean startVm(String name, String type, int timeout) { IMachine m = vbox.findMachine(name); if (m == null) return false; ISession session = getSessionObject(); if (type == null) type = "gui"; IProgress p = m.launchVMProcess(session, type, ""); progressBar(p, timeout); session.unlockMachine(); return true; } public void waitForEvents(long tmo) { mozilla.waitForEvents(tmo); } } ]]> wrap(short[] vals) { if (vals==null) return null; if (vals.length == 0) return Collections.emptyList(); List ret = new ArrayList(vals.length); for (short v : vals) { ret.add(v); } return ret; } public static List wrap(int[] vals) { if (vals == null) return null; if (vals.length == 0) return Collections.emptyList(); List ret = new ArrayList(vals.length); for (int v : vals) { ret.add(v); } return ret; } public static List wrap(long[] vals) { if (vals==null) return null; if (vals.length == 0) return Collections.emptyList(); List ret = new ArrayList(vals.length); for (long v : vals) { ret.add(v); } return ret; } public static List wrap(String[] vals) { if (vals==null) return null; if (vals.length == 0) return Collections.emptyList(); List ret = new ArrayList(vals.length); for (String v : vals) { ret.add(v); } return ret; } public static T wrapDispatch(Class wrapperClass, Dispatch d) { try { if (d == null || d.m_pDispatch == 0) return null; Constructor c = wrapperClass.getConstructor(Dispatch.class); return (T)c.newInstance(d); } catch (NoSuchMethodException e) { throw new AssertionError(e); } catch (InstantiationException e) { throw new AssertionError(e); } catch (IllegalAccessException e) { throw new AssertionError(e); } catch (InvocationTargetException e) { throw new AssertionError(e); } } @SuppressWarnings("unchecked") public static Object wrapVariant(Class wrapperClass, Variant v) { if (v == null) return null; short vt = v.getvt(); switch (vt) { case Variant.VariantNull: return null; case Variant.VariantBoolean: return v.getBoolean(); case Variant.VariantByte: return v.getByte(); case Variant.VariantShort: return v.getShort(); case Variant.VariantInt: return v.getInt(); case Variant.VariantLongInt: return v.getLong(); case Variant.VariantString: return v.getString(); case Variant.VariantDispatch: return wrapDispatch(wrapperClass, v.getDispatch()); default: throw new RuntimeException("unhandled variant type "+vt); } } public static byte[] wrapBytes(SafeArray sa) { if (sa==null) return null; int saLen = sa.getUBound() - sa.getLBound() + 1; byte[] ret = new byte[saLen]; int j = 0; for (int i = sa.getLBound(); i <= sa.getUBound(); i++) { Variant v = sa.getVariant(i); // come upo with more effective approach!!! ret[j++] = v.getByte(); } return ret; } @SuppressWarnings("unchecked") public static List wrap(Class wrapperClass, SafeArray sa) { if (sa==null) return null; int saLen = sa.getUBound() - sa.getLBound() + 1; if (saLen == 0) return Collections.emptyList(); List ret = new ArrayList(saLen); for (int i = sa.getLBound(); i <= sa.getUBound(); i++) { Variant v = sa.getVariant(i); ret.add((T)wrapVariant(wrapperClass, v)); } return ret; } public static List wrapEnum(Class wrapperClass, SafeArray sa) { try { if (sa==null) return null; int saLen = sa.getUBound() - sa.getLBound() + 1; if (saLen == 0) return Collections.emptyList(); List ret = new ArrayList(saLen); Constructor c = wrapperClass.getConstructor(int.class); for (int i = sa.getLBound(); i <= sa.getUBound(); i++) { Variant v = sa.getVariant(i); ret.add(c.newInstance(v.getInt())); } return ret; } catch (NoSuchMethodException e) { throw new AssertionError(e); } catch (InstantiationException e) { throw new AssertionError(e); } catch (IllegalAccessException e) { throw new AssertionError(e); } catch (InvocationTargetException e) { throw new AssertionError(e); } } public static SafeArray unwrapInt(List vals) { if (vals==null) return null; SafeArray ret = new SafeArray(Variant.VariantInt, vals.size()); int i = 0; for (int l : vals) { ret.setInt(i++, l); } return ret; } public static SafeArray unwrapLong(List vals) { if (vals==null) return null; SafeArray ret = new SafeArray(Variant.VariantLongInt, vals.size()); int i = 0; for (long l : vals) { ret.setLong(i++, l); } return ret; } public static SafeArray unwrapBool(List vals) { if (vals==null) return null; SafeArray result = new SafeArray(Variant.VariantBoolean, vals.size()); int i = 0; for (boolean l : vals) { result.setBoolean(i, l); } return result; } public static > SafeArray unwrapEnum(Class enumClass, List values) { if (values == null) return null; SafeArray result = new SafeArray(Variant.VariantInt, values.size()); try { java.lang.reflect.Method valueM = enumClass.getMethod("value"); int i = 0; for (T v : values) { result.setInt(i, (Integer)valueM.invoke(v)); } return result; } catch (NoSuchMethodException e) { throw new AssertionError(e); } catch(SecurityException e) { throw new AssertionError(e); } catch (IllegalAccessException e) { throw new AssertionError(e); } catch (IllegalArgumentException e) { throw new AssertionError(e); } catch (InvocationTargetException e) { throw new AssertionError(e); } } public static SafeArray unwrapString(List vals) { if (vals==null) return null; SafeArray result = new SafeArray(Variant.VariantString, vals.size()); int i = 0; for (String l : vals) { result.setString(i, l); } return result; } public static List wrap2(Class wrapperClass1, Class wrapperClass2, T2[] thisPtrs) { try { if (thisPtrs==null) return null; if (thisPtrs.length == 0) return Collections.emptyList(); Constructor c = wrapperClass1.getConstructor(wrapperClass2); List ret = new ArrayList(thisPtrs.length); for (T2 thisPtr : thisPtrs) { ret.add(c.newInstance(thisPtr)); } return ret; } catch (NoSuchMethodException e) { throw new AssertionError(e); } catch (InstantiationException e) { throw new AssertionError(e); } catch (IllegalAccessException e) { throw new AssertionError(e); } catch (InvocationTargetException e) { throw new AssertionError(e); } } @SuppressWarnings("unchecked") public static T[] unwrap(Class wrapperClass, List thisPtrs) { if (thisPtrs==null) return null; return (T[])thisPtrs.toArray((T[])Array.newInstance(wrapperClass, thisPtrs.size())); } @SuppressWarnings("unchecked") public static T2[] unwrap2(Class wrapperClass1, Class wrapperClass2, List thisPtrs) { if (thisPtrs==null) return null; T2 ret[] = (T2[])Array.newInstance(wrapperClass2, thisPtrs.size()); int i = 0; for (T1 obj : thisPtrs) { ret[i++] = (T2)obj.getWrapped(); } return ret; } /* We have very long invoke lists sometimes */ public static Variant invoke(Dispatch d, String method, Object ... args) { return Dispatch.callN(d, method, args); } } ]]> = end) return false; } return true; } public boolean startVm(String name, String type, int timeout) { IMachine m = vbox.findMachine(name); if (m == null) return false; ISession session = getSessionObject(); if (type == null) type = "gui"; IProgress p = m.launchVMProcess(session, type, ""); progressBar(p, timeout); session.unlockMachine(); return true; } public void waitForEvents(long tmo) { // what to do here? try { Thread.sleep(tmo); } catch (InterruptedException ie) { } } } ]]> List wrap(Class wrapperClass, VboxPortType pt, List thisPtrs) { try { if(thisPtrs==null) return null; Constructor c = wrapperClass.getConstructor(String.class, VboxPortType.class); List ret = new ArrayList(thisPtrs.size()); for (String thisPtr : thisPtrs) { ret.add(c.newInstance(thisPtr,pt)); } return ret; } catch (NoSuchMethodException e) { throw new AssertionError(e); } catch (InstantiationException e) { throw new AssertionError(e); } catch (IllegalAccessException e) { throw new AssertionError(e); } catch (InvocationTargetException e) { throw new AssertionError(e); } } public static List wrap2(Class wrapperClass1, Class wrapperClass2, VboxPortType pt, List thisPtrs) { try { if(thisPtrs==null) return null; Constructor c = wrapperClass1.getConstructor(wrapperClass2, VboxPortType.class); List ret = new ArrayList(thisPtrs.size()); for (T2 thisPtr : thisPtrs) { ret.add(c.newInstance(thisPtr,pt)); } return ret; } catch (NoSuchMethodException e) { throw new AssertionError(e); } catch (InstantiationException e) { throw new AssertionError(e); } catch (IllegalAccessException e) { throw new AssertionError(e); } catch (InvocationTargetException e) { throw new AssertionError(e); } } public static List unwrap(List thisPtrs) { if (thisPtrs==null) return null; List ret = new ArrayList(thisPtrs.size()); for (T obj : thisPtrs) { ret.add(obj.getWrapped()); } return ret; } @SuppressWarnings("unchecked" ) public static , T2 extends Enum > List convertEnums(Class fromClass, Class toClass, List values) { try { if (values==null) return null; java.lang.reflect.Method fromValue = toClass.getMethod("fromValue", String.class); List ret = new ArrayList(values.size()); for (T1 v : values) { // static method is called with null this ret.add((T2)fromValue.invoke(null, v.name())); } return ret; } catch (NoSuchMethodException e) { throw new AssertionError(e); } catch (IllegalAccessException e) { throw new AssertionError(e); } catch (InvocationTargetException e) { throw new AssertionError(e); } } // temporary method, will bo away soon public static byte[] wrapBytes(List arr) { if (arr == null) return null; int i = 0; byte[] rv = new byte[arr.size()]; for (short s : arr) rv[i++] = (byte)(s & 0xff); return rv; } } ]]> import java.net.URL; import java.math.BigInteger; import java.util.List; import java.util.Map; import java.util.HashMap; import javax.xml.namespace.QName; import javax.xml.ws.BindingProvider; import javax.xml.ws.Holder; import javax.xml.ws.WebServiceException; class PortPool { private final static String wsdlFile = ; known; private boolean initStarted; private VboxService svc; PortPool(boolean usePreinit) { known = new HashMap(); if (usePreinit) { new Thread(new Runnable() { public void run() { // need to sync on something else but 'this' synchronized (known) { initStarted = true; known.notify(); } preinit(); } }).start(); synchronized (known) { while (!initStarted) { try { known.wait(); } catch (InterruptedException e) { break; } } } } } private synchronized void preinit() { VboxPortType port = getPort(); releasePort(port); } synchronized VboxPortType getPort() { VboxPortType port = null; int ttl = 0; for (VboxPortType cur: known.keySet()) { int value = known.get(cur); if ((value & 0x10000) == 0) { port = cur; ttl = value & 0xffff; break; } } if (port == null) { if (svc == null) { URL wsdl = PortPool.class.getClassLoader().getResource(wsdlFile); if (wsdl == null) throw new LinkageError(wsdlFile+" not found, but it should have been in the jar"); svc = new VboxService(wsdl, new QName("http://www.virtualbox.org/Service", "vboxService")); } port = svc.getVboxServicePort(); // reuse this object 0x10 times ttl = 0x10; } // mark as used known.put(port, new Integer(0x10000 | ttl)); return port; } synchronized void releasePort(VboxPortType port) { Integer val = known.get(port); if (val == null || val == 0) { // know you not return; } int v = val; int ttl = v & 0xffff; // decrement TTL, and throw away port if used too much times if (--ttl <= 0) { known.remove(port); } else { v = ttl; // set new TTL and clear busy bit known.put(port, v); } } } public class VirtualBoxManager { private static PortPool pool = new PortPool(true); protected VboxPortType port; private IVirtualBox vbox; private VirtualBoxManager() { } public static void initPerThread() { } public static void deinitPerThread() { } public void connect(String url, String username, String passwd) { this.port = pool.getPort(); try { ((BindingProvider)port).getRequestContext(). put(BindingProvider.ENDPOINT_ADDRESS_PROPERTY, url); String handle = port.iWebsessionManagerLogon(username, passwd); this.vbox = new IVirtualBox(handle, port); } catch (Throwable t) { if (this.port != null) pool.releasePort(this.port); // we have to throw smth derived from RuntimeException throw new VBoxException(t, t.getMessage()); } } public void connect(String url, String username, String passwd, Map requestContext, Map responseContext) { this.port = pool.getPort(); try { ((BindingProvider)port).getRequestContext(); if (requestContext != null) ((BindingProvider)port).getRequestContext().putAll(requestContext); if (responseContext != null) ((BindingProvider)port).getResponseContext().putAll(responseContext); ((BindingProvider)port).getRequestContext(). put(BindingProvider.ENDPOINT_ADDRESS_PROPERTY, url); String handle = port.iWebsessionManagerLogon(username, passwd); this.vbox = new IVirtualBox(handle, port); } catch (Throwable t) { if (this.port != null) pool.releasePort(port); // we have to throw smth derived from RuntimeException throw new VBoxException(t, t.getMessage()); } } public void disconnect() { try { if (this.vbox != null) port.iWebsessionManagerLogoff(this.vbox.getWrapped()); } catch (InvalidObjectFaultMsg e) { throw new VBoxException(e, e.getMessage()); } catch (RuntimeFaultMsg e) { throw new VBoxException(e, e.getMessage()); } finally { if (this.port != null) { pool.releasePort(this.port); this.port = null; } } } public IVirtualBox getVBox() { return this.vbox; } public ISession getSessionObject() { if (this.vbox == null) throw new RuntimeException("connect first"); try { String handle = port.iWebsessionManagerGetSessionObject(this.vbox.getWrapped()); return new ISession(handle, port); } catch (InvalidObjectFaultMsg e) { throw new VBoxException(e, e.getMessage()); } catch (RuntimeFaultMsg e) { throw new VBoxException(e, e.getMessage()); } } public ISession openMachineSession(IMachine m) throws Exception { ISession s = getSessionObject(); m.lockMachine(s, LockType.Shared); return s; } public void closeMachineSession(ISession s) { if (s != null) s.unlockMachine(); } public static synchronized VirtualBoxManager createInstance(String home) { return new VirtualBoxManager(); } public IEventListener createListener(Object sink) { throw new RuntimeException("no active listeners here"); } public void cleanup() { deinitPerThread(); } public boolean progressBar(IProgress p, int wait) { long end = System.currentTimeMillis() + wait; while (!p.getCompleted()) { p.waitForCompletion(wait); if (System.currentTimeMillis() >= end) return false; } return true; } public boolean startVm(String name, String type, int timeout) { IMachine m = vbox.findMachine(name); if (m == null) return false; ISession session = getSessionObject(); if (type == null) type = "gui"; IProgress p = m.launchVMProcess(session, type, ""); progressBar(p, timeout); session.unlockMachine(); return true; } public void waitForEvents(long tmo) { } } ]]>