1 23 24 70 71 package com.sun.enterprise.admin.meta.naming; 72 73 75 76 77 import javax.management.ObjectName ; 79 import javax.management.MalformedObjectNameException ; 80 81 import java.lang.reflect.Constructor ; 82 import java.util.ArrayList ; 83 import java.util.Hashtable ; 84 import java.util.Enumeration ; 85 import java.text.MessageFormat ; 86 87 90 91 94 public class MBeanNamingDescriptor 95 { 96 String m_type; 98 String m_className; 99 String m_objectPattern; 100 String [] m_dottedPatterns; 101 String m_xpathPattern; 102 int m_mode; 103 104 int m_parmListSize = 0; 106 Object [][] m_dottedTokens = null; 107 Object [] m_objectTokens = null; 108 Object [] m_xpathTokens = null; 109 110 114 public MBeanNamingDescriptor(Object [] description) throws MBeanNamingException 115 { 116 this((String )description[0], (Integer )description[1], (String )description[2], (String )description[3], (String )description[4], (String )description[5]); 117 } 118 119 public MBeanNamingDescriptor(String type, Integer mode, String dottedPatterns, 120 String objectPattern, String xpathPattern, String className) throws MBeanNamingException 121 { 122 m_type = type; 123 m_className = className; 124 m_dottedPatterns = splitDottedPatternsString(dottedPatterns); 125 m_xpathPattern = xpathPattern; 126 m_objectPattern = objectPattern; 127 m_mode = mode.intValue(); 128 129 try 130 { 131 if(m_dottedPatterns!=null) 132 { 133 m_dottedTokens = new Object [m_dottedPatterns.length][]; 134 for(int i=0; i<m_dottedPatterns.length; i++) 135 m_dottedTokens[i] = getDottedNamePatternTokens(m_dottedPatterns[i]); 136 } 137 m_objectTokens = getObjectNamePatternTokens(m_objectPattern); 138 m_xpathTokens = getXPathTokens(m_xpathPattern); 139 m_parmListSize = getMaxTokenIndex(m_objectTokens) + 1; 140 141 checkConsistency(); 142 } 143 catch(Exception e) 144 { 145 String msg = ( "admin.server.core.mbean.config.naming.mbeandescriptor_creation_failure_for_object_pattern"+ objectPattern+ e.getMessage() ); 146 throw new MBeanNamingException( msg ); 147 } 148 } 149 150 private void checkConsistency() throws MBeanNamingException 151 { 152 } 153 154 155 boolean isDottedPatternMatch(Name name) 157 { 158 if(findDottedPatternTokens(name)!=null) 159 return true; 160 return false; 161 } 162 163 private Object [] findDottedPatternTokens(Name name) 164 { 165 if(m_dottedTokens!=null) 166 { 167 for(int i=0; i<m_dottedTokens.length; i++) 168 { 169 if(isDottedPatternMatch(name, m_dottedTokens[i], true)) 170 return m_dottedTokens[i]; 171 } 172 } 173 return null; 174 } 175 176 public boolean isObjectNamePatternMatch(ObjectName objectName) 178 { 179 Hashtable ht = objectName.getKeyPropertyList(); 180 ht.put(":",objectName.getDomain()); return isObjectNamePatternMatch(ht); 182 } 183 184 public boolean isObjectNamePatternMatch(Hashtable sample) 186 { 187 if(m_objectTokens.length!=(sample.size()*2)) 188 return false; 189 for(int i=0; i<m_objectTokens.length; i = i+2) 190 { 191 String sampleVal = (String )sample.get(m_objectTokens[i]); 192 if(sampleVal==null || 193 ((m_objectTokens[i+1] instanceof String ) && !sampleVal.equals((String )m_objectTokens[i+1])) ) 194 return false; 195 } 196 return (true); 197 } 198 199 200 String [] extractParmList(String dottedName) throws MalformedObjectNameException 201 { 202 if(m_dottedTokens==null) 203 return null; 204 Name name = new Name(dottedName); 205 Object [] tokens = findDottedPatternTokens(name); 206 if(tokens == null) 207 return null; 208 int nTokens = name.getNumParts(); 209 if(name.getNumParts()!=tokens.length) 210 return null; 211 212 String [] parmList = new String [m_parmListSize]; 213 214 for(int i=0; i<nTokens; i++) 215 { 216 if( tokens[i] instanceof Integer ) 217 { 218 parmList[((Integer )tokens[i]).intValue()] = name.getNamePart(i).toString(); 219 } 220 } 221 return parmList; 222 } 223 224 225 226 private Object [] getDottedNamePatternTokens(String dottedPattern) throws MalformedObjectNameException 227 { 228 ArrayList list = new ArrayList (); 229 int idx = 0, idx2 = 0; 230 231 if(dottedPattern!=null) 232 { 233 while(idx<dottedPattern.length() && (idx2=dottedPattern.indexOf('.', idx))>=0) 234 { 235 if(idx == idx2) 236 list.add(""); 237 else 238 list.add(dottedPattern.substring(idx,idx2).trim()); 239 idx = idx2+1; 240 } 241 if(idx<dottedPattern.length()) 242 list.add(dottedPattern.substring(idx).trim()); 243 Object [] tokens = list.toArray(); 244 replacePlaceholdersToIntegers(tokens); 245 return tokens; 246 } 247 return null; 248 } 249 250 public static Object [] getXPathTokens(String xpathPattern) 252 { 253 ArrayList list = new ArrayList (); 254 int idx = 0, idx2 = 0; 255 256 if(xpathPattern!=null) 257 { 258 while(idx<xpathPattern.length() && (idx2=xpathPattern.indexOf("'", idx))>=0) 259 { 260 if(idx != idx2) 261 list.add(xpathPattern.substring(idx,idx2)); 262 idx = idx2+1; 263 } 264 if(idx<xpathPattern.length()) 265 list.add(xpathPattern.substring(idx).trim()); 266 Object [] tokens = list.toArray(); 267 replacePlaceholdersToIntegers(tokens); 268 return tokens; 269 } 270 return null; 271 } 272 273 private Object [] getObjectNamePatternTokens(String objectPattern) throws MalformedObjectNameException 275 { 276 if(objectPattern!=null) 277 { 278 ObjectName objName = new ObjectName (objectPattern); 279 Hashtable ht = objName.getKeyPropertyList(); 280 ht.put(":",objName.getDomain()); Enumeration ee = ht.keys(); 282 Object [] tokens = new Object [ht.size()*2]; 283 int i = 0; 284 while(ee.hasMoreElements()) 285 { 286 String key = ((String )ee.nextElement()); 287 tokens[i++] = key; tokens[i++] = ht.get(key); 289 } 290 replacePlaceholdersToIntegers(tokens); 291 return tokens; 292 } 293 return null; 294 } 295 296 public int getParmListSize() 297 { 298 return m_parmListSize; 299 } 300 public String [] extractParmList(ObjectName objectName) 301 { 302 if(m_objectTokens==null) 303 return null; 304 Hashtable ht = objectName.getKeyPropertyList(); 305 ht.put(":",objectName.getDomain()); String [] parmList = new String [m_parmListSize]; 307 for(int i=0; i<m_objectTokens.length; i=i+2) 308 { 309 if( m_objectTokens[i+1] instanceof Integer ) 310 { 311 parmList[((Integer )m_objectTokens[i+1]).intValue()] = (String )ht.get(m_objectTokens[i]); 312 } 313 } 314 return parmList; 315 } 316 317 static private void replacePlaceholdersToIntegers(Object [] tokens) 318 { 319 for(int i=0; i<tokens.length; i++) 320 { 321 Object idx = getIndexForPlaceholder((String )tokens[i]); 322 if(idx!=null) 323 tokens[i] = idx; 324 } 325 } 326 327 static private Integer getIndexForPlaceholder(String str) 328 { 329 int len = str.length(); 330 if(len<3 || str.charAt(0)!='{' || str.charAt(len-1)!='}') 331 return null; 332 try 333 { 334 return Integer.valueOf(str.substring(1,len-1)); 335 } 336 catch(Throwable e) 337 { 338 } 339 return null; 340 } 341 342 private int getMaxTokenIndex(Object [] tokens) 343 { 344 int res = -1; 345 int current; 346 for(int i=0; i<tokens.length; i++) 347 { 348 if(tokens[i] instanceof Integer && 349 res < (current=((Integer )tokens[i]).intValue())) 350 res = current; 351 } 352 return res; 353 } 354 355 public String getMBeanClassName() 356 { 357 return m_className; 358 } 359 360 public String getType() 361 { 362 return m_type; 363 } 364 365 public int getMode() 366 { 367 return m_mode; 368 } 369 370 public String [] getDottedPatterns() 371 { 372 return m_dottedPatterns; 373 } 374 375 public Object [][] getDottedTokens() 376 { 377 return m_dottedTokens; 378 } 379 380 public String getXPathPattern() 381 { 382 return m_xpathPattern; 383 } 384 385 public ObjectName createObjectName(Object [] params) throws MalformedObjectNameException 386 { 387 return new ObjectName (formatPattern(m_objectPattern, params)); 388 } 389 public String [] createDottedNames(Object [] params) 390 { 391 if(m_dottedPatterns==null || m_dottedPatterns.length<1) 392 return null; 393 String [] names = new String [m_dottedPatterns.length]; 394 for(int i=0; i<m_dottedPatterns.length;i++) 395 names[i] = formatPattern(m_dottedPatterns[i], params); 396 return names; 397 } 398 public String createXPath(Object [] params) 399 { 400 return formatPattern(m_xpathPattern, params); 401 } 402 private String formatPattern(String pattern, Object [] params) 403 { 404 if(pattern==null) 405 return null; 406 return MessageFormat.format(pattern, params); 407 } 408 409 private String [] splitDottedPatternsString(String names) 410 { 411 if(names==null) 412 return null; 413 ArrayList list = new ArrayList (); 414 int idx = 0, idx2 = 0; 415 while(idx<names.length() && (idx2=names.indexOf(MBeansNaming.PATTERNS_SEPARATOR, idx))>=0) 416 { 417 if(idx2!=idx) 418 list.add(names.substring(idx, idx2)); 419 idx = idx2+1; 420 } 421 if(idx2<0) 422 list.add(names.substring(idx)); 423 return (String [])list.toArray(new String [list.size()]); 424 } 425 426 public boolean isXpathTokensMatch(Object [] tokens) 427 { 428 if(m_xpathTokens==null || tokens==null) 429 return false; 430 if(m_xpathTokens.length!= tokens.length) 431 return false; 432 for(int i=0; i<tokens.length; i++) 433 { 434 if( (m_xpathTokens[i] instanceof String ) && 435 !m_xpathTokens[i].equals(tokens[i])) 436 return false; 437 } 438 return true; 439 } 440 public String [] extractParmListFromXPath(String xpath) 441 { 442 Object [] tokens = MBeanNamingDescriptor.getXPathTokens(xpath); 443 if(m_xpathTokens==null || tokens==null || m_parmListSize<=0) 444 return null; 445 if(m_xpathTokens.length!= tokens.length) 446 return null; 447 String [] parmList = new String [m_parmListSize]; 448 for(int i=0; i<tokens.length; i++) 449 { 450 if(m_xpathTokens[i] instanceof Integer ) 451 { 452 parmList[((Integer )m_xpathTokens[i]).intValue()] = (String )tokens[i]; 453 } 454 } 455 return parmList; 456 } 457 458 public static boolean isDottedPatternMatch(Name name, Object [] tokens, boolean bExactMatch) 459 { 460 if(tokens==null) 461 return false; 462 int nTokens = name.getNumParts(); 463 if(bExactMatch) 464 { 465 if(nTokens!=tokens.length) 466 return false; 467 } 468 else 469 { 470 if(nTokens>tokens.length) 471 return false; 472 } 473 for(int i=0; i<nTokens; i++) 474 { 475 if( (tokens[i] instanceof String ) && 476 !name.getNamePart(i).toString().equals((String )tokens[i])) 477 return false; 478 } 479 return true; 480 } 481 482 public static String XPATH_TO_MASK(String xpath) 483 { 484 if(xpath==null || xpath.length()==0) 485 return xpath; 486 487 char[] chrs = xpath.toCharArray(); 488 char[] newchrs = new char[chrs.length*2]; 489 int j = 0; 490 for(int i=0; i<chrs.length; i++) 491 { 492 newchrs[j++] = chrs[i]; 493 if(chrs[i]=='\'') 494 newchrs[j++] = '\''; 495 } 496 return String.valueOf(newchrs, 0, j); 497 } 498 499 } 500 | Popular Tags |