From 83d4958bb03c29dbf67f5865e4b9e3e9175347f4 Mon Sep 17 00:00:00 2001 From: Doug MacEachern Date: Tue, 10 Jan 2006 22:37:27 +0000 Subject: [PATCH] add win32 methods to help find exe associated w/ a script extension --- bindings/java/src/jni/javasigar.c | 28 +++++++++ .../src/net/hyperic/sigar/win32/Win32.java | 62 ++++++++++++++++++- 2 files changed, 89 insertions(+), 1 deletion(-) diff --git a/bindings/java/src/jni/javasigar.c b/bindings/java/src/jni/javasigar.c index 350ef2a8..50926554 100644 --- a/bindings/java/src/jni/javasigar.c +++ b/bindings/java/src/jni/javasigar.c @@ -1286,3 +1286,31 @@ JNIEXPORT jlong SIGAR_JNI(ResourceLimit_INFINITY) return RLIM_INFINITY; #endif } + +JNIEXPORT jstring SIGAR_JNI(win32_Win32_findExecutable) +(JNIEnv *env, jclass sigar_class, jstring jname) +{ +#ifdef WIN32 +#include "shellapi.h" + const char *name; + jboolean is_copy; + char exe[MAX_PATH]; + LONG result; + jstring jexe = NULL; + + name = JENV->GetStringUTFChars(env, jname, &is_copy); + + if ((result = (LONG)FindExecutable(name, ".", exe)) > 32) { + jexe = JENV->NewStringUTF(env, exe); + } + + if (is_copy) { + JENV->ReleaseStringUTFChars(env, jname, name); + } + + return jexe; +#else + sigar_throw_notimpl(env, "win32 only"); + return NULL; +#endif +} diff --git a/bindings/java/src/net/hyperic/sigar/win32/Win32.java b/bindings/java/src/net/hyperic/sigar/win32/Win32.java index f27a194e..030ce6b2 100644 --- a/bindings/java/src/net/hyperic/sigar/win32/Win32.java +++ b/bindings/java/src/net/hyperic/sigar/win32/Win32.java @@ -1,9 +1,11 @@ package net.hyperic.sigar.win32; +import java.io.File; + import net.hyperic.sigar.Sigar; import net.hyperic.sigar.SigarException; -abstract class Win32 { +public abstract class Win32 { static { try { @@ -12,4 +14,62 @@ abstract class Win32 { } } + + public static native String findExecutable(String name) + throws SigarException; + + public static String findScriptExecutable(String name) { + int ix = name.lastIndexOf("."); + if (ix == -1) { + return null; + } + + String ext = name.substring(ix+1); + if (ext.equals("exe") || + ext.equals("bat") || + ext.equals("com")) + { + return null; + } + + String exe; + try { + exe = findExecutable(name); + } catch (SigarException e) { + return null; + } + if (exe == null) { + return null; //no association + } + + exe = exe.toLowerCase(); + name = name.toLowerCase(); + if (exe.equals(name) || exe.endsWith(name)) { + return null; //same thing + } + + File file = new File(exe); + //rewrite to use cscript for command line stuff + if (file.getName().equals("wscript.exe")) { + exe = + file.getParent() + + File.separator + + "cscript.exe"; + } + + return exe; + } + + public static void main(String[] args) throws Exception { + for (int i=0; i