1 19 20 package org.netbeans.modules.db.explorer.infos; 21 22 import java.beans.PropertyChangeListener ; 23 import java.beans.PropertyChangeSupport ; 24 import java.lang.ref.WeakReference ; 25 import java.io.InputStream ; 26 import java.io.IOException ; 27 import java.sql.Connection ; 28 import java.text.MessageFormat ; 29 import java.util.*; 30 31 import org.openide.nodes.Node; 32 import org.openide.util.NbBundle; 33 import org.openide.util.actions.SystemAction; 34 35 import org.netbeans.lib.ddl.DatabaseSpecification; 36 import org.netbeans.lib.ddl.DatabaseSpecificationFactory; 37 import org.netbeans.lib.ddl.DBConnection; 38 import org.netbeans.lib.ddl.impl.DriverSpecification; 39 import org.netbeans.lib.ddl.util.PListReader; 40 import org.netbeans.api.db.explorer.DatabaseException; 41 import org.netbeans.modules.db.explorer.ConnectionList; 42 import org.netbeans.modules.db.explorer.DatabaseConnection; 43 import org.netbeans.modules.db.explorer.DatabaseDriver; 44 import org.netbeans.modules.db.explorer.DatabaseNodeChildren; 45 import org.netbeans.modules.db.explorer.DbMetaDataListenerSupport; 46 import org.netbeans.modules.db.explorer.actions.DatabaseAction; 47 import org.netbeans.modules.db.explorer.nodes.DatabaseNode; 48 import org.netbeans.modules.db.explorer.nodes.RootNode; 49 import org.openide.nodes.Children; 50 51 public class DatabaseNodeInfo extends Hashtable implements Node.Cookie { 52 public static final String SPECIFICATION_FACTORY = "specfactory"; public static final String SPECIFICATION = "spec"; public static final String DRIVER_SPECIFICATION = "drvspec"; public static final String DBPRODUCT = "dbproduct"; public static final String DBVERSION = "dbversion"; public static final String SUPPORTED_DBS = "suppdbs"; public static final String DRIVER = "driver"; public static final String DBDRIVER = "dbdriver"; public static final String DATABASE = "db"; public static final String URL = "url"; public static final String PREFIX = "prefix"; public static final String CONNECTION = "connection"; public static final String CODE = "code"; public static final String NODE = "node"; public static final String CLASS = "class"; public static final String INFOCLASS = "infoclass"; public static final String NAME = "name"; public static final String USER = "user"; public static final String SCHEMA = "schema"; public static final String PASSWORD = "password"; public static final String CHILDREN = "children"; public static final String ACTIONS = "actions"; public static final String ICONBASE = "iconbase"; public static final String PROPERTIES = "properties"; public static final String RESULTSET = "resultset"; public static final String REMEMBER_PWD = "rememberpwd"; public static final String WRITABLE = "writable"; public static final String DELETABLE = "deletable"; public static final String DESCRIPTION = "description"; public static final String READONLYDB = "readonlydatabase"; public static final String GROUPSUP = "groupbysupport"; public static final String OJOINSUP = "outerjoinsupport"; public static final String UNIONSUP = "unionsupport"; public static final String SYSTEM_ACTION = "system"; public static final String CHILDREN_ORDERING = "children_ordering"; public static final String READONLY = "readOnly"; public static final String PERM = "perm"; public static final String ADAPTOR = "adaptor"; public static final String ADAPTOR_CLASSNAME = "adaptorClass"; 92 private static Map gtab = null; 93 static final String gtabfile = "org/netbeans/modules/db/resources/explorer.plist"; 95 private boolean connected = false; 96 97 transient boolean passwordWasSet = false; 98 99 protected static ResourceBundle bundle() { 100 return NbBundle.getBundle("org.netbeans.modules.db.resources.Bundle"); 101 } 102 103 public static Map getGlobalNodeInfo() { 104 if (gtab == null) 105 gtab = readInfo(); 106 107 return gtab; 108 } 109 110 public static Map readInfo() { 111 Map data; 112 try { 113 ClassLoader cl = DatabaseNodeInfo.class.getClassLoader(); 114 InputStream stream = cl.getResourceAsStream(gtabfile); 115 if (stream == null) { 116 String message = MessageFormat.format(bundle().getString("EXC_UnableToOpenStream"), new String [] {gtabfile}); throw new Exception (message); 118 } 119 PListReader reader = new PListReader(stream); 120 data = reader.getData(); 121 stream.close(); 122 } catch (Exception e) { 123 e.printStackTrace(); 124 data = null; 125 } 126 127 return data; 128 } 129 130 public static Object getGlobalNodeInfo(String key) { 131 return getGlobalNodeInfo().get(key); 132 } 133 134 public static DatabaseNodeInfo createNodeInfo(DatabaseNodeInfo parent, String nodecode) throws DatabaseException { 135 DatabaseNodeInfo e_ni = null; 136 try { 137 String nodec = (String )((Map)DatabaseNodeInfo.getGlobalNodeInfo().get(nodecode)).get(INFOCLASS); 138 if (nodec != null) 139 e_ni = (DatabaseNodeInfo)Class.forName(nodec).newInstance(); 140 else { 141 String message = MessageFormat.format(bundle().getString("EXC_UnableToFindClassInfo"), new String [] {nodecode}); throw new Exception (message); 143 } 144 } catch (Exception exc) { 145 throw new DatabaseException(exc.getMessage()); 146 } 147 148 if (e_ni != null) 149 e_ni.setParentInfo(parent, nodecode); 150 else { 151 String message = MessageFormat.format(bundle().getString("EXC_UnableToCreateNodeInfo"), new String [] {nodecode}); throw new DatabaseException(message); 153 } 154 return e_ni; 155 } 156 157 public static DatabaseNodeInfo createNodeInfo(DatabaseNodeInfo parent, String nodecode, HashMap rset) throws DatabaseException { 158 int colidx = 1; 159 String key = null; 160 DatabaseNodeInfo nfo = createNodeInfo(parent, nodecode); 161 Vector rsnames = (Vector)nfo.get(DatabaseNodeInfo.RESULTSET); 162 Iterator rsnames_i = rsnames.iterator(); 163 Hashtable data = new Hashtable(); 164 while (rsnames_i.hasNext()) { 165 key = (String )rsnames_i.next(); 166 if (!key.equals("unused")) { Object value = rset.get(new Integer (colidx)); 168 if (value != null) data.put(key, value); 169 } 170 colidx++; 171 } 172 173 nfo.putAll(data); 174 nfo.put(nodecode, nfo.getName()); 175 if (parent != null && parent.isReadOnly()) nfo.setReadOnly(true); 176 return nfo; 177 } 178 179 180 private DatabaseNodeInfo parent = null; 181 182 183 WeakReference nodewr = null; 184 private PropertyChangeSupport pcs = null; 185 private Set connectionpcsKeys = null; 186 187 private PropertyChangeSupport driverpcs = null; 188 private Set driverpcsKeys = null; 189 190 static final long serialVersionUID =1176243907461868244L; 191 public DatabaseNodeInfo() 192 { 193 super(); 194 } 195 196 public DatabaseNodeInfo(DatabaseNodeInfo parent, String sname) 197 throws DatabaseException 198 { 199 DatabaseNodeInfo nfo = new DatabaseNodeInfo(); 200 nfo.setParentInfo(parent, sname); 201 } 202 203 public void setParentInfo(DatabaseNodeInfo parent, String sname) 204 throws DatabaseException 205 { 206 if (parent != null) { 207 putAll(parent); 208 this.parent = parent; 209 } 210 Map ltab = (Map)getGlobalNodeInfo(sname); 211 if (ltab != null) 212 putAll(ltab); 213 else { 214 String message = MessageFormat.format(bundle().getString("EXC_UnableToReadInfo"), new String [] {sname}); throw new DatabaseException(message); 216 } 217 put(CODE, sname); 218 if (parent != null && parent.isReadOnly()) setReadOnly(true); 219 } 220 221 public DatabaseNodeInfo getParent() 222 { 223 return parent; 224 } 225 226 229 public DatabaseNodeInfo getParent(String code) 230 { 231 DatabaseNodeInfo iinfo = this; 232 if (code != null) { 233 while (iinfo != null) { 234 String iicode = iinfo.getCode(); 235 if (iicode.equals(code)) return iinfo; 236 else iinfo = iinfo.getParent(); 237 } 238 } 239 240 return iinfo; 241 } 242 243 public boolean canAdd(Map propmap, String propname) 244 { 245 return true; 246 } 247 248 public boolean canWrite(Map propmap, String propname, boolean defa) 249 { 250 if (isReadOnly()) return false; 251 String wflag = (String )propmap.get(DatabaseNodeInfo.WRITABLE); 252 if (wflag != null) return wflag.toUpperCase().equals("YES"); return defa; 254 } 255 256 public DatabaseNode getNode() 257 { 258 if (nodewr != null) return (DatabaseNode)nodewr.get(); 259 return null; 260 } 261 262 public void setNode(DatabaseNode node) 263 { 264 nodewr = new WeakReference (node); 265 } 266 267 private PropertyChangeSupport getConnectionPCS() 268 { 269 if (pcs == null) 270 pcs = new PropertyChangeSupport (this); 271 272 return pcs; 273 } 274 275 276 private PropertyChangeSupport getDriverPCS() 277 { 278 if (driverpcs == null) 279 driverpcs = new PropertyChangeSupport (this); 280 281 return driverpcs; 282 } 283 284 285 private Set getDriverPCSKeys() 286 { 287 if (driverpcsKeys == null) { 288 driverpcsKeys = new HashSet(); 289 driverpcsKeys.add(NAME); 290 driverpcsKeys.add(URL); 291 driverpcsKeys.add(PREFIX); 292 driverpcsKeys.add(ADAPTOR_CLASSNAME); 293 } 294 295 return driverpcsKeys; 296 } 297 298 299 private Set getConnectionPCSKeys() 300 { 301 if (connectionpcsKeys == null) { 302 connectionpcsKeys = new HashSet(); 303 connectionpcsKeys.add(SCHEMA); 304 connectionpcsKeys.add(USER); 305 connectionpcsKeys.add(DATABASE); 306 307 } 308 309 return connectionpcsKeys; 310 } 311 312 public Object put(Object key, Object obj) 313 { 314 Object old = get(key); 315 316 if (key == null) 317 throw new NullPointerException (); 318 319 if (obj != null) 320 super.put(key, obj); 321 else 322 remove(key); 323 324 if(key.equals(DatabaseNodeInfo.PASSWORD)) 326 passwordWasSet = true; 327 328 if (getDriverPCSKeys().contains(key)){ 329 getDriverPCS().firePropertyChange((String )key, old, obj); 330 } 331 if (getConnectionPCSKeys().contains(key)) 332 getConnectionPCS().firePropertyChange((String )key, null, obj); 333 334 return old; 335 } 336 337 public void delete() throws IOException 338 { 339 } 340 341 public void refreshChildren() throws DatabaseException { 342 Vector charr = new Vector(); 344 put(DatabaseNodeInfo.CHILDREN, charr); 345 initChildren(charr); 346 347 try { 349 final Node[] subTreeNodes = new Node[charr.size()]; 350 351 final DatabaseNodeChildren children = (DatabaseNodeChildren) getNode().getChildren(); 353 354 for(int i = 0; i < charr.size(); i++) 356 subTreeNodes[i] = children.createNode((DatabaseNodeInfo) charr.elementAt(i)); 357 358 Children.MUTEX.postWriteRequest(new Runnable () { 359 public void run() { 360 children.remove(children.getNodes()); 362 363 children.add(subTreeNodes); 365 } 366 }); 367 368 fireRefresh(); 369 } catch (ClassCastException ex) { 370 } catch (Exception ex) { 372 org.openide.ErrorManager.getDefault().notify(org.openide.ErrorManager.INFORMATIONAL, ex); 373 } 374 } 375 376 protected void fireRefresh() { 377 boolean allTables = true; 378 379 if (!(this instanceof TableListNodeInfo)) { 380 allTables = false; 381 if (!(this instanceof TableNodeInfo)) { 382 return; 383 } 384 } 385 386 ConnectionNodeInfo cnnfo = (ConnectionNodeInfo)getParent(DatabaseNode.CONNECTION); 387 if (cnnfo == null) { 388 return; 389 } 390 391 DatabaseConnection dbconn = ConnectionList.getDefault().getConnection(cnnfo.getDatabaseConnection()); 392 if (dbconn != null) { 393 if (allTables) { 394 DbMetaDataListenerSupport.fireTablesChanged(dbconn.getDatabaseConnection()); 395 } else { 396 String tableName = (String )get(DatabaseNode.TABLE); 397 DbMetaDataListenerSupport.fireTableChanged(dbconn.getDatabaseConnection(), tableName); 398 } 399 } 400 } 401 402 403 public Object getProperty(String key) 404 { 405 return get(key); 406 } 407 408 409 public void setProperty(String key, Object obj) 410 { 411 put(key, obj); 412 } 413 414 415 public void addConnectionListener(PropertyChangeListener l) 416 { 417 getConnectionPCS().addPropertyChangeListener(l); 418 } 419 420 421 public void removeConnectionListener(PropertyChangeListener l) 422 { 423 pcs.removePropertyChangeListener(l); 424 } 425 426 public void addDriverListener(PropertyChangeListener l) 427 { 428 getDriverPCS().addPropertyChangeListener(l); 429 } 430 431 public void removeDriverListener(PropertyChangeListener l) 432 { 433 getDriverPCS().removePropertyChangeListener(l); 434 } 435 436 public DatabaseSpecificationFactory getSpecificationFactory() 437 { 438 return (DatabaseSpecificationFactory)get(SPECIFICATION_FACTORY); 439 } 440 441 public void setSpecificationFactory(DatabaseSpecificationFactory fac) 442 { 443 put(SPECIFICATION_FACTORY, fac); 444 put(SUPPORTED_DBS, fac.supportedDatabases()); 445 } 446 447 protected String getDatabaseAdaptorClassName() { 448 return "org.netbeans.lib.ddl.adaptors.DefaultAdaptor"; } 450 451 public DatabaseSpecification getSpecification() 452 { 453 DatabaseSpecification spec = (DatabaseSpecification)get(SPECIFICATION); 454 if (spec == null) return spec; 455 String adaname = getDatabaseAdaptorClassName(); 456 if (!spec.getMetaDataAdaptorClassName().equals(adaname)) { 457 spec.setMetaDataAdaptorClassName(adaname); 458 } 459 460 return spec; 461 } 462 463 public void setSpecification(DatabaseSpecification spec) 464 { 465 put(SPECIFICATION, spec); 466 } 467 468 public String getDriver() 469 { 470 return (String )get(DRIVER); 471 } 472 473 public void setDriver(String drv) 474 { 475 put(DRIVER, drv); 476 } 477 478 public Connection getConnection() 479 { 480 return (Connection )get(CONNECTION); 481 } 482 483 public void setConnection(Connection con) throws DatabaseException 484 { 485 Connection oldval = getConnection(); 486 if (con != null) { 487 if (oldval != null && oldval.equals(con)) return; 488 put(CONNECTION, con); 489 setConnected(true); 490 } else { 491 remove(CONNECTION); 492 setConnected(false); 493 } 494 495 497 if (con != null && isReadOnly()) { 498 Enumeration enu = getChildren().elements(); 499 while(enu.hasMoreElements()) { 500 DatabaseNodeInfo ninfo = (DatabaseNodeInfo)enu.nextElement(); 501 ninfo.setReadOnly(true); 502 } 503 } 504 505 getConnectionPCS().firePropertyChange(CONNECTION, oldval, con); 506 507 } 508 509 public void setConnected(boolean connected) { 510 this.connected = connected; 511 } 512 513 public boolean isConnected() { 514 return connected; 515 } 516 517 public DatabaseConnection getDatabaseConnection() 518 { 519 DatabaseConnection con = new DatabaseConnection(getDriver(), getDatabase(), getUser(), getPassword()); 520 if(get(REMEMBER_PWD)!=null) 521 con.setRememberPassword(((Boolean )get(REMEMBER_PWD)).booleanValue()); 522 else 523 con.setRememberPassword(Boolean.FALSE.booleanValue()); 524 con.setSchema(getSchema()); 525 con.setDriverName((String )get("drivername")); 526 return con; 527 } 528 529 public DatabaseDriver getDatabaseDriver() 530 { 531 return (DatabaseDriver)get(DBDRIVER); 532 } 533 534 public void setDatabaseConnection(DBConnection cinfo) 535 { 536 String pwd = cinfo.getPassword(); 537 put(DRIVER, cinfo.getDriver()); 538 put(DATABASE, cinfo.getDatabase()); 539 put(USER, cinfo.getUser()); 540 put(SCHEMA, cinfo.getSchema()); 541 if (pwd != null) 542 put(PASSWORD, pwd); 543 put(REMEMBER_PWD, (cinfo.rememberPassword() ? Boolean.TRUE : Boolean.FALSE)); 544 put("drivername", cinfo.getDriverName()); 545 } 546 547 public String getCode() 548 { 549 return (String )get(CODE); 550 } 551 552 public void setCode(String nam) 553 { 554 put(CODE, nam); 555 } 556 557 public String getName() 558 { 559 return (String )get(NAME); 560 } 561 562 public void setName(String nam) 563 { 564 put(NAME, nam); 565 } 566 567 public String getUser() 568 { 569 return (String )get(USER); 570 } 571 572 public void setUser(String usr) 573 { 574 put(USER, usr); 575 } 576 577 public String getSchema() { 578 return (String ) get(SCHEMA); 579 } 580 581 public void setSchema(String schema) { 582 put(SCHEMA, schema); 583 } 584 585 public String getDatabase() 586 { 587 return (String )get(DATABASE); 588 } 589 590 public void setDatabase(String db) 591 { 592 put(DATABASE, db); 593 } 594 595 public String getPassword() 596 { 597 return (String )get(PASSWORD); 598 } 599 600 public void setPassword(String pwd) 601 { 602 put(PASSWORD, pwd); 603 } 604 605 public String getTable() 606 { 607 return (String )get("table"); } 609 610 public String getView() 611 { 612 return (String )get("view"); } 614 615 public void setTable(String nam) 616 { 617 put("table", nam); } 619 620 public String getIconBase() { 621 return (String ) get("iconbase"); } 623 624 public void setIconBase(String base) { 625 put("iconbase", base); } 627 628 public String getDisplayname() 629 { 630 return (String )get("displayname"); } 632 633 public void setDisplayname(String name) 634 { 635 put("displayname", name); } 637 638 public String getURL() 639 { 640 return (String )get(URL); 641 } 642 643 public void setURL(String url) 644 { 645 put(URL, url); 646 } 647 648 651 public Properties getConnectionProperties() 652 { 653 Properties props = new Properties(); 654 try { 655 props.put("user", getUser()); props.put("password", getPassword()); props.put("schema", getSchema()); } catch (Exception e) { props = null; } 659 660 return props; 661 } 662 663 protected void initChildren(Vector children) 664 throws DatabaseException 665 { 666 } 667 668 public Vector getChildren() 669 throws DatabaseException 670 { 671 Vector children = (Vector)get(CHILDREN); 672 if (children.size() > 0 && children.elementAt(0) instanceof DatabaseNodeInfo) return children; 673 674 Vector chalt = new Vector(); 675 initChildren(chalt); 676 chalt.addAll(children); 677 678 try { 679 for (int i=0; i<chalt.size();i++) { 680 Object e_child = chalt.elementAt(i); 681 if (e_child instanceof String ) { 682 DatabaseNodeInfo e_ni = createNodeInfo(this, (String )e_child); 683 chalt.setElementAt(e_ni,i); 684 } 685 } 686 687 children = chalt; 688 put(CHILDREN, children); 689 690 } catch (Exception exc) { 691 String message = MessageFormat.format(bundle().getString("EXC_UnableToCreateChildren"), new String [] {exc.getMessage()}); throw new DatabaseException(message); 693 } 694 695 return children; 696 } 697 698 public void setChildren(Vector chvec) 699 { 700 put(CHILDREN, chvec); 701 } 702 703 public Vector getActions() 704 { 705 Vector actions = (Vector)get(ACTIONS); 706 actions = (Vector)actions.clone(); 707 if (actions == null) { 708 actions = new Vector(); 709 put(ACTIONS, actions); 710 } 711 712 if (actions.size() == 0) return actions; 713 boolean ro = isReadOnly(); 714 for (int i=0; i<actions.size();i++) { 715 716 Object e_act = actions.elementAt(i); 717 SystemAction action = null; 718 if (e_act instanceof Map) { 719 Map e_action = (Map)e_act; 720 try { 721 722 724 String perm = (String )e_action.get(PERM); 725 if (ro && perm != null && perm instanceof String && perm.indexOf("write") != -1) { actions.setElementAt(null, i); 727 continue; 728 } 729 730 boolean systemact = false; 731 String sysactstr = (String )e_action.get(SYSTEM_ACTION); 732 if (sysactstr != null) systemact = sysactstr.toUpperCase().equals("YES"); String actnode = (String )e_action.get(NODE); 734 String actcn = (String )e_action.get(CLASS); 735 736 if (!systemact) { 737 String locname, xname = (String )e_action.get(NAME); 738 try { 739 locname = bundle().getString(xname); 740 } catch (MissingResourceException e) { 741 locname = xname; 742 743 String message = MessageFormat.format(bundle().getString("ERR_UnableToLocateLocalizedMenuItem"), new String [] {xname}); System.out.println(message); 745 } 746 747 action = SystemAction.get(Class.forName(actcn)); 750 ((DatabaseAction)action).setName(locname); 751 ((DatabaseAction)action).setNode(actnode); 752 } else { 753 ClassLoader l = (ClassLoader )org.openide.util.Lookup.getDefault().lookup(ClassLoader .class); 754 if (l == null) { 755 l = getClass().getClassLoader(); 756 } 757 action = SystemAction.get(Class.forName(actcn, true, l)); 758 } 759 760 } catch (Exception e) { 761 e.printStackTrace(); 762 } 763 } 764 765 actions.setElementAt(action, i); 766 } 767 768 return actions; 769 } 770 771 public String toString() 772 { 773 String result = ""; Enumeration keys = keys(); 775 while (keys.hasMoreElements()) { 776 Object key = keys.nextElement(); 777 result = result + key+": "+get(key)+ "\n"; } 779 780 return result; 781 } 782 783 public boolean isDebugMode() 784 { 785 return RootNode.getOption().getDebugMode(); 786 } 787 788 public void setDebugMode(boolean mode) 789 { 790 RootNode.getOption().setDebugMode(mode); 791 } 792 793 public boolean isReadOnly() 794 { 795 Boolean roobj = (Boolean )get(READONLY); 796 if (roobj != null) return roobj.booleanValue(); 797 return false; 798 } 799 800 public void setReadOnly(boolean flag) 801 { 802 put(READONLY, flag ? Boolean.TRUE : Boolean.FALSE); 803 } 804 805 808 public DriverSpecification getDriverSpecification() { 809 return (DriverSpecification) get(DRIVER_SPECIFICATION); 810 } 811 812 815 public void setDriverSpecification(DriverSpecification driverSpecification) { 816 put(DRIVER_SPECIFICATION, driverSpecification); 817 } 818 819 public boolean isPasswordSet() { 820 return passwordWasSet; 821 } 822 823 } 824 | Popular Tags |