diff --git a/bindings/java/src/jni/javasigar.c b/bindings/java/src/jni/javasigar.c index 03c7eee9..4e6d34ad 100644 --- a/bindings/java/src/jni/javasigar.c +++ b/bindings/java/src/jni/javasigar.c @@ -1264,6 +1264,64 @@ JNIEXPORT jlong SIGAR_JNI(ptql_SigarProcessQuery_findProcess) return pid; } +JNIEXPORT jlongArray SIGAR_JNI(ptql_SigarProcessQuery_findProcesses) +(JNIEnv *env, jobject obj, jobject sigar_obj) +{ + int status; + jlongArray procarray; + sigar_proc_list_t proclist; + jlong *pids = NULL; + jni_ptql_re_data_t re; + sigar_ptql_query_t *query = + (sigar_ptql_query_t *)sigar_get_pointer(env, obj); + dSIGAR(NULL); + + if ((status = sigar_proc_list_get(sigar, &proclist)) != SIGAR_OK) { + sigar_throw_error(env, jsigar, status); + return NULL; + } + + re_impl_set(env, sigar, obj, &re); + + status = sigar_ptql_query_find_processes(sigar, query, &proclist); + + sigar_ptql_re_impl_set(sigar, NULL, NULL); + + if (status < 0) { + sigar_throw_ptql_malformed(env, sigar->errbuf); + return NULL; + } + else if (status != SIGAR_OK) { + sigar_throw_error(env, jsigar, status); + return NULL; + } + + procarray = JENV->NewLongArray(env, proclist.number); + + if (sizeof(jlong) == sizeof(sigar_pid_t)) { + pids = (jlong *)proclist.data; + } + else { + unsigned int i; + pids = (jlong *)malloc(sizeof(jlong) * proclist.number); + + for (i=0; iSetLongArrayRegion(env, procarray, 0, + proclist.number, pids); + + if (pids != (jlong *)proclist.data) { + free(pids); + } + + sigar_proc_list_destroy(sigar, &proclist); + + return procarray; +} + #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 f83cf427..ceb7ba40 100644 --- a/bindings/java/src/org/hyperic/sigar/ptql/ProcessFinder.java +++ b/bindings/java/src/org/hyperic/sigar/ptql/ProcessFinder.java @@ -62,7 +62,7 @@ public class ProcessFinder { return ((SigarProcessQuery)query).findProcess(this.proxy); } - throw new MalformedQueryException(); + throw new SigarNotImplementedException(); } public static long[] find(Sigar sigar, String query) @@ -105,52 +105,11 @@ public class ProcessFinder { public long[] find(ProcessQuery query) throws SigarException, SigarNotImplementedException { - int matches=0, lastMatch=-1; - - //because we modify the array below. XXX this sucks. - long[] pids = (long[])this.proxy.getProcList().clone(); - - for (int i=0; idata[proclist->number++] = pids.data[i]; + } + else if (query_status == SIGAR_ENOTIMPL) { + /* let caller know query is invalid. */ + status = query_status; + break; + } + } + + sigar_proc_list_destroy(sigar, &pids); + + if (status != SIGAR_OK) { + sigar_proc_list_create(proclist); + return status; + } + + return SIGAR_OK; +}