diff --git a/bindings/java/src/jni/javasigar.c b/bindings/java/src/jni/javasigar.c index 2fcd01d9..03c7eee9 100644 --- a/bindings/java/src/jni/javasigar.c +++ b/bindings/java/src/jni/javasigar.c @@ -1163,6 +1163,16 @@ static int jsigar_ptql_re_impl(void *data, JENV->NewStringUTF(env, needle)); } +static void re_impl_set(JNIEnv *env, sigar_t *sigar, jobject obj, jni_ptql_re_data_t *re) +{ + re->env = env; + re->cls = NULL; + re->obj = obj; + re->id = NULL; + + sigar_ptql_re_impl_set(sigar, re, jsigar_ptql_re_impl); +} + JNIEXPORT jboolean SIGAR_JNI(ptql_SigarProcessQuery_match) (JNIEnv *env, jobject obj, jobject sigar_obj, jlong pid) { @@ -1172,12 +1182,7 @@ JNIEXPORT jboolean SIGAR_JNI(ptql_SigarProcessQuery_match) (sigar_ptql_query_t *)sigar_get_pointer(env, obj); dSIGAR(JNI_FALSE); - re.env = env; - re.cls = NULL; - re.obj = obj; - re.id = NULL; - - sigar_ptql_re_impl_set(sigar, &re, jsigar_ptql_re_impl); + re_impl_set(env, sigar, obj, &re); status = sigar_ptql_query_match(sigar, query, pid); @@ -1233,6 +1238,32 @@ JNIEXPORT void SIGAR_JNI(ptql_SigarProcessQuery_destroy) } } +JNIEXPORT jlong SIGAR_JNI(ptql_SigarProcessQuery_findProcess) +(JNIEnv *env, jobject obj, jobject sigar_obj) +{ + sigar_pid_t pid; + int status; + jni_ptql_re_data_t re; + sigar_ptql_query_t *query = + (sigar_ptql_query_t *)sigar_get_pointer(env, obj); + dSIGAR(0); + + re_impl_set(env, sigar, obj, &re); + + status = sigar_ptql_query_find_process(sigar, query, &pid); + + sigar_ptql_re_impl_set(sigar, NULL, NULL); + + if (status < 0) { + sigar_throw_ptql_malformed(env, sigar->errbuf); + } + else if (status != SIGAR_OK) { + sigar_throw_error(env, jsigar, status); + } + + return pid; +} + #include "sigar_getline.h" JNIEXPORT jboolean SIGAR_JNI(util_Getline_isatty) diff --git a/bindings/java/src/org/hyperic/sigar/ptql/ProcessFinder.java b/bindings/java/src/org/hyperic/sigar/ptql/ProcessFinder.java index d60a4941..c547c7a0 100644 --- a/bindings/java/src/org/hyperic/sigar/ptql/ProcessFinder.java +++ b/bindings/java/src/org/hyperic/sigar/ptql/ProcessFinder.java @@ -51,10 +51,10 @@ public class ProcessFinder { public long findSingleProcess(ProcessQuery query) throws SigarException, SigarNotImplementedException, MalformedQueryException { - //XXX bring back in another form - //if (query instanceof PidQuery) { - // return ((PidQuery)query).getPid(); - //} + + if (query instanceof SigarProcessQuery) { + return ((SigarProcessQuery)query).findProcess(this.proxy); + } int i, matches = 0; diff --git a/bindings/java/src/org/hyperic/sigar/ptql/SigarProcessQuery.java b/bindings/java/src/org/hyperic/sigar/ptql/SigarProcessQuery.java index 4cbb2e8d..7efcb306 100644 --- a/bindings/java/src/org/hyperic/sigar/ptql/SigarProcessQuery.java +++ b/bindings/java/src/org/hyperic/sigar/ptql/SigarProcessQuery.java @@ -20,6 +20,7 @@ package org.hyperic.sigar.ptql; import org.hyperic.sigar.Sigar; import org.hyperic.sigar.SigarException; +import org.hyperic.sigar.SigarNotImplementedException; import org.hyperic.sigar.SigarProxy; import org.hyperic.sigar.SigarProxyCache; @@ -45,6 +46,15 @@ public class SigarProcessQuery implements ProcessQuery { return match(SigarProxyCache.getSigar(sigar), pid); } + public native long findProcess(Sigar sigar) + throws SigarException, SigarNotImplementedException, MalformedQueryException; + + public long findProcess(SigarProxy sigar) + throws SigarException, SigarNotImplementedException, MalformedQueryException { + + return findProcess(SigarProxyCache.getSigar(sigar)); + } + static boolean re(String haystack, String needle) { if (haystack == null) { return false; diff --git a/include/sigar_ptql.h b/include/sigar_ptql.h index 1c5b1ecf..02b26d72 100644 --- a/include/sigar_ptql.h +++ b/include/sigar_ptql.h @@ -37,4 +37,8 @@ SIGAR_DECLARE(int) sigar_ptql_query_match(sigar_t *sigar, SIGAR_DECLARE(int) sigar_ptql_query_destroy(sigar_ptql_query_t *query); +SIGAR_DECLARE(int) sigar_ptql_query_find_process(sigar_t *sigar, + sigar_ptql_query_t *query, + sigar_pid_t *pid); + #endif /*SIGAR_PTQL_H*/ diff --git a/src/sigar_ptql.c b/src/sigar_ptql.c index ad57f889..c15f0757 100644 --- a/src/sigar_ptql.c +++ b/src/sigar_ptql.c @@ -1363,3 +1363,52 @@ SIGAR_DECLARE(int) sigar_ptql_query_match(sigar_t *sigar, return SIGAR_OK; } + +SIGAR_DECLARE(int) sigar_ptql_query_find_process(sigar_t *sigar, + sigar_ptql_query_t *query, + sigar_pid_t *pid) +{ + sigar_proc_list_t pids; + int status = sigar_proc_list_get(sigar, &pids); + int i, matches=0; + + if (status != SIGAR_OK) { + return status; + } + + for (i=0; i