1 19 20 package org.netbeans.modules.dbschema; 21 22 import java.io.*; 23 import java.text.MessageFormat ; 24 import java.util.*; 25 26 import org.netbeans.modules.dbschema.util.*; 27 28 import org.netbeans.modules.dbschema.migration.archiver.*; 29 30 32 public class SchemaElement extends DBElement { 33 34 public static final int STATUS_NOT = 0; 35 36 37 public static final int STATUS_ERROR = 1; 38 39 40 public static final int STATUS_PARTIAL = 2; 41 42 43 public static final int STATUS_OK = 3; 44 45 46 public static final int CURRENT_VERSION_NO = 2; 47 private int versionNo; 48 49 51 public SchemaElement() { 52 this(new Memory()); 53 } 54 55 58 public SchemaElement(Impl impl) { 59 super(impl); 60 } 61 62 65 final Impl getSchemaImpl() { 66 return (Impl)getElementImpl(); 67 } 68 69 72 public boolean isCompatibleVersion() { 73 return (getVersionNo() == CURRENT_VERSION_NO); 74 } 75 76 79 public int getVersionNo() { 80 return versionNo; 81 } 82 83 86 public void setVersionNo(int versionNo) { 87 this.versionNo = versionNo; 88 } 89 90 91 protected static Map schemaCache = new HashMap(); 92 93 94 private static SchemaElement lastSchema; 95 96 99 protected static SchemaElement getLastSchema() { 100 return lastSchema; 101 } 102 103 106 protected static void setLastSchema(SchemaElement last) { 107 lastSchema = last; 108 } 109 110 113 public static void removeFromCache(String name) { 114 synchronized (schemaCache) { 115 if (getLastSchema() != null) 116 if (getLastSchema().getName().getFullName().equals(name)) 117 setLastSchema(null); 118 119 schemaCache.remove(name); 120 } 121 } 122 123 126 public static void addToCache(SchemaElement schema) { 127 synchronized (schemaCache) { 128 schemaCache.put(schema.getName().getFullName(), schema); 129 SchemaElement.setLastSchema(schema); 130 } 131 } 132 133 145 public static SchemaElement forName(String name, Object obj) { 146 if (IDEUtil.isIDERunning()) 147 return SchemaElementUtil.forName(name, obj); 148 149 if (obj == null) 150 return forNameInternal(name, SchemaElement.class.getClassLoader()); 151 if (obj instanceof ClassLoader ) 152 return forNameInternal(name, (ClassLoader )obj); 153 154 throw new UnsupportedOperationException ("Cannot lookup schema " + 158 name + " in context of type " + obj.getClass() + 159 " expected ClassLoader or null."); 160 } 161 162 167 private static SchemaElement forNameInternal(String name, ClassLoader cl) { 168 SchemaElement se = getLastSchema(); 169 170 if (se != null && se.getName().getFullName().equals(name)) 171 return se; 172 else 173 synchronized (schemaCache) { 174 se = (SchemaElement) schemaCache.get(name); 175 if (se != null) 176 return se; 177 178 InputStream is = cl.getResourceAsStream(NameUtil.getSchemaResourceName(name)); 179 180 if (is != null) 181 try { 182 ObjectInput i = new XMLInputStream(is); 183 se = (SchemaElement) i.readObject(); 184 if (!se.isCompatibleVersion()) { 185 String message = MessageFormat.format(ResourceBundle.getBundle("org.netbeans.modules.dbschema.resources.Bundle").getString("PreviousVersion"), new String [] {name}); System.out.println(message); 187 } 188 i.close(); 189 190 se.setName(DBIdentifier.create(name)); 191 192 SchemaElement.addToCache(se); 193 194 TableElement tables[] = se.getTables(); 196 int size = (tables != null) ? tables.length : 0; 197 for (int j = 0; j < size; j++) 198 tables[j].setDeclaringSchema(se); 199 } catch (Exception exc) { 200 if (Boolean.getBoolean("netbeans.debug.exceptions")) System.out.println(ResourceBundle.getBundle("org.netbeans.modules.dbschema.resources.Bundle").getString("SchemaNotFound")); } 203 else 204 if (Boolean.getBoolean("netbeans.debug.exceptions")) System.out.println(ResourceBundle.getBundle("org.netbeans.modules.dbschema.resources.Bundle").getString("SchemaNotFound")); 207 return se; 208 } 209 } 210 211 215 public static SchemaElement forName(String name) { 216 return forName(name, null); 217 } 218 219 224 public int getStatus() { 225 return getSchemaImpl().getStatus(); 226 } 227 228 232 public void setSchema(DBIdentifier schema) throws DBException { 233 getSchemaImpl().setSchema(schema); 234 } 235 236 240 public DBIdentifier getSchema() { 241 return getSchemaImpl().getSchema(); 242 } 243 244 248 public void setCatalog(DBIdentifier catalog) throws DBException { 249 getSchemaImpl().setCatalog(catalog); 250 } 251 252 256 public DBIdentifier getCatalog() { 257 return getSchemaImpl().getCatalog(); 258 } 259 260 264 public void addTable(TableElement el) throws DBException { 265 addTables(new TableElement[] {el}); 266 } 267 268 272 public void addTables(final TableElement[] els) throws DBException { 273 for (int i = 0; i < els.length; i++) { 274 if (getTable(els[i].getName()) != null) 275 throwAddException("FMT_EXC_AddTable", els[i]); if (els[i].getDeclaringSchema() == null) 277 els[i].setDeclaringSchema(this); 278 } 279 280 getSchemaImpl().changeTables(els, Impl.ADD); 281 } 282 283 287 public void removeTable(TableElement el) throws DBException { 288 removeTables(new TableElement[] {el}); 289 } 290 291 295 public void removeTables(final TableElement[] els) throws DBException { 296 getSchemaImpl().changeTables(els, Impl.REMOVE); 297 } 298 299 304 public void setTables(TableElement[] els) throws DBException { 305 if (els == null) 306 throw new NullPointerException (ResourceBundle.getBundle("org.netbeans.modules.dbschema.resources.Bundle").getString("NullTables")); 308 getSchemaImpl().changeTables(els, Impl.SET); 309 } 310 311 314 public TableElement[] getTables() { 315 return getSchemaImpl().getTables(); 316 } 317 318 322 public TableElement getTable(DBIdentifier name) { 323 return getSchemaImpl().getTable(name); 324 } 325 326 332 private void throwAddException(String formatKey, TableElement element) throws DBException { 333 String msg = element.getName().getName(); throw new DBException(msg); 336 } 337 338 341 public String getUrl() { 342 return getSchemaImpl().getUrl(); 343 } 344 345 348 public void setUrl(String url) throws DBException { 349 getSchemaImpl().setUrl(url); 350 } 351 352 355 public String getUsername() { 356 return getSchemaImpl().getUsername(); 357 } 358 359 362 public void setUsername(String username) throws DBException { 363 getSchemaImpl().setUsername(username); 364 } 365 366 369 public String getDriver() { 370 return getSchemaImpl().getDriver(); 371 } 372 373 376 public void setDriver(String driver) { 377 getSchemaImpl().setDriver(driver); 378 } 379 380 383 public String getDatabaseProductName() { 384 return getSchemaImpl().getDatabaseProductName(); 385 } 386 387 390 public void setDatabaseProductName(String databaseProductName) throws DBException { 391 getSchemaImpl().setDatabaseProductName(databaseProductName); 392 } 393 394 397 public String getDatabaseProductVersion() { 398 return getSchemaImpl().getDatabaseProductVersion(); 399 } 400 401 404 public void setDatabaseProductVersion(String databaseProductVersion) throws DBException { 405 getSchemaImpl().setDatabaseProductVersion(databaseProductVersion); 406 } 407 408 411 public String getDriverName() { 412 return getSchemaImpl().getDriverName(); 413 } 414 415 418 public void setDriverName(String driverName) throws DBException { 419 getSchemaImpl().setDriverName(driverName); 420 } 421 422 425 public String getDriverVersion() { 426 return getSchemaImpl().getDriverVersion(); 427 } 428 429 432 public void setDriverVersion(String driverVersion) throws DBException { 433 getSchemaImpl().setDriverVersion(driverVersion); 434 } 435 436 439 public void save(String filename) { 440 setVersionNo(CURRENT_VERSION_NO); 441 442 try { 443 OutputStream s = new FileOutputStream(filename); 444 ObjectOutput o = new XMLOutputStream(s); 445 o.writeObject(this); 446 o.close(); 447 } 448 catch (Exception e) { 449 e.printStackTrace(); 450 } 451 } 452 453 456 public void save(OutputStream s) { 457 setVersionNo(CURRENT_VERSION_NO); 458 459 try { 460 ObjectOutput o = new XMLOutputStream(s); 461 o.writeObject(this); 462 o.close(); 463 } 464 catch (Exception e) { 465 e.printStackTrace(); 466 } 467 } 468 469 472 public static interface Impl extends DBElement.Impl { 473 478 public int getStatus(); 479 480 484 public void setSchema(DBIdentifier id) throws DBException; 485 486 490 public DBIdentifier getSchema(); 491 492 496 public void setCatalog(DBIdentifier id) throws DBException; 497 498 502 public DBIdentifier getCatalog(); 503 504 509 public void changeTables(TableElement[] elems, int action) throws DBException; 510 511 514 public TableElement[] getTables(); 515 516 520 public TableElement getTable(DBIdentifier name); 521 522 525 public String getUrl(); 526 527 530 public void setUrl(String url) throws DBException; 531 532 535 public String getUsername(); 536 537 540 public void setUsername(String username) throws DBException; 541 542 545 public String getDriver(); 546 547 550 public void setDriver(String driver); 551 552 555 public String getDatabaseProductName(); 556 557 560 public void setDatabaseProductName(String databaseProductName) throws DBException; 561 562 565 public String getDatabaseProductVersion(); 566 567 570 public void setDatabaseProductVersion(String databaseProductVersion) throws DBException; 571 572 575 public String getDriverName(); 576 577 580 public void setDriverName(String driverName) throws DBException; 581 582 585 public String getDriverVersion(); 586 587 590 public void setDriverVersion(String driverVersion) throws DBException; 591 } 592 593 595 static final class Memory extends DBElement.Memory implements Impl { 596 597 private DBMemoryCollection.Table tables; 598 599 private DBIdentifier _catalog; 600 private DBIdentifier _schema; 601 private int _status; 602 private String _url; 603 private String _username; 604 private String _driver; 605 private String _databaseProductName; 606 private String _databaseProductVersion; 607 private String _driverName; 608 private String _driverVersion; 609 610 612 public Memory() { 613 super(); 614 } 615 616 619 public Memory(SchemaElement el) { 620 super(el); 621 _catalog = el.getCatalog(); 622 _schema = el.getSchema(); 623 _status = el.getStatus(); 624 _url = el.getUrl(); 625 _username = el.getUsername(); 626 _driver = el.getDriver(); 627 _databaseProductName = el.getDatabaseProductName(); 628 _databaseProductVersion = el.getDatabaseProductVersion(); 629 _driverName = el.getDriverName(); 630 _driverVersion = el.getDriverVersion(); 631 } 632 633 635 public void copyFrom(SchemaElement copyFrom) throws DBException { 636 changeTables(copyFrom.getTables(), SET); 637 } 638 639 643 public synchronized void changeTables(TableElement[] elems, int action) throws DBException { 644 initTables(); 645 tables.change(elems, action); 646 } 647 648 651 public synchronized TableElement[] getTables() { 652 initTables(); 653 return (TableElement[]) tables.getElements(); 654 } 655 656 660 public synchronized TableElement getTable(DBIdentifier name) { 661 initTables(); 662 return (TableElement) tables.getElement(name); 663 } 664 665 667 void initTables() { 668 if (tables == null) 669 tables = new DBMemoryCollection.Table(this); 670 } 671 672 675 final SchemaElement getSchemaElement() { 676 return (SchemaElement) _element; 677 } 678 679 684 public int getStatus() { 685 return _status; 686 } 687 688 public void setSchema(DBIdentifier id) throws DBException { 689 DBIdentifier old = _schema; 690 691 _schema = id; 692 firePropertyChange (PROP_SCHEMA, old, id); 693 } 694 695 public DBIdentifier getSchema() { 696 if (_schema == null) _schema = DBIdentifier.create(""); 699 return _schema; 700 } 701 702 public void setCatalog(DBIdentifier id) throws DBException { 703 DBIdentifier old = _catalog; 704 705 _catalog = id; 706 firePropertyChange (PROP_CATALOG, old, id); 707 } 708 709 public DBIdentifier getCatalog() { 710 if (_catalog == null) _catalog = DBIdentifier.create(""); 713 return _catalog; 714 } 715 716 public String getUrl() { 717 return _url; 718 } 719 720 public void setUrl(String url) throws DBException{ 721 _url = url; 722 } 723 724 public String getUsername() { 725 return _username; 726 } 727 728 public void setUsername(String username) throws DBException{ 729 _username = username; 730 } 731 732 public String getDriver() { 733 return _driverName; 734 } 735 736 public void setDriver(String driver){ 737 _driver = driver; 738 } 739 740 public String getDatabaseProductName() { 741 return _databaseProductName; 742 } 743 744 public void setDatabaseProductName(String databaseProductName) throws DBException { 745 _databaseProductName = databaseProductName; 746 } 747 748 public String getDatabaseProductVersion() { 749 return _databaseProductVersion; 750 } 751 752 public void setDatabaseProductVersion(String databaseProductVersion) throws DBException{ 753 _databaseProductVersion = databaseProductVersion; 754 } 755 756 public String getDriverName() { 757 return _driverName; 758 } 759 760 public void setDriverName(String driverName) throws DBException { 761 _driverName = driverName; 762 } 763 764 public String getDriverVersion() { 765 return _driverVersion; 766 } 767 768 public void setDriverVersion(String driverVersion) throws DBException { 769 _driverVersion = driverVersion; 770 } 771 } 772 } 773 | Popular Tags |