[SIGAR-104] Add interface to WindowsGetFileVersionInfo function
This commit is contained in:
		
							parent
							
								
									9e9b25bcd5
								
							
						
					
					
						commit
						b7f7b84fc2
					
				@ -691,6 +691,10 @@ static int jni_env_getall(void *data,
 | 
			
		||||
    return SIGAR_OK;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
#define MAP_PUT_SIG \
 | 
			
		||||
    "(Ljava/lang/Object;Ljava/lang/Object;)" \
 | 
			
		||||
    "Ljava/lang/Object;"
 | 
			
		||||
 | 
			
		||||
JNIEXPORT jobject SIGAR_JNI(ProcEnv_getAll)
 | 
			
		||||
(JNIEnv *env, jobject cls, jobject sigar_obj, jlong pid)
 | 
			
		||||
{
 | 
			
		||||
@ -703,9 +707,8 @@ JNIEXPORT jobject SIGAR_JNI(ProcEnv_getAll)
 | 
			
		||||
    jmethodID mapid =
 | 
			
		||||
        JENV->GetMethodID(env, mapclass, "<init>", "()V");
 | 
			
		||||
    jmethodID putid =
 | 
			
		||||
        JENV->GetMethodID(env, mapclass, "put",
 | 
			
		||||
                          "(Ljava/lang/Object;Ljava/lang/Object;)"
 | 
			
		||||
                          "Ljava/lang/Object;");
 | 
			
		||||
        JENV->GetMethodID(env, mapclass, "put", MAP_PUT_SIG);
 | 
			
		||||
 | 
			
		||||
    dSIGAR(NULL);
 | 
			
		||||
 | 
			
		||||
    hashmap = JENV->NewObject(env, mapclass, mapid);
 | 
			
		||||
@ -1636,3 +1639,61 @@ JNIEXPORT jstring SIGAR_JNI(win32_Win32_findExecutable)
 | 
			
		||||
    return NULL;
 | 
			
		||||
#endif
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
JNIEXPORT jboolean SIGAR_JNI(win32_FileVersion_gather)
 | 
			
		||||
(JNIEnv *env, jobject obj, jstring jname)
 | 
			
		||||
{
 | 
			
		||||
#ifdef WIN32
 | 
			
		||||
    int status;
 | 
			
		||||
    sigar_file_version_t version;
 | 
			
		||||
    jboolean is_copy;
 | 
			
		||||
    jfieldID id;
 | 
			
		||||
    jclass cls = JENV->GetObjectClass(env, obj);
 | 
			
		||||
    const char *name = JENV->GetStringUTFChars(env, jname, &is_copy);
 | 
			
		||||
    sigar_proc_env_t infocb;
 | 
			
		||||
    jobject hashmap;
 | 
			
		||||
    jni_env_put_t put;
 | 
			
		||||
 | 
			
		||||
    id = JENV->GetFieldID(env, cls, "string_file_info",
 | 
			
		||||
                          "Ljava/util/Map;");
 | 
			
		||||
    hashmap = JENV->GetObjectField(env, obj, id);
 | 
			
		||||
    put.env = env;
 | 
			
		||||
    put.id = 
 | 
			
		||||
        JENV->GetMethodID(env,
 | 
			
		||||
                          JENV->GetObjectClass(env, hashmap),
 | 
			
		||||
                          "put", MAP_PUT_SIG);
 | 
			
		||||
    put.map = hashmap;
 | 
			
		||||
 | 
			
		||||
    infocb.type = SIGAR_PROC_ENV_ALL;
 | 
			
		||||
    infocb.env_getter = jni_env_getall;
 | 
			
		||||
    infocb.data = &put;
 | 
			
		||||
 | 
			
		||||
    status = sigar_file_version_get(&version, (char *)name, &infocb);
 | 
			
		||||
 | 
			
		||||
    if (is_copy) {
 | 
			
		||||
        JENV->ReleaseStringUTFChars(env, jname, name);
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    if (status != SIGAR_OK) {
 | 
			
		||||
        return JNI_FALSE;
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
#define set_vfield(name) \
 | 
			
		||||
    id = JENV->GetFieldID(env, cls, #name, "I"); \
 | 
			
		||||
    JENV->SetIntField(env, obj, id, version.name)
 | 
			
		||||
 | 
			
		||||
    set_vfield(product_major);
 | 
			
		||||
    set_vfield(product_minor);
 | 
			
		||||
    set_vfield(product_build);
 | 
			
		||||
    set_vfield(product_revision);
 | 
			
		||||
    set_vfield(file_major);
 | 
			
		||||
    set_vfield(file_minor);
 | 
			
		||||
    set_vfield(file_build);
 | 
			
		||||
    set_vfield(file_revision);
 | 
			
		||||
#undef set_vfield
 | 
			
		||||
 | 
			
		||||
    return JNI_TRUE;
 | 
			
		||||
#else
 | 
			
		||||
    return JNI_FALSE;
 | 
			
		||||
#endif
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
							
								
								
									
										97
									
								
								bindings/java/src/org/hyperic/sigar/cmd/FileVersionInfo.java
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										97
									
								
								bindings/java/src/org/hyperic/sigar/cmd/FileVersionInfo.java
									
									
									
									
									
										Normal file
									
								
							@ -0,0 +1,97 @@
 | 
			
		||||
/*
 | 
			
		||||
 * 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.cmd;
 | 
			
		||||
 | 
			
		||||
import java.io.File;
 | 
			
		||||
import java.util.Iterator;
 | 
			
		||||
import java.util.Map;
 | 
			
		||||
import org.hyperic.sigar.SigarException;
 | 
			
		||||
import org.hyperic.sigar.ProcExe;
 | 
			
		||||
import org.hyperic.sigar.win32.Win32;
 | 
			
		||||
import org.hyperic.sigar.win32.FileVersion;
 | 
			
		||||
 | 
			
		||||
/**
 | 
			
		||||
 * Display process file information.
 | 
			
		||||
 */
 | 
			
		||||
public class FileVersionInfo extends SigarCommandBase {
 | 
			
		||||
 | 
			
		||||
    public FileVersionInfo(Shell shell) {
 | 
			
		||||
        super(shell);
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    public FileVersionInfo() {
 | 
			
		||||
        super();
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    protected boolean validateArgs(String[] args) {
 | 
			
		||||
        return args.length >= 1;
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    public String getUsageShort() {
 | 
			
		||||
        return "Display file version info";
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    public void output(String[] args) throws SigarException {
 | 
			
		||||
        for (int i=0; i<args.length; i++) {
 | 
			
		||||
            String exe = args[i];
 | 
			
		||||
            if (!new File(exe).exists()) {
 | 
			
		||||
                try {
 | 
			
		||||
                    exe = sigar.getProcExe(exe).getName();
 | 
			
		||||
                } catch (SigarException e) {
 | 
			
		||||
                    println(exe + ": " + e.getMessage());
 | 
			
		||||
                    continue;
 | 
			
		||||
                }
 | 
			
		||||
            }
 | 
			
		||||
            output(exe);
 | 
			
		||||
            println("\n------------------------\n");
 | 
			
		||||
        }
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    private void output(String key, String val) {
 | 
			
		||||
        final int max = 20;
 | 
			
		||||
        int len = max - key.length();
 | 
			
		||||
        StringBuffer sb = new StringBuffer();
 | 
			
		||||
        sb.append("  ").append(key);
 | 
			
		||||
        while (len-- > 0) {
 | 
			
		||||
            sb.append('.');
 | 
			
		||||
        }
 | 
			
		||||
        sb.append(val);
 | 
			
		||||
        println(sb.toString());
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    public void output(String exe) throws SigarException {
 | 
			
		||||
        FileVersion info = Win32.getFileVersion(exe);
 | 
			
		||||
        if (info == null) {
 | 
			
		||||
            return;
 | 
			
		||||
        }
 | 
			
		||||
        println("Version info for file '" + exe + "':");
 | 
			
		||||
        output("FileVersion", info.getFileVersion());
 | 
			
		||||
        output("ProductVersion", info.getProductVersion());
 | 
			
		||||
        for (Iterator it = info.getInfo().entrySet().iterator();
 | 
			
		||||
             it.hasNext();)
 | 
			
		||||
        {
 | 
			
		||||
            Map.Entry entry = (Map.Entry)it.next();
 | 
			
		||||
            output((String)entry.getKey(), (String)entry.getValue());
 | 
			
		||||
        }
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    public static void main(String[] args) throws Exception {
 | 
			
		||||
        new FileVersionInfo().processCommand(args);
 | 
			
		||||
    }
 | 
			
		||||
}
 | 
			
		||||
@ -106,6 +106,7 @@ public class Shell extends ShellBase {
 | 
			
		||||
        registerCommandHandler("who", new Who(this));
 | 
			
		||||
        if (SigarLoader.IS_WIN32) {
 | 
			
		||||
            registerCommandHandler("service", new Win32Service(this));
 | 
			
		||||
            registerCommandHandler("fversion", new FileVersionInfo(this));
 | 
			
		||||
        }
 | 
			
		||||
        try {
 | 
			
		||||
            //requires junit.jar
 | 
			
		||||
 | 
			
		||||
@ -87,6 +87,7 @@ public class SigarTestRunner extends SigarCommandBase {
 | 
			
		||||
        TestMetaBase.class,
 | 
			
		||||
        TestRegistryKey.class,
 | 
			
		||||
        TestService.class,
 | 
			
		||||
        TestFileVersion.class
 | 
			
		||||
    };
 | 
			
		||||
    
 | 
			
		||||
    static {
 | 
			
		||||
 | 
			
		||||
							
								
								
									
										93
									
								
								bindings/java/src/org/hyperic/sigar/win32/FileVersion.java
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										93
									
								
								bindings/java/src/org/hyperic/sigar/win32/FileVersion.java
									
									
									
									
									
										Normal file
									
								
							@ -0,0 +1,93 @@
 | 
			
		||||
/*
 | 
			
		||||
 * 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.win32;
 | 
			
		||||
 | 
			
		||||
import java.util.LinkedHashMap;
 | 
			
		||||
import java.util.Map;
 | 
			
		||||
 | 
			
		||||
public class FileVersion {
 | 
			
		||||
    private int
 | 
			
		||||
        product_major, product_minor, product_build, product_revision;
 | 
			
		||||
    private int
 | 
			
		||||
        file_major, file_minor, file_build, file_revision;
 | 
			
		||||
    private Map string_file_info = new LinkedHashMap();
 | 
			
		||||
 | 
			
		||||
    native boolean gather(String name);
 | 
			
		||||
 | 
			
		||||
    FileVersion() {}
 | 
			
		||||
 | 
			
		||||
    public int getProductMajor() {
 | 
			
		||||
        return this.product_major;
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    public int getProductMinor() {
 | 
			
		||||
        return this.product_minor;
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    public int getProductBuild() {
 | 
			
		||||
        return this.product_build;
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    public int getProductRevision() {
 | 
			
		||||
        return this.product_revision;
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    public int getFileMajor() {
 | 
			
		||||
        return this.file_major;
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    public int getFileMinor() {
 | 
			
		||||
        return this.file_minor;
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    public int getFileBuild() {
 | 
			
		||||
        return this.file_build;
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    public int getFileRevision() {
 | 
			
		||||
        return this.file_revision;
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    public Map getInfo() {
 | 
			
		||||
        return this.string_file_info;
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    private String toVersion(int major, int minor,
 | 
			
		||||
                             int build, int revision) {
 | 
			
		||||
        return
 | 
			
		||||
            major + "." +
 | 
			
		||||
            minor + "." +
 | 
			
		||||
            build + "." +
 | 
			
		||||
            revision;
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    public String getProductVersion() {
 | 
			
		||||
        return toVersion(this.product_major,
 | 
			
		||||
                         this.product_minor,
 | 
			
		||||
                         this.product_build,
 | 
			
		||||
                         this.product_revision);
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    public String getFileVersion() {
 | 
			
		||||
        return toVersion(this.file_major,
 | 
			
		||||
                         this.file_minor,
 | 
			
		||||
                         this.file_build,
 | 
			
		||||
                         this.file_revision);
 | 
			
		||||
    }
 | 
			
		||||
}
 | 
			
		||||
@ -79,6 +79,16 @@ public abstract class Win32 {
 | 
			
		||||
        return exe;
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    public static FileVersion getFileVersion(String name) {
 | 
			
		||||
        FileVersion version = new FileVersion();
 | 
			
		||||
        if (version.gather(name)) {
 | 
			
		||||
            return version;
 | 
			
		||||
        }
 | 
			
		||||
        else {
 | 
			
		||||
            return null;
 | 
			
		||||
        }
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    public static void main(String[] args) throws Exception {
 | 
			
		||||
        for (int i=0; i<args.length; i++) {
 | 
			
		||||
            String file =
 | 
			
		||||
 | 
			
		||||
@ -0,0 +1,64 @@
 | 
			
		||||
/*
 | 
			
		||||
 * 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.win32.test;
 | 
			
		||||
 | 
			
		||||
import java.io.File;
 | 
			
		||||
 | 
			
		||||
import org.hyperic.sigar.test.SigarTestCase;
 | 
			
		||||
 | 
			
		||||
import org.hyperic.sigar.Sigar;
 | 
			
		||||
import org.hyperic.sigar.SigarException;
 | 
			
		||||
import org.hyperic.sigar.ProcExe;
 | 
			
		||||
import org.hyperic.sigar.win32.Win32;
 | 
			
		||||
import org.hyperic.sigar.win32.FileVersion;
 | 
			
		||||
 | 
			
		||||
public class TestFileVersion extends SigarTestCase {
 | 
			
		||||
 | 
			
		||||
    public TestFileVersion(String name) {
 | 
			
		||||
        super(name);
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    private void printExe(long pid) {
 | 
			
		||||
        traceln("\npid=" + pid);        
 | 
			
		||||
 | 
			
		||||
        String name;
 | 
			
		||||
        try {
 | 
			
		||||
            name = getSigar().getProcExe(pid).getName();
 | 
			
		||||
        } catch (SigarException e) {
 | 
			
		||||
            return;
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
        FileVersion info = Win32.getFileVersion(name);
 | 
			
		||||
        if (info != null) {
 | 
			
		||||
            traceln("exe='" + name + "'");
 | 
			
		||||
            traceln("version=" + info.getProductVersion());
 | 
			
		||||
        }
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    public void testCreate() throws Exception {
 | 
			
		||||
        assertTrue(Win32.getFileVersion("DoEsNoTeXist.exe") == null);
 | 
			
		||||
 | 
			
		||||
	long[] pids = getSigar().getProcList();
 | 
			
		||||
 | 
			
		||||
        //go through all just to make sure no crashes
 | 
			
		||||
	for (int i=0; i<pids.length; i++) {
 | 
			
		||||
            printExe(pids[i]);
 | 
			
		||||
	}
 | 
			
		||||
    }
 | 
			
		||||
}
 | 
			
		||||
		Loading…
	
		Reference in New Issue
	
	Block a user