diff --git a/bindings/java/src/jni/javasigar.c b/bindings/java/src/jni/javasigar.c index 05a4a0a2..0f15a985 100644 --- a/bindings/java/src/jni/javasigar.c +++ b/bindings/java/src/jni/javasigar.c @@ -1090,16 +1090,55 @@ JNIEXPORT jstring SIGAR_JNIx(getFQDN) return JENV->NewStringUTF(env, fqdn); } +typedef struct { + JNIEnv *env; + jobject obj; + jclass cls; + jmethodID id; +} jni_ptql_re_data_t; + +static int jsigar_ptql_re_impl(void *data, + char *haystack, char *needle) +{ + jni_ptql_re_data_t *re = (jni_ptql_re_data_t *)data; + JNIEnv *env = re->env; + + if (!re->cls) { + re->cls = JENV->GetObjectClass(env, re->obj); + re->id = + JENV->GetStaticMethodID(env, re->cls, "re", + "(Ljava/lang/String;Ljava/lang/String;)" + "Z"); + if (!re->id) { + return 0; + } + } + + return JENV->CallStaticBooleanMethod(env, re->cls, re->id, + JENV->NewStringUTF(env, haystack), + JENV->NewStringUTF(env, needle)); +} + JNIEXPORT jboolean SIGAR_JNI(ptql_SigarProcessQuery_match) (JNIEnv *env, jobject obj, jobject sigar_obj, jlong pid) { int status; + jni_ptql_re_data_t re; sigar_ptql_query_t *query = (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); + status = sigar_ptql_query_match(sigar, query, pid); + sigar_ptql_re_impl_set(sigar, NULL, NULL); + if (status == SIGAR_OK) { return JNI_TRUE; } diff --git a/bindings/java/src/org/hyperic/sigar/ptql/SigarProcessQuery.java b/bindings/java/src/org/hyperic/sigar/ptql/SigarProcessQuery.java index e689c999..a3d8442f 100644 --- a/bindings/java/src/org/hyperic/sigar/ptql/SigarProcessQuery.java +++ b/bindings/java/src/org/hyperic/sigar/ptql/SigarProcessQuery.java @@ -44,4 +44,14 @@ public class SigarProcessQuery implements ProcessQuery { return match(SigarProxyCache.getSigar(sigar), pid); } + + static boolean re(String haystack, String needle) { + if (haystack == null) { + return false; + } + if (needle == null) { + return false; + } + return StringPattern.matches(haystack, needle); + } } diff --git a/bindings/java/src/org/hyperic/sigar/test/TestPTQL.java b/bindings/java/src/org/hyperic/sigar/test/TestPTQL.java index c8850833..e0c64c67 100644 --- a/bindings/java/src/org/hyperic/sigar/test/TestPTQL.java +++ b/bindings/java/src/org/hyperic/sigar/test/TestPTQL.java @@ -102,8 +102,14 @@ public class TestPTQL extends SigarTestCase { QueryLoadException, SigarException { - ProcessQuery query = - ProcessQueryFactory.getInstance(qs); + ProcessQuery query; + try { + query = + ProcessQueryFactory.getInstance(qs); + } catch (MalformedQueryException e) { + traceln("parse error: " + qs); + throw e; + } ProcessFinder finder = new ProcessFinder(proxy);