1 7 8 9 package java.util.logging; 10 11 import java.io.*; 12 import java.util.*; 13 import java.security.*; 14 import java.beans.PropertyChangeListener ; 15 import java.beans.PropertyChangeSupport ; 16 import java.net.URL ; 17 import sun.security.action.GetPropertyAction; 18 19 128 129 public class LogManager { 130 private static LogManager manager; 132 133 private final static Handler [] emptyHandlers = { }; 134 private Properties props = new Properties(); 135 private PropertyChangeSupport changes 136 = new PropertyChangeSupport (LogManager .class); 137 private final static Level defaultLevel = Level.INFO; 138 139 private Hashtable<String ,Logger > loggers = new Hashtable<String ,Logger >(); 141 private LogNode root = new LogNode(null); 143 private Logger rootLogger; 144 145 private volatile boolean readPrimordialConfiguration; 149 private boolean initializedGlobalHandlers = true; 152 private boolean deathImminent; 154 155 static { 156 AccessController.doPrivileged(new PrivilegedAction() { 157 public Object run() { 158 String cname = null; 159 try { 160 cname = System.getProperty("java.util.logging.manager"); 161 if (cname != null) { 162 try { 163 Class clz = ClassLoader.getSystemClassLoader().loadClass(cname); 164 manager = (LogManager ) clz.newInstance(); 165 } catch (ClassNotFoundException ex) { 166 Class clz = Thread.currentThread().getContextClassLoader().loadClass(cname); 167 manager = (LogManager ) clz.newInstance(); 168 } 169 } 170 } catch (Exception ex) { 171 System.err.println("Could not load Logmanager \"" + cname + "\""); 172 ex.printStackTrace(); 173 } 174 if (manager == null) { 175 manager = new LogManager (); 176 } 177 178 manager.rootLogger = manager.new RootLogger(); 180 manager.addLogger(manager.rootLogger); 181 182 Logger.global.setLogManager(manager); 185 manager.addLogger(Logger.global); 186 187 return null; 191 } 192 }); 193 } 194 195 196 private class Cleaner extends Thread { 199 public void run() { 200 LogManager mgr = manager; 203 204 synchronized (LogManager.this) { 207 deathImminent = true; 209 initializedGlobalHandlers = true; 210 } 211 212 reset(); 214 } 215 } 216 217 218 224 protected LogManager() { 225 try { 227 Runtime.getRuntime().addShutdownHook(new Cleaner()); 228 } catch (IllegalStateException e) { 229 } 232 } 233 234 237 public static LogManager getLogManager() { 238 if (manager != null) { 239 manager.readPrimordialConfiguration(); 240 } 241 return manager; 242 } 243 244 private void readPrimordialConfiguration() { 245 if (!readPrimordialConfiguration) { 246 synchronized (this) { 247 if (!readPrimordialConfiguration) { 248 if (System.out == null) { 252 return; 253 } 254 readPrimordialConfiguration = true; 255 try { 256 AccessController.doPrivileged(new PrivilegedExceptionAction() { 257 public Object run() throws Exception { 258 readConfiguration(); 259 return null; 260 } 261 }); 262 } catch (Exception ex) { 263 } 266 } 267 } 268 } 269 } 270 271 282 public void addPropertyChangeListener(PropertyChangeListener l) throws SecurityException { 283 if (l == null) { 284 throw new NullPointerException (); 285 } 286 checkAccess(); 287 changes.addPropertyChangeListener(l); 288 } 289 290 304 public void removePropertyChangeListener(PropertyChangeListener l) throws SecurityException { 305 checkAccess(); 306 changes.removePropertyChangeListener(l); 307 } 308 309 325 public synchronized boolean addLogger(Logger logger) { 326 final String name = logger.getName(); 327 if (name == null) { 328 throw new NullPointerException (); 329 } 330 331 Logger old = loggers.get(name); 332 if (old != null) { 333 return false; 335 } 336 337 loggers.put(name, logger); 341 342 Level level = getLevelProperty(name+".level", null); 344 if (level != null) { 345 doSetLevel(logger, level); 346 } 347 348 if (getProperty(name+".handlers") != null) { 351 AccessController.doPrivileged(new PrivilegedAction() { 353 public Object run() { 354 String names[] = parseClassNames(name+".handlers"); 356 for (int i = 0; i < names.length; i++) { 357 String word = names[i]; 358 try { 359 Class clz = ClassLoader.getSystemClassLoader().loadClass(word); 360 Handler h = (Handler ) clz.newInstance(); 361 try { 362 String levs = getProperty(word + ".level"); 365 if (levs != null) { 366 h.setLevel(Level.parse(levs)); 367 } 368 boolean useParent = getBooleanProperty(name + ".useParentHandlers", true); 369 if (!useParent) { 370 getLogger(name).setUseParentHandlers(false); 371 } 372 } catch (Exception ex) { 373 System.err.println("Can't set level for " + word); 374 } 376 getLogger(name).addHandler(h); 378 } catch (Exception ex) { 379 System.err.println("Can't load log handler \"" + word + "\""); 380 System.err.println("" + ex); 381 ex.printStackTrace(); 382 } 383 } 384 return null; 385 }}); 386 } 388 int ix = 1; 391 for (;;) { 392 int ix2 = name.indexOf(".", ix); 393 if (ix2 < 0) { 394 break; 395 } 396 String pname = name.substring(0,ix2); 397 if (getProperty(pname+".level") != null) { 398 Logger plogger = Logger.getLogger(pname); 400 } 401 if (getProperty(pname+".handlers") != null) { 405 final String nname=pname; 406 407 AccessController.doPrivileged(new PrivilegedAction() { 408 public Object run() { 409 String names[] = parseClassNames(nname+".handlers"); 410 411 for (int i = 0; i < names.length; i++) { 412 String word = names[i]; 413 try { 414 Class clz = ClassLoader.getSystemClassLoader().loadClass(word); 415 Handler h = (Handler ) clz.newInstance(); 416 try { 417 String levs = getProperty(word + ".level"); 420 if (levs != null) { 421 h.setLevel(Level.parse(levs)); 422 } 423 } catch (Exception ex) { 424 System.err.println("Can't set level for " + word); 425 } 427 if (getLogger(nname) == null ) { 428 Logger nplogger=Logger.getLogger(nname); 429 addLogger(nplogger); 430 } 431 boolean useParent = getBooleanProperty(nname + ".useParentHandlers", true); 432 if (!useParent) { 433 getLogger(nname).setUseParentHandlers(false); 434 } 435 } catch (Exception ex) { 436 System.err.println("Can't load log handler \"" + word + "\""); 437 System.err.println("" + ex); 438 ex.printStackTrace(); 439 } 440 } 441 return null; 442 }}); 443 } 445 ix = ix2+1; 446 } 447 448 LogNode node = findNode(name); 450 node.logger = logger; 451 Logger parent = null; 452 LogNode nodep = node.parent; 453 while (nodep != null) { 454 if (nodep.logger != null) { 455 parent = nodep.logger; 456 break; 457 } 458 nodep = nodep.parent; 459 } 460 461 if (parent != null) { 462 doSetParent(logger, parent); 463 } 464 node.walkAndSetParent(logger); 466 467 return true; 468 } 469 470 471 private static void doSetLevel(final Logger logger, final Level level) { 474 SecurityManager sm = System.getSecurityManager(); 475 if (sm == null) { 476 logger.setLevel(level); 478 return; 479 } 480 AccessController.doPrivileged(new PrivilegedAction() { 483 public Object run() { 484 logger.setLevel(level); 485 return null; 486 }}); 487 } 488 489 490 491 private static void doSetParent(final Logger logger, final Logger parent) { 494 SecurityManager sm = System.getSecurityManager(); 495 if (sm == null) { 496 logger.setParent(parent); 498 return; 499 } 500 AccessController.doPrivileged(new PrivilegedAction() { 503 public Object run() { 504 logger.setParent(parent); 505 return null; 506 }}); 507 } 508 509 private LogNode findNode(String name) { 512 if (name == null || name.equals("")) { 513 return root; 514 } 515 LogNode node = root; 516 while (name.length() > 0) { 517 int ix = name.indexOf("."); 518 String head; 519 if (ix > 0) { 520 head = name.substring(0,ix); 521 name = name.substring(ix+1); 522 } else { 523 head = name; 524 name = ""; 525 } 526 if (node.children == null) { 527 node.children = new HashMap<Object ,Object >(); 528 } 529 LogNode child = (LogNode)node.children.get(head); 530 if (child == null) { 531 child = new LogNode(node); 532 node.children.put(head, child); 533 } 534 node = child; 535 } 536 return node; 537 } 538 539 549 public synchronized Logger getLogger(String name) { 550 return loggers.get(name); 551 } 552 553 561 public synchronized Enumeration<String > getLoggerNames() { 562 return loggers.keys(); 563 } 564 565 581 public void readConfiguration() throws IOException, SecurityException { 582 checkAccess(); 583 584 String cname = System.getProperty("java.util.logging.config.class"); 586 if (cname != null) { 587 try { 588 try { 592 Class clz = ClassLoader.getSystemClassLoader().loadClass(cname); 593 clz.newInstance(); 594 return; 595 } catch (ClassNotFoundException ex) { 596 Class clz = Thread.currentThread().getContextClassLoader().loadClass(cname); 597 clz.newInstance(); 598 return; 599 } 600 } catch (Exception ex) { 601 System.err.println("Logging configuration class \"" + cname + "\" failed"); 602 System.err.println("" + ex); 603 } 605 } 606 607 String fname = System.getProperty("java.util.logging.config.file"); 608 if (fname == null) { 609 fname = System.getProperty("java.home"); 610 if (fname == null) { 611 throw new Error ("Can't find java.home ??"); 612 } 613 File f = new File(fname, "lib"); 614 f = new File(f, "logging.properties"); 615 fname = f.getCanonicalPath(); 616 } 617 InputStream in = new FileInputStream(fname); 618 BufferedInputStream bin = new BufferedInputStream(in); 619 try { 620 readConfiguration(bin); 621 } finally { 622 if (in != null) { 623 in.close(); 624 } 625 } 626 } 627 628 638 639 public void reset() throws SecurityException { 640 checkAccess(); 641 synchronized (this) { 642 props = new Properties(); 643 initializedGlobalHandlers = true; 646 } 647 Enumeration enum_ = getLoggerNames(); 648 while (enum_.hasMoreElements()) { 649 String name = (String )enum_.nextElement(); 650 resetLogger(name); 651 } 652 } 653 654 655 private void resetLogger(String name) { 657 Logger logger = getLogger(name); 658 if (logger == null) { 659 return; 660 } 661 Handler [] targets = logger.getHandlers(); 663 for (int i = 0; i < targets.length; i++) { 664 Handler h = targets[i]; 665 logger.removeHandler(h); 666 try { 667 h.close(); 668 } catch (Exception ex) { 669 } 671 } 672 if (name != null && name.equals("")) { 673 logger.setLevel(defaultLevel); 675 } else { 676 logger.setLevel(null); 677 } 678 } 679 680 private String [] parseClassNames(String propertyName) { 682 String hands = getProperty(propertyName); 683 if (hands == null) { 684 return new String [0]; 685 } 686 hands = hands.trim(); 687 int ix = 0; 688 Vector<String > result = new Vector<String >(); 689 while (ix < hands.length()) { 690 int end = ix; 691 while (end < hands.length()) { 692 if (Character.isWhitespace(hands.charAt(end))) { 693 break; 694 } 695 if (hands.charAt(end) == ',') { 696 break; 697 } 698 end++; 699 } 700 String word = hands.substring(ix, end); 701 ix = end+1; 702 word = word.trim(); 703 if (word.length() == 0) { 704 continue; 705 } 706 result.add(word); 707 } 708 return result.toArray(new String [result.size()]); 709 } 710 711 724 public void readConfiguration(InputStream ins) throws IOException, SecurityException { 725 checkAccess(); 726 reset(); 727 728 props.load(ins); 730 String names[] = parseClassNames("config"); 732 733 for (int i = 0; i < names.length; i++) { 734 String word = names[i]; 735 try { 736 Class clz = ClassLoader.getSystemClassLoader().loadClass(word); 737 clz.newInstance(); 738 } catch (Exception ex) { 739 System.err.println("Can't load config class \"" + word + "\""); 740 System.err.println("" + ex); 741 } 743 } 744 745 setLevelsOnExistingLoggers(); 747 748 changes.firePropertyChange(null, null, null); 750 751 synchronized (this) { 754 initializedGlobalHandlers = false; 755 } 756 } 757 758 764 public String getProperty(String name) { 765 return props.getProperty(name); 766 } 767 768 String getStringProperty(String name, String defaultValue) { 772 String val = getProperty(name); 773 if (val == null) { 774 return defaultValue; 775 } 776 return val.trim(); 777 } 778 779 int getIntProperty(String name, int defaultValue) { 783 String val = getProperty(name); 784 if (val == null) { 785 return defaultValue; 786 } 787 try { 788 return Integer.parseInt(val.trim()); 789 } catch (Exception ex) { 790 return defaultValue; 791 } 792 } 793 794 boolean getBooleanProperty(String name, boolean defaultValue) { 798 String val = getProperty(name); 799 if (val == null) { 800 return defaultValue; 801 } 802 val = val.toLowerCase(); 803 if (val.equals("true") || val.equals("1")) { 804 return true; 805 } else if (val.equals("false") || val.equals("0")) { 806 return false; 807 } 808 return defaultValue; 809 } 810 811 Level getLevelProperty(String name, Level defaultValue) { 815 String val = getProperty(name); 816 if (val == null) { 817 return defaultValue; 818 } 819 try { 820 return Level.parse(val.trim()); 821 } catch (Exception ex) { 822 return defaultValue; 823 } 824 } 825 826 Filter getFilterProperty(String name, Filter defaultValue) { 831 String val = getProperty(name); 832 try { 833 if (val != null) { 834 Class clz = ClassLoader.getSystemClassLoader().loadClass(val); 835 return (Filter ) clz.newInstance(); 836 } 837 } catch (Exception ex) { 838 } 842 return defaultValue; 844 } 845 846 847 Formatter getFormatterProperty(String name, Formatter defaultValue) { 852 String val = getProperty(name); 853 try { 854 if (val != null) { 855 Class clz = ClassLoader.getSystemClassLoader().loadClass(val); 856 return (Formatter ) clz.newInstance(); 857 } 858 } catch (Exception ex) { 859 } 863 return defaultValue; 865 } 866 867 private synchronized void initializeGlobalHandlers() { 871 if (initializedGlobalHandlers) { 872 return; 873 } 874 875 initializedGlobalHandlers = true; 876 877 if (deathImminent) { 878 return; 882 } 883 884 AccessController.doPrivileged(new PrivilegedAction() { 888 public Object run() { 889 String names[] = parseClassNames("handlers"); 891 for (int i = 0; i < names.length; i++) { 892 String word = names[i]; 893 try { 894 Class clz = ClassLoader.getSystemClassLoader().loadClass(word); 895 Handler h = (Handler ) clz.newInstance(); 896 try { 897 String levs = getProperty(word + ".level"); 900 if (levs != null) { 901 h.setLevel(Level.parse(levs)); 902 } 903 } catch (Exception ex) { 904 System.err.println("Can't set level for " + word); 905 } 907 rootLogger.addHandler(h); 908 } catch (Exception ex) { 909 System.err.println("Can't load log handler \"" + word + "\""); 910 System.err.println("" + ex); 911 ex.printStackTrace(); 912 } 913 } 914 return null; 915 }}); 916 } 917 918 919 private Permission ourPermission = new LoggingPermission ("control", null); 920 921 931 public void checkAccess() throws SecurityException { 932 SecurityManager sm = System.getSecurityManager(); 933 if (sm == null) { 934 return; 935 } 936 sm.checkPermission(ourPermission); 937 } 938 939 private static class LogNode { 941 HashMap<Object ,Object > children; 942 Logger logger; 943 LogNode parent; 944 945 LogNode(LogNode parent) { 946 this.parent = parent; 947 } 948 949 void walkAndSetParent(Logger parent) { 952 if (children == null) { 953 return; 954 } 955 Iterator values = children.values().iterator(); 956 while (values.hasNext()) { 957 LogNode node = (LogNode) values.next(); 958 if (node.logger == null) { 959 node.walkAndSetParent(parent); 960 } else { 961 doSetParent(node.logger, parent); 962 } 963 } 964 } 965 } 966 967 private class RootLogger extends Logger { 971 972 private RootLogger() { 973 super("", null); 974 setLevel(defaultLevel); 975 } 976 977 public void log(LogRecord record) { 978 initializeGlobalHandlers(); 980 super.log(record); 981 } 982 983 public void addHandler(Handler h) { 984 initializeGlobalHandlers(); 985 super.addHandler(h); 986 } 987 988 public void removeHandler(Handler h) { 989 initializeGlobalHandlers(); 990 super.removeHandler(h); 991 } 992 993 public Handler [] getHandlers() { 994 initializeGlobalHandlers(); 995 return super.getHandlers(); 996 } 997 } 998 999 1000 synchronized private void setLevelsOnExistingLoggers() { 1003 Enumeration enum_ = props.propertyNames(); 1004 while (enum_.hasMoreElements()) { 1005 String key = (String )enum_.nextElement(); 1006 if (!key.endsWith(".level")) { 1007 continue; 1009 } 1010 int ix = key.length() - 6; 1011 String name = key.substring(0, ix); 1012 Level level = getLevelProperty(key, null); 1013 if (level == null) { 1014 System.err.println("Bad level value for property: " + key); 1015 continue; 1016 } 1017 Logger l = getLogger(name); 1018 if (l == null) { 1019 continue; 1020 } 1021 l.setLevel(level); 1022 } 1023 } 1024 1025 private static LoggingMXBean loggingMXBean = null; 1027 1031 public final static String LOGGING_MXBEAN_NAME 1032 = "java.util.logging:type=Logging"; 1033 1034 1044 public static synchronized LoggingMXBean getLoggingMXBean() { 1045 if (loggingMXBean == null) { 1046 loggingMXBean = new Logging (); 1047 } 1048 return loggingMXBean; 1049 } 1050 1051} 1052 | Popular Tags |