1 22 package org.jboss.mx.util; 23 24 import java.util.Hashtable ; 25 import java.util.Iterator ; 26 import java.util.Map ; 27 28 import javax.management.ObjectName ; 29 30 41 public class ObjectNamePatternHelper 42 { 43 45 51 public static boolean patternMatch(ObjectName test, ObjectName pattern) 52 { 53 if (pattern.equals("*:*")) 54 return true; 55 56 if (patternMatch(test.getDomain(), pattern.getDomain())) 57 { 58 PropertyPattern propertyPattern = new PropertyPattern(pattern); 59 return propertyPattern.patternMatch(test); 60 } 61 return false; 62 } 63 64 70 public static boolean patternMatch(String test, String pattern) 71 { 72 if (pattern.equals("*")) 73 return true; 74 return patternMatch(test.toCharArray(), 0, pattern.toCharArray(), 0); 75 } 76 77 90 public static boolean patternMatch(char[] test, int tpos, char[] pattern, int ppos) 91 { 92 int tlen = test.length; 93 int plen = pattern.length; 94 95 while (ppos < plen) 96 { 97 char c = pattern[ppos++]; 98 if ('?' == c) 99 { 100 if (tpos++ == tlen) 103 return false; 104 } 105 else if ('*' == c) 106 { 107 if (ppos == plen) return true; 109 110 if (tpos == tlen) 112 return false; 113 114 do 117 { 118 if (patternMatch(test, tpos, pattern, ppos)) 119 return true; 120 } 121 while (++tpos < tlen); 122 } 123 else if (tpos == tlen || c != test[tpos++]) 124 return false; 125 } 126 return (tpos == tlen); 128 } 129 130 133 public static class PropertyPattern 134 { 135 138 boolean isPropertyPattern; 139 140 143 Object [] propertyKeys; 144 145 148 Object [] propertyValues; 149 150 153 String canonicalKeyPropertyString; 154 155 160 public PropertyPattern(ObjectName pattern) 161 { 162 isPropertyPattern = pattern.isPropertyPattern(); 163 if (isPropertyPattern) 164 { 165 Hashtable patternKPList = pattern.getKeyPropertyList(); 166 int length = patternKPList.size(); 167 propertyKeys = new Object [length]; 168 propertyValues = new Object [length]; 169 170 int i = 0; 171 for (Iterator iterator = patternKPList.entrySet().iterator(); iterator.hasNext(); i++) 172 { 173 Map.Entry entry = (Map.Entry ) iterator.next(); 174 propertyKeys[i] = entry.getKey(); 175 propertyValues[i] = entry.getValue(); 176 } 177 } 178 else 179 canonicalKeyPropertyString = pattern.getCanonicalKeyPropertyListString(); 180 } 181 182 187 public boolean patternMatch(ObjectName name) 188 { 189 if (isPropertyPattern) 190 { 191 if (propertyKeys.length == 0) 193 return true; 194 195 Hashtable kplist = name.getKeyPropertyList(); 196 197 for (int i = 0; i < propertyKeys.length; i++) 198 { 199 if (propertyValues[i].equals(kplist.get(propertyKeys[i])) == false) 200 return false; 201 } 202 return true; 203 } 204 else 205 return canonicalKeyPropertyString.equals(name.getCanonicalKeyPropertyListString()); 206 } 207 } 208 } 209 | Popular Tags |