1 11 12 package org.jivesoftware.util; 13 14 import org.dom4j.Document; 15 import org.dom4j.io.SAXReader; 16 17 import javax.naming.InitialContext ; 18 import java.io.File ; 19 import java.io.IOException ; 20 import java.io.InputStream ; 21 import java.text.DateFormat ; 22 import java.util.*; 23 24 48 public class JiveGlobals { 49 50 private static String JIVE_CONFIG_FILENAME = null; 51 52 56 public static String home = null; 57 58 public static boolean failedLoading = false; 59 60 private static XMLProperties xmlProperties = null; 61 private static JiveProperties properties = null; 62 63 private static Locale locale = null; 64 private static TimeZone timeZone = null; 65 private static DateFormat dateFormat = null; 66 private static DateFormat dateTimeFormat = null; 67 private static DateFormat timeFormat = null; 68 69 76 public static Locale getLocale() { 77 if (locale == null) { 78 if (xmlProperties != null) { 79 String [] localeArray; 80 String localeProperty = (String ) xmlProperties.getProperty("locale"); 81 if (localeProperty != null) { 82 localeArray = localeProperty.split("_"); 83 } 84 else { 85 localeArray = new String [] {"", ""}; 86 } 87 88 String language = localeArray[0]; 89 if (language == null) { 90 language = ""; 91 } 92 String country = ""; 93 if (localeArray.length == 2) { 94 country = localeArray[1]; 95 } 96 if (language.equals("") && country.equals("")) { 98 locale = Locale.getDefault(); 99 } 100 else { 101 locale = new Locale(language, country); 102 } 103 } 104 else { 105 return Locale.getDefault(); 106 } 107 } 108 return locale; 109 } 110 111 118 public static void setLocale(Locale newLocale) { 119 locale = newLocale; 120 setXMLProperty("locale", locale.toString()); 122 123 timeFormat = DateFormat.getTimeInstance(DateFormat.SHORT, locale); 125 dateFormat = DateFormat.getDateInstance(DateFormat.MEDIUM, locale); 126 dateTimeFormat = DateFormat.getDateTimeInstance(DateFormat.MEDIUM, 127 DateFormat.MEDIUM, locale); 128 timeFormat.setTimeZone(timeZone); 129 dateFormat.setTimeZone(timeZone); 130 dateTimeFormat.setTimeZone(timeZone); 131 } 132 133 139 public static TimeZone getTimeZone() { 140 if (timeZone == null) { 141 if (properties != null) { 142 String timeZoneID = (String )properties.get("locale.timeZone"); 143 if (timeZoneID == null) { 144 timeZone = TimeZone.getDefault(); 145 } 146 else { 147 timeZone = TimeZone.getTimeZone(timeZoneID); 148 } 149 } 150 else { 151 return TimeZone.getDefault(); 152 } 153 } 154 return timeZone; 155 } 156 157 161 public static void setTimeZone(TimeZone newTimeZone) { 162 timeZone = newTimeZone; 163 timeFormat.setTimeZone(timeZone); 164 dateFormat.setTimeZone(timeZone); 165 dateTimeFormat.setTimeZone(timeZone); 166 setProperty("locale.timeZone", timeZone.getID()); 167 } 168 169 175 public static String formatTime(Date date) { 176 if (timeFormat == null) { 177 if (properties != null) { 178 timeFormat = DateFormat.getTimeInstance(DateFormat.SHORT, getLocale()); 179 timeFormat.setTimeZone(getTimeZone()); 180 } 181 else { 182 DateFormat instance = DateFormat.getTimeInstance(DateFormat.SHORT, getLocale()); 183 instance.setTimeZone(getTimeZone()); 184 return instance.format(date); 185 } 186 } 187 return timeFormat.format(date); 188 } 189 190 196 public static String formatDate(Date date) { 197 if (dateFormat == null) { 198 if (properties != null) { 199 dateFormat = DateFormat.getDateInstance(DateFormat.MEDIUM, getLocale()); 200 dateFormat.setTimeZone(getTimeZone()); 201 } 202 else { 203 DateFormat instance = DateFormat.getDateInstance(DateFormat.MEDIUM, getLocale()); 204 instance.setTimeZone(getTimeZone()); 205 return instance.format(date); 206 } 207 } 208 return dateFormat.format(date); 209 } 210 211 217 public static String formatDateTime(Date date) { 218 if (dateTimeFormat == null) { 219 if (properties != null) { 220 dateTimeFormat = DateFormat.getDateTimeInstance(DateFormat.MEDIUM, 221 DateFormat.MEDIUM, getLocale()); 222 dateTimeFormat.setTimeZone(getTimeZone()); 223 } 224 else { 225 DateFormat instance = DateFormat.getDateTimeInstance(DateFormat.MEDIUM, 226 DateFormat.MEDIUM, getLocale()); 227 instance.setTimeZone(getTimeZone()); 228 return instance.format(date); 229 } 230 } 231 return dateTimeFormat.format(date); 232 } 233 234 239 public static String getHomeDirectory() { 240 if (home == null) { 241 loadSetupProperties(); 242 } 243 return home; 244 } 245 246 262 public static String getXMLProperty(String name) { 263 if (xmlProperties == null) { 264 loadSetupProperties(); 265 } 266 267 if (xmlProperties == null) { 269 return null; 270 } 271 272 return xmlProperties.getProperty(name); 273 } 274 275 294 public static String getXMLProperty(String name, String defaultValue) { 295 if (xmlProperties == null) { 296 loadSetupProperties(); 297 } 298 299 if (xmlProperties == null) { 301 return null; 302 } 303 304 String value = xmlProperties.getProperty(name); 305 if (value == null) { 306 value = defaultValue; 307 } 308 return value; 309 } 310 311 332 public static int getXMLProperty(String name, int defaultValue) { 333 String value = getXMLProperty(name); 334 if (value != null) { 335 try { 336 return Integer.parseInt(value); 337 } 338 catch (NumberFormatException nfe) { } 339 } 340 return defaultValue; 341 } 342 343 360 public static void setXMLProperty(String name, String value) { 361 if (xmlProperties == null) { 362 loadSetupProperties(); 363 } 364 365 if (xmlProperties != null) { 367 xmlProperties.setProperty(name, value); 368 } 369 } 370 371 387 public static void setXMLProperties(Map propertyMap) { 388 if (xmlProperties == null) { 389 loadSetupProperties(); 390 } 391 392 if (xmlProperties != null) { 393 xmlProperties.setProperties(propertyMap); 394 } 395 } 396 397 419 public static List getXMLProperties(String parent) { 420 if (xmlProperties == null) { 421 loadSetupProperties(); 422 } 423 424 if (xmlProperties == null) { 426 return Collections.EMPTY_LIST; 427 } 428 429 String [] propNames = xmlProperties.getChildrenProperties(parent); 430 List values = new ArrayList(); 431 for (int i = 0; i < propNames.length; i++) { 432 String propName = propNames[i]; 433 String value = getProperty(parent + "." + propName); 434 if (value != null) { 435 values.add(value); 436 } 437 } 438 439 return values; 440 } 441 442 448 public static void deleteXMLProperty(String name) { 449 if (xmlProperties == null) { 450 loadSetupProperties(); 451 } 452 xmlProperties.deleteProperty(name); 453 } 454 455 461 public static String getProperty(String name) { 462 if (properties == null) { 463 if (isSetupMode()) { 464 return null; 465 } 466 properties = JiveProperties.getInstance(); 467 } 468 return (String )properties.get(name); 469 } 470 471 479 public static String getProperty(String name, String defaultValue) { 480 if (properties == null) { 481 if (isSetupMode()) { 482 return defaultValue; 483 } 484 properties = JiveProperties.getInstance(); 485 } 486 String value = (String )properties.get(name); 487 if (value != null) { 488 return value; 489 } 490 else { 491 return defaultValue; 492 } 493 } 494 495 504 public static int getIntProperty(String name, int defaultValue) { 505 String value = getProperty(name); 506 if (value != null) { 507 try { 508 return Integer.parseInt(value); 509 } 510 catch (NumberFormatException nfe) { } 511 } 512 return defaultValue; 513 } 514 515 522 public static boolean getBooleanProperty(String name) { 523 return Boolean.valueOf(getProperty(name)).booleanValue(); 524 } 525 526 538 public static boolean getBooleanProperty(String name, boolean defaultValue) { 539 String value = getProperty(name); 540 if (value != null) { 541 return Boolean.valueOf(getProperty(name)).booleanValue(); 542 } 543 else { 544 return defaultValue; 545 } 546 } 547 548 557 public static List<String > getPropertyNames(String parent) { 558 if (properties == null) { 559 if (isSetupMode()) { 560 return new ArrayList<String >(); 561 } 562 properties = JiveProperties.getInstance(); 563 } 564 return new ArrayList<String >(properties.getChildrenNames(parent)); 565 } 566 567 577 public static List<String > getProperties(String parent) { 578 if (properties == null) { 579 if (isSetupMode()) { 580 return new ArrayList<String >(); 581 } 582 properties = JiveProperties.getInstance(); 583 } 584 585 Collection<String > propertyNames = properties.getChildrenNames(parent); 586 List<String > values = new ArrayList<String >(); 587 for (Iterator i=propertyNames.iterator(); i.hasNext(); ) { 588 String propName = (String )i.next(); 589 String value = getProperty(propName); 590 if (value != null) { 591 values.add(value); 592 } 593 } 594 595 return values; 596 } 597 598 603 public static List<String > getPropertyNames() { 604 if (properties == null) { 605 if (isSetupMode()) { 606 return new ArrayList<String >(); 607 } 608 properties = JiveProperties.getInstance(); 609 } 610 return new ArrayList<String >(properties.getPropertyNames()); 611 } 612 613 620 public static void setProperty(String name, String value) { 621 if (properties == null) { 622 if (isSetupMode()) { 623 return; 624 } 625 properties = JiveProperties.getInstance(); 626 } 627 properties.put(name, value); 628 } 629 630 636 public static void setProperties(Map propertyMap) { 637 if (properties == null) { 638 if (isSetupMode()) { 639 return; 640 } 641 properties = JiveProperties.getInstance(); 642 } 643 644 properties.putAll(propertyMap); 645 } 646 647 653 public static void deleteProperty(String name) { 654 if (properties == null) { 655 if (isSetupMode()) { 656 return; 657 } 658 properties = JiveProperties.getInstance();; 659 } 660 properties.remove(name); 661 } 662 663 669 public static void setConfigName(String configName) { 670 JIVE_CONFIG_FILENAME = configName; 671 } 672 673 678 static String getConfigName() { 679 if (JIVE_CONFIG_FILENAME == null) { 680 JIVE_CONFIG_FILENAME = "jive-messenger.xml"; 681 }; 682 return JIVE_CONFIG_FILENAME; 683 } 684 685 690 private static boolean isSetupMode() { 691 return !(Boolean.valueOf(JiveGlobals.getXMLProperty("setup")).booleanValue()); 692 } 693 694 698 private synchronized static void loadSetupProperties() { 699 if (failedLoading) { 700 return; 701 } 702 if (xmlProperties == null) { 703 if (home == null) { 707 home = new InitPropLoader().getHome(); 708 } 709 if (home == null) { 711 try { 712 InitialContext context = new InitialContext (); 713 home = (String )context.lookup("java:comp/env/home"); 714 } 715 catch (Exception e) { } 716 } 717 if (home == null) { 719 home = System.getProperty("home"); 720 } 721 722 if(home == null){ 723 try { 724 home = new File ("..").getCanonicalPath(); 725 if(!new File (home, "conf/" + getConfigName()).exists()){ 726 home = null; 727 } 728 } 729 catch (IOException e) { 730 e.printStackTrace(); 731 } 732 } 733 734 if(home == null){ 735 try { 736 home = new File ("").getCanonicalPath(); 737 if(!new File (home, "conf/" + getConfigName()).exists()){ 738 home = null; 739 } 740 } 741 catch (IOException e) { 742 e.printStackTrace(); 743 } 744 } 745 746 if (home == null) { 748 failedLoading = true; 749 StringBuilder msg = new StringBuilder (); 750 msg.append("Critical Error! The home directory could not be loaded, \n"); 751 msg.append("which will prevent the application from working correctly.\n\n"); 752 msg.append("You must set home in one of four ways:\n"); 753 msg.append(" 1) Add a messenger_init.xml file to your classpath, which points \n "); 754 msg.append(" to home.\n"); 755 msg.append(" 3) Set the JNDI value \"java:comp/env/home\" with a String \n"); 756 msg.append(" that points to your home directory. \n"); 757 msg.append(" 4) Set the Java system property \"home\".\n\n"); 758 msg.append("Further instructions for setting home can be found in the \n"); 759 msg.append("installation documentation."); 760 System.err.println(msg.toString()); 761 return; 762 } 763 try { 765 File mh = new File (home); 767 if (!mh.exists()) { 768 Log.error("Error - the specified home directory does not exist (" + home + ")"); 769 } 770 else { 771 if (!mh.canRead() || !mh.canWrite()) { 772 Log.error("Error - the user running this application can not read " + 773 "and write to the specified home directory (" + home + "). " + 774 "Please grant the executing user read and write permissions."); 775 } 776 } 777 xmlProperties = new XMLProperties(home + File.separator + "conf" + 778 File.separator + getConfigName()); 779 } 780 catch (IOException ioe) { 781 Log.error(ioe); 782 failedLoading = true; 783 return; 784 } 785 } 786 } 787 } 788 789 794 class InitPropLoader { 795 796 public String getHome() { 797 String home = null; 798 InputStream in = null; 799 try { 800 in = getClass().getResourceAsStream("/messenger_init.xml"); 801 if (in != null) { 802 SAXReader reader = new SAXReader(); 803 Document doc = reader.read(in); 804 home = doc.getRootElement().getText(); 805 } 806 } 807 catch (Exception e) { 808 Log.error("Error loading messenger_init.xml to find home.", e); 809 } 810 finally { 811 try { if (in != null) { in.close(); } } 812 catch (Exception e) { } 813 } 814 if (home != null) { 815 home = home.trim(); 816 while (home.endsWith("/") || home.endsWith("\\")) { 818 home = home.substring(0, home.length() - 1); 819 } 820 } 821 if ("".equals(home)) { 822 home = null; 823 } 824 return home; 825 } 826 } | Popular Tags |