1 19 20 package org.netbeans.modules.schema2beans; 21 22 import java.util.*; 23 24 import java.beans.*; 25 26 49 public class DDRegistry extends Object { 50 51 63 public static class DDChangeMarker { 64 65 private DDRegistry reg; 66 private long timestamp; 67 68 private ArrayList elts = null; 69 70 DDChangeMarker(DDRegistry reg) { 71 this.reg = reg; 72 this.elts = new ArrayList(); 73 this.timestamp = 0L; 74 } 75 76 77 public int size() { 78 return this.elts.size(); 79 } 80 81 85 public void add(DDChangeMarker cm) { 86 if (cm == this) { 87 Thread.dumpStack(); 88 } 89 this.elts.add(cm); 90 } 91 92 96 public void add(BaseBean b) { 97 RegEntry re = this.reg.getRegEntry(b, false); 98 if (re != null && !this.elts.contains(re)) { 99 this.elts.add(re); 100 } 101 } 102 103 106 public void add(DDRegistryParser.DDCursor c) { 107 String id = this.reg.getID(c); 108 if (id != null) { 109 BaseBean b = this.reg.getRoot(id); 110 this.add(b); 111 } 112 } 113 114 117 public void remove(DDChangeMarker cm) { 118 this.elts.remove(cm); 119 } 120 121 public void remove(BaseBean b) { 122 RegEntry re = this.reg.getRegEntry(b, false); 123 if (re != null) 124 this.elts.remove(re); 125 } 126 127 public void remove(DDRegistryParser.DDCursor c) { 128 String id = this.reg.getID(c); 129 if (id != null) { 130 BaseBean b = this.reg.getRoot(id); 131 this.remove(b); 132 } 133 } 134 135 139 public void resetTime() { 140 this.timestamp = System.currentTimeMillis(); 141 } 142 143 147 public boolean hasChanged() { 148 boolean b = this.hasChanged(this.timestamp); 149 return b; 150 } 151 152 private boolean hasChanged(long ts) { 153 for(int i=0; i<this.elts.size(); i++) { 154 Object o = this.elts.get(i); 155 156 if (o == null) 157 continue; 158 159 if (o instanceof DDChangeMarker) { 160 if (((DDChangeMarker)o).hasChanged(ts)) { 161 return true; 162 } 163 } else 164 if (o instanceof RegEntry) { 165 if (((RegEntry)o).getTimestamp() > ts) { 166 return true; 167 } 168 } 169 } 170 return false; 171 } 172 173 176 public String dump() { 177 return this.dump(new StringBuffer (), "", this.timestamp).toString(); 179 } 180 181 public StringBuffer dump(StringBuffer sb, String indent, long ts) { 183 sb.append(indent + this.toString() + "\n"); 184 185 for(int i=0; i<this.elts.size(); i++) { 186 Object o = this.elts.get(i); 187 188 if (o == null) 189 continue; 190 191 if (o instanceof DDChangeMarker) { 192 ((DDChangeMarker)o).dump(sb, indent + " ", ts); 193 } else 194 if (o instanceof RegEntry) { 195 RegEntry re = (RegEntry)o; 196 sb.append(indent + " " + re.getBean() + "-0x" + 197 Integer.toHexString(re.getBean().hashCode())); 198 long l = re.getTimestamp(); 199 if (l > ts) { 200 sb.append(" Changed (bean ts:" + l 201 + " > cm ts:" + ts ); 202 } else { 204 sb.append(" No_Change (bean ts:" + l 205 + " < cm ts:" + ts + ")"); 206 } 207 sb.append("\n"); 208 } 209 } 210 return sb; 211 } 213 public String toString() { 214 return "DDChangeMarker-0x" + Integer.toHexString(this.hashCode()); } 216 } 217 218 221 public class ChangeTracer implements PropertyChangeListener { 222 DDRegistry reg; 223 224 public ChangeTracer(DDRegistry reg) { 225 this.reg = reg; 226 } 227 228 public void propertyChange(PropertyChangeEvent e) { 229 230 try { 231 BaseBean s = (BaseBean)e.getSource(); 232 RegEntry re = this.reg.getRegEntry(s, false); 233 re.setTimestamp(); 234 } catch(Exception ex) { 238 } 239 } 240 } 241 242 private ArrayList scopes; 243 private ChangeTracer changeTracer; 244 245 246 public DDRegistry() { 248 this.scopes = new ArrayList(); 249 this.changeTracer = new ChangeTracer(this); 250 } 251 252 262 public void createEntry(BaseBean bean, String ID, String name) { 263 RegEntry entry = this.getRegEntry(bean, false); 264 if (entry != null) { 265 throw new IllegalArgumentException (Common.getMessage( 266 "BeanGraphAlreadyInRegistry_msg", bean.name())); 267 } 268 269 entry = this.getRegEntry(ID); 270 if (entry != null) { 271 throw new IllegalArgumentException (Common.getMessage( 272 "CantRegisterGraphSameID_msg", bean.name(), entry, ID)); 273 } 274 bean.addPropertyChangeListener(this.changeTracer); 275 this.scopes.add(new RegEntry(bean, ID, name)); 276 } 277 278 282 public void updateEntry(String ID, BaseBean bean) { 283 RegEntry entry = this.getRegEntry(ID); 284 if (entry != null) 285 entry.setBean(bean); 286 else 287 throw new IllegalArgumentException (Common.getMessage( 288 "CantUpdateGraphNotInRegistry_msg", ID)); 289 } 290 291 294 public void removeEntry(BaseBean bean) { 295 RegEntry entry = this.getRegEntry(bean, false); 296 if (entry != null) { 297 entry.getBean().removePropertyChangeListener(this.changeTracer); 298 this.removeRegEntry(entry); 299 } 300 } 301 302 306 public void renameEntry(String oldID, String newID, String newName) { 307 RegEntry entry = this.getRegEntry(oldID); 308 if (entry != null) { 309 entry.setID(newID); 310 if (newName != null) 311 entry.setName(newName); 312 } 313 } 314 315 318 public void renameEntry(String oldID, String newID) { 319 this.renameEntry(oldID, newID, null); 320 } 321 322 325 public void removeEntry(String ID) { 326 RegEntry entry = this.getRegEntry(ID); 327 if (entry != null) 328 entry.getBean().removePropertyChangeListener(this.changeTracer); 329 this.removeRegEntry(entry); 330 } 331 332 336 public DDChangeMarker createChangeMarker() { 337 return new DDChangeMarker(this); 338 } 339 340 344 public boolean hasType(BaseBean bean, String type) { 345 RegEntry r = this.getRegEntry(bean, false); 346 if (r != null) 347 return r.hasType(type); 348 return false; 349 } 350 351 354 public void clearCache() { 355 for (int i=0; i<this.scopes.size(); i++) { 356 RegEntry se = (RegEntry)this.scopes.get(i); 357 if (se != null) 358 se.setTimestamp(); 359 } 360 } 361 362 365 public void clearCache(BaseBean bean) { 366 this.setTimestamp(bean); 367 } 368 369 public void setTimestamp(BaseBean bean) { 370 RegEntry r = this.getRegEntry(bean, false); 371 if (r != null) 372 r.setTimestamp(); 373 } 374 375 public long getTimestamp(BaseBean bean) { 376 RegEntry r = this.getRegEntry(bean, false); 377 if (r != null) 378 return r.getTimestamp(); 379 return 0L; 380 } 381 382 385 public String getID(BaseBean bean) { 386 RegEntry r = this.getRegEntry(bean, false); 387 if (r != null) 388 return r.getID(); 389 return null; 390 } 391 392 397 public String getID(DDRegistryParser.DDCursor c) { 398 RegEntry r = this.getRegEntry(c.getRoot(), false); 399 if (r != null) 400 return r.getID(); 401 return null; 402 } 403 404 407 public String getName(DDRegistryParser.DDCursor c) { 408 RegEntry r = this.getRegEntry(c.getRoot(), false); 409 if (r != null) 410 return r.getName(); 411 return null; 412 } 413 414 415 419 public DDRegistryParser newParser(DDRegistryParser parser, String path) { 420 return new DDRegistryParser(this, parser, path); 421 } 422 423 427 public DDRegistryParser newParser(DDRegistryParser.DDCursor cursor, 428 String path) { 429 return new DDRegistryParser(this, cursor, path); 430 } 431 432 441 public DDRegistryParser newParser(String scope) { 442 return new DDRegistryParser(this, scope); 443 } 444 445 448 public DDRegistryParser.DDCursor newCursor(DDRegistryParser.DDCursor c, 449 String path) { 450 return new DDRegistryParser.DDCursor(c, path); 451 } 452 453 456 public DDRegistryParser.DDCursor newCursor(String path) { 457 return new DDRegistryParser.DDCursor(this, path); 458 } 459 460 463 public DDRegistryParser.DDCursor newCursor(BaseBean bean) { 464 return new DDRegistryParser.DDCursor(this, bean); 465 } 466 467 470 public void addType(BaseBean bean, String type) { 471 472 if (DDLogFlags.debug) { 473 TraceLogger.put(TraceLogger.DEBUG, TraceLogger.SVC_DD, 474 DDLogFlags.DBG_REG, 1, DDLogFlags.ADDTYPE, 475 bean.name() + " " + type); 476 } 477 478 RegEntry se = this.getRegEntry(bean, true); 479 se.add(type); 480 } 481 482 485 public String getName(String ID) { 486 RegEntry r = this.getRegEntry(ID); 487 if (r!= null) 488 return r.getName(); 489 return null; 490 } 491 492 495 public String dump() { 496 StringBuffer s = new StringBuffer (); 497 for (int i=0; i<this.scopes.size(); i++) { 498 RegEntry se = (RegEntry)this.scopes.get(i); 499 if (se != null) { 500 s.append(se.toString() + "\n"); } 502 } 503 return s.toString(); 504 } 505 506 509 public String dumpAll() { 510 StringBuffer s = new StringBuffer (); 511 for (int i=0; i<this.scopes.size(); i++) { 512 RegEntry se = (RegEntry)this.scopes.get(i); 513 if (se != null) { 514 s.append("\n*** Graph:" + se.toString() + "\n"); s.append(se.getBean().dumpBeanNode()); 516 } 517 } 518 return s.toString(); 519 } 520 521 524 public RegEntry getRegEntry(BaseBean bean, boolean raise) { 525 for (int i=0; i<this.scopes.size(); i++) { 526 RegEntry se = (RegEntry)this.scopes.get(i); 527 if (se.getBean() == bean) { 528 return se; 529 } 530 } 531 if ((bean != null) && !bean.isRoot()) { 533 do { 534 bean = bean.parent(); 535 } while(bean != null && !bean.isRoot()); 536 return this.getRegEntry(bean, raise); 537 } 538 539 if (raise) { 540 throw new IllegalArgumentException (Common.getMessage( 541 "BeanGraphEntryNotInRegistry_msg", bean.name())); 542 } else 543 return null; 544 } 545 546 549 private RegEntry getRegEntry(String ID) { 550 for (int i=0; i<this.scopes.size(); i++) { 551 RegEntry se = (RegEntry)this.scopes.get(i); 552 if (se.getID().equals(ID)) 553 return se; 554 } 555 return null; 556 } 557 558 561 private void removeRegEntry(RegEntry entry) { 562 int i = this.scopes.indexOf(entry); 563 if (i != -1) 564 this.scopes.remove(i); 565 } 566 567 public static String getGraphName(String s) { 569 if (s != null) { 570 s = s.trim(); 571 if (s.startsWith("[")) { int i = s.indexOf(']'); 573 if (i != -1) 574 s = s.substring(1,i); 575 else 576 return null; 577 } 578 } 579 return s; 580 } 581 582 585 public boolean hasGraph(String str) { 586 return this.hasGraph(str, null); 587 } 588 589 public boolean hasGraph(String str, DDRegistryParser.DDCursor c) { 590 String s = getGraphName(str); 591 if (s != null) { 592 if (!s.equals(DDRegistryParser.CURRENT_CURSOR)) 593 return (this.getRoot(s) != null); 594 else { 595 if (c == null) 596 return true; 597 else 598 return (this.getID(c) != null); 599 } 600 } 601 return false; 602 } 603 604 static public String createGraphName(String s) { 605 if (s != null) { 606 int i = s.indexOf('['); 608 if (i != -1 && i<s.indexOf(']')) 609 return s; 610 else 611 return "["+s+"]"; } else { 613 throw new IllegalArgumentException (Common.getMessage( 614 "GraphNameCantBeNull_msg")); 615 } 616 } 617 618 621 public BaseBean getRoot(String s) { 622 BaseBean[] r = this.getRoots(s); 623 if (r.length>0) 624 return r[0]; 625 return null; 626 } 627 628 631 public BaseBean[] getRoots(String s) { 632 s = getGraphName(s); 633 RegEntry se = this.getRegEntry(s); 635 if (se == null) { 636 ArrayList list = new ArrayList(); 637 for (int i=0; i<this.scopes.size(); i++) { 638 se = (RegEntry)this.scopes.get(i); 639 if (se.hasType(s)) 640 list.add(se.getBean()); 641 } 642 BaseBean[] ret = new BaseBean[list.size()]; 643 return (BaseBean[])list.toArray(ret); 644 } else 645 return (new BaseBean[] {se.getBean()}); 646 } 647 648 651 class IterateParser implements Iterator { 652 private ArrayList list; 653 private int pos; 654 public IterateParser() { 655 this.list = new ArrayList(); 656 } 657 658 void addParser(BaseBean b, String parsingPath) { 659 this.list.add(new DDParser(b, parsingPath)); 660 } 661 662 public boolean hasNext() { 663 if (pos < list.size()) 664 return true; 665 else 666 return false; 667 } 668 669 public Object next() { 670 return this.list.get(pos++); 671 } 672 673 public void remove() { 674 throw new UnsupportedOperationException (); 675 } 676 } 677 678 679 682 683 static class RegEntry { 684 private BaseBean bean; 685 private ArrayList types; 686 private String ID; 687 private String name; 688 private long timestamp; 689 691 RegEntry(BaseBean b, String ID, String name) { 692 this.bean = b; 693 this.types = new ArrayList(); 694 this.ID = ID; 695 this.name = name; 696 this.setTimestamp(); 697 } 698 699 707 708 void setTimestamp() { 710 this.timestamp = System.currentTimeMillis(); 711 } 712 713 long getTimestamp() { 714 return this.timestamp; 715 } 716 717 void setBean(BaseBean bean) { 718 this.bean = bean; 719 } 720 721 BaseBean getBean() { 722 return this.bean; 723 } 724 725 String getName() { 726 return this.name; 727 } 728 729 String getID() { 730 return this.ID; 731 } 732 733 void setID(String ID) { 734 this.ID = ID; 735 } 736 737 void setName(String name) { 738 this.name = name; 739 } 740 741 void add(String type) { 742 this.types.add(type); 743 } 744 745 boolean hasType(String type) { 746 for (int i=0; i<this.types.size(); i++) { 747 String t = (String )this.types.get(i); 748 if (type != null && t.equals(type)) 749 return true; 750 } 751 return false; 752 } 753 754 public String toString() { 755 String s = this.bean.name() + "(id:" + ID + "-name:" + name + ")" + " ["; for (int i=0; i<this.types.size(); i++) { 757 String t = (String )this.types.get(i); 758 s += " " + t; } 760 return s + "]"; 762 } 763 } 764 } 765 | Popular Tags |