1 21 22 package org.opensubsystems.core.util; 23 24 import java.io.BufferedInputStream ; 25 import java.io.BufferedOutputStream ; 26 import java.io.File ; 27 import java.io.FileInputStream ; 28 import java.io.FileNotFoundException ; 29 import java.io.FileOutputStream ; 30 import java.io.IOException ; 31 import java.io.InputStream ; 32 import java.io.OutputStream ; 33 import java.net.URL ; 34 import java.util.Properties ; 35 import java.util.logging.Level ; 36 import java.util.logging.Logger ; 37 38 import org.opensubsystems.core.error.OSSConfigException; 39 40 48 public class Config 49 { 50 52 55 public static final String CONFIG_FILE_NAME = "oss.config.file"; 56 57 64 public static final String DEPENDENT_CONFIG_FILE_NAME = "oss.config.dependent.file"; 65 66 68 72 public static final String VALUE_YES_TRUE = "1"; 73 74 77 public static final String VALUE_YES_TRUE_BOOLEAN = Boolean.TRUE.toString(); 78 79 84 public static final String VALUE_NO_FALSE = "0"; 85 86 89 public static final String VALUE_NO_FALSE_BOOLEAN = Boolean.FALSE.toString(); 90 91 94 public static final String DEFAULT_CONFIG_FILE_NAME = "oss.properties"; 95 96 99 private static final String IMPL_LOCK = "IMPL_LOCK"; 100 101 104 protected String m_strMutex = "config.mutex"; 105 106 108 113 protected Properties m_prpProperties; 114 115 118 protected String m_strPropertyFileName; 119 120 124 protected URL m_urlDefaultPropertyFile; 125 126 129 protected String m_strConfigFile; 130 131 133 136 private static Logger s_logger = Log.getInstance(Config.class); 137 138 141 private static Config s_defaultInstance; 142 143 145 148 public Config( 149 ) 150 { 151 m_prpProperties = null; 152 m_strPropertyFileName = null; 153 m_urlDefaultPropertyFile = null; 154 } 155 156 161 public Config( 162 String strPropertyFileName 163 ) 164 { 165 m_prpProperties = null; 166 m_strPropertyFileName = strPropertyFileName; 167 m_urlDefaultPropertyFile = null; 168 } 169 170 175 public Config( 176 Properties predefinedProperties 177 ) 178 { 179 m_prpProperties = predefinedProperties; 180 m_strPropertyFileName = null; 181 m_urlDefaultPropertyFile = null; 182 } 183 184 186 191 public static Config getInstance( 192 ) 193 { 194 if (s_defaultInstance == null) 195 { 196 synchronized (IMPL_LOCK) 199 { 200 if (s_defaultInstance == null) 201 { 202 setInstance(new Config()); 204 } 205 } 206 } 207 208 return s_defaultInstance; 209 } 210 211 218 public static void setInstance( 219 Config defaultInstance 220 ) 221 { 222 if (GlobalConstants.ERROR_CHECKING) 223 { 224 assert defaultInstance != null 225 : "Default config instance cannot be null"; 226 } 227 228 synchronized (IMPL_LOCK) 229 { 230 s_defaultInstance = defaultInstance; 231 } 232 } 233 234 236 243 public Properties getProperties( 244 ) throws IOException 245 { 246 if (m_prpProperties == null) 247 { 248 synchronized (m_strMutex) 249 { 250 InputStream isConfigFile = null; 251 252 try 253 { 254 if (m_strPropertyFileName == null) 255 { 256 m_strPropertyFileName = System.getProperty(CONFIG_FILE_NAME, ""); 258 if ((m_strPropertyFileName == null) || (m_strPropertyFileName.length() == 0)) 259 { 260 m_strPropertyFileName = null; 261 s_logger.config("Name of configuration file is not specified" 262 + " using property " + CONFIG_FILE_NAME 263 + ". Trying to load default configuration file" 264 + " from system classpath."); 265 266 isConfigFile = findConfigFile(DEFAULT_CONFIG_FILE_NAME); 267 } 268 else 269 { 270 s_logger.config(CONFIG_FILE_NAME + " = " 271 + m_strPropertyFileName); 272 273 try 274 { 275 isConfigFile = new FileInputStream (m_strPropertyFileName); 277 m_strConfigFile = m_strPropertyFileName; 278 } 279 catch (FileNotFoundException fnfExc) 280 { 281 isConfigFile = findConfigFile(m_strPropertyFileName); 283 } 284 } 285 } 286 else 287 { 288 s_logger.config("Name of configuration file" + 289 " is specified programatically = " 290 + m_strPropertyFileName); 291 292 try 293 { 294 isConfigFile = new FileInputStream (m_strPropertyFileName); 296 m_strConfigFile = m_strPropertyFileName; 297 } 298 catch (FileNotFoundException fnfExc) 299 { 300 isConfigFile = findConfigFile(m_strPropertyFileName); 302 } 303 } 304 305 BufferedInputStream bisConfigFile = null; 306 307 s_logger.config("Using configuration file " + m_strConfigFile); 309 m_prpProperties = new Properties (); 310 try 311 { 312 bisConfigFile = new BufferedInputStream (isConfigFile); 313 m_prpProperties.load(bisConfigFile); 314 } 315 finally 316 { 317 try 319 { 320 if (bisConfigFile != null) 321 { 322 bisConfigFile.close(); 323 } 324 } 325 catch (IOException ioeExc) 326 { 327 s_logger.log(Level.WARNING, 329 "Failed to close buffer for configuration file.", 330 ioeExc); 331 } 332 } 333 } 334 finally 335 { 336 try 337 { 338 if (isConfigFile != null) 339 { 340 isConfigFile.close(); 341 } 342 } 343 catch (IOException ioeExc) 344 { 345 s_logger.log(Level.WARNING, 347 "Failed to close configuration file.", 348 ioeExc); 349 } 350 } 351 352 if (m_prpProperties != null) 356 { 357 String strDependentPropertyFile; 358 359 strDependentPropertyFile = Config.getStringProperty( 360 m_prpProperties, 361 DEPENDENT_CONFIG_FILE_NAME, 362 "", 363 "Dependent property file", 364 true); 365 if (strDependentPropertyFile.length() > 0) 366 { 367 Config dependentFile; 370 Properties prpSettings; 371 372 dependentFile = new Config(strDependentPropertyFile); 373 prpSettings = dependentFile.getPropertiesSafely(); 374 prpSettings.putAll(m_prpProperties); 376 m_prpProperties = prpSettings; 378 } 379 } 380 } 381 } 382 383 return m_prpProperties; 384 } 385 386 395 protected InputStream findConfigFile( 396 String strConfigFileName 397 ) throws IOException , 398 FileNotFoundException 399 { 400 InputStream isConfigFile = null; 401 402 m_urlDefaultPropertyFile = ClassLoader.getSystemResource(strConfigFileName); 403 if (m_urlDefaultPropertyFile == null) 404 { 405 m_urlDefaultPropertyFile = this.getClass().getClassLoader().getResource( 415 strConfigFileName); 416 } 417 if (m_urlDefaultPropertyFile == null) 418 { 419 throw new FileNotFoundException ("Cannot find configuration file " 420 + strConfigFileName); 421 } 422 else 423 { 424 isConfigFile = m_urlDefaultPropertyFile.openStream(); 425 m_strConfigFile = m_urlDefaultPropertyFile.toString(); 426 } 427 428 return isConfigFile; 429 } 430 431 438 public Properties getPropertiesSafely( 439 ) 440 { 441 Properties prpSettings; 442 443 try 444 { 445 prpSettings = getProperties(); 446 } 447 catch (FileNotFoundException fnfeExc) 448 { 449 s_logger.log(Level.WARNING, 450 "Cannot find default configuration file " + m_strPropertyFileName, 451 fnfeExc); 452 prpSettings = new Properties (); 454 m_prpProperties = prpSettings; 455 if ((m_strPropertyFileName == null) 457 || (m_strPropertyFileName.length() == 0)) 458 { 459 setPropertyFileName(Config.DEFAULT_CONFIG_FILE_NAME); 460 } 461 else 462 { 463 setPropertyFileName(m_strPropertyFileName); 464 } 465 } 466 catch (IOException ioeExc) 467 { 468 s_logger.log(Level.CONFIG, 469 "Error has occured while accessing default configuration file.", 470 ioeExc); 471 prpSettings = new Properties (); 473 m_prpProperties = prpSettings; 474 if ((m_strPropertyFileName == null) 476 || (m_strPropertyFileName.length() == 0)) 477 { 478 setPropertyFileName(Config.DEFAULT_CONFIG_FILE_NAME); 479 } 480 else 481 { 482 setPropertyFileName(m_strPropertyFileName); 483 484 } 485 } 486 487 return prpSettings; 488 } 489 490 496 public void save( 497 ) throws IOException , 498 FileNotFoundException 499 { 500 synchronized (m_prpProperties) 504 { 505 OutputStream osConfigFile = null; 507 508 try 509 { 510 if (m_strPropertyFileName != null) 511 { 512 osConfigFile = new FileOutputStream (m_strPropertyFileName); 514 } 515 else if (m_urlDefaultPropertyFile != null) 516 { 517 osConfigFile = new FileOutputStream (m_urlDefaultPropertyFile.getFile()); 518 } 519 else 520 { 521 throw new FileNotFoundException ("No configuration file defined."); 522 } 523 524 BufferedOutputStream bosConfigFile = null; 525 526 try 528 { 529 bosConfigFile = new BufferedOutputStream (osConfigFile); 530 531 m_prpProperties.store(bosConfigFile, "DO NOT MODIFY THIS FILE DIRECTLY."); 536 } 537 finally 538 { 539 try 541 { 542 if (bosConfigFile != null) 543 { 544 bosConfigFile.close(); 545 } 546 } 547 catch (IOException ioeExc) 548 { 549 s_logger.log(Level.WARNING, 551 "Failed to close buffer for configuration file " 552 + m_strPropertyFileName, ioeExc); 553 } 554 } 555 } 556 finally 557 { 558 try 559 { 560 if (osConfigFile != null) 561 { 562 osConfigFile.close(); 563 } 564 } 565 catch (IOException ioeExc) 566 { 567 s_logger.log(Level.WARNING, 569 "Failed to close configuration file " 570 + m_strPropertyFileName, ioeExc); 571 } 572 } 573 } 574 } 575 576 584 public static void save( 585 File fileConfig, 586 Properties prpSettings 587 ) throws IOException , 588 FileNotFoundException 589 { 590 OutputStream osConfigFile = null; 592 593 try 594 { 595 osConfigFile = new FileOutputStream (fileConfig); 597 598 BufferedOutputStream bosConfigFile = null; 599 600 try 602 { 603 bosConfigFile = new BufferedOutputStream (osConfigFile); 604 605 prpSettings.store(bosConfigFile, "DO NOT MODIFY THIS FILE DIRECTLY."); 610 } 611 finally 612 { 613 try 615 { 616 if (bosConfigFile != null) 617 { 618 bosConfigFile.close(); 619 } 620 } 621 catch (IOException ioeExc) 622 { 623 s_logger.log(Level.WARNING, 625 "Failed to close buffer for configuration file " 626 + fileConfig.getCanonicalPath(), ioeExc); 627 } 628 } 629 } 630 finally 631 { 632 try 633 { 634 if (osConfigFile != null) 635 { 636 osConfigFile.close(); 637 } 638 } 639 catch (IOException ioeExc) 640 { 641 s_logger.log(Level.WARNING, 643 "Failed to close configuration file " 644 + fileConfig.getCanonicalPath(), ioeExc); 645 } 646 } 647 } 648 649 654 public String getPropertyFileName( 655 ) 656 { 657 return m_strPropertyFileName; 658 } 659 660 666 public String getFullPropertyFileName( 667 ) 668 { 669 return m_strConfigFile; 670 } 671 672 677 public void setPropertyFileName( 678 String strPropertyFileName 679 ) 680 { 681 m_strPropertyFileName = strPropertyFileName; 682 } 683 684 690 public static boolean isTrue( 691 String strValue 692 ) 693 { 694 return VALUE_YES_TRUE.equals(strValue) 696 || VALUE_YES_TRUE_BOOLEAN.equalsIgnoreCase(strValue); 697 } 698 699 712 public static int getIntPropertyInRange( 713 Properties prpSettings, 714 String strProperty, 715 int iDefaultValue, 716 String strDisplayName, 717 int iMinValue, 718 int iMaxValue 719 ) 720 { 721 String strParam; 722 int iValue; 723 724 strParam = prpSettings.getProperty(strProperty, strProperty); 727 if ((strParam.length() == 0) || (strParam.equals(strProperty))) 728 { 729 s_logger.config(strDisplayName + " is not set in property " 730 + strProperty + ", using default value " 731 + iDefaultValue); 732 iValue = iDefaultValue; 733 } 734 else 735 { 736 try 737 { 738 iValue = Integer.parseInt(strParam); 739 if ((iValue < iMinValue) || (iValue > iMaxValue)) 740 { 741 s_logger.config("Value of " + strProperty 742 + " property is outside of valid range (" 743 + iMinValue + " - " + iMaxValue 744 + "), using default value " 745 + iDefaultValue); 746 iValue = iDefaultValue; 747 } 748 } 749 catch (NumberFormatException nfeExc) 750 { 751 s_logger.config("Value of " + strProperty 752 + " property is incorrect (" 753 + strParam + ", valid range is " 754 + iMinValue + " - " + iMaxValue 755 + "), using default value " 756 + iDefaultValue); 757 iValue = iDefaultValue; 758 } 759 } 760 s_logger.config(strProperty + " = " + iValue); 761 762 return iValue; 763 } 764 765 776 public static String getStringProperty( 777 Config configFile, 778 String strProperty, 779 String strDisplayName 780 ) throws OSSConfigException 781 { 782 Properties prpSettings; 783 String strParam; 784 String strValue; 785 786 prpSettings = configFile.getPropertiesSafely(); 787 strParam = prpSettings.getProperty(strProperty, strProperty); 788 if ((strParam.length() == 0) || (strParam.equals(strProperty))) 789 { 790 throw new OSSConfigException(strDisplayName + " is not set in property " 791 + strProperty + " in configuration file" 792 + configFile.getPropertyFileName()); 793 } 794 else 795 { 796 strValue = strParam; 797 } 798 s_logger.config(strProperty + " = " + strValue); 799 800 return strValue; 801 } 802 803 813 public static String getStringProperty( 814 Properties prpSettings, 815 String strProperty, 816 String strDisplayName 817 ) throws OSSConfigException 818 { 819 return getStringProperty(prpSettings, strProperty, strDisplayName, false); 820 } 821 822 833 public static String getStringProperty( 834 Properties prpSettings, 835 String strProperty, 836 String strDisplayName, 837 boolean bAllowEmpty 838 ) throws OSSConfigException 839 { 840 String strParam; 841 String strValue; 842 843 strParam = prpSettings.getProperty(strProperty, strProperty); 844 if ((strParam.equals(strProperty)) 845 || ((!bAllowEmpty) && (strParam.length() == 0))) 846 { 847 throw new OSSConfigException(strDisplayName + " is not set in property " 848 + strProperty); 849 } 850 else 851 { 852 strValue = strParam; 853 } 854 s_logger.config(strProperty + " = " + strValue); 855 856 return strValue; 857 } 858 859 870 public static String getStringProperty( 871 Properties prpSettings, 872 String strProperty, 873 String strDefaultValue, 874 String strDisplayName 875 ) 876 { 877 return getStringProperty(prpSettings, strProperty, strDefaultValue, 878 strDisplayName, false); 879 } 880 881 892 public static String getStringProperty( 893 Properties prpSettings, 894 String strProperty, 895 String strDefaultValue, 896 String strDisplayName, 897 boolean bAllowEmpty 898 ) 899 { 900 String strParam; 901 String strValue; 902 903 strParam = prpSettings.getProperty(strProperty, strProperty); 906 if ((strParam.equals(strProperty)) 907 || ((!bAllowEmpty) && (strParam.length() == 0))) 908 { 909 s_logger.config(strDisplayName + " is not set in property " 910 + strProperty + ", using default value " 911 + strDefaultValue); 912 strValue = strDefaultValue; 913 } 914 else 915 { 916 strValue = strParam; 917 } 918 s_logger.config(strProperty + " = " + strValue); 919 920 return strValue; 921 } 922 923 933 public static boolean getBooleanProperty( 934 Properties prpSettings, 935 String strProperty, 936 String strDisplayName 937 ) throws OSSConfigException 938 { 939 String strParam; 940 boolean bValue; 941 942 strParam = prpSettings.getProperty(strProperty, strProperty); 943 if ((strParam.equals(strProperty)) || (strParam.length() == 0)) 944 { 945 throw new OSSConfigException(strDisplayName + " is not set in property " 946 + strProperty); 947 } 948 else 949 { 950 bValue = Config.isTrue(strParam); 951 } 952 s_logger.config(strProperty + " = " + bValue); 953 954 return bValue; 955 } 956 957 968 public static boolean getBooleanProperty( 969 Properties prpSettings, 970 String strProperty, 971 boolean bDefaultValue, 972 String strDisplayName 973 ) 974 { 975 String strParam; 976 boolean bValue; 977 978 strParam = prpSettings.getProperty(strProperty, strProperty); 979 if ((strParam.equals(strProperty)) || (strParam.length() == 0)) 980 { 981 s_logger.config(strDisplayName + " is not set in property " 982 + strProperty + ", using default value " 983 + bDefaultValue); 984 bValue = bDefaultValue; 985 } 986 else 987 { 988 bValue = Config.isTrue(strParam); 989 } 990 s_logger.config(strProperty + " = " + bValue); 991 992 return bValue; 993 } 994 995 1006 public static String getBooleanPropertyAsString( 1007 Properties prpSettings, 1008 String strProperty, 1009 boolean bDefaultValue, 1010 String strDisplayName 1011 ) 1012 { 1013 String strReturn; 1014 1015 if (getBooleanProperty(prpSettings, strProperty, bDefaultValue, strDisplayName)) 1016 { 1017 strReturn = Boolean.TRUE.toString(); 1018 } 1019 else 1020 { 1021 strReturn = Boolean.FALSE.toString(); 1022 } 1023 1024 return strReturn; 1025 } 1026} 1027 | Popular Tags |