1 23 24 31 package org.enhydra.dods; 32 33 import java.io.File ; 34 import java.io.InputStream ; 35 import java.net.URL ; 36 import java.net.URLClassLoader ; 37 import java.io.FileInputStream ; 38 import java.io.FilenameFilter ; 39 import java.io.IOException ; 40 import java.util.HashMap ; 41 import java.util.HashSet ; 42 import java.util.Iterator ; 43 import java.util.Map ; 44 import java.util.Properties ; 45 import java.util.Set ; 46 import java.util.Vector ; 47 48 import org.enhydra.dods.wizard.DirectoryNameFilter; 49 import org.enhydra.xml.XMLConfig; 50 import org.enhydra.xml.XMLDocumentFactory; 51 import com.lutris.logging.Logger; 52 import com.lutris.util.ConfigException; 53 import javax.xml.parsers.DocumentBuilderFactory ; 54 import javax.xml.parsers.DocumentBuilder ; 55 import org.w3c.dom.Document ; 56 import org.w3c.dom.Element ; 57 import org.w3c.dom.NodeList ; 58 import org.w3c.dom.Node ; 59 60 public class Common { 61 62 66 67 static public final String VENDOR_ORDERED_RESULT_SET = "OrderedResultSet"; 68 69 70 73 static protected boolean configured = false; 74 75 78 static protected String configDir = null; 79 80 81 84 static public final String DODS_CONF_JAR_PATH = "org/enhydra/dods/conf"; 85 86 89 90 static public final String DATABASE_MANAGER_CONF_FILE="dods/conf/databaseManager.conf"; 91 92 93 94 97 static protected Properties dods_conf; 98 99 102 static protected String dbase; 103 104 107 static protected XMLConfig dodsConf; 108 109 112 static protected HashMap dodsVendorConfs; 113 114 115 119 static protected HashMap dodsDriversMap; 120 121 125 static protected Vector dodsDriversNamesVec; 126 static protected Vector dodsVendorsNamesVec; 127 static protected Vector dodsDriverClassVec; 128 129 130 131 132 135 static protected String projRoot; 136 137 140 static protected String domlfile; 141 142 145 static protected String customTemplateDir = null; 146 147 148 152 static protected HashMap changeAutocommit = new HashMap (); 153 154 157 static protected String templateSet; 158 159 160 165 public static void setChangeAutocommit(String dbName, boolean value){ 166 Boolean newValue= new Boolean (value); 167 changeAutocommit.put(dbName, newValue); 168 } 169 170 171 176 public static boolean isChangeAutocommitEnabled(String dbName){ 177 Boolean res = (Boolean )changeAutocommit.get(dbName); 178 if(res==null){ 179 return true; 180 }else{ 181 return res.booleanValue(); 182 } 183 } 184 185 186 static private void init() { 187 if (!configured) { 188 if (configDir != null) { 189 init(configDir); 190 } else { 191 init(getDefaultConfigDir()); 192 } 193 } 194 195 } 196 197 static private void init(String confDir) { 198 setConfigDir(confDir); 199 String dodsFile = confDir + File.separator + "dodsConf.xml"; 200 dodsFile.replace('\\', '/'); 201 202 Document doc = null; 203 if (confDir != null) { 204 doc = XMLDocumentFactory.parse(dodsFile); 205 }else{ 206 try { 207 Class comClass = Class.forName("org.enhydra.dods.Common"); 208 InputStream inStream =comClass.getClassLoader().getResourceAsStream(DODS_CONF_JAR_PATH+"/"+"dodsConf.xml"); 209 DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); 210 DocumentBuilder builder = factory.newDocumentBuilder(); 211 doc = builder.parse(inStream); 212 } catch (Exception e) { 213 e.printStackTrace(); 214 } 215 } 216 217 dodsConf = XMLConfig.newXMLConfigInstance(doc); 218 dodsVendorConfs = new HashMap (); 219 dodsDriversMap = new HashMap (); 220 dodsDriversNamesVec = new Vector (); 221 dodsVendorsNamesVec = new Vector (); 222 dodsDriverClassVec = new Vector (); 223 XMLConfig vendor = null; 224 String vendorName = null; 225 String vendorFile = null; 226 227 228 NodeList vendors = dodsConf.getSubElementsByTagName("Database/Vendor"); 229 230 for (int i = 0; i < vendors.getLength(); i++) { 231 vendor = (XMLConfig) vendors.item(i); 232 vendorName = vendor.getAttribute("name"); 233 vendorFile = confDir + File.separator + vendor.getText(); 234 235 if (confDir != null) { 236 doc = XMLDocumentFactory.parse(vendorFile); 237 }else{ 238 try { 239 Class comClass = Class.forName("org.enhydra.dods.Common"); 240 InputStream inStream =comClass.getClassLoader().getResourceAsStream(DODS_CONF_JAR_PATH+"/"+vendor.getText()); 241 DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); 242 DocumentBuilder builder = factory.newDocumentBuilder(); 243 doc = builder.parse(inStream); 244 } catch (Exception e) { 245 e.printStackTrace(); 246 } 247 } 248 249 XMLConfig dodsVendorConf = XMLConfig.newXMLConfigInstance(doc); 250 dodsVendorConfs.put(vendorName, dodsVendorConf); 251 } 252 253 254 XMLConfig driverN; 255 XMLConfig driver; 256 NodeList driverClassesNl; 257 String driverVendorName; 258 String driverClassName; 259 String driverMetadataName; 260 NodeList driverVendorsNl = dodsConf.getSubElementsByTagName("Drivers/DatabaseVendor"); 261 for (int i = 0; i < driverVendorsNl.getLength(); i++) { 262 driverN = (XMLConfig)driverVendorsNl.item(i); 263 driverVendorName = driverN.getAttribute("name"); 264 driverClassesNl = driverN.getChildrenByTagName("Driver"); 265 for (int j = 0; j < driverClassesNl.getLength(); j++) { 266 driverClassName = null; 267 driverMetadataName = null; 268 driver = (XMLConfig)driverClassesNl.item(j); 269 driverClassName = driver.getAttribute("class"); 270 driverMetadataName = driver.getAttribute("name"); 271 if (driverClassName !=null && driverClassName!=""){ 272 dodsDriversMap.put(driverClassName,driverVendorName); 273 } 274 if (driverMetadataName!=null && driverMetadataName!=""){ 275 dodsDriverClassVec.add(driverClassName); 276 dodsDriversNamesVec.add(driverMetadataName); 277 dodsVendorsNamesVec.add(driverVendorName); 278 } 279 } 280 } 281 configured = true; 282 } 283 284 290 static public String getDatabaseVendor(String driverClassName) { 291 String res=null; 292 init(); 293 if(driverClassName!=null && dodsDriversMap.containsKey(driverClassName)){ 294 res = (String )dodsDriversMap.get(driverClassName); 295 }else{ 296 System.out.println("Unknown JDBC Driver Vendor for : "+driverClassName+" using default (Standard) insted"); 297 res = "Standard"; 298 } 299 return res; 300 } 301 302 308 static public String getDatabaseVendorFromDriverName(String driverMetadataName) 309 throws ConfigException { 310 init(); 311 int vendorIndex=-1; 312 for(int i=0;i<dodsDriversNamesVec.size();i++){ 313 if (driverMetadataName.indexOf((String )dodsDriversNamesVec.elementAt(i))!=-1){ 314 String res =(String )dodsVendorsNamesVec.elementAt(i); 315 return (String )dodsVendorsNamesVec.elementAt(i); 316 } 317 } 318 throw new ConfigException("Unknown JDBC Driver Vendor for : "+driverMetadataName); 319 } 320 321 327 static public String getDatabaseDriverClassFromDriverName(String driverMetadataName) 328 throws ConfigException { 329 init(); 330 int vendorIndex=-1; 331 for(int i=0;i<dodsDriversNamesVec.size();i++){ 332 if (driverMetadataName.indexOf((String )dodsDriversNamesVec.elementAt(i))!=-1){ 333 String res =(String )dodsVendorsNamesVec.elementAt(i); 334 return (String )dodsDriverClassVec.elementAt(i); 335 } 336 } 337 throw new ConfigException("Unknown JDBC Driver for : "+driverMetadataName); 338 } 339 340 341 346 static public XMLConfig getDodsConf() { 347 init(); 348 return dodsConf; 349 } 350 351 359 static public String getDodsConfProperty(String key, String database) { 360 init(); 361 XMLConfig vendor = (XMLConfig) dodsVendorConfs.get(database); 362 363 if (vendor != null) { 364 return vendor.getText(key); 365 } 366 return null; 367 } 368 369 372 static public void showDodsConf() { 373 init(); 374 StringBuffer ret = new StringBuffer (); 375 Iterator iter = dods_conf.entrySet().iterator(); 376 377 while (iter.hasNext()) { 378 Map.Entry elm = (Map.Entry ) iter.next(); 379 380 ret.append(elm.getKey()).append("=").append(elm.getValue()).append("\n"); 381 } 382 } 383 384 389 static public Set getDodsConfVendorNames() { 390 init(); 391 return dodsVendorConfs.keySet(); 392 } 393 394 399 public static String getDodsEjenPropertyFilename() { 400 final String SYS_USER_HOME = "user.home"; 401 final String DIR_ENHYDRA = ".enhydra"; 402 final String PROPERTY_FILENAME = "dods-ejen.properties"; 403 StringBuffer buf = new StringBuffer (); 404 405 buf.append(System.getProperties().getProperty(SYS_USER_HOME)); 406 buf.append(File.separator); 407 buf.append(DIR_ENHYDRA); 408 buf.append(File.separator); 409 buf.append(PROPERTY_FILENAME); 410 return buf.toString(); 411 } 412 413 417 public static String getDomlFile() { 418 init(); 419 String strDoml = null; 420 421 strDoml = System.getProperty("DOML_FILE"); 422 String domlFile = null; 423 424 if (strDoml == null) { 425 File current = new File ("."); 426 String [] files = current.list(); 427 428 out: 429 for (int i = 0; i < files.length; i++) { 430 if (files[i].toLowerCase().endsWith(".doml")) { 431 domlFile = files[i]; 432 break out; 433 } else { 434 domlFile = null; 435 } 436 } 437 strDoml = domlFile; 438 } 439 return strDoml; 440 } 441 442 446 public static String getDomlFileName() { 447 init(); 448 String domlFileName = getDomlFile(); 449 String fileSep = System.getProperty("file.separator"); 450 451 if (System.getProperty("os.name").toLowerCase().startsWith("win")) { 452 if (fileSep != "/") { 453 domlFileName = domlFileName.replace('\\', '/'); 454 } 455 } 456 return domlFileName.substring(domlFileName.lastIndexOf("/") + 1); 457 } 458 459 463 public static String getFileName() { 464 init(); 465 String fileName = getDomlFileName(); 466 int index = fileName.lastIndexOf("."); 467 468 if (index != -1) { 469 fileName = fileName.substring(0, index); 470 } 471 return fileName; 472 } 473 474 478 public static String getDatabaseVendor() { 479 init(); 480 String database = System.getProperty("DATABASE_VENDOR"); 481 return database; 482 } 483 484 488 public static String getSplitSQLPrimary(){ 489 String ret="true"; 490 try{ 491 ret=Common.getDodsConfProperty("SplitSQLPrimary",getDatabaseVendor()); 492 493 }catch(Exception e){} 494 if (ret==null) return "true"; 495 else return ret; 496 497 } 498 499 500 501 505 public static void setDomlFile(String doml) { 506 domlfile = doml; 507 } 508 509 513 public static void setConfigDir(String confD) { 514 if (confD != null) { 515 File tempFile = new File (confD + File.separator + "dodsConf.xml"); 516 if (!tempFile.isFile()) { 517 System.out.println("File dodsConf.xml not exists on path: '" 518 + confD + "' using default insted"); 519 confD = getDefaultConfigDir(); 520 } 521 configDir = confD; 522 } else { 523 configDir = getDefaultConfigDir(); 525 } 526 configured = false; 527 528 } 529 530 534 public static String getDefaultConfigDir() { 535 String dodsHome = System.getProperty("DODS_HOME"); 536 if (dodsHome == null) { 537 dodsHome = System.getProperty("enhydra.home"); 538 if (dodsHome != null) { 539 dodsHome = dodsHome + File.separator + "dods"; 540 } else { 541 return null; 542 } 543 } 544 String resultStr = dodsHome + File.separator + "build" + File.separator 545 + "conf"; 546 return resultStr; 547 } 548 549 553 public static String getConfigDir() { 554 init(); 555 String resultStr = configDir; 556 557 if (configDir == null) { 558 resultStr = getDefaultConfigDir(); 559 } 560 return resultStr; 561 } 562 563 572 public static InputStream getConfFileFromURL(URL confURL, String confFile) 573 throws ConfigException { 574 URL [] cURL; 575 if (confURL!=null){ 576 cURL= new URL [1]; 577 cURL[0]=confURL; 578 }else{ 579 cURL= new URL [0]; 580 } 581 ClassLoader ucl = Common.class.getClassLoader(); 583 584 InputStream configIS = null; 585 if (confFile!=null){ 586 try{ 587 configIS = ucl.getResourceAsStream(confFile); 588 }catch(Exception e){} 589 } 590 if (configIS==null){ 591 try{ 592 configIS = ucl.getResourceAsStream(DATABASE_MANAGER_CONF_FILE); 593 }catch(Exception e){ 594 throw new ConfigException(e); 595 } 596 } 597 return configIS; 598 } 599 600 601 602 606 public static void setCustomTemplateDir(String dirString) { 607 customTemplateDir = dirString; 608 } 609 610 614 public static String getCustomTemplateDir() { 615 init(); 616 return customTemplateDir; 617 } 618 619 623 public static String getProjectRoot() { 624 String projectRoot = null; 625 626 projectRoot = System.getProperty("PROJECT_ROOT"); 627 if (projectRoot == null) { 628 projectRoot = "."; 629 } 630 return projectRoot; 631 } 632 633 637 public static void setProjectRoot(String projectRoot) { 638 projRoot = projectRoot; 639 } 640 641 645 public static String getTemplateSet() { 646 String tempSet = System.getProperty("TEMPLATESET"); 647 648 if (tempSet == null) { 649 tempSet = "standard"; 650 } 651 if (tempSet.equals("multidb")) { 652 tempSet = "standard"; 653 } 654 if (tempSet.equals("webdocwf")) { 655 tempSet = "webdocwf"; 656 } 657 if (tempSet.equals("multidb_webdocwf")) { 658 tempSet = "webdocwf"; 659 } 660 return tempSet; 661 } 662 663 667 public static void setTemplateSet(String set) { 668 templateSet = set; 669 } 670 671 675 public static String getExtensions() { 676 String tempSet = null; 677 678 tempSet = System.getProperty("TEMPLATESET"); 679 if (tempSet == null) { 680 tempSet = "standard"; 681 } 682 return tempSet; 683 } 684 685 689 public static String getForce() { 690 String force = System.getProperty("FORCE"); 691 692 return force; 693 } 694 695 699 public static String getInstallProperties() { 700 String dodsHome = null; 701 String home = null; 702 try { 703 Properties properties = new Properties (); 704 String strFileSep = System.getProperty("file.separator"); 705 home = System.getProperty("DODS_HOME"); 706 dodsHome = home + strFileSep + "build"+ strFileSep + "dods.properties"; 707 if (System.getProperty("os.name").toLowerCase().startsWith("win")) { 708 if (dodsHome != "/") { 709 dodsHome = dodsHome.replace('/', '\\'); 710 } 711 } 712 if (home == null) { 713 dodsHome = System.getProperty("ENHYDRA_DIR") + strFileSep 714 + "dods.properties"; 715 if (System.getProperty("os.name").toLowerCase().startsWith("win")) { 716 if (dodsHome != "/") { 717 dodsHome = dodsHome.replace('/', '\\'); 718 } 719 } 720 } 721 } catch (NullPointerException nullpointerexception) {} 722 return dodsHome; 723 } 724 725 729 public static String getDODSRoot() { 730 String enhHome = null; 731 732 try { 733 String strFileSep = System.getProperty("file.separator"); 734 735 enhHome = System.getProperty("DODS_HOME"); 736 if (System.getProperty("os.name").toLowerCase().startsWith("win")) { 737 if (enhHome != "/") { 738 enhHome = enhHome.replace('\\', '/'); 739 } 740 } 741 if (enhHome == null) { 742 String strInstall = getInstallProperties(); 743 744 if (System.getProperty("os.name").toLowerCase().startsWith("win")) { 745 if (System.getProperty("file.separator") != "/") { 746 strInstall = strInstall.replace('/', '\\'); 747 } 748 } 749 FileInputStream fisInstallProp = new FileInputStream (strInstall); 750 Properties properties = new Properties (); 751 752 properties.load(fisInstallProp); 753 enhHome = properties.getProperty("enhydra.root"); 754 if (System.getProperty("os.name").toLowerCase().startsWith("win")) { 755 if (enhHome != "/") { 756 enhHome = enhHome.replace('/', '\\'); 757 } 758 } 759 } 760 } catch (NullPointerException nullpointerexception) {} catch (IOException ioexception) {} 761 return enhHome; 762 } 763 764 768 public static String getInstallPropertiesParam() { 769 String enhydraRoot = null; 770 String dodsConf = null; 771 String strReturn = null; 772 773 init(); 774 try { 775 Properties properties = new Properties (); 776 String strFileSep = System.getProperty("file.separator"); 777 String strInstall = getInstallProperties(); 778 779 if (System.getProperty("os.name").toLowerCase().startsWith("win")) { 780 if (strFileSep != "/") { 781 strInstall = strInstall.replace('/', '\\'); 782 } 783 } 784 FileInputStream fisInstallProp = new FileInputStream (strInstall); 785 786 properties.load(fisInstallProp); 787 enhydraRoot = properties.getProperty("enhydra.root"); 788 if (System.getProperty("os.name").toLowerCase().startsWith("win")) { 789 if (enhydraRoot != "/") { 790 enhydraRoot = enhydraRoot.replace('/', '\\'); 791 } 792 } 793 dodsConf = properties.getProperty("dods.conf"); 794 if (System.getProperty("os.name").toLowerCase().startsWith("win")) { 795 if (dodsConf != "/") { 796 dodsConf = dodsConf.replace('/', '\\'); 797 } 798 } 799 fisInstallProp.close(); 800 } catch (IOException ioexception) {} catch (NullPointerException nullpointerexception) {} 801 strReturn = "<property name=\"enhydra.root\" value=\"" + enhydraRoot 802 + "\"/>\n"; 803 strReturn = strReturn + "<property name=\"dods.conf\" value=\"" 804 + dodsConf + "\"/>\n"; 805 return strReturn; 806 } 807 808 812 public static String getTemplateDir() { 813 String templateDir = null; 814 815 init(); 816 try { 817 Properties properties = new Properties (); 818 String strFileSep = System.getProperty("file.separator"); 819 String strInstall = getInstallProperties(); 820 821 if (System.getProperty("os.name").toLowerCase().startsWith("win")) { 822 if (strFileSep != "/") { 823 strInstall = strInstall.replace('/', '\\'); 824 } 825 } 826 FileInputStream fisDodsEjen = new FileInputStream (strInstall); 827 828 properties.load(fisDodsEjen); 829 fisDodsEjen.close(); 830 templateDir = properties.getProperty("template.dir"); 831 if (System.getProperty("os.name").toLowerCase().startsWith("win")) { 832 if (templateDir != "/") { 833 templateDir = templateDir.replace('/', '\\'); 834 } 835 } 836 } catch (IOException ioexception) {} catch (NullPointerException nullpointerexception) {return null;} 837 return templateDir; 838 } 839 840 844 public static HashSet getAllTemplateSets() { 845 HashSet dirs = new HashSet (); 846 847 init(); 848 try { 849 File templateDirs = new File (Common.getTemplateDir()); 850 String [] temp = templateDirs.list((FilenameFilter ) new DirectoryNameFilter()); 851 852 dirs.add("standard"); 853 for (int i = 0; i < temp.length; i++) { 854 dirs.add(temp[i]); 855 } 856 } catch (NullPointerException nullpointerexception) {} 857 return dirs; 858 } 859 860 867 static public String capitalizeName(String name) { String otherLetters = name.substring(1); 869 870 return name.toUpperCase(java.util.Locale.ENGLISH).substring(0, 1).concat(otherLetters); 871 } 872 873 880 static public String upperCaseName(String name) { 881 return name.toUpperCase(); 882 } 883 884 885 886 893 public static String replaceAll(String input, String forReplace, 894 String replaceWith) { 895 if( input == null ) return null; 896 StringBuffer result = new StringBuffer (); 897 boolean hasMore = true; 898 while (hasMore) { 899 int start = input.indexOf(forReplace); 900 int end = start + forReplace.length(); 901 if (start != -1) { 902 result.append(input.substring(0, start) + replaceWith); 903 input = input.substring(end); 904 } else { 905 hasMore = false; 906 result.append(input); 907 } 908 } 909 if (result.toString().equals("")) return input; else return result.toString(); 911 } 912 913 914 public static void main(String [] args) { 915 showDodsConf(); 916 System.out.println("Database.OidJdbcType = " 917 + getDodsConfProperty("Database.OidJdbcType", "Standard")); 918 } 919 } 920 | Popular Tags |