1 7 8 package com.sun.jmx.mbeanserver; 9 10 11 import java.util.Hashtable ; 13 import java.util.Enumeration ; 14 import java.util.Set ; 15 import java.util.ArrayList ; 16 import java.util.HashSet ; 17 import java.util.Vector ; 18 19 import javax.management.* ; 21 import com.sun.jmx.defaults.ServiceName; 22 import com.sun.jmx.trace.Trace; 23 24 30 public class RepositorySupport implements Repository { 31 32 34 38 private final static ObjectName _WholeWordQueryObjectName; 39 static { 40 try { 41 _WholeWordQueryObjectName = new ObjectName("*:*"); 42 } catch (MalformedObjectNameException e) { 43 throw new UnsupportedOperationException (e.getMessage()); 44 } 45 } 46 47 51 private static int _slen; 52 private static int _plen; 53 54 61 private final Hashtable domainTb; 62 63 66 private int nbElements = 0; 67 68 73 private final String domain; 74 75 76 private final static String dbgTag = "Repository"; 77 78 80 81 83 86 private final static boolean isTraceOn() { 87 return Trace.isSelected(Trace.LEVEL_TRACE, Trace.INFO_MBEANSERVER); 88 } 89 90 private final static void trace(String clz, String func, String info) { 91 Trace.send(Trace.LEVEL_TRACE, Trace.INFO_MBEANSERVER, clz, func, 92 info); 93 } 94 95 private final static void trace(String func, String info) { 96 trace(dbgTag, func, info); 97 } 98 99 private final static boolean isDebugOn() { 100 return Trace.isSelected(Trace.LEVEL_DEBUG, Trace.INFO_MBEANSERVER); 101 } 102 103 private final static void debug(String clz, String func, String info) { 104 Trace.send(Trace.LEVEL_DEBUG, Trace.INFO_MBEANSERVER, clz, func, 105 info); 106 } 107 108 private final static void debug(String func, String info) { 109 debug(dbgTag, func, info); 110 } 111 112 113 private final static class ObjectNamePattern { 114 private final char[] domain; 115 private final String [] keys; 116 private final String [] values; 117 private final String properties; 118 private final boolean isPropertyPattern; 119 120 123 public final ObjectName pattern; 124 125 129 public ObjectNamePattern(ObjectName pattern) { 130 this(pattern.isPattern(),pattern.getDomain(), 131 pattern.isPropertyPattern(), 132 pattern.getCanonicalKeyPropertyListString(), 133 pattern.getKeyPropertyList(),pattern); 134 } 135 136 146 ObjectNamePattern(boolean domainPattern, String domain, 147 boolean propertyPattern, String canonicalProps, 148 Hashtable keyPropertyList, ObjectName pattern) { 149 final int len = (keyPropertyList==null?0:keyPropertyList.size()); 150 final Enumeration e = 151 (keyPropertyList==null?null:keyPropertyList.keys()); 152 this.domain = (domain == null?null:domain.toCharArray()); 153 this.keys = new String [len]; 154 this.values = new String [len]; 155 for (int i = 0 ; i < len ; i++ ) { 156 final String k = (String )e.nextElement(); 157 keys[i] = k; 158 values[i] = (String )keyPropertyList.get(k); 159 } 160 this.properties = canonicalProps; 161 this.isPropertyPattern = propertyPattern; 162 this.pattern = pattern; 163 } 164 165 174 public boolean matchKeys(ObjectName name) { 175 if (isPropertyPattern) { 176 for (int i= keys.length -1; i >= 0 ; i--) { 178 179 String v = name.getKeyProperty(keys[i]); 182 183 if (v == null) return false; 185 186 if (v.equals(values[i])) continue; 189 return false; 190 } 191 return true; 192 } else { 193 if (keys.length != name.getKeyPropertyList().size()) 194 return false; 195 final String p1 = name.getCanonicalKeyPropertyListString(); 196 final String p2 = properties; 197 return (p1.equals(p2)); 200 } 201 } 202 } 203 204 210 private final void addAllMatching(final Hashtable moiTb, final Set result, 211 final ObjectNamePattern pattern) { 212 synchronized (moiTb) { 213 for (Enumeration e = moiTb.elements(); e.hasMoreElements();) { 214 final NamedObject no = (NamedObject) e.nextElement(); 215 final ObjectName on = no.getName(); 216 217 if (pattern.matchKeys(on)) result.add(no); 219 } 220 } 221 } 222 223 private final void addNewDomMoi(final Object object, final String dom, 224 final ObjectName name) { 225 final Hashtable moiTb= new Hashtable (); 226 domainTb.put(dom, moiTb); 227 moiTb.put(name.getCanonicalKeyPropertyListString(), 228 new NamedObject(name, object)); 229 nbElements++; 230 } 231 232 238 private static boolean wildmatch(char[] s, char[] p, int si, int pi) { 239 char c; 240 _slen = s.length; 243 _plen = p.length; 244 246 while (pi < _plen) { c = p[pi++]; 248 if (c == '?') { 249 if (++si > _slen) return false; 250 } else if (c == '*') { if (pi >= _plen) return true; 252 do { 253 if (wildmatch(s,p,si,pi)) return true; 254 } while (++si < _slen); 255 return false; 256 } else { 257 if (si >= _slen || c != s[si++]) return false; 258 } 259 } 260 return (si == _slen); 261 } 262 263 267 private NamedObject retrieveNamedObject(ObjectName name) { 268 269 if (name.isPattern() == true) return null; 271 272 String dom= name.getDomain().intern(); 274 275 if (dom.length() == 0) { 277 dom = domain; 278 } 279 280 Object tmp_object = domainTb.get(dom); 281 if (tmp_object == null) { 282 return null; } 284 285 Hashtable moiTb= (Hashtable ) tmp_object; 287 288 Object o = moiTb.get(name.getCanonicalKeyPropertyListString()); 289 if (o != null ) { 290 return (NamedObject) o; 291 } 292 else return null; 293 } 294 295 297 300 301 303 307 public RepositorySupport(String domain) { 308 domainTb= new Hashtable (5); 309 310 if (domain != null && domain.length() != 0) 311 this.domain = domain; 312 else 313 this.domain = ServiceName.DOMAIN; 314 315 domainTb.put(this.domain.intern(), new Hashtable ()); 317 318 } 321 322 331 public void setConfigParameters(ArrayList configParameters) { 332 return; 333 } 334 335 341 public String [] getDomains() { 342 final ArrayList result; 343 synchronized(domainTb) { 344 result = new ArrayList (domainTb.size()); 346 347 for (Enumeration e = domainTb.keys();e.hasMoreElements();) { 349 final String key = (String )e.nextElement(); 351 if (key == null) continue; 352 353 final Hashtable t = (Hashtable )domainTb.get(key); 355 if (t == null || t.size()==0) continue; 356 357 result.add(key); 359 } 360 } 361 362 return (String []) result.toArray(new String [result.size()]); 364 365 } 366 367 368 369 376 public boolean isFiltering() { 377 return false; 379 } 380 381 388 public void addMBean(final Object object, ObjectName name) 389 throws InstanceAlreadyExistsException { 390 391 if (isTraceOn()) { 392 trace("addMBean", "name=" + name); 393 } 394 395 String dom = name.getDomain().intern(); 397 boolean to_default_domain = false; 398 399 if (dom.length() == 0) { 401 402 try { 403 name = new ObjectName(domain + name.toString()); 404 405 } catch (MalformedObjectNameException e) { 406 407 if (isDebugOn()) { 408 debug("addMBean", 409 "Unexpected MalformedObjectNameException"); 410 } 411 } 412 } 413 414 if (dom == domain) { 416 to_default_domain = true; 417 dom = domain; 418 } else { 419 to_default_domain = false; 420 } 421 422 425 if (name.isPattern() == true) { 427 throw new RuntimeOperationsException( 428 new IllegalArgumentException ("Repository: cannot add mbean for pattern name " + name.toString())); 429 } 430 431 if ( !to_default_domain && 433 dom.equals("JMImplementation") && 434 domainTb.containsKey("JMImplementation")) { 435 436 throw new RuntimeOperationsException( 437 new IllegalArgumentException ( 438 "Repository: domain name cannot be JMImplementation")); 439 } 440 441 final Hashtable moiTb= (Hashtable ) domainTb.get(dom); 443 if (moiTb == null) { 444 addNewDomMoi(object, dom, name); 445 return; 446 } 447 else { 448 String cstr = name.getCanonicalKeyPropertyListString(); 450 Object elmt= moiTb.get(cstr); 451 if (elmt != null) { 452 throw new InstanceAlreadyExistsException(name.toString()); 453 } else { 454 nbElements++; 455 moiTb.put(cstr, new NamedObject(name, object)); 456 } 457 } 458 } 459 460 470 public boolean contains(ObjectName name) { 471 472 if (isTraceOn()) { 473 trace("contains", "name=" + name); 474 } 475 return (retrieveNamedObject(name) != null); 476 } 477 478 488 public Object retrieve(ObjectName name) { 489 490 if (isTraceOn()) { 493 trace("retrieve", "name=" + name); 494 } 495 496 NamedObject no = retrieveNamedObject(name); 498 if (no == null) return null; 499 else return no.getObject(); 500 501 } 502 503 504 519 public Set query(ObjectName pattern, QueryExp query) { 520 521 ObjectNamePattern on_pattern = null; final HashSet result = new HashSet (); 525 526 532 ObjectName name = null; 534 if (pattern == null || 535 pattern.getCanonicalName().length() == 0 || 536 pattern.equals(_WholeWordQueryObjectName)) 537 name = _WholeWordQueryObjectName; 538 else name = pattern; 539 540 if (!name.isPattern()) { 542 final NamedObject no = retrieveNamedObject(name); 543 if (no != null) result.add(no); 544 return result; 545 } 546 547 if (name == _WholeWordQueryObjectName) { 549 synchronized(domainTb) { 550 for(final Enumeration e = domainTb.elements(); 551 e.hasMoreElements();) { 552 final Hashtable moiTb = (Hashtable ) e.nextElement(); 553 result.addAll(moiTb.values()); 554 } 555 } 556 return result; 557 } 558 559 String canonical_key_property_list_string = name.getCanonicalKeyPropertyListString(); 560 561 if (name.getDomain().length() == 0) { 567 final Hashtable moiTb = (Hashtable ) domainTb.get(domain); 568 if (canonical_key_property_list_string.length() == 0) { 569 result.addAll(moiTb.values()); 570 } else { 571 if (on_pattern == null) 572 on_pattern = new ObjectNamePattern(name); 573 addAllMatching(moiTb,result,on_pattern); 574 } 575 return result; 576 } 577 578 synchronized (domainTb) { 580 char[] dom2Match = name.getDomain().toCharArray(); 581 String nextDomain; 582 char [] theDom; 583 for (final Enumeration enumi = domainTb.keys(); enumi.hasMoreElements();) { 584 nextDomain = (String ) enumi.nextElement(); 585 theDom = nextDomain.toCharArray(); 586 587 if (wildmatch(theDom, dom2Match, 0, 0)) { 588 final Hashtable moiTb = 589 (Hashtable ) domainTb.get(nextDomain); 590 591 if (canonical_key_property_list_string.length() == 0) 592 result.addAll(moiTb.values()); 593 else { 594 if (on_pattern == null) 595 on_pattern = new ObjectNamePattern(name); 596 addAllMatching(moiTb,result,on_pattern); 597 } 598 } 599 } 600 } 601 return result; 602 } 603 604 612 public void remove(final ObjectName name) 613 throws InstanceNotFoundException { 614 615 if (isTraceOn()) { 617 trace("remove", "name=" + name); 618 } 619 620 String dom= name.getDomain().intern(); 622 623 if (dom.length() == 0) dom = domain; 625 626 Object tmp_object = domainTb.get(dom); 628 if (tmp_object == null) { 629 throw new InstanceNotFoundException(name.toString()); 630 } 631 632 Hashtable moiTb= (Hashtable ) tmp_object; 634 if (moiTb.remove(name.getCanonicalKeyPropertyListString()) == null) { 635 throw new InstanceNotFoundException(name.toString()); 636 } 637 638 nbElements--; 640 641 if (moiTb.isEmpty()) { 643 domainTb.remove(dom); 644 645 if (dom == domain) 650 domainTb.put(domain, new Hashtable ()); 651 } 652 } 653 654 659 660 public Integer getCount() { 661 return new Integer (nbElements); 662 } 663 664 670 public String getDefaultDomain() { 671 return domain; 672 } 673 674 } 675 676 677 678 | Popular Tags |