add windows linkage

This commit is contained in:
Doug MacEachern 2006-12-04 23:17:57 +00:00
parent b3a94c71eb
commit ed05e577cd
1 changed files with 89 additions and 1 deletions

View File

@ -23,8 +23,17 @@ import java.io.FileNotFoundException;
import java.io.IOException; import java.io.IOException;
import java.util.Arrays; import java.util.Arrays;
import java.util.ArrayList;
import java.util.List;
import org.hyperic.sigar.SigarLoader;
import org.hyperic.sigar.win32.RegistryKey;
import org.hyperic.sigar.win32.Win32Exception;
public class VMControlLibrary { public class VMControlLibrary {
public static final String REGISTRY_ROOT =
"SOFTWARE\\VMware, Inc.";
public static final String PROP_VMCONTROL_SHLIB = public static final String PROP_VMCONTROL_SHLIB =
"vmcontrol.shlib"; "vmcontrol.shlib";
@ -36,6 +45,9 @@ public class VMControlLibrary {
private static final String VMCONTROL = "vmcontrol"; private static final String VMCONTROL = "vmcontrol";
private static final String VMCONTROL_DLL =
VMCONTROL + "lib.dll";
private static final String VMCONTROL_OBJ = private static final String VMCONTROL_OBJ =
getProperty("vmcontrol.o", "control-only/" + VMCONTROL + ".o"); getProperty("vmcontrol.o", "control-only/" + VMCONTROL + ".o");
@ -90,9 +102,71 @@ public class VMControlLibrary {
link(VMCONTROL + ".so"); link(VMCONTROL + ".so");
} }
private static void linkWin32() {
List dlls = new ArrayList();
RegistryKey root = null;
try {
root =
RegistryKey.LocalMachine.openSubKey(REGISTRY_ROOT);
String[] keys = root.getSubKeyNames();
for (int i=0; i<keys.length; i++) {
String name = keys[i];
if (!name.startsWith("VMware ")) {
continue;
}
RegistryKey subkey = null;
try {
subkey = root.openSubKey(name);
String path = subkey.getStringValue("InstallPath");
if (path == null) {
continue;
}
path = path.trim();
if (path.length() == 0) {
continue;
}
File dll = new File(path + VMCONTROL_DLL);
if (dll.exists()) {
//prefer VMware Server or VMware GSX Server
if (name.endsWith(" Server")) {
dlls.add(0, dll.getPath());
}
//Scripting API will also work
else if (name.endsWith(" API")) {
dlls.add(dll.getPath());
}
}
} catch (Win32Exception e) {
} finally {
if (subkey != null) {
subkey.close();
}
}
}
} catch (Win32Exception e) {
} finally {
if (root != null) {
root.close();
}
}
if (dlls.size() != 0) {
setSharedLibrary((String)dlls.get(0));
}
}
public static void link(String name) public static void link(String name)
throws IOException { throws IOException {
if (SigarLoader.IS_WIN32) {
linkWin32();
return;
}
File out = new File(name).getAbsoluteFile(); File out = new File(name).getAbsoluteFile();
setSharedLibrary(out.getPath()); setSharedLibrary(out.getPath());
@ -141,6 +215,20 @@ public class VMControlLibrary {
} }
public static void main(String[] args) throws Exception { public static void main(String[] args) throws Exception {
link(args[0]); if (args.length == 0) {
link();
}
else {
link(args[0]);
}
String shlib = getSharedLibrary();
if (shlib == null) {
System.out.println("No library found");
}
else {
System.out.println(PROP_VMCONTROL_SHLIB + "=" + shlib +
" (loaded=" + isLoaded() + ")");
}
} }
} }