From 894f7e52ad5a32684ee4f47b39b03d5bb2bf2a95 Mon Sep 17 00:00:00 2001 From: Doug MacEachern Date: Mon, 4 Dec 2006 21:07:46 +0000 Subject: [PATCH] Add Java wrapper for VMware vmcontrol API --- ChangeLog | 2 + bindings/java/build.xml | 10 +- bindings/java/src/jni/vmware/javavmware.c | 748 ++++++++++++++++++ .../java/src/jni/vmware/vmcontrol_wrapper.c | 203 +++++ .../java/src/jni/vmware/vmcontrol_wrapper.h | 235 ++++++ .../hyperic/sigar/vmware/ConnectParams.java | 37 + .../java/src/org/hyperic/sigar/vmware/VM.java | 334 ++++++++ .../sigar/vmware/VMControlLibrary.java | 134 ++++ .../hyperic/sigar/vmware/VMwareException.java | 29 + .../hyperic/sigar/vmware/VMwareObject.java | 77 ++ .../hyperic/sigar/vmware/VMwareServer.java | 50 ++ 11 files changed, 1858 insertions(+), 1 deletion(-) create mode 100644 bindings/java/src/jni/vmware/javavmware.c create mode 100644 bindings/java/src/jni/vmware/vmcontrol_wrapper.c create mode 100644 bindings/java/src/jni/vmware/vmcontrol_wrapper.h create mode 100644 bindings/java/src/org/hyperic/sigar/vmware/ConnectParams.java create mode 100644 bindings/java/src/org/hyperic/sigar/vmware/VM.java create mode 100644 bindings/java/src/org/hyperic/sigar/vmware/VMControlLibrary.java create mode 100644 bindings/java/src/org/hyperic/sigar/vmware/VMwareException.java create mode 100644 bindings/java/src/org/hyperic/sigar/vmware/VMwareObject.java create mode 100644 bindings/java/src/org/hyperic/sigar/vmware/VMwareServer.java diff --git a/ChangeLog b/ChangeLog index 64d3570f..7b7be5dd 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,5 +1,7 @@ 2006-12-04 Doug MacEachern + * Added Java wrapper for VMware vmcontrol API + * [SIGAR-26] Change Pdh.getFormattedValue() to collect 2 counters if needed 2006-12-03 Doug MacEachern diff --git a/bindings/java/build.xml b/bindings/java/build.xml index 40c1d4b6..dfd0cdcb 100644 --- a/bindings/java/build.xml +++ b/bindings/java/build.xml @@ -150,7 +150,14 @@ - + + + + + + + + @@ -161,6 +168,7 @@ + diff --git a/bindings/java/src/jni/vmware/javavmware.c b/bindings/java/src/jni/vmware/javavmware.c new file mode 100644 index 00000000..b0f8ef84 --- /dev/null +++ b/bindings/java/src/jni/vmware/javavmware.c @@ -0,0 +1,748 @@ +/* + * Copyright (C) [2004, 2005, 2006], Hyperic, Inc. + * This file is part of SIGAR. + * + * SIGAR is free software; you can redistribute it and/or modify + * it under the terms version 2 of the GNU General Public License as + * published by the Free Software Foundation. This program is distributed + * in the hope that it will be useful, but WITHOUT ANY WARRANTY; without + * even the implied warranty of MERCHANTABILITY or FITNESS FOR A + * PARTICULAR PURPOSE. See the GNU General Public License for more + * details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 + * USA. + */ + +#if defined(WIN32) || defined(__linux__) + +#include +#include + +#include "vmcontrol_wrapper.h" + +#define JENV (*env) + +#define VMWARE_PACKAGE "org/hyperic/sigar/vmware/" + +#define VMWARE_JNI(m) JNICALL Java_org_hyperic_sigar_vmware_##m + +#define VMWARE_FIND_CLASS(name) \ + JENV->FindClass(env, VMWARE_PACKAGE name) + +#define VMWARE_CLASS_SIG(name) \ + "L" VMWARE_PACKAGE name ";" + +#define VMWARE_EX_SERVER 1 +#define VMWARE_EX_VM 2 + +static void vmware_throw_last_error(JNIEnv *env, void *ptr, int type) +{ + jclass errorClass = VMWARE_FIND_CLASS("VMwareException"); + char *msg; + int retval; + + switch (type) { + case VMWARE_EX_SERVER: + retval = VMControl_ServerGetLastError((VMControlServer *)ptr, + &msg); + break; + case VMWARE_EX_VM: + retval = VMControl_VMGetLastError((VMControlVM *)ptr, + &msg); + break; + } + + JENV->ThrowNew(env, errorClass, msg); + free(msg); +} + +#define vmware_throw_last_vm_error() \ + vmware_throw_last_error(env, vm, VMWARE_EX_VM); + +#define vmware_throw_last_server_error() \ + vmware_throw_last_error(env, server, VMWARE_EX_SERVER) + +static jint vmware_get_pointer(JNIEnv *env, jobject obj) +{ + jclass cls = JENV->GetObjectClass(env, obj); + jfieldID field = JENV->GetFieldID(env, cls, "ptr", "I"); + return JENV->GetIntField(env, obj, field); +} + +#define dVM(obj) \ + VMControlVM *vm = \ + (VMControlVM *)vmware_get_pointer(env, obj) + +#define dSERVER(obj) \ + VMControlServer *server = \ + (VMControlServer *)vmware_get_pointer(env, obj) + +#define dPARAMS(obj) \ + VMControlConnectParams *params = \ + (VMControlConnectParams *)vmware_get_pointer(env, obj) + +JNIEXPORT jboolean VMWARE_JNI(VMwareObject_init) +(JNIEnv *env, jclass classinstance, jstring jlib) +{ + const char *lib; + int retval; + + if (jlib) { + lib = JENV->GetStringUTFChars(env, jlib, NULL); + } + else { + lib = getenv("VMCONTROL_SHLIB"); + } + + retval = vmcontrol_wrapper_api_init(lib); + + if (jlib) { + JENV->ReleaseStringUTFChars(env, jlib, lib); + } + + if (retval != 0) { + return JNI_FALSE; + } + + return VMControl_Init() && VMControl_VMInit(); +} + +JNIEXPORT jint VMWARE_JNI(ConnectParams_create) +(JNIEnv *env, jclass classinstance, + jstring jhost, jint port, jstring juser, jstring jpass) +{ + jint ptr; + const char *host=NULL; + const char *user=NULL; + const char *pass=NULL; + + if (jhost) { + host = JENV->GetStringUTFChars(env, jhost, NULL); + } + if (juser) { + user = JENV->GetStringUTFChars(env, juser, NULL); + } + if (jpass) { + pass = JENV->GetStringUTFChars(env, jpass, NULL); + } + + ptr = (jint)VMControl_ConnectParamsNew(host, port, user, pass); + + if (host) { + JENV->ReleaseStringUTFChars(env, jhost, host); + } + if (user) { + JENV->ReleaseStringUTFChars(env, juser, user); + } + if (pass) { + JENV->ReleaseStringUTFChars(env, jpass, pass); + } + + return ptr; +} + +JNIEXPORT void VMWARE_JNI(ConnectParams_destroy) +(JNIEnv *env, jobject obj) +{ + dPARAMS(obj); + VMControl_ConnectParamsDestroy(params); +} + +JNIEXPORT jint VMWARE_JNI(VMwareServer_create) +(JNIEnv *env, jclass classinstance) +{ + return (jint)VMControl_ServerNewEx(); +} + +JNIEXPORT void VMWARE_JNI(VMwareServer_destroy) +(JNIEnv *env, jclass obj) +{ + dSERVER(obj); + VMControl_ServerDestroy(server); +} + +JNIEXPORT void VMWARE_JNI(VMwareServer_disconnect) +(JNIEnv *env, jclass obj) +{ + dSERVER(obj); + VMControl_ServerDisconnect(server); +} + +JNIEXPORT jboolean VMWARE_JNI(VMwareServer_isConnected) +(JNIEnv *env, jclass obj) +{ + dSERVER(obj); + return VMControl_ServerIsConnected(server); +} + +JNIEXPORT void VMWARE_JNI(VMwareServer_connect) +(JNIEnv *env, jobject obj, jobject params_obj) +{ + dSERVER(obj); + dPARAMS(params_obj); + + if (!VMControl_ServerConnectEx(server, params)) { + vmware_throw_last_server_error(); + } +} + +JNIEXPORT jboolean VMWARE_JNI(VMwareServer_isRegistered) +(JNIEnv *env, jclass obj, jstring jconfig) +{ + dSERVER(obj); + const char *config = + JENV->GetStringUTFChars(env, jconfig, NULL); + jboolean value; + jboolean retval = + VMControl_ServerIsRegistered(server, config, &value); + + JENV->ReleaseStringUTFChars(env, jconfig, config); + + if (!retval) { + vmware_throw_last_server_error(); + return JNI_FALSE; + } + + return value; +} + +JNIEXPORT jobject VMWARE_JNI(VMwareServer_getRegisteredVmNames) +(JNIEnv *env, jclass obj) +{ + dSERVER(obj); + char **ptr, **names; + jobject listobj; + jclass listclass = + JENV->FindClass(env, "java/util/ArrayList"); + jmethodID listid = + JENV->GetMethodID(env, listclass, "", "()V"); + jmethodID addid = + JENV->GetMethodID(env, listclass, "add", + "(Ljava/lang/Object;)Z"); + + listobj = JENV->NewObject(env, listclass, listid); + + ptr = names = VMControl_ServerEnumerate(server); + + if (ptr) { + while (*ptr) { + JENV->CallBooleanMethod(env, listobj, addid, + JENV->NewStringUTF(env, *ptr)); + if (JENV->ExceptionOccurred(env)) { + JENV->ExceptionDescribe(env); + } + free(*ptr); + ptr++; + } + free(names); + } + + return listobj; +} + +JNIEXPORT jstring VMWARE_JNI(VMwareServer_getResource) +(JNIEnv *env, jclass obj, jstring jkey) +{ + dSERVER(obj); + jstring retval; + const char *key = + JENV->GetStringUTFChars(env, jkey, NULL); + char *value = + VMControl_ServerGetResource(server, (char *)key); + + JENV->ReleaseStringUTFChars(env, jkey, key); + + if (!value) { + vmware_throw_last_server_error(); + return NULL; + } + + retval = JENV->NewStringUTF(env, value); + free(value); + return retval; +} + +JNIEXPORT jstring VMWARE_JNI(VMwareServer_exec) +(JNIEnv *env, jclass obj, jstring jxml) +{ + dSERVER(obj); + jstring retval; + const char *xml = + JENV->GetStringUTFChars(env, jxml, NULL); + char *value = + VMControl_ServerExec(server, xml); + + JENV->ReleaseStringUTFChars(env, jxml, xml); + + if (!value) { + vmware_throw_last_server_error(); + return NULL; + } + + retval = JENV->NewStringUTF(env, value); + free(value); + return retval; +} + +JNIEXPORT jint VMWARE_JNI(VM_create) +(JNIEnv *env, jclass classinstance) +{ + return (jint)VMControl_VMNewEx(); +} + +JNIEXPORT void VMWARE_JNI(VM_destroy) +(JNIEnv *env, jclass obj) +{ + dVM(obj); + VMControl_VMDestroy(vm); +} + +JNIEXPORT void VMWARE_JNI(VM_disconnect) +(JNIEnv *env, jclass obj) +{ + dVM(obj); + VMControl_VMDisconnect(vm); +} + +JNIEXPORT jboolean VMWARE_JNI(VM_isConnected) +(JNIEnv *env, jclass obj) +{ + dVM(obj); + return VMControl_VMIsConnected(vm); +} + +JNIEXPORT void VMWARE_JNI(VM_connect) +(JNIEnv *env, jobject obj, jobject params_obj, + jstring jconfig, jint mks) +{ + dVM(obj); + dPARAMS(params_obj); + const char *config = + JENV->GetStringUTFChars(env, jconfig, NULL); + + jboolean retval = + VMControl_VMConnectEx(vm, params, config, mks); + + JENV->ReleaseStringUTFChars(env, jconfig, config); + + if (!retval) { + vmware_throw_last_vm_error(); + } +} + +JNIEXPORT jint VMWARE_JNI(VM_getExecutionState) +(JNIEnv *env, jclass obj) +{ + dVM(obj); + unsigned int state; + + if (!VMControl_VMGetExecutionState(vm, &state)) { + vmware_throw_last_vm_error(); + return -1; + } + + return state; +} + +JNIEXPORT jint VMWARE_JNI(VM_getRemoteConnections) +(JNIEnv *env, jclass obj) +{ + dVM(obj); + unsigned int num; + + if (!VMControl_VMGetRemoteConnections(vm, &num)) { + vmware_throw_last_vm_error(); + return -1; + } + + return num; +} + +JNIEXPORT jint VMWARE_JNI(VM_getUptime) +(JNIEnv *env, jclass obj) +{ + dVM(obj); + unsigned int uptime; + + if (!VMControl_VMGetUptime(vm, &uptime)) { + vmware_throw_last_vm_error(); + return -1; + } + + return uptime; +} + +JNIEXPORT jint VMWARE_JNI(VM_getHeartbeat) +(JNIEnv *env, jclass obj) +{ + dVM(obj); + unsigned int heartbeat; + + if (!VMControl_VMGetHeartbeat(vm, &heartbeat)) { + vmware_throw_last_vm_error(); + return -1; + } + + return heartbeat; +} + +JNIEXPORT jint VMWARE_JNI(VM_getToolsLastActive) +(JNIEnv *env, jclass obj) +{ + dVM(obj); + unsigned int seconds; + + if (!VMControl_VMToolsLastActive(vm, &seconds)) { + vmware_throw_last_vm_error(); + return -1; + } + + return seconds; +} + +JNIEXPORT jstring VMWARE_JNI(VM_getRunAsUser) +(JNIEnv *env, jclass obj) +{ + dVM(obj); + char *user; + jstring juser; + + if (!VMControl_VMGetRunAsUser(vm, &user)) { + vmware_throw_last_vm_error(); + return NULL; + } + + juser = JENV->NewStringUTF(env, user); + free(user); + return juser; +} + +JNIEXPORT jint VMWARE_JNI(VM_getPermissions) +(JNIEnv *env, jclass obj) +{ + dVM(obj); + unsigned int permissions; + + if (!VMControl_VMGetCapabilities(vm, &permissions)) { + vmware_throw_last_vm_error(); + return -1; + } + + return permissions; +} + +JNIEXPORT jstring VMWARE_JNI(VM_getConfig) +(JNIEnv *env, jclass obj, jstring jkey) +{ + dVM(obj); + jstring retval; + const char *key = + JENV->GetStringUTFChars(env, jkey, NULL); + char *value = + VMControl_VMGetConfig(vm, (char *)key); + + JENV->ReleaseStringUTFChars(env, jkey, key); + + if (!value) { + vmware_throw_last_vm_error(); + return NULL; + } + + retval = JENV->NewStringUTF(env, value); + free(value); + return retval; +} + +JNIEXPORT void VMWARE_JNI(VM_setConfig) +(JNIEnv *env, jclass obj, jstring jkey, jstring jvalue) +{ + dVM(obj); + jboolean retval; + const char *key = + JENV->GetStringUTFChars(env, jkey, NULL); + const char *value = + JENV->GetStringUTFChars(env, jvalue, NULL); + + retval = VMControl_VMSetConfig(vm, (char *)key, (char *)value); + + JENV->ReleaseStringUTFChars(env, jkey, key); + JENV->ReleaseStringUTFChars(env, jvalue, value); + + if (!retval) { + vmware_throw_last_vm_error(); + } +} + +JNIEXPORT jstring VMWARE_JNI(VM_getResource) +(JNIEnv *env, jclass obj, jstring jkey) +{ + dVM(obj); + jstring retval; + const char *key = + JENV->GetStringUTFChars(env, jkey, NULL); + char *value = + VMControl_VMGetResource(vm, (char *)key); + + JENV->ReleaseStringUTFChars(env, jkey, key); + + if (!value) { + vmware_throw_last_vm_error(); + return NULL; + } + + retval = JENV->NewStringUTF(env, value); + free(value); + return retval; +} + +JNIEXPORT jstring VMWARE_JNI(VM_getGuestInfo) +(JNIEnv *env, jclass obj, jstring jkey) +{ + dVM(obj); + jstring retval; + const char *key = + JENV->GetStringUTFChars(env, jkey, NULL); + char *value = + VMControl_VMGetGuestInfo(vm, (char *)key); + + JENV->ReleaseStringUTFChars(env, jkey, key); + + if (!value) { + vmware_throw_last_vm_error(); + return NULL; + } + + retval = JENV->NewStringUTF(env, value); + free(value); + return retval; +} + +JNIEXPORT void VMWARE_JNI(VM_setGuestInfo) +(JNIEnv *env, jclass obj, jstring jkey, jstring jvalue) +{ + dVM(obj); + jboolean retval; + const char *key = + JENV->GetStringUTFChars(env, jkey, NULL); + const char *value = + JENV->GetStringUTFChars(env, jvalue, NULL); + + retval = VMControl_VMSetGuestInfo(vm, (char *)key, (char *)value); + + JENV->ReleaseStringUTFChars(env, jkey, key); + JENV->ReleaseStringUTFChars(env, jvalue, value); + + if (!retval) { + vmware_throw_last_vm_error(); + } +} + +JNIEXPORT jint VMWARE_JNI(VM_getProductInfo) +(JNIEnv *env, jclass obj, jint type) +{ + dVM(obj); + unsigned int value; + + if (!VMControl_VMGetProductInfo(vm, type, &value)) { + vmware_throw_last_vm_error(); + return -1; + } + + return value; +} + +JNIEXPORT void VMWARE_JNI(VM_start) +(JNIEnv *env, jclass obj, jint mode) +{ + dVM(obj); + + if (!VMControl_VMStart(vm, mode)) { + vmware_throw_last_vm_error(); + } +} + +JNIEXPORT void VMWARE_JNI(VM_stop) +(JNIEnv *env, jclass obj, jint mode) +{ + dVM(obj); + + if (!VMControl_VMStopOrReset(vm, 1, mode)) { + vmware_throw_last_vm_error(); + } +} + +JNIEXPORT void VMWARE_JNI(VM_reset) +(JNIEnv *env, jclass obj, jint mode) +{ + dVM(obj); + + if (!VMControl_VMStopOrReset(vm, 0, mode)) { + vmware_throw_last_vm_error(); + } +} + +JNIEXPORT void VMWARE_JNI(VM_suspend) +(JNIEnv *env, jclass obj, jint mode) +{ + dVM(obj); + + if (!VMControl_VMSuspendToDisk(vm, mode)) { + vmware_throw_last_vm_error(); + } +} + +JNIEXPORT void VMWARE_JNI(VM_createSnapshot) +(JNIEnv *env, jclass obj, + jstring jname, jstring jdescr, + jboolean quiesce, jboolean memory) +{ + dVM(obj); + const char *name = + JENV->GetStringUTFChars(env, jname, NULL); + const char *descr = + JENV->GetStringUTFChars(env, jdescr, NULL); + + if (!VMControl_VMCreateSnapshot(vm, name, descr, quiesce, memory)) { + vmware_throw_last_vm_error(); + } + + JENV->ReleaseStringUTFChars(env, jname, name); + JENV->ReleaseStringUTFChars(env, jdescr, descr); +} + +JNIEXPORT void VMWARE_JNI(VM_revertToSnapshot) +(JNIEnv *env, jclass obj) +{ + dVM(obj); + + if (!VMControl_VMRevertToSnapshot(vm)) { + vmware_throw_last_vm_error(); + } +} + +JNIEXPORT void VMWARE_JNI(VM_removeAllSnapshots) +(JNIEnv *env, jclass obj) +{ + dVM(obj); + + if (!VMControl_VMRemoveAllSnapshots(vm)) { + vmware_throw_last_vm_error(); + } +} + +JNIEXPORT jboolean VMWARE_JNI(VM_hasSnapshot) +(JNIEnv *env, jclass obj) +{ + dVM(obj); + jboolean value; + + if (!VMControl_VMHasSnapshot(vm, &value)) { + vmware_throw_last_vm_error(); + return JNI_FALSE; + } + + return value; +} + +JNIEXPORT jlong VMWARE_JNI(VM_getPid) +(JNIEnv *env, jclass obj) +{ + dVM(obj); + unsigned int pid; + + if (!VMControl_VMGetPid(vm, &pid)) { + vmware_throw_last_vm_error(); + return -1; + } + + return (jlong)pid; +} + +JNIEXPORT jint VMWARE_JNI(VM_getId) +(JNIEnv *env, jclass obj) +{ + dVM(obj); + unsigned int id; + + if (!VMControl_VMGetId(vm, &id)) { + vmware_throw_last_vm_error(); + return -1; + } + + return id; +} + +JNIEXPORT void VMWARE_JNI(VM_saveScreenshot) +(JNIEnv *env, jclass obj, jstring jname) +{ + dVM(obj); + jboolean retval; + const char *name = + JENV->GetStringUTFChars(env, jname, NULL); + + retval = VMControl_MKSSaveScreenshot(vm, name, "PNG"); + + JENV->ReleaseStringUTFChars(env, jname, name); + + if (!retval) { + vmware_throw_last_vm_error(); + } +} + +JNIEXPORT void VMWARE_JNI(VM_deviceConnect) +(JNIEnv *env, jclass obj, jstring jdevice) +{ + dVM(obj); + const char *device = + JENV->GetStringUTFChars(env, jdevice, NULL); + jboolean retval = + VMControl_VMDeviceConnect(vm, device); + + JENV->ReleaseStringUTFChars(env, jdevice, device); + + if (!retval) { + vmware_throw_last_vm_error(); + } +} + +JNIEXPORT void VMWARE_JNI(VM_deviceDisconnect) +(JNIEnv *env, jclass obj, jstring jdevice) +{ + dVM(obj); + const char *device = + JENV->GetStringUTFChars(env, jdevice, NULL); + jboolean retval = + VMControl_VMDeviceDisconnect(vm, device); + + JENV->ReleaseStringUTFChars(env, jdevice, device); + + if (!retval) { + vmware_throw_last_vm_error(); + } +} + +JNIEXPORT jboolean VMWARE_JNI(VM_deviceIsConnected) +(JNIEnv *env, jclass obj, jstring jdevice) +{ + dVM(obj); + const char *device = + JENV->GetStringUTFChars(env, jdevice, NULL); + jboolean isConnected; + jboolean retval = + VMControl_VMDeviceIsConnected(vm, device, + &isConnected); + + JENV->ReleaseStringUTFChars(env, jdevice, device); + + if (!retval) { + vmware_throw_last_vm_error(); + return JNI_FALSE; + } + + return isConnected; +} + +#endif /* WIN32 || linux */ diff --git a/bindings/java/src/jni/vmware/vmcontrol_wrapper.c b/bindings/java/src/jni/vmware/vmcontrol_wrapper.c new file mode 100644 index 00000000..0a985b8b --- /dev/null +++ b/bindings/java/src/jni/vmware/vmcontrol_wrapper.c @@ -0,0 +1,203 @@ +/* + * Copyright (C) [2004, 2005, 2006], Hyperic, Inc. + * This file is part of SIGAR. + * + * SIGAR is free software; you can redistribute it and/or modify + * it under the terms version 2 of the GNU General Public License as + * published by the Free Software Foundation. This program is distributed + * in the hope that it will be useful, but WITHOUT ANY WARRANTY; without + * even the implied warranty of MERCHANTABILITY or FITNESS FOR A + * PARTICULAR PURPOSE. See the GNU General Public License for more + * details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 + * USA. + */ + +#if defined(WIN32) || defined(__linux__) + +#ifdef WIN32 +#include +#else +#include +#include +#include +#include +#include +#endif + +#include "vmcontrol_wrapper.h" + +#ifdef WIN32 +#define DL_LOAD(lib) LoadLibrary(lib) +#define DL_CLOSE(h) FreeLibrary(h) +#define DL_SYM GetProcAddress +#define DL_ERR GetLastError() +#define DL_ENOENT ERROR_FILE_NOT_FOUND +#else +#define DL_LOAD(lib) dlopen(lib, RTLD_LAZY) +#define DL_CLOSE(h) dlclose(h) +#define DL_SYM dlsym +#define DL_ERR errno +#define DL_ENOENT ENOENT +#endif + +typedef struct { + const char *name; + unsigned long offset; + const char *alias; +} vmcontrol_entry_t; + +#define OffsetOf(structure, field) \ + (unsigned long)(&((structure *)NULL)->field) + +#define VMCONTROL_ENTRY(name) \ + { #name, OffsetOf(vmcontrol_wrapper_api_t, x##name), NULL } + +#define VMCONTROL_ENTRY_ALIAS(name, alias) \ + { #name, OffsetOf(vmcontrol_wrapper_api_t, x##name), alias } + +static vmcontrol_entry_t vmcontrol_entries[] = { + VMCONTROL_ENTRY(VMControl_ConnectParamsDestroy), + VMCONTROL_ENTRY(VMControl_ConnectParamsNew), + VMCONTROL_ENTRY(VMControl_Init), + VMCONTROL_ENTRY(VMControl_MKSSaveScreenshot), + VMCONTROL_ENTRY(VMControl_ServerConnectEx), + VMCONTROL_ENTRY(VMControl_ServerDestroy), + VMCONTROL_ENTRY(VMControl_ServerDisconnect), + VMCONTROL_ENTRY(VMControl_ServerEnumerate), + VMCONTROL_ENTRY(VMControl_ServerExec), + VMCONTROL_ENTRY(VMControl_ServerGetLastError), + VMCONTROL_ENTRY(VMControl_ServerGetResource), + VMCONTROL_ENTRY(VMControl_ServerIsConnected), + VMCONTROL_ENTRY(VMControl_ServerIsRegistered), + VMCONTROL_ENTRY(VMControl_ServerNewEx), + VMCONTROL_ENTRY(VMControl_VMConnectEx), + VMCONTROL_ENTRY(VMControl_VMCreateSnapshot), + VMCONTROL_ENTRY(VMControl_VMDestroy), + VMCONTROL_ENTRY(VMControl_VMDeviceConnect), + VMCONTROL_ENTRY(VMControl_VMDeviceDisconnect), + VMCONTROL_ENTRY(VMControl_VMDeviceIsConnected), + VMCONTROL_ENTRY(VMControl_VMDisconnect), + VMCONTROL_ENTRY(VMControl_VMGetCapabilities), + VMCONTROL_ENTRY(VMControl_VMGetConfig), + VMCONTROL_ENTRY(VMControl_VMGetConfigFileName), + VMCONTROL_ENTRY(VMControl_VMGetExecutionState), + VMCONTROL_ENTRY(VMControl_VMGetGuestInfo), + VMCONTROL_ENTRY(VMControl_VMGetHeartbeat), + VMCONTROL_ENTRY(VMControl_VMGetId), + VMCONTROL_ENTRY(VMControl_VMGetLastError), + VMCONTROL_ENTRY(VMControl_VMGetPid), + VMCONTROL_ENTRY(VMControl_VMGetProductInfo), + VMCONTROL_ENTRY(VMControl_VMGetRemoteConnections), + VMCONTROL_ENTRY(VMControl_VMGetResource), + VMCONTROL_ENTRY(VMControl_VMGetRunAsUser), + VMCONTROL_ENTRY(VMControl_VMGetUptime), + VMCONTROL_ENTRY(VMControl_VMHasSnapshot), + VMCONTROL_ENTRY(VMControl_VMInit), + VMCONTROL_ENTRY(VMControl_VMIsConnected), + VMCONTROL_ENTRY(VMControl_VMNewEx), + VMCONTROL_ENTRY_ALIAS(VMControl_VMRemoveAllSnapshots, + "VMControl_VMDeleteSnapshot"), + VMCONTROL_ENTRY_ALIAS(VMControl_VMRevertToSnapshot, + "VMControl_VMRevertSnapshot"), + VMCONTROL_ENTRY(VMControl_VMSetConfig), + VMCONTROL_ENTRY(VMControl_VMSetGuestInfo), + VMCONTROL_ENTRY(VMControl_VMStart), + VMCONTROL_ENTRY(VMControl_VMStopOrReset), + VMCONTROL_ENTRY(VMControl_VMSuspendToDisk), + VMCONTROL_ENTRY(VMControl_VMToolsLastActive), + { NULL, 0, NULL } +}; + +static vmcontrol_wrapper_api_t *vmcontrol_api = NULL; + +vmcontrol_wrapper_api_t *vmcontrol_wrapper_api_get(void) +{ + return vmcontrol_api; +} + +typedef void (*any_function_t)(void); + +static int unsupported_function(void *obj, ...) +{ + return 0; +} + +int vmcontrol_wrapper_api_init(const char *lib) +{ + int i; + char *api; + + if (vmcontrol_api) { + return 0; + } + + if (!lib) { /* sanity check */ + return DL_ENOENT; + } + + vmcontrol_api = malloc(sizeof(*vmcontrol_api)); + api = (char *)vmcontrol_api; + memset(vmcontrol_api, 0, sizeof(*vmcontrol_api)); + + if (!(vmcontrol_api->handle = DL_LOAD(lib))) { + return DL_ERR; + } + + for (i=0; vmcontrol_entries[i].name; i++) { + any_function_t *ptr = + (any_function_t *)(api + (int)(long)vmcontrol_entries[i].offset); + + *ptr = + (any_function_t)DL_SYM(vmcontrol_api->handle, + vmcontrol_entries[i].name); + + if ((*ptr == NULL) && vmcontrol_entries[i].alias) { + *ptr = + (any_function_t)DL_SYM(vmcontrol_api->handle, + vmcontrol_entries[i].alias); + +#ifdef DL_DEBUG + printf("%s -> %s\n", + vmcontrol_entries[i].name, + vmcontrol_entries[i].alias); +#endif + } + + if (!*ptr) { +#ifdef DL_DEBUG + printf("%s -> UNDEFINED\n", vmcontrol_entries[i].name); +#endif + *ptr = (any_function_t)unsupported_function; + } + } + + if ((void *)vmcontrol_api->xVMControl_VMInit == (void *)&unsupported_function) { +#ifdef DL_DEBUG + printf("%s unuseable\n", lib); +#endif + vmcontrol_wrapper_api_shutdown(); + return DL_ENOENT; + } + + return 0; +} + +int vmcontrol_wrapper_api_shutdown(void) +{ + if (vmcontrol_api) { + if (vmcontrol_api->handle) { + DL_CLOSE(vmcontrol_api->handle); + } + + free(vmcontrol_api); + vmcontrol_api = NULL; + } + + return 0; +} + +#endif /* WIN32 || linux */ diff --git a/bindings/java/src/jni/vmware/vmcontrol_wrapper.h b/bindings/java/src/jni/vmware/vmcontrol_wrapper.h new file mode 100644 index 00000000..56b7aa2e --- /dev/null +++ b/bindings/java/src/jni/vmware/vmcontrol_wrapper.h @@ -0,0 +1,235 @@ +/* + * Copyright (C) [2004, 2005, 2006], Hyperic, Inc. + * This file is part of SIGAR. + * + * SIGAR is free software; you can redistribute it and/or modify + * it under the terms version 2 of the GNU General Public License as + * published by the Free Software Foundation. This program is distributed + * in the hope that it will be useful, but WITHOUT ANY WARRANTY; without + * even the implied warranty of MERCHANTABILITY or FITNESS FOR A + * PARTICULAR PURPOSE. See the GNU General Public License for more + * details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 + * USA. + */ + +#ifndef VMCONTROL_WRAPPER_H +#define VMCONTROL_WRAPPER_H + +/* types defined by vmcontrol.h */ +typedef struct VMControlServer VMControlServer; + +typedef struct VMControlVM VMControlVM; + +typedef char Bool; + +typedef struct { + const char *hostname; + int port; + const char *username; + char *password; +} VMControlConnectParams; + +/* function pointers to api defined by vmcontrol.h */ +typedef struct { + void *handle; + void (*xVMControl_ConnectParamsDestroy)(VMControlConnectParams *); + VMControlConnectParams * (*xVMControl_ConnectParamsNew)(const char *, int, const char *, const char *); + Bool (*xVMControl_Init)(void); + Bool (*xVMControl_MKSSaveScreenshot)(VMControlVM *, const char *, const char *); + Bool (*xVMControl_ServerConnectEx)(VMControlServer *, VMControlConnectParams *); + void (*xVMControl_ServerDestroy)(VMControlServer *); + void (*xVMControl_ServerDisconnect)(VMControlServer *); + char ** (*xVMControl_ServerEnumerate)(VMControlServer *); + char * (*xVMControl_ServerExec)(VMControlServer *, const char *); + int (*xVMControl_ServerGetLastError)(VMControlServer *, char **); + char * (*xVMControl_ServerGetResource)(VMControlServer *, char *); + Bool (*xVMControl_ServerIsConnected)(VMControlServer *); + Bool (*xVMControl_ServerIsRegistered)(VMControlServer *, const char *, Bool *); + VMControlServer * (*xVMControl_ServerNewEx)(void); + Bool (*xVMControl_VMConnectEx)(VMControlVM *, VMControlConnectParams *, const char *, Bool); + Bool (*xVMControl_VMCreateSnapshot)(VMControlVM *, const char *, const char *, Bool, Bool); + void (*xVMControl_VMDestroy)(VMControlVM *); + Bool (*xVMControl_VMDeviceConnect)(VMControlVM *, const char *); + Bool (*xVMControl_VMDeviceDisconnect)(VMControlVM *, const char *); + Bool (*xVMControl_VMDeviceIsConnected)(VMControlVM *, const char *, Bool *); + void (*xVMControl_VMDisconnect)(VMControlVM *); + Bool (*xVMControl_VMGetCapabilities)(VMControlVM *, unsigned int *); + char * (*xVMControl_VMGetConfig)(VMControlVM *, char *); + char * (*xVMControl_VMGetConfigFileName)(VMControlVM *); + Bool (*xVMControl_VMGetExecutionState)(VMControlVM *, int *); + char * (*xVMControl_VMGetGuestInfo)(VMControlVM *, char *); + Bool (*xVMControl_VMGetHeartbeat)(VMControlVM *, unsigned int *); + Bool (*xVMControl_VMGetId)(VMControlVM *, unsigned int *); + int (*xVMControl_VMGetLastError)(VMControlVM *, char **); + Bool (*xVMControl_VMGetPid)(VMControlVM *, unsigned int * ); + Bool (*xVMControl_VMGetProductInfo)(VMControlVM *, int, int *); + Bool (*xVMControl_VMGetRemoteConnections)(VMControlVM *, unsigned int *); + char * (*xVMControl_VMGetResource)(VMControlVM *, char *); + Bool (*xVMControl_VMGetRunAsUser)(VMControlVM *, char **); + Bool (*xVMControl_VMGetUptime)(VMControlVM *, unsigned int *); + Bool (*xVMControl_VMHasSnapshot)(VMControlVM *, Bool *); + char (*xVMControl_VMInit)(void); + Bool (*xVMControl_VMIsConnected)(VMControlVM *); + VMControlVM * (*xVMControl_VMNewEx)(void); + Bool (*xVMControl_VMRemoveAllSnapshots)(VMControlVM *); + Bool (*xVMControl_VMRevertToSnapshot)(VMControlVM *); + Bool (*xVMControl_VMSetConfig)(VMControlVM *, char *, char *); + Bool (*xVMControl_VMSetGuestInfo)(VMControlVM *, char *, char *); + Bool (*xVMControl_VMStart)(VMControlVM *, int); + Bool (*xVMControl_VMStopOrReset)(VMControlVM *, Bool, int); + Bool (*xVMControl_VMSuspendToDisk)(VMControlVM *, int); + Bool (*xVMControl_VMToolsLastActive)(VMControlVM *, int *); +} vmcontrol_wrapper_api_t; + +int vmcontrol_wrapper_api_init(const char *lib); + +int vmcontrol_wrapper_api_shutdown(void); + +vmcontrol_wrapper_api_t *vmcontrol_wrapper_api_get(void); + +#define VMControl_ConnectParamsDestroy \ + vmcontrol_wrapper_api_get()->xVMControl_ConnectParamsDestroy + +#define VMControl_ConnectParamsNew \ + vmcontrol_wrapper_api_get()->xVMControl_ConnectParamsNew + +#define VMControl_Init \ + vmcontrol_wrapper_api_get()->xVMControl_Init + +#define VMControl_MKSSaveScreenshot \ + vmcontrol_wrapper_api_get()->xVMControl_MKSSaveScreenshot + +#define VMControl_ServerConnectEx \ + vmcontrol_wrapper_api_get()->xVMControl_ServerConnectEx + +#define VMControl_ServerDestroy \ + vmcontrol_wrapper_api_get()->xVMControl_ServerDestroy + +#define VMControl_ServerDisconnect \ + vmcontrol_wrapper_api_get()->xVMControl_ServerDisconnect + +#define VMControl_ServerEnumerate \ + vmcontrol_wrapper_api_get()->xVMControl_ServerEnumerate + +#define VMControl_ServerExec \ + vmcontrol_wrapper_api_get()->xVMControl_ServerExec + +#define VMControl_ServerGetLastError \ + vmcontrol_wrapper_api_get()->xVMControl_ServerGetLastError + +#define VMControl_ServerGetResource \ + vmcontrol_wrapper_api_get()->xVMControl_ServerGetResource + +#define VMControl_ServerIsConnected \ + vmcontrol_wrapper_api_get()->xVMControl_ServerIsConnected + +#define VMControl_ServerIsRegistered \ + vmcontrol_wrapper_api_get()->xVMControl_ServerIsRegistered + +#define VMControl_ServerNewEx \ + vmcontrol_wrapper_api_get()->xVMControl_ServerNewEx + +#define VMControl_VMConnectEx \ + vmcontrol_wrapper_api_get()->xVMControl_VMConnectEx + +#define VMControl_VMCreateSnapshot \ + vmcontrol_wrapper_api_get()->xVMControl_VMCreateSnapshot + +#define VMControl_VMDestroy \ + vmcontrol_wrapper_api_get()->xVMControl_VMDestroy + +#define VMControl_VMDeviceConnect \ + vmcontrol_wrapper_api_get()->xVMControl_VMDeviceConnect + +#define VMControl_VMDeviceDisconnect \ + vmcontrol_wrapper_api_get()->xVMControl_VMDeviceDisconnect + +#define VMControl_VMDeviceIsConnected \ + vmcontrol_wrapper_api_get()->xVMControl_VMDeviceIsConnected + +#define VMControl_VMDisconnect \ + vmcontrol_wrapper_api_get()->xVMControl_VMDisconnect + +#define VMControl_VMGetCapabilities \ + vmcontrol_wrapper_api_get()->xVMControl_VMGetCapabilities + +#define VMControl_VMGetConfig \ + vmcontrol_wrapper_api_get()->xVMControl_VMGetConfig + +#define VMControl_VMGetConfigFileName \ + vmcontrol_wrapper_api_get()->xVMControl_VMGetConfigFileName + +#define VMControl_VMGetExecutionState \ + vmcontrol_wrapper_api_get()->xVMControl_VMGetExecutionState + +#define VMControl_VMGetGuestInfo \ + vmcontrol_wrapper_api_get()->xVMControl_VMGetGuestInfo + +#define VMControl_VMGetHeartbeat \ + vmcontrol_wrapper_api_get()->xVMControl_VMGetHeartbeat + +#define VMControl_VMGetId \ + vmcontrol_wrapper_api_get()->xVMControl_VMGetId + +#define VMControl_VMGetLastError \ + vmcontrol_wrapper_api_get()->xVMControl_VMGetLastError + +#define VMControl_VMGetPid \ + vmcontrol_wrapper_api_get()->xVMControl_VMGetPid + +#define VMControl_VMGetProductInfo \ + vmcontrol_wrapper_api_get()->xVMControl_VMGetProductInfo + +#define VMControl_VMGetRemoteConnections \ + vmcontrol_wrapper_api_get()->xVMControl_VMGetRemoteConnections + +#define VMControl_VMGetResource \ + vmcontrol_wrapper_api_get()->xVMControl_VMGetResource + +#define VMControl_VMGetRunAsUser \ + vmcontrol_wrapper_api_get()->xVMControl_VMGetRunAsUser + +#define VMControl_VMGetUptime \ + vmcontrol_wrapper_api_get()->xVMControl_VMGetUptime + +#define VMControl_VMHasSnapshot \ + vmcontrol_wrapper_api_get()->xVMControl_VMHasSnapshot + +#define VMControl_VMInit \ + vmcontrol_wrapper_api_get()->xVMControl_VMInit + +#define VMControl_VMIsConnected \ + vmcontrol_wrapper_api_get()->xVMControl_VMIsConnected + +#define VMControl_VMNewEx \ + vmcontrol_wrapper_api_get()->xVMControl_VMNewEx + +#define VMControl_VMRemoveAllSnapshots \ + vmcontrol_wrapper_api_get()->xVMControl_VMRemoveAllSnapshots + +#define VMControl_VMRevertToSnapshot \ + vmcontrol_wrapper_api_get()->xVMControl_VMRevertToSnapshot + +#define VMControl_VMSetConfig \ + vmcontrol_wrapper_api_get()->xVMControl_VMSetConfig + +#define VMControl_VMSetGuestInfo \ + vmcontrol_wrapper_api_get()->xVMControl_VMSetGuestInfo + +#define VMControl_VMStart \ + vmcontrol_wrapper_api_get()->xVMControl_VMStart + +#define VMControl_VMStopOrReset \ + vmcontrol_wrapper_api_get()->xVMControl_VMStopOrReset + +#define VMControl_VMSuspendToDisk \ + vmcontrol_wrapper_api_get()->xVMControl_VMSuspendToDisk + +#define VMControl_VMToolsLastActive \ + vmcontrol_wrapper_api_get()->xVMControl_VMToolsLastActive + +#endif /* VMCONTROL_WRAPPER_H */ diff --git a/bindings/java/src/org/hyperic/sigar/vmware/ConnectParams.java b/bindings/java/src/org/hyperic/sigar/vmware/ConnectParams.java new file mode 100644 index 00000000..823c68ea --- /dev/null +++ b/bindings/java/src/org/hyperic/sigar/vmware/ConnectParams.java @@ -0,0 +1,37 @@ +/* + * Copyright (C) [2004, 2005, 2006], Hyperic, Inc. + * This file is part of SIGAR. + * + * SIGAR is free software; you can redistribute it and/or modify + * it under the terms version 2 of the GNU General Public License as + * published by the Free Software Foundation. This program is distributed + * in the hope that it will be useful, but WITHOUT ANY WARRANTY; without + * even the implied warranty of MERCHANTABILITY or FITNESS FOR A + * PARTICULAR PURPOSE. See the GNU General Public License for more + * details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 + * USA. + */ + +package org.hyperic.sigar.vmware; + +public class ConnectParams extends VMwareObject { + + private static native int create(String host, int port, + String user, String pass); + + native void destroy(); + + public ConnectParams() { + this(null, 0, null, null); + } + + public ConnectParams(String host, int port, + String user, String pass) + { + this.ptr = create(host, port, user, pass); + } +} diff --git a/bindings/java/src/org/hyperic/sigar/vmware/VM.java b/bindings/java/src/org/hyperic/sigar/vmware/VM.java new file mode 100644 index 00000000..aa64e17e --- /dev/null +++ b/bindings/java/src/org/hyperic/sigar/vmware/VM.java @@ -0,0 +1,334 @@ +/* + * Copyright (C) [2004, 2005, 2006], Hyperic, Inc. + * This file is part of SIGAR. + * + * SIGAR is free software; you can redistribute it and/or modify + * it under the terms version 2 of the GNU General Public License as + * published by the Free Software Foundation. This program is distributed + * in the hope that it will be useful, but WITHOUT ANY WARRANTY; without + * even the implied warranty of MERCHANTABILITY or FITNESS FOR A + * PARTICULAR PURPOSE. See the GNU General Public License for more + * details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 + * USA. + */ + +package org.hyperic.sigar.vmware; + +public class VM extends VMwareObject { + public static final int EXECUTION_STATE_ON = 1; + public static final int EXECUTION_STATE_OFF = 2; + public static final int EXECUTION_STATE_SUSPENDED = 3; + public static final int EXECUTION_STATE_STUCK = 4; + public static final int EXECUTION_STATE_UNKNOWN = 5; + + public static final String[] EXECUTION_STATES = { + "INVALID", "ON", "OFF", "SUSPENDED", "STUCK", "UNKNOWN" + }; + + public static final int POWEROP_MODE_HARD = 1; + public static final int POWEROP_MODE_SOFT = 2; + public static final int POWEROP_MODE_TRYSOFT = 3; + private static final int POWEROP_MODE_DEFAULT = + POWEROP_MODE_TRYSOFT; + + public static final int PRODUCT_WS = 1; + public static final int PRODUCT_GSX = 2; + public static final int PRODUCT_ESX = 3; + public static final int PRODUCT_SERVER = 4; + public static final int PRODUCT_UNKNOWN = 5; + + public static final String GSX = "GSX"; + public static final String ESX = "ESX"; + public static final String SERVER = "Server"; + + public static final String[] PRODUCTS = { + "INVALID", "Workstation", GSX, ESX, SERVER, "UNKNOWN" + }; + + public static final int PLATFORM_WINDOWS = 1; + public static final int PLATFORM_LINUX = 2; + public static final int PLATFORM_VMNIX = 3; + public static final int PLATFORM_UNKNOWN = 4; + + public static final String[] PLATFORMS = { + "INVALID", "Windows", "Linux", "VmNix", "UNKNOWN" + }; + + public static final int PRODINFO_PRODUCT = 1; + public static final int PRODINFO_PLATFORM = 2; + public static final int PRODINFO_BUILD = 3; + public static final int PRODINFO_VERSION_MAJOR = 4; + public static final int PRODINFO_VERSION_MINOR = 5; + public static final int PRODINFO_VERSION_REVISION = 6; + + public static final int PERM_READ = 4; + public static final int PERM_WRITE = 2; + public static final int PERM_EXECUTE = 1; + + native void destroy(); + + private static native int create(); + + private native void connect(ConnectParams params, String config, int mks) + throws VMwareException; + + public void connect(ConnectParams params, String config, boolean mks) + throws VMwareException { + connect(params, config, mks ? 1 : 0); + } + + public void connect(ConnectParams params, String config) + throws VMwareException { + connect(params, config, 0); + } + + public native void disconnect(); + + public native boolean isConnected(); + + public native int getExecutionState() + throws VMwareException; + + public native int getRemoteConnections() + throws VMwareException; + + public native int getUptime() + throws VMwareException; + + public native int getHeartbeat() + throws VMwareException; + + public native int getToolsLastActive() + throws VMwareException; + + public native String getRunAsUser() + throws VMwareException; + + public native int getPermissions() + throws VMwareException; + + public String getPermissionsString() { + char[] perms = { '-', '-', '-' }; + + try { + int bits = getPermissions(); + if ((bits & PERM_READ) != 0) { + perms[0] = 'r'; + } + if ((bits & PERM_WRITE) != 0) { + perms[1] = 'w'; + } + if ((bits & PERM_EXECUTE) != 0) { + perms[2] = 'x'; + } + } catch (VMwareException e) {} + + return new String(perms); + } + + private boolean checkPermission(int perm) { + try { + return (getPermissions() & perm) != 0; + } catch (VMwareException e) { + return false; + } + } + + public boolean canRead() { + return checkPermission(PERM_READ); + } + + public boolean canWrite() { + return checkPermission(PERM_WRITE); + } + + public boolean canExecute() { + return checkPermission(PERM_EXECUTE); + } + + public native String getConfig(String key) + throws VMwareException; + + public native String getResource(String key) + throws VMwareException; + + public native String getGuestInfo(String key) + throws VMwareException; + + public native String setGuestInfo(String key, String value) + throws VMwareException; + + public native int getProductInfo(int type) + throws VMwareException; + + public native long getPid() + throws VMwareException; + + public native int getId() + throws VMwareException; + + public String getVersion() throws VMwareException { + return + getProductInfo(PRODINFO_VERSION_MAJOR) + "." + + getProductInfo(PRODINFO_VERSION_MINOR); + } + + public String getFullVersion() throws VMwareException { + return + getVersion() + "." + + getProductInfo(PRODINFO_VERSION_REVISION) + "." + + getProductInfo(PRODINFO_BUILD); + } + + private String getConfigEx(String key) { + try { + return getConfig(key); + } catch (VMwareException e) { + return null; + } + } + + public String getDisplayName() { + return getConfigEx("displayName"); + } + + public String getGuestOS() { + return getConfigEx("guestOS"); + } + + public String getMemSize() { + return getConfigEx("memsize"); + } + + public String getProductName() { + try { + int info = getProductInfo(PRODINFO_PRODUCT); + return PRODUCTS[info] + " " + getVersion(); + } catch (VMwareException e) { + return null; + } + } + + public String getProductPlatform() { + try { + int info = getProductInfo(PRODINFO_PLATFORM); + return PLATFORMS[info]; + } catch (VMwareException e) { + return null; + } + } + + private boolean isState(int state) { + try { + return getExecutionState() == state; + } catch (VMwareException e) { + return false; + } + } + + public boolean isOn() { + return isState(EXECUTION_STATE_ON); + } + + public boolean isOff() { + return isState(EXECUTION_STATE_OFF); + } + + public boolean isSuspended() { + return isState(EXECUTION_STATE_SUSPENDED); + } + + public boolean isStuck() { + return isState(EXECUTION_STATE_STUCK); + } + + public boolean isESX() { + try { + return getProductInfo(PRODINFO_PRODUCT) == PRODUCT_ESX; + } catch (VMwareException e) { + return false; + } + } + + public boolean isGSX() { + try { + return getProductInfo(PRODINFO_PRODUCT) == PRODUCT_GSX; + } catch (VMwareException e) { + return false; + } + } + + public native void start(int mode) + throws VMwareException; + + public void start() throws VMwareException { + start(POWEROP_MODE_DEFAULT); + } + + public native void stop(int mode) + throws VMwareException; + + public void stop() throws VMwareException { + stop(POWEROP_MODE_DEFAULT); + } + + public native void reset(int mode) + throws VMwareException; + + public void reset() throws VMwareException { + reset(POWEROP_MODE_DEFAULT); + } + + public native void suspend(int mode) + throws VMwareException; + + public void suspend() throws VMwareException { + suspend(POWEROP_MODE_DEFAULT); + } + + public void resume(int mode) + throws VMwareException { + + int state = getExecutionState(); + if (state != EXECUTION_STATE_SUSPENDED) { + throw new VMwareException("VM state is not suspended: " + + EXECUTION_STATES[state]); + } + start(mode); + } + + public void resume() throws VMwareException { + resume(POWEROP_MODE_DEFAULT); + } + + public native void createSnapshot(String name, + String description, + boolean quiesce, + boolean memory) throws VMwareException; + + public native void revertToSnapshot() throws VMwareException; + + public native void removeAllSnapshots() throws VMwareException; + + public native boolean hasSnapshot() throws VMwareException; + + public native void saveScreenshot(String name) + throws VMwareException; + + public native void deviceConnect(String device) + throws VMwareException; + + public native void deviceDisconnect(String device) + throws VMwareException; + + public native boolean deviceIsConnected(String device) + throws VMwareException; + + public VM() { + this.ptr = create(); + } +} diff --git a/bindings/java/src/org/hyperic/sigar/vmware/VMControlLibrary.java b/bindings/java/src/org/hyperic/sigar/vmware/VMControlLibrary.java new file mode 100644 index 00000000..ed646cc0 --- /dev/null +++ b/bindings/java/src/org/hyperic/sigar/vmware/VMControlLibrary.java @@ -0,0 +1,134 @@ +/* + * Copyright (C) [2004, 2005, 2006], Hyperic, Inc. + * This file is part of SIGAR. + * + * SIGAR is free software; you can redistribute it and/or modify + * it under the terms version 2 of the GNU General Public License as + * published by the Free Software Foundation. This program is distributed + * in the hope that it will be useful, but WITHOUT ANY WARRANTY; without + * even the implied warranty of MERCHANTABILITY or FITNESS FOR A + * PARTICULAR PURPOSE. See the GNU General Public License for more + * details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 + * USA. + */ + +package org.hyperic.sigar.vmware; + +import java.io.File; +import java.io.FileNotFoundException; +import java.io.IOException; + +import java.util.Arrays; + +public class VMControlLibrary { + public static final String PROP_VMCONTROL_SHLIB = + "vmcontrol.shlib"; + + private static final String VMWARE_LIB = + getProperty("lib.vmware", "/usr/lib/vmware"); + + private static final String VMCONTROL_TAR = + getProperty("control.tar", VMWARE_LIB + "/perl/control.tar"); + + private static final String VMCONTROL_OBJ = + getProperty("vmcontrol.o", "control-only/vmcontrol.o"); + + private static final String GCC = + getProperty("bin.gcc", "/usr/bin/gcc"); + + private static final String TAR = + getProperty("bin.tar", "/bin/tar"); + + private static final String LIBSSL = + getProperty("libssl", "libssl.so.0.9.7"); + + private static String getProperty(String key, String defval) { + return System.getProperty("vmcontrol." + key, defval); + } + + private static File getLibSSL() { + File libssl = new File(VMWARE_LIB, "lib/" + LIBSSL); + if (libssl.isDirectory()) { + libssl = new File(libssl, LIBSSL); + } + return libssl; + } + + private static void exec(String[] args) + throws IOException { + + Process proc = Runtime.getRuntime().exec(args); + try { + int exitVal = proc.waitFor(); + if (exitVal != 0) { + String msg = + "exec" + Arrays.asList(args) + + " failed: " + exitVal; + throw new IOException(msg); + } + } catch (InterruptedException e) { + } + } + + public static String getSharedLibrary() { + return System.getProperty(PROP_VMCONTROL_SHLIB); + } + + public static void setSharedLibrary(String lib) { + System.setProperty(PROP_VMCONTROL_SHLIB, lib); + } + + public static void link(String name) + throws IOException { + + setSharedLibrary(name); + + File out = new File(name); + if (out.exists()) { + return; //already linked + } + + File dir = out.getParentFile(); + if (!(dir.isDirectory() && dir.canWrite())) { + throw new IOException("Cannot write to: " + dir); + } + + File libssl = getLibSSL(); + + if (!libssl.exists()) { + throw new FileNotFoundException(libssl.toString()); + } + + if (!new File(dir, VMCONTROL_OBJ).exists()) { + //extract vmcontrol.o + String[] extract_args = { + TAR, + "-xf", + VMCONTROL_TAR, + "-C", dir.toString(), + VMCONTROL_OBJ + }; + + exec(extract_args); + } + + //create vmcontrol.so from vmcontrol.o + String[] link_args = { + GCC, + "-shared", + "-o", name, + VMCONTROL_OBJ, + libssl.getPath() + }; + + exec(link_args); + } + + public static void main(String[] args) throws Exception { + link(args[0]); + } +} diff --git a/bindings/java/src/org/hyperic/sigar/vmware/VMwareException.java b/bindings/java/src/org/hyperic/sigar/vmware/VMwareException.java new file mode 100644 index 00000000..90132e81 --- /dev/null +++ b/bindings/java/src/org/hyperic/sigar/vmware/VMwareException.java @@ -0,0 +1,29 @@ +/* + * Copyright (C) [2004, 2005, 2006], Hyperic, Inc. + * This file is part of SIGAR. + * + * SIGAR is free software; you can redistribute it and/or modify + * it under the terms version 2 of the GNU General Public License as + * published by the Free Software Foundation. This program is distributed + * in the hope that it will be useful, but WITHOUT ANY WARRANTY; without + * even the implied warranty of MERCHANTABILITY or FITNESS FOR A + * PARTICULAR PURPOSE. See the GNU General Public License for more + * details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 + * USA. + */ + +package org.hyperic.sigar.vmware; + +public class VMwareException extends Exception { + public VMwareException() { + super(); + } + + public VMwareException(String msg) { + super(msg); + } +} diff --git a/bindings/java/src/org/hyperic/sigar/vmware/VMwareObject.java b/bindings/java/src/org/hyperic/sigar/vmware/VMwareObject.java new file mode 100644 index 00000000..7332558a --- /dev/null +++ b/bindings/java/src/org/hyperic/sigar/vmware/VMwareObject.java @@ -0,0 +1,77 @@ +/* + * Copyright (C) [2004, 2005, 2006], Hyperic, Inc. + * This file is part of SIGAR. + * + * SIGAR is free software; you can redistribute it and/or modify + * it under the terms version 2 of the GNU General Public License as + * published by the Free Software Foundation. This program is distributed + * in the hope that it will be useful, but WITHOUT ANY WARRANTY; without + * even the implied warranty of MERCHANTABILITY or FITNESS FOR A + * PARTICULAR PURPOSE. See the GNU General Public License for more + * details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 + * USA. + */ + +package org.hyperic.sigar.vmware; + +import java.io.File; + +import org.hyperic.sigar.Sigar; +import org.hyperic.sigar.SigarLoader; + +abstract class VMwareObject { + public static final boolean LOADED; + + protected int ptr = 0; + + private static native boolean init(String lib); + + static { + LOADED = loadLibraries(); + }; + + private static boolean loadLibraries() { + if (SigarLoader.IS_LINUX) { + if (!new File("/etc/vmware/config").exists()) { + return false; + } + } + else if (SigarLoader.IS_WIN32) { + try { + System.loadLibrary("ssleay32"); + } catch (UnsatisfiedLinkError e) { + return false; + } + } + else { + return false; + } + + try { + Sigar.load(); + String lib = + VMControlLibrary.getSharedLibrary(); + return init(lib); + } catch (Exception e) { + //e.printStackTrace(); + return false; + } + } + + abstract void destroy(); + + public void dispose() { + if (this.ptr != 0) { + destroy(); + this.ptr = 0; + } + } + + protected void finalize() { + dispose(); + } +} diff --git a/bindings/java/src/org/hyperic/sigar/vmware/VMwareServer.java b/bindings/java/src/org/hyperic/sigar/vmware/VMwareServer.java new file mode 100644 index 00000000..bda93929 --- /dev/null +++ b/bindings/java/src/org/hyperic/sigar/vmware/VMwareServer.java @@ -0,0 +1,50 @@ +/* + * Copyright (C) [2004, 2005, 2006], Hyperic, Inc. + * This file is part of SIGAR. + * + * SIGAR is free software; you can redistribute it and/or modify + * it under the terms version 2 of the GNU General Public License as + * published by the Free Software Foundation. This program is distributed + * in the hope that it will be useful, but WITHOUT ANY WARRANTY; without + * even the implied warranty of MERCHANTABILITY or FITNESS FOR A + * PARTICULAR PURPOSE. See the GNU General Public License for more + * details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 + * USA. + */ + +package org.hyperic.sigar.vmware; + +import java.util.List; + +public class VMwareServer extends VMwareObject { + native void destroy(); + + private static native int create(); + + public native boolean connect(ConnectParams params) + throws VMwareException; + + public native void disconnect(); + + public native boolean isConnected(); + + public native boolean isRegistered(String config) + throws VMwareException; + + public native List getRegisteredVmNames() + throws VMwareException; + + public native String getResource(String key) + throws VMwareException; + + public native String exec(String xml) + throws VMwareException; + + public VMwareServer() { + this.ptr = create(); + } +}