diff --git a/bindings/java/src/jni/javasigar.c b/bindings/java/src/jni/javasigar.c index fe7c116f..9114efa3 100644 --- a/bindings/java/src/jni/javasigar.c +++ b/bindings/java/src/jni/javasigar.c @@ -21,6 +21,7 @@ #include "sigar_fileinfo.h" #include "sigar_log.h" #include "sigar_private.h" +#include "sigar_ptql.h" #include "sigar_util.h" #include "sigar_os.h" @@ -57,7 +58,7 @@ typedef struct { } jni_sigar_t; #define dSIGAR_GET \ - jni_sigar_t *jsigar = sigar_get_pointer(env, sigar_obj); \ + jni_sigar_t *jsigar = sigar_get_jpointer(env, sigar_obj); \ sigar_t *sigar #define dSIGAR_VOID \ @@ -177,20 +178,22 @@ static void sigar_throw_error(JNIEnv *env, jni_sigar_t *jsigar, int err) sigar_strerror(jsigar->sigar, err)); } -static jni_sigar_t *sigar_get_pointer(JNIEnv *env, jobject obj) { +static void *sigar_get_pointer(JNIEnv *env, jobject obj) { jfieldID pointer_field; - jni_sigar_t *jsigar; - jclass cls; - - cls = JENV->GetObjectClass(env, obj); + jclass cls = JENV->GetObjectClass(env, obj); #ifdef SIGAR_POINTER_LONG pointer_field = JENV->GetFieldID(env, cls, "longSigarWrapper", "J"); - jsigar = (jni_sigar_t *) JENV->GetLongField(env, obj, pointer_field); + return (void *)JENV->GetLongField(env, obj, pointer_field); #else pointer_field = JENV->GetFieldID(env, cls, "sigarWrapper", "I"); - jsigar = (jni_sigar_t *) JENV->GetIntField(env, obj, pointer_field); + return (void *)JENV->GetIntField(env, obj, pointer_field); #endif +} + +static jni_sigar_t *sigar_get_jpointer(JNIEnv *env, jobject obj) { + jni_sigar_t *jsigar = + (jni_sigar_t *)sigar_get_pointer(env, obj); if (!jsigar) { sigar_throw_exception(env, "sigar has been closed"); @@ -1069,6 +1072,58 @@ JNIEXPORT jstring SIGAR_JNIx(getFQDN) return JENV->NewStringUTF(env, fqdn); } +JNIEXPORT jboolean SIGAR_JNI(ptql_SigarProcessQuery_match) +(JNIEnv *env, jobject obj, jobject sigar_obj, jlong pid) +{ + int status; + sigar_ptql_query_t *query = + (sigar_ptql_query_t *)sigar_get_pointer(env, obj); + dSIGAR(JNI_FALSE); + + status = sigar_ptql_query_match(sigar, query, pid); + + if (status == SIGAR_OK) { + return JNI_TRUE; + } + else { + return JNI_FALSE; + } +} + +JNIEXPORT void SIGAR_JNI(ptql_SigarProcessQuery_create) +(JNIEnv *env, jobject obj, jstring jptql) +{ + int status; + jboolean is_copy; + const char *ptql; + sigar_ptql_query_t *query; + + ptql = JENV->GetStringUTFChars(env, jptql, &is_copy); + status = sigar_ptql_query_create(&query, (char *)ptql); + if (is_copy) { + JENV->ReleaseStringUTFChars(env, jptql, ptql); + } + + if (status != SIGAR_OK) { + sigar_throw_exception(env, "Malformed query"); /*XXX*/ + } + else { + sigar_set_pointer(env, obj, query); + } +} + +JNIEXPORT void SIGAR_JNI(ptql_SigarProcessQuery_destroy) +(JNIEnv *env, jobject obj) +{ + sigar_ptql_query_t *query = + (sigar_ptql_query_t *)sigar_get_pointer(env, obj); + + if (query) { + sigar_ptql_query_destroy(query); + sigar_set_pointer(env, obj, 0); + } +} + #include "sigar_getline.h" JNIEXPORT jboolean SIGAR_JNI(util_Getline_isatty) diff --git a/bindings/java/src/org/hyperic/sigar/ptql/SigarProcessQuery.java b/bindings/java/src/org/hyperic/sigar/ptql/SigarProcessQuery.java new file mode 100644 index 00000000..e689c999 --- /dev/null +++ b/bindings/java/src/org/hyperic/sigar/ptql/SigarProcessQuery.java @@ -0,0 +1,47 @@ +/* + * 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.ptql; + +import org.hyperic.sigar.Sigar; +import org.hyperic.sigar.SigarException; +import org.hyperic.sigar.SigarProxy; +import org.hyperic.sigar.SigarProxyCache; + +public class SigarProcessQuery implements ProcessQuery { + int sigarWrapper = 0; //holds the sigar_ptql_query_t * + long longSigarWrapper = 0; //same, but where sizeof(void*) > sizeof(int) + + native void create(String ptql) + throws SigarException; + + native void destroy(); + + protected void finalize() { + destroy(); + } + + private native boolean match(Sigar sigar, long pid) + throws SigarException; + + public boolean match(SigarProxy sigar, long pid) + throws SigarException { + + return match(SigarProxyCache.getSigar(sigar), pid); + } +}