1 40 package org.dspace.core; 41 42 import java.io.BufferedReader ; 43 import java.io.File ; 44 import java.io.FileInputStream ; 45 import java.io.FileOutputStream ; 46 import java.io.FileReader ; 47 import java.io.FileWriter ; 48 import java.io.IOException ; 49 import java.io.InputStream ; 50 import java.io.InputStreamReader ; 51 import java.io.OutputStreamWriter ; 52 import java.io.PrintWriter ; 53 import java.net.MalformedURLException ; 54 import java.util.Enumeration ; 55 import java.util.Properties ; 56 57 import org.apache.log4j.Logger; 58 import org.apache.log4j.PropertyConfigurator; 59 import org.apache.log4j.xml.DOMConfigurator; 60 61 85 public class ConfigurationManager 86 { 87 88 private static Logger log = null; 89 90 91 private static Properties properties = null; 92 93 94 private static String license; 95 96 private final static int RECURSION_LIMIT = 9; 99 100 109 public static String getProperty(String property) 110 { 111 if (properties == null) 112 { 113 loadConfig(null); 114 } 115 116 return properties.getProperty(property); 117 } 118 119 129 public static int getIntProperty(String property) 130 { 131 if (properties == null) 132 { 133 loadConfig(null); 134 } 135 136 String stringValue = properties.getProperty(property); 137 int intValue = 0; 138 139 if (stringValue != null) 140 { 141 try 142 { 143 intValue = Integer.parseInt(stringValue.trim()); 144 } 145 catch (NumberFormatException e) 146 { 147 warn("Warning: Number format error in property: " + property); 148 } 149 } 150 151 return intValue; 152 } 153 154 167 public static boolean getBooleanProperty(String property) 168 { 169 return getBooleanProperty(property, false); 170 } 171 172 189 public static boolean getBooleanProperty(String property, boolean defaultValue) 190 { 191 if (properties == null) 192 { 193 loadConfig(null); 194 } 195 196 String stringValue = properties.getProperty(property); 197 198 if (stringValue != null) 199 { 200 stringValue = stringValue.trim(); 201 return stringValue.equalsIgnoreCase("true") || 202 stringValue.equalsIgnoreCase("yes"); 203 } 204 else 205 { 206 return defaultValue; 207 } 208 } 209 210 215 public static Enumeration propertyNames() 216 { 217 if (properties == null) 218 loadConfig(null); 219 220 return properties.propertyNames(); 221 } 222 223 237 public static Email getEmail(String template) throws IOException 238 { 239 String subject = ""; 240 StringBuffer contentBuffer = new StringBuffer (); 241 242 BufferedReader reader = null; 244 try 245 { 246 reader = new BufferedReader (new FileReader ( 247 getProperty("dspace.dir") + 248 249 File.separator + "config" + File.separator + "emails" 250 + File.separator + template)); 251 252 boolean more = true; 253 254 while (more) 255 { 256 String line = reader.readLine(); 257 258 if (line == null) 259 { 260 more = false; 261 } 262 else if (line.toLowerCase().startsWith("subject:")) 263 { 264 subject = line.substring(8).trim(); 267 } 268 else if (!line.startsWith("#")) 269 { 270 contentBuffer.append(line); 272 contentBuffer.append("\n"); 273 } 274 } 275 } 276 finally 277 { 278 if (reader != null) 279 { 280 reader.close(); 281 } 282 } 283 Email email = new Email(); 285 email.setSubject(subject); 286 email.setContent(contentBuffer.toString()); 287 288 return email; 289 } 290 291 296 public static String getDefaultSubmissionLicense() 297 { 298 if (properties == null) 299 { 300 loadConfig(null); 301 } 302 303 return license; 304 } 305 306 310 public static String getNewsFilePath() 311 { 312 String filePath = ConfigurationManager.getProperty("dspace.dir") 313 + File.separator + "config" + File.separator; 314 315 return filePath; 316 } 317 318 325 public static String readNewsFile(int position) 326 { 327 String fileName = getNewsFilePath(); 328 329 if (position == Constants.NEWS_TOP) 330 { 331 fileName += "news-top.html"; 332 } 333 else 334 { 335 fileName += "news-side.html"; 336 } 337 338 String text = ""; 339 340 try 341 { 342 FileInputStream fir = new FileInputStream (fileName); 344 InputStreamReader ir = new InputStreamReader (fir, "UTF-8"); 345 BufferedReader br = new BufferedReader (ir); 346 347 String lineIn; 348 349 while ((lineIn = br.readLine()) != null) 350 { 351 text += lineIn; 352 } 353 354 br.close(); 355 } 356 catch (IOException e) 357 { 358 warn("news_read: " + e.getLocalizedMessage()); 359 } 360 361 return text; 362 } 363 364 374 public static String writeNewsFile(int position, String news) 375 { 376 String fileName = getNewsFilePath(); 377 378 if (position == Constants.NEWS_TOP) 379 { 380 fileName += "news-top.html"; 381 } 382 else 383 { 384 fileName += "news-side.html"; 385 } 386 387 try 388 { 389 FileOutputStream fos = new FileOutputStream (fileName); 391 OutputStreamWriter osr = new OutputStreamWriter (fos, "UTF-8"); 392 PrintWriter out = new PrintWriter (osr); 393 out.print(news); 394 out.close(); 395 } 396 catch (IOException e) 397 { 398 warn("news_write: " + e.getLocalizedMessage()); 399 } 400 401 return news; 402 } 403 404 410 public static void writeLicenseFile(String newLicense) 411 { 412 String licenseFile = getProperty("dspace.dir") + File.separator 413 + "config" + File.separator + "default.license"; 414 415 try 416 { 417 FileOutputStream fos = new FileOutputStream (licenseFile); 419 OutputStreamWriter osr = new OutputStreamWriter (fos, "UTF-8"); 420 PrintWriter out = new PrintWriter (osr); 421 out.print(newLicense); 422 out.close(); 423 } 424 catch (IOException e) 425 { 426 warn("license_write: " + e.getLocalizedMessage()); 427 } 428 429 license = newLicense; 430 } 431 432 private static File loadedFile = null; 433 434 440 public static File getConfigurationFile() 441 { 442 loadConfig(null); 444 445 return loadedFile; 446 } 447 448 457 public static void loadConfig(String configFile) 458 { 459 InputStream is; 460 461 if (properties != null) 462 { 463 return; 464 } 465 466 String configProperty = System.getProperty("dspace.configuration"); 467 468 try 469 { 470 if (configFile != null) 471 { 472 is = new FileInputStream (configFile); 473 loadedFile = new File (configFile); 474 } 475 else if (configProperty != null) 477 { 478 is = new FileInputStream (configProperty); 480 loadedFile = new File (configProperty); 481 } 482 else 483 { 484 is = ConfigurationManager.class 486 .getResourceAsStream("/dspace.cfg"); 487 loadedFile = new File (ConfigurationManager.class.getResource( 488 "/dspace.cfg").getPath()); 489 } 490 491 if (is == null) 492 { 493 fatal("Cannot find dspace.cfg"); 494 throw new RuntimeException ("Cannot find dspace.cfg"); 495 } 496 else 497 { 498 properties = new Properties (); 499 properties.load(is); 500 501 for (Enumeration pe = properties.propertyNames(); pe.hasMoreElements(); ) 503 { 504 String key = (String )pe.nextElement(); 505 String value = interpolate(key, 1); 506 if (value != null) 507 properties.setProperty(key, value); 508 } 509 } 510 511 String licenseFile = getProperty("dspace.dir") + File.separator 513 + "config" + File.separator + "default.license"; 514 515 FileInputStream fir = new FileInputStream (licenseFile); 516 InputStreamReader ir = new InputStreamReader (fir, "UTF-8"); 517 BufferedReader br = new BufferedReader (ir); 518 String lineIn; 519 license = ""; 520 521 while ((lineIn = br.readLine()) != null) 522 { 523 license = license + lineIn + '\n'; 524 } 525 526 is.close(); 527 528 String log4jConfProp = ConfigurationManager 532 .getProperty("dspace.dir") 533 + File.separator 534 + "config" 535 + File.separator 536 + "log4j.properties"; 537 String log4jConfXml = ConfigurationManager 538 .getProperty("dspace.dir") 539 + File.separator + "config" + File.separator + "log4j.xml"; 540 541 File xmlFile = new File (log4jConfXml); 542 if (xmlFile.exists()) 543 { 544 try 545 { 546 DOMConfigurator.configure(xmlFile.toURL()); 547 initLog(); 548 info("DSpace logging installed using log4j.xml"); 549 } 550 catch (MalformedURLException e) 551 { 552 PropertyConfigurator.configure(log4jConfProp); 553 initLog(); 554 error("Logger failed to load log4j.xml, defaulted to " 555 + "log4j.properties: " + e); 556 } 557 } 558 else 559 { 560 PropertyConfigurator.configure(log4jConfProp); 561 initLog(); 562 info("DSpace logging installed using log4j.properties"); 563 } 564 } 565 catch (IOException e) 566 { 567 fatal("Can't load configuration", e); 568 569 throw new RuntimeException ("Cannot find dspace.cfg",e); 572 } 573 } 574 575 581 private static String interpolate(String key, int level) 582 { 583 if (level > RECURSION_LIMIT) 584 throw new IllegalArgumentException ("ConfigurationManager: Too many levels of recursion in configuration property variable interpolation, property="+key); 585 String value = (String )properties.get(key); 586 int from = 0; 587 StringBuffer result = null; 588 while (from < value.length()) 589 { 590 int start = value.indexOf("${", from); 591 if (start >= 0) 592 { 593 int end = value.indexOf("}", start); 594 if (end < 0) 595 break; 596 String var = value.substring(start+2, end); 597 if (result == null) 598 result = new StringBuffer (value.substring(from, start)); 599 else 600 result.append(value.substring(from, start)); 601 if (properties.containsKey(var)) 602 { 603 String ivalue = interpolate(var, level+1); 604 if (ivalue != null) 605 { 606 result.append(ivalue); 607 properties.setProperty(var, ivalue); 608 } 609 else 610 result.append((String )properties.getProperty(var)); 611 } 612 else 613 { 614 log.warn("Interpolation failed in value of property \""+key+ 615 "\", there is no property named \""+var+"\""); 616 } 617 from = end+1; 618 } 619 else 620 break; 621 } 622 if (result != null && from < value.length()) 623 result.append(value.substring(from)); 624 return (result == null) ? null : result.toString(); 625 } 626 627 640 public static void installConfigurations() throws IOException 641 { 642 File templateDir = new File (getProperty("dspace.dir") + File.separator 644 + "config" + File.separator + "templates"); 645 646 File [] templateFiles = templateDir.listFiles(); 647 648 for (int i = 0; i < templateFiles.length; i++) 649 { 650 installConfigurationFile(templateFiles[i].getName()); 651 } 652 } 653 654 673 private static void installConfigurationFile(String template) 674 throws IOException 675 { 676 String destination = getProperty("config.template." + template); 678 679 if (destination == null) 680 { 681 info("Not processing config file template " + template 683 + " because no destination specified (no property " 684 + "config.template." + template + ")"); 685 686 return; 687 } 688 689 info("Installing configuration file template " + template + " to " 690 + destination); 691 692 BufferedReader in = new BufferedReader (new FileReader ( 694 getProperty("dspace.dir") + File.separator + "config" 695 + File.separator + "templates" + File.separator 696 + template)); 697 698 PrintWriter out = new PrintWriter (new FileWriter (destination)); 700 701 int lineNumber = 0; 703 String line; 704 705 while ((line = in.readLine()) != null) 707 { 708 lineNumber++; 709 710 boolean moreValues = true; 712 713 while (moreValues) 714 { 715 int first = line.indexOf("@@"); 717 718 if (first > -1) 719 { 720 int second = line.indexOf("@@", first + 2); 722 723 if (second > -1) 724 { 725 String propName = line.substring(first + 2, second); 727 728 String propValue = getProperty(propName); 729 730 if (propValue == null) 731 { 732 warn(template + " line " + lineNumber 733 + ": Property " + propName 734 + " not defined in DSpace configuration - " 735 + "using empty string"); 736 737 propValue = ""; 738 } 739 740 line = line.substring(0, first) + propValue 742 + line.substring(second + 2); 743 } 744 else 745 { 746 warn(template + " line " + lineNumber 748 + ": Single @@ - leaving as-is"); 749 moreValues = false; 750 } 751 } 752 else 753 { 754 moreValues = false; 756 } 757 } 758 759 out.println(line); 761 } 762 763 in.close(); 764 out.close(); 765 } 766 767 781 public static void main(String [] argv) 782 { 783 if ((argv.length == 1) && argv[0].equals("-installTemplates")) 784 { 785 try 786 { 787 info("Installing configuration files for other tools"); 788 installConfigurations(); 789 System.exit(0); 790 } 791 catch (IOException ie) 792 { 793 warn("Error installing configuration files", ie); 794 } 795 } 796 else if ((argv.length == 2) && argv[0].equals("-property")) 797 { 798 String val = getProperty(argv[1]); 799 800 if (val != null) 801 { 802 System.out.println(val); 803 } 804 else 805 { 806 System.out.println(""); 807 } 808 809 System.exit(0); 810 } 811 else 812 { 813 System.err 814 .println("Usage: ConfigurationManager OPTION\n -installTemplates install config files for external tools\n -property prop.name get value of prop.name from dspace.cfg"); 815 } 816 817 System.exit(1); 818 } 819 820 private static void info(String string) 821 { 822 if (log == null) 823 { 824 System.out.println("INFO: " + string); 825 } 826 else 827 { 828 log.info(string); 829 } 830 } 831 832 private static void warn(String string, Exception e) 833 { 834 if (log == null) 835 { 836 System.out.println("WARN: " + string); 837 e.printStackTrace(); 838 } 839 else 840 { 841 log.warn(string, e); 842 } 843 } 844 845 private static void warn(String string) 846 { 847 if (log == null) 848 { 849 System.out.println("WARN: " + string); 850 } 851 else 852 { 853 log.warn(string); 854 } 855 } 856 857 private static void error(String string) 858 { 859 if (log == null) 860 { 861 System.err.println("ERROR: " + string); 862 } 863 else 864 { 865 log.error(string); 866 } 867 } 868 869 private static void fatal(String string, Exception e) 870 { 871 if (log == null) 872 { 873 System.out.println("FATAL: " + string); 874 e.printStackTrace(); 875 } 876 else 877 { 878 log.fatal(string, e); 879 } 880 } 881 882 private static void fatal(String string) 883 { 884 if (log == null) 885 { 886 System.out.println("FATAL: " + string); 887 } 888 else 889 { 890 log.fatal(string); 891 } 892 } 893 894 private static void initLog() 895 { 896 log = Logger.getLogger(ConfigurationManager.class); 897 } 898 899 } 900 | Popular Tags |