< prev index next >

src/java.logging/share/classes/sun/util/logging/internal/LoggingProviderImpl.java

Print this page




  15  * accompanied this code).
  16  *
  17  * You should have received a copy of the GNU General Public License version
  18  * 2 along with this work; if not, write to the Free Software Foundation,
  19  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  20  *
  21  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  22  * or visit www.oracle.com if you need additional information or have any
  23  * questions.
  24  */
  25 
  26 
  27 package sun.util.logging.internal;
  28 
  29 import java.security.AccessController;
  30 import java.security.PrivilegedAction;
  31 import java.util.ResourceBundle;
  32 import java.util.function.Supplier;
  33 import java.lang.System.LoggerFinder;
  34 import java.lang.System.Logger;

  35 import java.util.Objects;
  36 import java.util.logging.LogManager;
  37 import jdk.internal.logger.DefaultLoggerFinder;
  38 import java.util.logging.LoggingPermission;
  39 import sun.util.logging.PlatformLogger;
  40 import sun.util.logging.PlatformLogger.ConfigurableBridge.LoggerConfiguration;
  41 
  42 /**
  43  * This {@code LoggingProviderImpl} is the JDK internal implementation of the
  44  * {@link jdk.internal.logger.DefaultLoggerFinder} which is used by
  45  * the default implementation of the {@link Logger}
  46  * when no {@link LoggerFinder} is found
  47  * and {@code java.util.logging} is present.
  48  * When {@code java.util.logging} is present, the {@code LoggingProviderImpl}
  49  * is {@linkplain java.util.ServiceLoader#loadInstalled(Class) installed} as
  50  * an internal service provider, making it possible to use {@code java.util.logging}
  51  * as the backend for loggers returned by the default LoggerFinder implementation.
  52  * <p>
  53  * This implementation of {@link DefaultLoggerFinder} returns instances of
  54  * {@link java.lang.System.Logger} which


 381         //            assert found != null;
 382         //            return found;
 383         //        }
 384         //
 385         // But given that it may end up eating more memory in the nominal case
 386         // (where each class that does logging has its own logger with the
 387         //  class name as logger name and stashes that logger away in a static
 388         //  field, thus making the cache redundant - as only one wrapper will
 389         //  ever be created anyway) - then we will simply return a new wrapper
 390         // for each invocation of JULWrapper.of(...) - which may
 391         // still prove more efficient in terms of memory consumption...
 392         //
 393         static JULWrapper of(java.util.logging.Logger logger) {
 394             return new JULWrapper(logger);
 395         }
 396 
 397 
 398     }
 399 
 400     /**
 401      * Creates a java.util.logging.Logger for the given caller.
 402      * @param name the logger name.
 403      * @param caller the caller for which the logger should be created.
 404      * @return a Logger suitable for use in the given caller.
 405      */
 406     private static java.util.logging.Logger demandJULLoggerFor(final String name,
 407                                                             /* Module */
 408                                                             final Class<?> caller) {
 409         final LogManager manager = LogManager.getLogManager();
 410         final SecurityManager sm = System.getSecurityManager();
 411         if (sm == null) {
 412             return logManagerAccess.demandLoggerFor(manager, name, caller);
 413         } else {
 414             final PrivilegedAction<java.util.logging.Logger> pa =
 415                     () -> logManagerAccess.demandLoggerFor(manager, name, caller);
 416             return AccessController.doPrivileged(pa, null, LOGGING_CONTROL_PERMISSION);
 417         }
 418     }
 419 
 420     /**
 421      * {@inheritDoc}
 422      *
 423      * @apiNote The logger returned by this method can be configured through
 424      * its {@linkplain java.util.logging.LogManager#getLogger(String)
 425      * corresponding java.util.logging.Logger backend}.
 426      *
 427      * @return {@inheritDoc}
 428      * @throws SecurityException if the calling code doesn't have the
 429      * {@code RuntimePermission("loggerFinder")}.
 430      */
 431     @Override
 432     protected Logger demandLoggerFor(String name, /* Module */ Class<?> caller) {
 433         final SecurityManager sm = System.getSecurityManager();
 434         if (sm != null) {
 435             sm.checkPermission(LOGGERFINDER_PERMISSION);
 436         }
 437         return JULWrapper.of(demandJULLoggerFor(name,caller));
 438     }
 439 
 440     public static interface LogManagerAccess {
 441         java.util.logging.Logger demandLoggerFor(LogManager manager,
 442                 String name, /* Module */ Class<?> caller);
 443     }
 444 
 445     // Hook for tests
 446     public static LogManagerAccess getLogManagerAccess() {
 447         final SecurityManager sm = System.getSecurityManager();
 448         if (sm != null) {
 449             sm.checkPermission(LOGGING_CONTROL_PERMISSION);
 450         }
 451         // Triggers initialization of accessJulLogger if not set.
 452         if (logManagerAccess == null) LogManager.getLogManager();
 453         return logManagerAccess;
 454     }
 455 
 456 
 457     private static volatile LogManagerAccess logManagerAccess;
 458     public static void setLogManagerAccess(LogManagerAccess accesLoggers) {
 459         final SecurityManager sm = System.getSecurityManager();
 460         if (sm != null) {
 461             sm.checkPermission(LOGGING_CONTROL_PERMISSION);
 462         }


  15  * accompanied this code).
  16  *
  17  * You should have received a copy of the GNU General Public License version
  18  * 2 along with this work; if not, write to the Free Software Foundation,
  19  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  20  *
  21  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  22  * or visit www.oracle.com if you need additional information or have any
  23  * questions.
  24  */
  25 
  26 
  27 package sun.util.logging.internal;
  28 
  29 import java.security.AccessController;
  30 import java.security.PrivilegedAction;
  31 import java.util.ResourceBundle;
  32 import java.util.function.Supplier;
  33 import java.lang.System.LoggerFinder;
  34 import java.lang.System.Logger;
  35 import java.lang.reflect.Module;
  36 import java.util.Objects;
  37 import java.util.logging.LogManager;
  38 import jdk.internal.logger.DefaultLoggerFinder;
  39 import java.util.logging.LoggingPermission;
  40 import sun.util.logging.PlatformLogger;
  41 import sun.util.logging.PlatformLogger.ConfigurableBridge.LoggerConfiguration;
  42 
  43 /**
  44  * This {@code LoggingProviderImpl} is the JDK internal implementation of the
  45  * {@link jdk.internal.logger.DefaultLoggerFinder} which is used by
  46  * the default implementation of the {@link Logger}
  47  * when no {@link LoggerFinder} is found
  48  * and {@code java.util.logging} is present.
  49  * When {@code java.util.logging} is present, the {@code LoggingProviderImpl}
  50  * is {@linkplain java.util.ServiceLoader#loadInstalled(Class) installed} as
  51  * an internal service provider, making it possible to use {@code java.util.logging}
  52  * as the backend for loggers returned by the default LoggerFinder implementation.
  53  * <p>
  54  * This implementation of {@link DefaultLoggerFinder} returns instances of
  55  * {@link java.lang.System.Logger} which


 382         //            assert found != null;
 383         //            return found;
 384         //        }
 385         //
 386         // But given that it may end up eating more memory in the nominal case
 387         // (where each class that does logging has its own logger with the
 388         //  class name as logger name and stashes that logger away in a static
 389         //  field, thus making the cache redundant - as only one wrapper will
 390         //  ever be created anyway) - then we will simply return a new wrapper
 391         // for each invocation of JULWrapper.of(...) - which may
 392         // still prove more efficient in terms of memory consumption...
 393         //
 394         static JULWrapper of(java.util.logging.Logger logger) {
 395             return new JULWrapper(logger);
 396         }
 397 
 398 
 399     }
 400 
 401     /**
 402      * Creates a java.util.logging.Logger for the given module.
 403      * @param name the logger name.
 404      * @param module the module for which the logger should be created.
 405      * @return a Logger suitable for use in the given module.
 406      */
 407     private static java.util.logging.Logger demandJULLoggerFor(final String name,
 408                                                                Module module) {

 409         final LogManager manager = LogManager.getLogManager();
 410         final SecurityManager sm = System.getSecurityManager();
 411         if (sm == null) {
 412             return logManagerAccess.demandLoggerFor(manager, name, module);
 413         } else {
 414             final PrivilegedAction<java.util.logging.Logger> pa =
 415                     () -> logManagerAccess.demandLoggerFor(manager, name, module);
 416             return AccessController.doPrivileged(pa, null, LOGGING_CONTROL_PERMISSION);
 417         }
 418     }
 419 
 420     /**
 421      * {@inheritDoc}
 422      *
 423      * @apiNote The logger returned by this method can be configured through
 424      * its {@linkplain java.util.logging.LogManager#getLogger(String)
 425      * corresponding java.util.logging.Logger backend}.
 426      *
 427      * @return {@inheritDoc}
 428      * @throws SecurityException if the calling code doesn't have the
 429      * {@code RuntimePermission("loggerFinder")}.
 430      */
 431     @Override
 432     protected Logger demandLoggerFor(String name, Module module) {
 433         final SecurityManager sm = System.getSecurityManager();
 434         if (sm != null) {
 435             sm.checkPermission(LOGGERFINDER_PERMISSION);
 436         }
 437         return JULWrapper.of(demandJULLoggerFor(name,module));
 438     }
 439 
 440     public static interface LogManagerAccess {
 441         java.util.logging.Logger demandLoggerFor(LogManager manager,
 442                 String name, Module module);
 443     }
 444 
 445     // Hook for tests
 446     public static LogManagerAccess getLogManagerAccess() {
 447         final SecurityManager sm = System.getSecurityManager();
 448         if (sm != null) {
 449             sm.checkPermission(LOGGING_CONTROL_PERMISSION);
 450         }
 451         // Triggers initialization of accessJulLogger if not set.
 452         if (logManagerAccess == null) LogManager.getLogManager();
 453         return logManagerAccess;
 454     }
 455 
 456 
 457     private static volatile LogManagerAccess logManagerAccess;
 458     public static void setLogManagerAccess(LogManagerAccess accesLoggers) {
 459         final SecurityManager sm = System.getSecurityManager();
 460         if (sm != null) {
 461             sm.checkPermission(LOGGING_CONTROL_PERMISSION);
 462         }
< prev index next >