1 19 24 25 package org.netbeans.modules.j2ee.sun.ide.sunresources.beans; 26 27 import java.io.BufferedOutputStream ; 28 import java.io.File ; 29 import java.io.IOException ; 30 import java.io.InputStream ; 31 import java.io.FileInputStream ; 32 import java.io.FileOutputStream ; 33 import java.util.ArrayList ; 34 import java.util.Arrays ; 35 import java.util.Enumeration ; 36 import java.util.HashMap ; 37 import java.util.HashSet ; 38 import java.util.Iterator ; 39 import java.util.List ; 40 import java.util.ResourceBundle ; 41 42 import javax.enterprise.deploy.spi.DeploymentManager ; 43 import org.netbeans.modules.j2ee.deployment.common.api.DatasourceAlreadyExistsException; 44 import org.netbeans.modules.j2ee.deployment.common.api.Datasource; 45 46 import org.openide.DialogDisplayer; 47 import org.openide.NotifyDescriptor; 48 import org.openide.ErrorManager; 49 import org.openide.util.RequestProcessor; 50 import org.netbeans.api.db.explorer.DatabaseConnection; 51 52 import org.netbeans.modules.j2ee.sun.api.ResourceConfiguratorInterface; 53 import org.netbeans.modules.j2ee.sun.dd.api.DDProvider; 54 import org.netbeans.modules.j2ee.sun.dd.api.serverresources.AdminObjectResource; 55 import org.netbeans.modules.j2ee.sun.dd.api.serverresources.ConnectorConnectionPool; 56 import org.netbeans.modules.j2ee.sun.dd.api.serverresources.ConnectorResource; 57 import org.netbeans.modules.j2ee.sun.dd.api.serverresources.Resources; 58 import org.netbeans.modules.j2ee.sun.dd.api.serverresources.JdbcResource; 59 import org.netbeans.modules.j2ee.sun.dd.api.serverresources.PropertyElement; 60 import org.netbeans.modules.j2ee.sun.dd.api.serverresources.JdbcConnectionPool; 61 import org.netbeans.modules.j2ee.sun.share.serverresources.SunDatasource; 62 import org.netbeans.modules.j2ee.sun.sunresources.beans.DatabaseUtils; 63 64 import org.netbeans.modules.j2ee.sun.sunresources.beans.Field; 65 import org.netbeans.modules.j2ee.sun.sunresources.beans.FieldGroup; 66 import org.netbeans.modules.j2ee.sun.sunresources.beans.FieldGroupHelper; 67 import org.netbeans.modules.j2ee.sun.sunresources.beans.FieldHelper; 68 import org.netbeans.modules.j2ee.sun.sunresources.beans.Wizard; 69 import org.netbeans.modules.j2ee.sun.sunresources.beans.WizardConstants; 70 import org.openide.filesystems.FileObject; 71 import org.openide.filesystems.FileUtil; 72 73 77 public class ResourceConfigurator implements ResourceConfiguratorInterface { 78 79 public static final String __JMSResource = "jms"; public static final String __JMSConnectionFactory = "jms_CF"; public static final String __JdbcConnectionPool = "connection-pool"; 83 public static final String __SunResourceExt = "sun-resource"; 85 private final static char BLANK = ' '; 86 private final static char DOT = '.'; 87 private final static char[] ILLEGAL_FILENAME_CHARS = {'/', '\\', ':', '*', '?', '"', '<', '>', '|', ',', '=', ';' }; 88 private final static char REPLACEMENT_CHAR = '_'; 90 private final static char DASH = '-'; 91 92 public static final String __ConnectionPool = "ConnectionPool"; private static final String DATAFILE = "org/netbeans/modules/j2ee/sun/sunresources/beans/CPWizard.xml"; 95 private boolean showMsg = false; 96 private DeploymentManager currentDM = null; 97 98 ResourceBundle bundle = ResourceBundle.getBundle("org.netbeans.modules.j2ee.sun.ide.sunresources.beans.Bundle"); 100 103 public ResourceConfigurator() { 104 } 105 106 public void setDeploymentManager(DeploymentManager dm) { 107 this.currentDM = dm; 108 } 109 110 119 public boolean isJMSResourceDefined(String jndiName, File dir) { 120 return resourceAlreadyDefined(jndiName, dir, __JMSResource); 121 } 122 123 131 public void createJMSResource(String jndiName, String msgDstnType, String msgDstnName, String ejbName, File dir) { 132 try { 133 Resources resources = DDProvider.getDefault().getResourcesGraph(); 134 AdminObjectResource aoresource = resources.newAdminObjectResource(); 135 aoresource.setJndiName(jndiName); 136 aoresource.setResType(msgDstnType); 137 aoresource.setResAdapter(WizardConstants.__JmsResAdapter); 138 aoresource.setEnabled("true"); aoresource.setDescription(""); PropertyElement prop = aoresource.newPropertyElement(); 141 prop.setName("Name"); prop.setValue(ejbName); 143 aoresource.addPropertyElement(prop); 144 resources.addAdminObjectResource(aoresource); 145 146 createFile(dir, jndiName, __JMSResource, resources); 147 148 resources = DDProvider.getDefault().getResourcesGraph(); 149 ConnectorResource connresource = resources.newConnectorResource(); 150 ConnectorConnectionPool connpoolresource = resources.newConnectorConnectionPool(); 151 152 String connectionFactoryJndiName= "jms/" + msgDstnName + "Factory"; connresource.setJndiName(connectionFactoryJndiName); 154 connresource.setDescription(""); 155 connresource.setEnabled("true"); 156 connresource.setPoolName(connectionFactoryJndiName); 157 158 connpoolresource.setName(connectionFactoryJndiName); 159 connpoolresource.setResourceAdapterName(WizardConstants.__JmsResAdapter); 160 161 if(msgDstnType.equals(WizardConstants.__QUEUE)) { 162 connpoolresource.setConnectionDefinitionName(WizardConstants.__QUEUE_CNTN_FACTORY); 163 } else { 164 if(msgDstnType.equals(WizardConstants.__TOPIC)) { 165 connpoolresource.setConnectionDefinitionName(WizardConstants.__TOPIC_CNTN_FACTORY); 166 } else { 167 assert false; } 169 } 170 resources.addConnectorResource(connresource); 171 resources.addConnectorConnectionPool(connpoolresource); 172 173 createFile(dir, jndiName, __JMSConnectionFactory, resources); 174 } catch(IOException ex) { 175 ErrorManager.getDefault().notify(ErrorManager.EXCEPTION, ex); 178 } 179 } 180 181 public void createJDBCDataSourceFromRef(String refName, String databaseInfo, File dir) { 182 215 } 216 217 public String createJDBCDataSourceForCmp(String beanName, String databaseInfo, File dir) { 218 259 return null; 260 } 261 262 private void createFile(File targetFolder, String beanName, String resourceType, Resources res) throws IOException { 264 if(resourceType.indexOf("/") != -1) { resourceType = resourceType.substring(0, resourceType.indexOf("/")) + "_" + resourceType.substring(resourceType.indexOf("/")+1, resourceType.length()); } 269 if(resourceType.indexOf("\\") != -1) { resourceType = resourceType.substring(0, resourceType.indexOf("\\")) + "_" + resourceType.substring(resourceType.indexOf("\\")+1, resourceType.length()); } 273 274 ensureFolderExists(targetFolder); 275 String filename = getFileName(beanName, resourceType); 276 File resourceFile = new File (targetFolder, filename); 277 278 if(!resourceFile.exists()) { 282 res.write(new BufferedOutputStream (new FileOutputStream (resourceFile))); 283 } 284 } 285 286 private boolean isLegalFilename(String filename) { 287 for(int i = 0; i < ILLEGAL_FILENAME_CHARS.length; i++) { 288 if(filename.indexOf(ILLEGAL_FILENAME_CHARS[i]) >= 0) { 289 return false; 290 } 291 } 292 293 return true; 294 } 295 296 private boolean isFriendlyFilename(String filename) { 297 if(filename.indexOf(BLANK) >= 0 || filename.indexOf(DOT) >= 0) { 298 return false; 299 } 300 301 return isLegalFilename(filename); 302 } 303 304 private String makeLegalFilename(String filename) { 305 for(int i = 0; i < ILLEGAL_FILENAME_CHARS.length; i++) { 306 filename = filename.replace(ILLEGAL_FILENAME_CHARS[i], REPLACEMENT_CHAR); 307 } 308 309 return filename; 310 } 311 312 private String makeShorterLegalFilename(String filename) { 313 if(filename.indexOf("://") != -1) { filename = filename.substring(0, filename.indexOf("://")) + "_" + filename.substring(filename.indexOf("://")+3, filename.length()); } 318 if(filename.indexOf("//") != -1) { filename = filename.substring(0, filename.indexOf("//")) + "_" + filename.substring(filename.indexOf("//")+2, filename.length()); } 322 filename = makeLegalFilename(filename); 323 324 return filename; 325 } 326 327 private void ensureFolderExists(File folder) throws IOException { 328 if(!folder.exists()) { 329 folder.mkdir(); 330 } 331 } 332 333 private String getFileName(String beanName, String resourceType) { 334 335 assert (beanName != null); 336 assert (beanName.length() != 0); 337 338 assert (resourceType != null); 339 assert (resourceType.length() != 0); 340 341 String fileName = resourceType; 342 343 if(!isFriendlyFilename(beanName)) { 344 beanName = makeLegalFilename(beanName); 345 } 346 347 if(!isFriendlyFilename(fileName)) { 348 fileName = makeLegalFilename(fileName); 349 } 350 351 fileName = fileName + DASH + beanName + DOT + __SunResourceExt; 352 return fileName; 353 } 354 355 private JdbcConnectionPool setDerbyProps(String vendorName, String url, JdbcConnectionPool jdbcConnectionPool){ 356 url = stripExtraDBInfo(url); 357 String workingUrl = url.substring(url.indexOf("//") + 2, url.length()); String hostName = getDerbyServerName(workingUrl); 359 PropertyElement servName = jdbcConnectionPool.newPropertyElement(); 360 servName.setName(WizardConstants.__ServerName); 361 servName.setValue(hostName); 362 363 String portNumber = getDerbyPortNo(workingUrl); 364 PropertyElement portno = jdbcConnectionPool.newPropertyElement(); 365 portno.setName(WizardConstants.__DerbyPortNumber); 366 portno.setValue(portNumber); 367 368 String databaseName = getDerbyDatabaseName(workingUrl); 369 PropertyElement dbName = jdbcConnectionPool.newPropertyElement(); 370 dbName.setName(WizardConstants.__DerbyDatabaseName); 371 dbName.setValue(databaseName); 372 373 String connectionAttr = getDerbyConnAttrs(workingUrl); 374 if(! connectionAttr.equals("")) { PropertyElement connAttr = jdbcConnectionPool.newPropertyElement(); 376 connAttr.setName(WizardConstants.__DerbyConnAttr); 377 connAttr.setValue(connectionAttr); 378 jdbcConnectionPool.addPropertyElement(connAttr); 379 } 380 jdbcConnectionPool.addPropertyElement(servName); 381 jdbcConnectionPool.addPropertyElement(portno); 382 jdbcConnectionPool.addPropertyElement(dbName); 383 return jdbcConnectionPool; 384 } 385 386 387 392 private JdbcConnectionPool setAdditionalProps(String vendorName, String url, JdbcConnectionPool jdbcConnectionPool){ 393 url = stripExtraDBInfo(url); 394 String workingUrl = url; 395 if(vendorName.equals("sybase2")){ int index = url.indexOf("Tds:"); if(index != -1){ 398 workingUrl = url.substring(index + 4, url.length()); } else { 400 return jdbcConnectionPool; 401 } 402 }else { 403 workingUrl = url.substring(url.indexOf("//") + 2, url.length()); } 405 String hostName = getUrlServerName(workingUrl); 406 PropertyElement servName = jdbcConnectionPool.newPropertyElement(); 407 servName.setName(WizardConstants.__ServerName); 408 servName.setValue(hostName); 409 410 String portNumber = getUrlPortNo(workingUrl); 411 PropertyElement portno = jdbcConnectionPool.newPropertyElement(); 412 portno.setName(WizardConstants.__PortNumber); 413 portno.setValue(portNumber); 414 415 if(Arrays.asList(WizardConstants.VendorsDBNameProp).contains(vendorName)) { PropertyElement dbName = jdbcConnectionPool.newPropertyElement(); 417 String databaseName = ""; 418 if(vendorName.equals("sun_oracle") || vendorName.equals("datadirect_oracle")) { databaseName = getUrlSIDName(workingUrl); 420 dbName.setName(WizardConstants.__SID); 421 }else{ 422 databaseName = getUrlDatabaseName(workingUrl); 423 dbName.setName(WizardConstants.__DatabaseName); 424 if(databaseName.equals("")) { databaseName = getUrlDbName(workingUrl); 426 } 427 } 428 dbName.setValue(databaseName); 429 jdbcConnectionPool.addPropertyElement(dbName); 430 } 431 jdbcConnectionPool.addPropertyElement(servName); 432 jdbcConnectionPool.addPropertyElement(portno); 433 return jdbcConnectionPool; 434 } 435 436 private JdbcConnectionPool setDBProp(String vendorName, String url, JdbcConnectionPool jdbcConnectionPool){ 437 url = stripExtraDBInfo(url); 438 String workingUrl = url.substring(url.indexOf("//") + 2, url.length()); String databaseName = getUrlDatabaseName(workingUrl); 440 PropertyElement dbName = jdbcConnectionPool.newPropertyElement(); 441 dbName.setName(WizardConstants.__DerbyDatabaseName); 442 dbName.setValue(databaseName); 443 444 jdbcConnectionPool.addPropertyElement(dbName); 445 return jdbcConnectionPool; 446 } 447 448 private String getDatasourceClassName(String vendorName, boolean isXA, Wizard wizard) { 449 if(vendorName == null) { 450 return null; 451 } 452 453 try { 454 FieldGroup generalGroup = FieldGroupHelper.getFieldGroup(wizard, WizardConstants.__General); 455 456 Field dsField = null; 457 if (isXA) { 458 dsField = FieldHelper.getField(generalGroup, WizardConstants.__XADatasourceClassname); 459 } else { 460 dsField = FieldHelper.getField(generalGroup, WizardConstants.__DatasourceClassname); 461 } 462 return FieldHelper.getConditionalFieldValue(dsField, vendorName); 463 } catch(Exception ex) { 464 467 } 470 471 return null; 472 } 473 474 475 public static String getDatabaseVendorName(String url, Wizard wizard) { 476 String vendorName = ""; 477 try { 478 if(wizard == null) { 479 wizard = getWizardInfo(); 480 } 481 FieldGroup propGroup = FieldGroupHelper.getFieldGroup(wizard, WizardConstants.__PropertiesURL); 482 Field urlField = FieldHelper.getField(propGroup, "vendorUrls"); vendorName = FieldHelper.getOptionNameFromValue(urlField, url); 484 } catch(Exception ex) { 485 488 } 491 492 return vendorName; 493 } 494 495 private String convertToValidName(String database) { 496 database = stripExtraDBInfo(database); 497 String vendorName = getDatabaseVendorName(database, null); 498 if(vendorName != null) { 499 if(!vendorName.equals("")) { if(!isFriendlyFilename(vendorName)) { 501 vendorName = makeLegalFilename(vendorName); 502 } 503 this.showMsg = false; 504 } else { 505 this.showMsg = true; 506 vendorName = makeShorterLegalFilename(database); 507 } 508 } 509 return vendorName; 510 } 511 512 private String getDatabaseName(String database) { 513 String result = null; 514 int index = database.lastIndexOf('/') + 1; 515 if(index > 0) { 516 result = database.substring(index); 517 } 518 return result; 519 } 520 521 private String getResourceType(boolean isXA) { 522 if(isXA) { 523 return "javax.sql.XADataSource"; } else { 525 return "javax.sql.DataSource"; } 527 } 528 529 private boolean resourceAlreadyDefined(String resName, File dir, String resType) { 530 boolean result = false; 531 if(dir != null && dir.exists()) { 532 String filename = getFileName(resName, resType); 533 File resourceFile = new File (dir, filename); 534 if(resourceFile.exists()) { 535 result = true; 536 } 537 } 538 539 return result; 540 } 541 542 private String isSameDatabaseConnection(File resourceFile, String databaseUrl, String username, String password) { 543 String poolJndiName = null; 544 try { 545 FileInputStream in = new FileInputStream (resourceFile); 546 Resources resources = DDProvider.getDefault().getResourcesGraph(in); 547 548 JdbcConnectionPool[] pools = (JdbcConnectionPool[])resources.getJdbcConnectionPool(); 550 if(pools.length != 0) { 551 JdbcConnectionPool connPool = pools[0]; 552 PropertyElement[] pl = (PropertyElement[])connPool.getPropertyElement(); 553 if(databaseUrl.startsWith("jdbc:derby:")){ databaseUrl = stripExtraDBInfo(databaseUrl); 555 String workingUrl = databaseUrl.substring(databaseUrl.indexOf("//") + 2, databaseUrl.length()); 556 String hostName = getDerbyServerName(workingUrl); 557 String portNumber = getDerbyPortNo(workingUrl); 558 String databaseName = getDerbyDatabaseName(workingUrl); 559 String hostProp = null; 560 String portProp = null; 561 String dbProp = null; 562 String dbUser = null; 563 String dbPwd = null; 564 for(int i=0; i<pl.length; i++) { 565 String prop = pl[i].getName(); 566 if(prop.equalsIgnoreCase(WizardConstants.__ServerName)) { 567 hostProp = pl[i].getValue(); 568 }else if(prop.equals(WizardConstants.__DerbyPortNumber)){ 569 portProp = pl[i].getValue(); 570 }else if(prop.equals(WizardConstants.__DerbyDatabaseName)){ 571 dbProp = pl[i].getValue(); 572 }else if(prop.equals(WizardConstants.__User)){ 573 dbUser = pl[i].getValue(); 574 }else if(prop.equals(WizardConstants.__Password)){ 575 dbPwd = pl[i].getValue(); 576 } 577 } 578 if(hostName.equals(hostProp) && portNumber.equals(portProp) && 579 databaseName.equals(dbProp)){ 580 if(dbUser != null && dbPwd != null && dbUser.equals(username) && dbPwd.equals(password)){ 581 poolJndiName = connPool.getName(); 582 } 583 } 584 }else{ 585 String hostName = ""; String portNumber = ""; String databaseName = ""; String sid = ""; String user = ""; String pwd = ""; for(int i=0; i<pl.length; i++) { 592 String prop = pl[i].getName(); 593 if(prop.equalsIgnoreCase(WizardConstants.__ServerName)) { 594 hostName = pl[i].getValue(); 595 }else if(prop.equals(WizardConstants.__PortNumber)){ 596 portNumber = pl[i].getValue(); 597 }else if(prop.equals(WizardConstants.__DatabaseName)){ 598 databaseName = pl[i].getValue(); 599 }else if(prop.equals(WizardConstants.__SID)){ 600 sid = pl[i].getValue(); 601 }else if(prop.equals(WizardConstants.__User)){ 602 user = pl[i].getValue(); 603 }else if(prop.equals(WizardConstants.__Password)){ 604 pwd = pl[i].getValue(); 605 } 606 } 607 String serverPort = hostName + ":" + portNumber; if((databaseUrl.indexOf(serverPort) != -1 ) && 609 ((databaseUrl.indexOf(databaseName) != -1) || (databaseUrl.indexOf(sid) != -1))){ 610 if((username != null && user.equals(username)) && (password != null && pwd.equals(password))){ 611 poolJndiName = connPool.getName(); 612 } 613 } 614 615 for(int i=0; i<pl.length; i++) { 616 String prop = pl[i].getName(); 617 if(prop.equals("URL") || prop.equals("databaseName")) { String urlValue = pl[i].getValue(); 619 if(urlValue.equals(databaseUrl)) { 620 if((username != null && user.equals(username)) && (password != null && pwd.equals(password))){ 621 poolJndiName = connPool.getName(); 622 break; 623 } 624 } 625 } 626 } } 628 } 629 in.close(); 630 } catch(IOException ex) { 631 } 633 return poolJndiName; 634 } 635 636 private String stripExtraDBInfo(String dbConnectionString) { 637 if(dbConnectionString.indexOf("[") != -1) { dbConnectionString = dbConnectionString.substring(0, dbConnectionString.indexOf("[")).trim(); } 640 return dbConnectionString; 641 } 642 643 public static void showInformation(final String msg) { 644 RequestProcessor.getDefault().post(new Runnable () { 646 public void run() { 647 NotifyDescriptor d = new NotifyDescriptor.Message(msg, NotifyDescriptor.WARNING_MESSAGE); 648 DialogDisplayer.getDefault().notify(d); 649 } 650 }); 651 } 652 653 private static Wizard getWizardInfo(){ 654 Wizard wizard = null; 655 try { 656 InputStream in = Wizard.class.getClassLoader().getResourceAsStream(DATAFILE); 657 wizard = Wizard.createGraph(in); 658 in.close(); 659 } catch(Exception ex) { 660 ErrorManager.getDefault().notify(ErrorManager.EXCEPTION, ex); 663 } 664 return wizard; 665 } 666 667 private String getDerbyServerName(String url){ 668 String hostName = ""; int index = url.indexOf(":"); if(index != -1) { 671 hostName = url.substring(0, index); 672 }else{ 673 index = url.indexOf("/"); if(index != -1){ 675 hostName = url.substring(0, index); 676 } 677 } 678 return hostName; 679 } 680 681 private String getDerbyPortNo(String url){ 682 String portNumber = "1527"; int index = url.indexOf(":"); if(index != -1) { 685 portNumber = url.substring(index + 1, url.indexOf("/")); } 687 return portNumber; 688 } 689 690 private String getDerbyDatabaseName(String url){ 691 String databaseName = ""; int index = url.indexOf("/"); if(index != -1){ 694 int colonIndex = url.indexOf(";"); if(colonIndex != -1) { 696 databaseName = url.substring(index + 1, colonIndex); 697 } else { 698 databaseName = url.substring(index + 1, url.length()); 699 } 700 } 701 return databaseName; 702 } 703 704 private String getDerbyConnAttrs(String url){ 705 String connAttr = ""; int colonIndex = url.indexOf(";"); if(colonIndex != -1) { 708 connAttr = url.substring(colonIndex, url.length()); 709 } 710 return connAttr; 711 } 712 713 private String getUrlServerName(String url){ 714 String hostName = ""; int index = url.indexOf(":"); if(index != -1) { 717 hostName = url.substring(0, index); 718 }else{ 719 index = url.indexOf("/"); if(index != -1) { 721 hostName = url.substring(0, index); 722 }else{ 723 index= url.indexOf(";"); if(index != -1) { 725 hostName = url.substring(0, index); 726 } 727 } 728 } 729 return hostName; 730 } 731 732 private String getUrlPortNo(String url){ 733 String portNumber = ""; int index = url.indexOf(":"); if(index != -1){ 736 int slashIndex = url.indexOf("/"); int colonIndex = url.indexOf(";"); if(slashIndex != -1) 739 portNumber = url.substring(index + 1, slashIndex); 740 else{ 741 if(colonIndex != -1) 742 portNumber = url.substring(index + 1, colonIndex); 743 else 744 portNumber = url.substring(index + 1, url.length()); 745 } 746 } 747 return portNumber; 748 } 749 750 755 private String getUrlDatabaseName(String url){ 756 String databaseName = ""; int dbIndex = url.indexOf(";databaseName="); if(dbIndex != -1){ 759 int eqIndex = url.indexOf("=", dbIndex); int lenIndex = url.indexOf(";", eqIndex); if(lenIndex != -1){ 762 databaseName = url.substring(eqIndex + 1, lenIndex); 763 }else{ 764 databaseName = url.substring(eqIndex + 1, url.length()); 765 } 766 } 767 return databaseName; 768 } 769 773 private String getUrlSIDName(String url){ 774 String databaseName = ""; int sidIndex = url.indexOf(";SID="); if(sidIndex != -1){ 777 int eqIndex = url.indexOf("=", sidIndex); databaseName = url.substring(eqIndex + 1, url.length()); 779 } 780 return databaseName; 781 } 782 789 private String getUrlDbName(String url){ 790 String databaseName = ""; int slashIndex = url.indexOf("/"); int clIndex = url.indexOf(";", slashIndex); int scIndex = url.indexOf(":", slashIndex); int qIndex = url.indexOf("?", slashIndex); if(slashIndex != -1){ 796 if(clIndex != -1) 797 databaseName = url.substring(slashIndex + 1, clIndex); 798 else if(scIndex != -1) 799 databaseName = url.substring(slashIndex + 1, scIndex); 800 else if(qIndex != -1) 801 databaseName = url.substring(slashIndex + 1, qIndex); 802 else 803 databaseName = url.substring(slashIndex + 1, url.length()); 804 805 } 806 return databaseName; 807 } 808 809 810 811 817 public HashSet getServerDataSources() { 818 return ResourceUtils.getServerDataSources(this.currentDM); 819 } 820 821 828 public HashSet getResources(File resourceDir) { 829 HashSet serverresources = getServerResourceFiles(resourceDir); 830 if (serverresources.size() == 0) { 831 return serverresources; 832 } 833 834 HashSet dsources = new HashSet (); 835 HashMap connPools = getConnectionPools(serverresources); 836 List dataSources = getJdbcResources(serverresources); 837 for(int i=0; i<dataSources.size(); i++){ 838 JdbcResource datasourceBean = (JdbcResource)dataSources.get(i); 839 String poolName = datasourceBean.getPoolName(); 840 try{ 841 JdbcConnectionPool connectionPoolBean =(JdbcConnectionPool)connPools.get(poolName); 842 String url = ""; 843 String username = ""; 844 String password = ""; 845 String driverClass = ""; 846 String serverName = ""; 847 String portNo = ""; 848 String dbName = ""; 849 String sid = ""; 850 if(connectionPoolBean != null){ 851 PropertyElement[] props = connectionPoolBean.getPropertyElement(); 852 driverClass = connectionPoolBean.getDatasourceClassname(); 853 HashMap properties = new HashMap (); 854 for (int j = 0; j < props.length; j++) { 855 Object val = props[j].getValue(); 856 String propValue = ""; 857 if(val != null) { 858 propValue = val.toString(); 859 } 860 String propName = props[j].getName(); 861 if(propName.equalsIgnoreCase(WizardConstants.__DatabaseName)){ 862 if(driverClass.indexOf("pointbase") != -1) { url = propValue; 864 } else if(driverClass.indexOf("derby") != -1) { dbName = propValue; 866 } else { 867 dbName = propValue; 868 } 869 }else if(propName.equalsIgnoreCase(WizardConstants.__User)) { 870 username = propValue; 871 }else if(propName.equalsIgnoreCase(WizardConstants.__Password)){ 872 password = propValue; 873 }else if(propName.equalsIgnoreCase(WizardConstants.__Url)){ 874 url = propValue; 875 }else if(propName.equalsIgnoreCase(WizardConstants.__ServerName)){ 876 serverName = propValue; 877 }else if(propName.equalsIgnoreCase(WizardConstants.__DerbyPortNumber)){ 878 portNo = propValue; 879 }else if(propName.equalsIgnoreCase(WizardConstants.__SID)){ 880 sid = propValue; 881 } 882 } 883 884 if(driverClass.indexOf("derby") != -1){ url = "jdbc:derby://"; 886 if(serverName != null){ 887 url = url + serverName; 888 if(portNo != null) { 889 url = url + ":" + portNo; } 891 url = url + "/" + dbName ; } 893 }else if(url.equals("")) { String urlPrefix = DatabaseUtils.getUrlPrefix(driverClass); 895 String vName = getDatabaseVendorName(urlPrefix, null); 896 if(serverName != null){ 897 if(vName.equals("sybase2")){ url = urlPrefix + serverName; 899 } else { 900 url = urlPrefix + "//" + serverName; } 902 if(portNo != null) { 903 url = url + ":" + portNo; } 905 } 906 if(vName.equals("sun_oracle") || vName.equals("datadirect_oracle")) { url = url + ";SID=" + sid; }else if(Arrays.asList(WizardConstants.Reqd_DBName).contains(vName)) { 909 url = url + ";databaseName=" + dbName; }else if(Arrays.asList(WizardConstants.VendorsDBNameProp).contains(vName)) { 911 url = url + "/" + dbName ; } 913 } 914 915 DatabaseConnection databaseConnection = ResourceUtils.getDatabaseConnection(url); 916 if(databaseConnection != null) { 917 driverClass = databaseConnection.getDriverClass(); 918 }else{ 919 String drivername = DatabaseUtils.getDriverName(url); 921 if(drivername != null) { 922 driverClass = drivername; 923 } 924 } 925 926 SunDatasource sunResource = new SunDatasource(datasourceBean.getJndiName(), url, username, password, driverClass); 927 sunResource.setResourceDir(resourceDir); 928 dsources.add(sunResource); 929 }else{ 930 HashMap poolValues = ResourceUtils.getConnPoolValues(resourceDir, poolName); 932 if(! poolValues.isEmpty()){ 933 username = (String )poolValues.get(WizardConstants.__User); 934 password = (String )poolValues.get(WizardConstants.__Password); 935 url = (String )poolValues.get(WizardConstants.__Url); 936 driverClass = (String )poolValues.get(WizardConstants.__DriverClassName); 937 if((url != null) && (! url.equals (""))) { SunDatasource sunResource = new SunDatasource (datasourceBean.getJndiName (), url, username, password, driverClass); 939 sunResource.setResourceDir (resourceDir); 940 dsources.add (sunResource); 941 } 942 } 943 } 944 }catch(Exception ex){ 945 ErrorManager.getDefault().log(ErrorManager.INFORMATIONAL, "Cannot construct SunDatasource for jdbc resource : " + datasourceBean.getJndiName() 947 + "with pool " + poolName); } 949 } 950 return dsources; 951 } 952 953 966 public Datasource createDataSource(String jndiName, String url, String username, String password, String driver, File dir) throws DatasourceAlreadyExistsException { 967 SunDatasource ds = null; 968 try { 969 if(isDataSourcePresent(jndiName, dir)){ 970 throw new DatasourceAlreadyExistsException(new SunDatasource(jndiName, url, username, password, driver)); 971 } 972 if(url != null){ 973 String vendorName = convertToValidName(url); 974 if(vendorName == null) { 975 vendorName = jndiName; 976 }else{ 977 if(vendorName.equals("derby_embedded")){ NotifyDescriptor d = new NotifyDescriptor.Message(bundle.getString("Err_UnSupportedDerby"), NotifyDescriptor.WARNING_MESSAGE); DialogDisplayer.getDefault().notify(d); 980 return null; 981 } 982 } 983 ensureFolderExists(dir); 984 String poolName = vendorName + WizardConstants.__ConnPoolSuffix; 986 HashMap poolMap = updatePoolName(jndiName, poolName, dir, url, username, password); 987 Object [] pools = poolMap.keySet().toArray(); 988 String newPoolName = (String )pools[0]; 989 Object resFile = poolMap.get(pools[0]); 990 if(resFile != null) { 991 if(resourceFileExists(jndiName, dir)) { 992 ds = null; 993 } else { 994 createJDBCResource(jndiName, newPoolName, dir); 995 ds = new SunDatasource(jndiName, url, username, password, driver); 996 } 997 } else { 998 createCPPoolResource(newPoolName, url, username, password, driver, dir); 999 createJDBCResource(jndiName, newPoolName, dir); 1000 ds = new SunDatasource(jndiName, url, username, password, driver); 1001 } 1002 } 1003 } catch(IOException ex) { 1004 ErrorManager.getDefault().notify(ErrorManager.EXCEPTION, ex); 1005 } 1006 return ds; 1007 } 1008 1009 private void createCPPoolResource(String name, String databaseUrl, String username, String password, String driver, File resourceDir) throws IOException { 1010 Resources resources = DDProvider.getDefault().getResourcesGraph(); 1011 1012 JdbcConnectionPool jdbcConnectionPool = resources.newJdbcConnectionPool(); 1013 jdbcConnectionPool.setName(name); 1014 jdbcConnectionPool.setResType(getResourceType(false)); 1015 1016 Wizard wizard = getWizardInfo(); 1020 String vendorName = getDatabaseVendorName(databaseUrl, wizard); 1021 String datasourceClassName = ""; if(!vendorName.equals("")) { datasourceClassName = getDatasourceClassName(vendorName, false, wizard); 1024 } 1025 1026 if(datasourceClassName.equals("")) { datasourceClassName = DatabaseUtils.getDSClassName(databaseUrl); 1028 if(datasourceClassName == null || datasourceClassName.equals("")) { datasourceClassName = driver; 1032 } 1033 } 1034 if(datasourceClassName != null) { 1035 jdbcConnectionPool.setDatasourceClassname(datasourceClassName); 1036 } 1037 PropertyElement user = jdbcConnectionPool.newPropertyElement(); 1038 user.setName(WizardConstants.__User); PropertyElement passElement = jdbcConnectionPool.newPropertyElement(); 1040 passElement.setName(WizardConstants.__Password); String dbUser = username; 1042 String dbPassword = password; 1043 if(vendorName.equals("derby_net")) { jdbcConnectionPool = setDerbyProps(vendorName, databaseUrl, jdbcConnectionPool); 1045 if(dbUser == null || dbUser.trim().length() == 0) { 1046 dbUser = "app"; } 1048 if(dbPassword == null || dbPassword.trim().length() == 0) { 1049 dbPassword = "app"; } 1051 }else { 1052 if(Arrays.asList(WizardConstants.VendorsExtraProps).contains(vendorName)) { 1053 jdbcConnectionPool = setAdditionalProps(vendorName, databaseUrl, jdbcConnectionPool); 1054 }else{ 1055 PropertyElement databaseOrUrl = jdbcConnectionPool.newPropertyElement(); 1056 if(vendorName.equals("pointbase")) { databaseOrUrl.setName(WizardConstants.__DatabaseName); } else { 1059 databaseOrUrl.setName(WizardConstants.__Url); } 1061 databaseOrUrl.setValue(databaseUrl); 1062 jdbcConnectionPool.addPropertyElement(databaseOrUrl); 1063 } 1064 } 1065 user.setValue(dbUser); 1066 jdbcConnectionPool.addPropertyElement(user); 1067 passElement.setValue(dbPassword); 1068 jdbcConnectionPool.addPropertyElement(passElement); 1069 resources.addJdbcConnectionPool(jdbcConnectionPool); 1070 1071 ResourceUtils.createFile(FileUtil.toFileObject(resourceDir), name, resources); 1072 try{ 1073 Thread.sleep(1000); 1074 }catch(Exception ex){} 1075 } 1076 1077 private void createJDBCResource(String jndiName, String poolName, File resourceDir) throws IOException { 1078 Resources resources = DDProvider.getDefault().getResourcesGraph(); 1079 JdbcResource jdbcResource = resources.newJdbcResource(); 1080 jdbcResource.setPoolName(poolName); 1081 jdbcResource.setJndiName(jndiName); 1082 resources.addJdbcResource(jdbcResource); 1083 ResourceUtils.createFile(FileUtil.toFileObject(resourceDir), jndiName, resources); 1084 } 1085 1086 private HashSet getServerResourceFiles(File resourceDir) { 1087 HashSet serverresources = new HashSet (); 1088 if(resourceDir.exists()){ 1089 FileObject resDir = FileUtil.toFileObject(resourceDir); 1090 Enumeration files = resDir.getChildren(true); 1091 while (files.hasMoreElements()) { 1092 FileObject file = (FileObject) files.nextElement(); 1093 if (!file.isFolder() && file.getNameExt().endsWith(".sun-resource") && file.canRead()) { serverresources.add(file); 1095 } 1096 } 1097 } 1098 return serverresources; 1099 } 1100 1101 private List getJdbcResources(HashSet serverresources) { 1102 List dataSources = new ArrayList (); 1103 for (Iterator it = serverresources.iterator(); it.hasNext();) { 1104 try { 1105 FileObject dsObj = (FileObject)it.next(); 1106 File dsFile = FileUtil.toFile(dsObj); 1107 if(! dsFile.isDirectory()){ 1108 FileInputStream in = new FileInputStream (dsFile); 1109 1110 Resources resources = DDProvider.getDefault().getResourcesGraph(in); 1111 1112 JdbcResource[] dSources = resources.getJdbcResource(); 1114 if(dSources.length != 0){ 1115 dataSources.add(dSources[0]); 1116 } 1117 } 1118 } catch (Exception ex) { 1119 ErrorManager.getDefault().notify(ex); 1120 } 1121 } 1122 return dataSources; 1123 } 1124 1125 private HashMap getConnectionPools(HashSet serverresources) { 1126 HashMap connPools = new HashMap (); 1127 for (Iterator it = serverresources.iterator(); it.hasNext();) { 1128 try { 1129 FileObject dsObj = (FileObject)it.next(); 1130 File dsFile = FileUtil.toFile(dsObj); 1131 if(! dsFile.isDirectory()){ 1132 FileInputStream in = new FileInputStream (dsFile); 1133 1134 Resources resources = DDProvider.getDefault().getResourcesGraph(in); 1135 1136 JdbcConnectionPool[] pools = resources.getJdbcConnectionPool(); 1138 if(pools.length != 0){ 1139 JdbcConnectionPool cp = pools[0]; 1140 connPools.put(cp.getName(), cp); 1141 } 1142 } 1143 } catch (Exception ex) { 1144 ErrorManager.getDefault().notify(ex); 1145 } 1146 } 1147 return connPools; 1148 } 1149 1150 private HashMap getPoolFiles(HashSet serverresources) { 1151 HashMap connPools = new HashMap (); 1152 for (Iterator it = serverresources.iterator(); it.hasNext();) { 1153 try { 1154 FileObject dsObj = (FileObject)it.next(); 1155 File dsFile = FileUtil.toFile(dsObj); 1156 1157 if(! dsFile.isDirectory()){ 1158 FileInputStream in = new FileInputStream (dsFile); 1159 1160 Resources resources = DDProvider.getDefault().getResourcesGraph(in); 1161 1162 JdbcConnectionPool[] pools = resources.getJdbcConnectionPool(); 1164 if(pools.length != 0){ 1165 connPools.put(dsObj.getName(), dsFile); 1166 } 1167 } 1168 } catch (Exception ex) { 1169 ErrorManager.getDefault().notify(ex); 1170 } 1171 } 1172 return connPools; 1173 } 1174 1175 private boolean isDataSourcePresent(String jndiName, File dir){ 1176 boolean exists = false; 1177 HashMap serverResources = getDataSourceMap(getResources(dir)); 1178 if(serverResources.containsKey(jndiName)) { 1179 exists = true; 1180 } 1181 return exists; 1182 } 1183 1184 private HashMap getDataSourceMap(HashSet resources){ 1185 HashMap dSources = new HashMap (); 1186 for (Iterator it = resources.iterator(); it.hasNext();) { 1187 SunDatasource ds = (SunDatasource)it.next(); 1188 dSources.put(ds.getJndiName(), ds); 1189 } 1190 return dSources; 1191 } 1192 1193 1203 private HashMap updatePoolName(String dsJndiName, String poolName, File dir, String url, String username, String password){ 1204 HashMap poolAndFile = new HashMap (); 1205 String cpName = poolName; 1206 HashSet resourceFiles = getServerResourceFiles(dir); 1207 HashMap poolFiles = getPoolFiles(resourceFiles); 1208 for(Iterator itr=poolFiles.values().iterator(); itr.hasNext();){ 1209 File resourceFile = (File )itr.next(); 1210 if(resourceFile != null && resourceFile.exists()) { 1211 String poolJndiName = isSameDatabaseConnection(resourceFile, url, username, password); 1212 if(poolJndiName != null){ 1213 cpName = poolJndiName; 1214 poolAndFile.put(cpName, resourceFile); 1215 break; 1216 } 1217 } 1218 } 1219 if(poolAndFile.size() == 0){ 1220 cpName = FileUtil.findFreeFileName(FileUtil.toFileObject(dir), poolName, __SunResourceExt); 1221 poolAndFile.put(cpName, null); 1222 } 1223 return poolAndFile; 1224 } 1225 1226 private File getResourceFile(String fileName, File dir){ 1227 File resourceFile = null; 1228 if(dir != null && dir.exists()) { 1229 String filename = fileName + DOT + __SunResourceExt; 1230 resourceFile = new File (dir, filename); 1231 } 1232 return resourceFile; 1233 } 1234 1235 private boolean resourceFileExists(String resName, File dir) { 1236 boolean result = false; 1237 if(dir != null && dir.exists()) { 1238 String filename = resName + DOT + __SunResourceExt; 1239 File resourceFile = new File (dir, filename); 1240 if(resourceFile.exists()) { 1241 result = true; 1242 } 1243 } 1244 return result; 1245 } 1246 1247} 1248 1249 | Popular Tags |