1 27 28 package com.adventnet.jmx.utils; 29 30 import java.util.Enumeration ; 31 import java.util.Hashtable ; 32 33 import javax.management.ObjectName ; 34 35 40 public class JmxUtilities 41 { 42 public JmxUtilities() 43 { 44 } 45 46 public static boolean followsObjectNamePattern(String string, String pattern) 47 throws Exception  48 { 49 ObjectName patON = new ObjectName ("tempDomain:"+pattern); 50 if(patON.isPropertyPattern()) 51 return true; 52 53 return false; 54 } 55 56 public static boolean objectNameSpecificChecks(String string, String pattern) throws Exception  57 { 58 ObjectName stringON = new ObjectName ("tempDomain:"+string); 59 ObjectName patON = new ObjectName ("tempDomain:"+pattern); 60 61 Hashtable patProps = patON.getKeyPropertyList(); 62 Hashtable stringProps = stringON.getKeyPropertyList(); 63 64 for(Enumeration e=patProps.keys();e.hasMoreElements();) 65 { 66 String key = (String )e.nextElement(); 67 String value = (String )patProps.get(key); 68 69 if(stringProps.get(key) != null && stringProps.get(key).equals(value)); 70 else 71 return false; 72 73 } 74 return true; 75 76 } 77 78 public static boolean checkPattern( String info, String pat,boolean wc) 79 { 80 boolean returnFlag; 81 82 if(!wc) 83 { 84 return info.equals(pat); 85 } 86 87 try 88 { 89 if(followsObjectNamePattern(info, pat)) 90 return objectNameSpecificChecks( info, pat); 91 } 92 catch(Exception e){} 93 94 returnFlag = wildcardMatch(pat,info); 95 96 return returnFlag; 97 } 98 99 public static boolean wildCPattern(String p_string,String p_pattern) 100 { 101 char c; 102 char p; 103 boolean retval; 104 105 for (;;) 106 { 107 if(p_pattern.length() == 0) 108 { 109 if(p_string.length() == 0) 110 { 111 return true; 112 } 113 else 114 { 115 return false; 116 } 117 } 118 119 if(p_string.length() == 0) 120 { 121 122 return false; 123 } 124 125 p = p_pattern.charAt(0); 126 p_pattern = p_pattern.substring(1); 127 128 if(p == '$') 129 { 130 if(p_pattern.length() == 0) 131 { 132 133 return false; 134 } 135 136 p = p_pattern.charAt(0); 137 p_pattern =p_pattern.substring(1); 138 140 switch (p) 141 { 142 case '*': 143 while (p_string.length() != 0) 144 { 145 p_string = p_string.substring(1); 146 148 149 retval = wildCPattern(p_string,p_pattern); 150 151 if (retval == true) 152 { 153 return true; 155 } 156 } 157 158 retval = wildCPattern (p_string,p_pattern); 159 160 return retval; 161 162 case '?': 163 164 if (p_string.length() == 0) 165 { 166 167 return false; 169 } 170 p_string = p_string.substring(1); 171 172 break; 173 case '$': 174 p_string = p_string.substring(1); 175 c = p_string.charAt(0); 176 if (c != '$') 178 { 179 return false; 180 } 181 break; 182 default: 183 return false; 185 } 186 } 187 else 188 { 189 c = p_string.charAt(0); 190 p_string = p_string.substring(1); 191 if (c != p) 192 { 193 return false; 195 } 196 } 197 198 } 199 200 } 201 202 public static boolean wildcardMatch(String pattern, String string) 203 { 204 int stringLength = string.length(); 205 int stringIndex = 0; 206 for (int patternIndex = 0; patternIndex < pattern.length(); ++patternIndex) 207 { 208 char c = pattern.charAt(patternIndex); 209 if (c == '*') 210 { 211 while (stringIndex < stringLength) 214 { 215 if (wildcardMatch(pattern.substring(patternIndex + 1), string.substring(stringIndex))) {return true;} 216 ++stringIndex; 218 } 219 } 220 else if (c == '?') 221 { 222 ++stringIndex; 224 if (stringIndex > stringLength) {return false;} 225 } 226 else 227 { 228 if (stringIndex >= stringLength || c != string.charAt(stringIndex)) {return false;} 230 ++stringIndex; 231 } 232 } 233 234 return stringIndex == stringLength; 236 } 237 } | Popular Tags |