422 public static LogManager getLogManager() {
423 if (manager != null) {
424 manager.ensureLogManagerInitialized();
425 }
426 return manager;
427 }
428
429 private void readPrimordialConfiguration() { // must be called while holding configurationLock
430 if (!readPrimordialConfiguration) {
431 // If System.in/out/err are null, it's a good
432 // indication that we're still in the
433 // bootstrapping phase
434 if (System.out == null) {
435 return;
436 }
437 readPrimordialConfiguration = true;
438 try {
439 readConfiguration();
440
441 // Platform loggers begin to delegate to java.util.logging.Logger
442 sun.util.logging.PlatformLogger.redirectPlatformLoggers();
443 } catch (Exception ex) {
444 assert false : "Exception raised while reading logging configuration: " + ex;
445 }
446 }
447 }
448
449 // LoggerContext maps from AppContext
450 private WeakHashMap<Object, LoggerContext> contextsMap = null;
451
452 // Returns the LoggerContext for the user code (i.e. application or AppContext).
453 // Loggers are isolated from each AppContext.
454 private LoggerContext getUserContext() {
455 LoggerContext context = null;
456
457 SecurityManager sm = System.getSecurityManager();
458 JavaAWTAccess javaAwtAccess = SharedSecrets.getJavaAWTAccess();
459 if (sm != null && javaAwtAccess != null) {
460 // for each applet, it has its own LoggerContext isolated from others
461 final Object ecx = javaAwtAccess.getAppletContext();
462 if (ecx != null) {
1650 // So in either case we need to wait for the lock.
1651 configurationLock.lock();
1652 try {
1653 if (globalHandlersState != STATE_UNINITIALIZED) {
1654 return; // recursive call or nothing to do
1655 }
1656 // set globalHandlersState to STATE_INITIALIZING first to avoid
1657 // getting an infinite recursion when loadLoggerHandlers(...)
1658 // is going to call addHandler(...)
1659 globalHandlersState = STATE_INITIALIZING;
1660 try {
1661 loadLoggerHandlers(rootLogger, null, "handlers");
1662 } finally {
1663 globalHandlersState = STATE_INITIALIZED;
1664 }
1665 } finally {
1666 configurationLock.unlock();
1667 }
1668 }
1669
1670 static final Permission controlPermission = new LoggingPermission("control", null);
1671
1672 void checkPermission() {
1673 SecurityManager sm = System.getSecurityManager();
1674 if (sm != null)
1675 sm.checkPermission(controlPermission);
1676 }
1677
1678 /**
1679 * Check that the current context is trusted to modify the logging
1680 * configuration. This requires LoggingPermission("control").
1681 * <p>
1682 * If the check fails we throw a SecurityException, otherwise
1683 * we return normally.
1684 *
1685 * @exception SecurityException if a security manager exists and if
1686 * the caller does not have LoggingPermission("control").
1687 */
1688 public void checkAccess() throws SecurityException {
1689 checkPermission();
1690 }
1894 // So we're taking a snapshot of the values list to avoid the risk of
1895 // ConcurrentModificationException while looping.
1896 //
1897 for (Runnable c : listeners.values().toArray(new Runnable[0])) {
1898 try {
1899 c.run();
1900 } catch (ThreadDeath death) {
1901 throw death;
1902 } catch (Error | RuntimeException x) {
1903 if (t == null) t = x;
1904 else t.addSuppressed(x);
1905 }
1906 }
1907 // Listeners are not supposed to throw exceptions, but if that
1908 // happens, we will rethrow the first error or exception that is raised
1909 // after all listeners have been invoked.
1910 if (t instanceof Error) throw (Error)t;
1911 if (t instanceof RuntimeException) throw (RuntimeException)t;
1912 }
1913
1914 }
|
422 public static LogManager getLogManager() {
423 if (manager != null) {
424 manager.ensureLogManagerInitialized();
425 }
426 return manager;
427 }
428
429 private void readPrimordialConfiguration() { // must be called while holding configurationLock
430 if (!readPrimordialConfiguration) {
431 // If System.in/out/err are null, it's a good
432 // indication that we're still in the
433 // bootstrapping phase
434 if (System.out == null) {
435 return;
436 }
437 readPrimordialConfiguration = true;
438 try {
439 readConfiguration();
440
441 // Platform loggers begin to delegate to java.util.logging.Logger
442 sun.util.logger.BootstrapLogger.redirectTemporaryLoggers();
443
444 } catch (Exception ex) {
445 assert false : "Exception raised while reading logging configuration: " + ex;
446 }
447 }
448 }
449
450 // LoggerContext maps from AppContext
451 private WeakHashMap<Object, LoggerContext> contextsMap = null;
452
453 // Returns the LoggerContext for the user code (i.e. application or AppContext).
454 // Loggers are isolated from each AppContext.
455 private LoggerContext getUserContext() {
456 LoggerContext context = null;
457
458 SecurityManager sm = System.getSecurityManager();
459 JavaAWTAccess javaAwtAccess = SharedSecrets.getJavaAWTAccess();
460 if (sm != null && javaAwtAccess != null) {
461 // for each applet, it has its own LoggerContext isolated from others
462 final Object ecx = javaAwtAccess.getAppletContext();
463 if (ecx != null) {
1651 // So in either case we need to wait for the lock.
1652 configurationLock.lock();
1653 try {
1654 if (globalHandlersState != STATE_UNINITIALIZED) {
1655 return; // recursive call or nothing to do
1656 }
1657 // set globalHandlersState to STATE_INITIALIZING first to avoid
1658 // getting an infinite recursion when loadLoggerHandlers(...)
1659 // is going to call addHandler(...)
1660 globalHandlersState = STATE_INITIALIZING;
1661 try {
1662 loadLoggerHandlers(rootLogger, null, "handlers");
1663 } finally {
1664 globalHandlersState = STATE_INITIALIZED;
1665 }
1666 } finally {
1667 configurationLock.unlock();
1668 }
1669 }
1670
1671 static final Permission controlPermission =
1672 new LoggingPermission("control", null);
1673 static final Permission demandLoggerPermission =
1674 new LoggingPermission("demandLogger", null);
1675
1676 void checkPermission() {
1677 SecurityManager sm = System.getSecurityManager();
1678 if (sm != null)
1679 sm.checkPermission(controlPermission);
1680 }
1681
1682 /**
1683 * Check that the current context is trusted to modify the logging
1684 * configuration. This requires LoggingPermission("control").
1685 * <p>
1686 * If the check fails we throw a SecurityException, otherwise
1687 * we return normally.
1688 *
1689 * @exception SecurityException if a security manager exists and if
1690 * the caller does not have LoggingPermission("control").
1691 */
1692 public void checkAccess() throws SecurityException {
1693 checkPermission();
1694 }
1898 // So we're taking a snapshot of the values list to avoid the risk of
1899 // ConcurrentModificationException while looping.
1900 //
1901 for (Runnable c : listeners.values().toArray(new Runnable[0])) {
1902 try {
1903 c.run();
1904 } catch (ThreadDeath death) {
1905 throw death;
1906 } catch (Error | RuntimeException x) {
1907 if (t == null) t = x;
1908 else t.addSuppressed(x);
1909 }
1910 }
1911 // Listeners are not supposed to throw exceptions, but if that
1912 // happens, we will rethrow the first error or exception that is raised
1913 // after all listeners have been invoked.
1914 if (t instanceof Error) throw (Error)t;
1915 if (t instanceof RuntimeException) throw (RuntimeException)t;
1916 }
1917
1918 /**
1919 * Demands a logger suitable for given caller.
1920 * <p>
1921 * If a named logger suitable for the given caller is found
1922 * returns it.
1923 * Otherwise, creates a new logger suitable for the given caller.
1924 *
1925 * @param name The logger name.
1926 * @param caller The caller on which behalf the logger is created/retrieved.
1927 * @return A logger for the given caller.
1928 *
1929 * @throws SecurityException if the calling code doesn't have the
1930 * {@link LoggingPermission LoggingPermission("demandLogger", null)}.
1931 * @since 9
1932 */
1933 public static Logger demandLoggerFor(String name, /* Module */ Class<?> caller) {
1934 SecurityManager sm = System.getSecurityManager();
1935 if (sm != null) {
1936 sm.checkPermission(demandLoggerPermission);
1937 }
1938 if (caller.getClassLoader() == null) {
1939 return LogManager.getLogManager().demandSystemLogger(name,
1940 Logger.SYSTEM_LOGGER_RB_NAME, caller);
1941 } else {
1942 return LogManager.getLogManager().demandLogger(name, null, caller);
1943 }
1944 }
1945
1946 }
|