diff --git a/bindings/java/.sigar_shellrc b/bindings/java/.sigar_shellrc index 4e3ddaed..7d48f0b2 100644 --- a/bindings/java/.sigar_shellrc +++ b/bindings/java/.sigar_shellrc @@ -28,7 +28,7 @@ alias tcfind ptql State.Name.eq=java,Args.*.eq=org.apache.catalina.startup.Boots #($1 is the return value of the first query in the string) #'Pne' => 'P' flags means parent of matched process #to filter out httpd child processes -alias apfind ptql State.Name.re=^(https?d.*|[Aa]pache2?)$,State.Name.Pne=$1 +alias apfind ptql State.Name.re=https?d.*|[Aa]pache2?$,State.Name.Pne=$1 #find ant processes (ant hangs on me sometimes) #(* matches any value in the array) diff --git a/bindings/java/src/net/hyperic/sigar/ptql/ProcessQueryHelper.java b/bindings/java/src/net/hyperic/sigar/ptql/ProcessQueryHelper.java index d933b5dc..2036a7c4 100644 --- a/bindings/java/src/net/hyperic/sigar/ptql/ProcessQueryHelper.java +++ b/bindings/java/src/net/hyperic/sigar/ptql/ProcessQueryHelper.java @@ -16,7 +16,7 @@ public class ProcessQueryHelper { stringMatchers.put("ne", new StringNeMatcher()); stringMatchers.put("sw", new StringSwMatcher()); stringMatchers.put("ew", new StringEwMatcher()); - //stringMatchers.put("re", new StringReMatcher()); + stringMatchers.put("re", new StringReMatcher()); stringMatchers.put("ct", new StringCtMatcher()); } @@ -104,14 +104,12 @@ public class ProcessQueryHelper { } } - //XXX requires jdk 1.4+ to compile - /* static class StringReMatcher implements StringMatcher { public boolean match(String left, String right) { - return left.matches(right); + return StringPattern.matches(left, right); } } - */ + public static boolean argsMatch(SigarProxy proxy, long pid, String value, String op) throws SigarException, SigarNotImplementedException { diff --git a/bindings/java/src/net/hyperic/sigar/ptql/StringPattern.java b/bindings/java/src/net/hyperic/sigar/ptql/StringPattern.java new file mode 100644 index 00000000..d7ad7254 --- /dev/null +++ b/bindings/java/src/net/hyperic/sigar/ptql/StringPattern.java @@ -0,0 +1,54 @@ +package net.hyperic.sigar.ptql; + +import java.lang.reflect.Method; +import java.util.Map; + +import net.hyperic.sigar.util.ReferenceMap; + +public class StringPattern { + static Class patternClass; + static Method compile, matcher, find; + private static Map patterns = null; + + //XXX avoiding 1.4 requirement + private static void init() throws Exception { + patterns = ReferenceMap.synchronizedMap(); + patternClass = Class.forName("java.util.regex.Pattern"); + Class matcherClass = Class.forName("java.util.regex.Matcher"); + Class[] arg = { String.class }; + compile = patternClass.getDeclaredMethod("compile", arg); + arg = new Class[] { Class.forName("java.lang.CharSequence") }; + matcher = patternClass.getDeclaredMethod("matcher", arg); + find = matcherClass.getDeclaredMethod("find", new Class[0]); + } + + /** + * Wrapper around Pattern.compile(regex).matcher(source).find() + */ + public static boolean matches(String source, String regex) { + try { + if (patterns == null) { + init(); + } + + Object pattern = patterns.get(regex); + if (pattern == null) { + //pattern = Pattern.compile(regex) + pattern = compile.invoke(patternClass, new Object[] { regex }); + patterns.put(regex, pattern); + } + //m = pattern.matcher(source) + Object m = matcher.invoke(pattern, new Object[] { source }); + //result = m.find() + Boolean result = (Boolean)find.invoke(m, new Object[0]); + return result.booleanValue(); + } catch (Exception e) { + System.err.println("Error matching '" + + regex + "' against '" + + source + "'"); + e.printStackTrace(); + return false; + } + } +} +