< prev index next >
test/jdk/java/lang/System/LoggerFinder/internal/LoggerBridgeTest/LoggerBridgeTest.java
Print this page
*** 1,7 ****
/*
! * Copyright (c) 2015, 2016, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License version 2 only, as
* published by the Free Software Foundation.
--- 1,7 ----
/*
! * Copyright (c) 2015, 2018, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License version 2 only, as
* published by the Free Software Foundation.
*** 21,37 ****
* questions.
*/
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.security.AccessControlException;
- import java.security.AccessController;
import java.security.CodeSource;
import java.security.Permission;
import java.security.PermissionCollection;
import java.security.Permissions;
import java.security.Policy;
- import java.security.PrivilegedAction;
import java.security.ProtectionDomain;
import java.util.Arrays;
import java.util.Collections;
import java.util.Enumeration;
import java.util.HashMap;
--- 21,35 ----
*** 46,56 ****
import java.util.function.Supplier;
import java.util.logging.Handler;
import java.util.logging.LogRecord;
import java.lang.System.LoggerFinder;
import java.lang.System.Logger;
- import java.lang.System.Logger.Level;
import java.util.stream.Stream;
import sun.util.logging.PlatformLogger;
/**
* @test
--- 44,53 ----
*** 59,69 ****
* Tests all bridge methods with the a custom backend whose
* loggers implement PlatformLogger.Bridge.
* @modules java.base/sun.util.logging
* java.base/jdk.internal.logger
* java.logging
! * @build CustomSystemClassLoader LoggerBridgeTest
* @run main/othervm -Djava.system.class.loader=CustomSystemClassLoader LoggerBridgeTest NOSECURITY
* @run main/othervm -Djava.system.class.loader=CustomSystemClassLoader LoggerBridgeTest NOPERMISSIONS
* @run main/othervm -Djava.system.class.loader=CustomSystemClassLoader LoggerBridgeTest WITHPERMISSIONS
* @author danielfuchs
*/
--- 56,66 ----
* Tests all bridge methods with the a custom backend whose
* loggers implement PlatformLogger.Bridge.
* @modules java.base/sun.util.logging
* java.base/jdk.internal.logger
* java.logging
! * @build CustomSystemClassLoader LogProducerFinder LoggerBridgeTest
* @run main/othervm -Djava.system.class.loader=CustomSystemClassLoader LoggerBridgeTest NOSECURITY
* @run main/othervm -Djava.system.class.loader=CustomSystemClassLoader LoggerBridgeTest NOPERMISSIONS
* @run main/othervm -Djava.system.class.loader=CustomSystemClassLoader LoggerBridgeTest WITHPERMISSIONS
* @author danielfuchs
*/
*** 229,265 ****
}
static final Class<?> providerClass;
static {
try {
// Preload classes before the security manager is on.
! providerClass = ClassLoader.getSystemClassLoader().loadClass("LoggerBridgeTest$LogProducerFinder");
((LoggerFinder)providerClass.newInstance()).getLogger("foo", providerClass.getModule());
} catch (Exception ex) {
throw new ExceptionInInitializerError(ex);
}
}
! public static class LogProducerFinder extends LoggerFinder {
! final ConcurrentHashMap<String, LoggerImpl> system = new ConcurrentHashMap<>();
! final ConcurrentHashMap<String, LoggerImpl> user = new ConcurrentHashMap<>();
!
! public class LoggerImpl implements Logger, PlatformLogger.Bridge {
private final String name;
! private sun.util.logging.PlatformLogger.Level level = sun.util.logging.PlatformLogger.Level.INFO;
! private sun.util.logging.PlatformLogger.Level OFF = sun.util.logging.PlatformLogger.Level.OFF;
! private sun.util.logging.PlatformLogger.Level FINE = sun.util.logging.PlatformLogger.Level.FINE;
! private sun.util.logging.PlatformLogger.Level FINER = sun.util.logging.PlatformLogger.Level.FINER;
! private sun.util.logging.PlatformLogger.Level FINEST = sun.util.logging.PlatformLogger.Level.FINEST;
! private sun.util.logging.PlatformLogger.Level CONFIG = sun.util.logging.PlatformLogger.Level.CONFIG;
! private sun.util.logging.PlatformLogger.Level INFO = sun.util.logging.PlatformLogger.Level.INFO;
! private sun.util.logging.PlatformLogger.Level WARNING = sun.util.logging.PlatformLogger.Level.WARNING;
! private sun.util.logging.PlatformLogger.Level SEVERE = sun.util.logging.PlatformLogger.Level.SEVERE;
public LoggerImpl(String name) {
this.name = name;
}
@Override
public String getName() {
return name;
}
--- 226,262 ----
}
static final Class<?> providerClass;
static {
try {
// Preload classes before the security manager is on.
! providerClass = ClassLoader.getSystemClassLoader().loadClass("LogProducerFinder");
((LoggerFinder)providerClass.newInstance()).getLogger("foo", providerClass.getModule());
} catch (Exception ex) {
throw new ExceptionInInitializerError(ex);
}
}
! public static class LoggerImpl implements System.Logger, PlatformLogger.Bridge {
private final String name;
! private PlatformLogger.Level level = PlatformLogger.Level.INFO;
! private PlatformLogger.Level OFF = PlatformLogger.Level.OFF;
! private PlatformLogger.Level FINE = PlatformLogger.Level.FINE;
! private PlatformLogger.Level FINER = PlatformLogger.Level.FINER;
! private PlatformLogger.Level FINEST = PlatformLogger.Level.FINEST;
! private PlatformLogger.Level CONFIG = PlatformLogger.Level.CONFIG;
! private PlatformLogger.Level INFO = PlatformLogger.Level.INFO;
! private PlatformLogger.Level WARNING = PlatformLogger.Level.WARNING;
! private PlatformLogger.Level SEVERE = PlatformLogger.Level.SEVERE;
public LoggerImpl(String name) {
this.name = name;
}
+ public void configureLevel(PlatformLogger.Level level) {
+ this.level = level;
+ }
+
@Override
public String getName() {
return name;
}
*** 294,411 ****
Throwable thrown) {
throw new UnsupportedOperationException();
}
@Override
! public void log(sun.util.logging.PlatformLogger.Level level, String msg) {
log(LogEvent.of(isLoggable(level), name, null, null,
! level, null, msg, null, (Object[])null));
}
@Override
! public void log(sun.util.logging.PlatformLogger.Level level,
Supplier<String> msgSupplier) {
log(LogEvent.of(isLoggable(level), name, null, null,
! level, null, msgSupplier, null, (Object[])null));
}
@Override
! public void log(sun.util.logging.PlatformLogger.Level level, String msg,
Object... params) {
log(LogEvent.of(isLoggable(level), name, null, null,
level, null, msg, null, params));
}
@Override
! public void log(sun.util.logging.PlatformLogger.Level level, String msg,
Throwable thrown) {
log(LogEvent.of(isLoggable(level), name, null, null,
! level, null, msg, thrown, (Object[])null));
}
@Override
! public void log(sun.util.logging.PlatformLogger.Level level, Throwable thrown,
Supplier<String> msgSupplier) {
log(LogEvent.of(isLoggable(level), name, null, null,
! level, null, msgSupplier, thrown, (Object[])null));
}
@Override
! public void logp(sun.util.logging.PlatformLogger.Level level, String sourceClass,
String sourceMethod, String msg) {
log(LogEvent.of(isLoggable(level), name,
sourceClass, sourceMethod,
! level, null, msg, null, (Object[])null));
}
@Override
! public void logp(sun.util.logging.PlatformLogger.Level level, String sourceClass,
String sourceMethod, Supplier<String> msgSupplier) {
log(LogEvent.of(isLoggable(level), name,
sourceClass, sourceMethod,
! level, null, msgSupplier, null, (Object[])null));
}
@Override
! public void logp(sun.util.logging.PlatformLogger.Level level, String sourceClass,
String sourceMethod, String msg, Object... params) {
log(LogEvent.of(isLoggable(level), name,
sourceClass, sourceMethod,
level, null, msg, null, params));
}
@Override
! public void logp(sun.util.logging.PlatformLogger.Level level, String sourceClass,
String sourceMethod, String msg, Throwable thrown) {
log(LogEvent.of(isLoggable(level), name,
sourceClass, sourceMethod,
! level, null, msg, thrown, (Object[])null));
}
@Override
! public void logp(sun.util.logging.PlatformLogger.Level level, String sourceClass,
String sourceMethod, Throwable thrown,
Supplier<String> msgSupplier) {
log(LogEvent.of(isLoggable(level), name,
sourceClass, sourceMethod,
! level, null, msgSupplier, thrown, (Object[])null));
}
@Override
! public void logrb(sun.util.logging.PlatformLogger.Level level, String sourceClass,
String sourceMethod, ResourceBundle bundle, String msg,
Object... params) {
log(LogEvent.of(isLoggable(level), name,
sourceClass, sourceMethod,
level, bundle, msg, null, params));
}
@Override
! public void logrb(sun.util.logging.PlatformLogger.Level level, ResourceBundle bundle,
String msg, Object... params) {
log(LogEvent.of(isLoggable(level), name, null, null,
level, bundle, msg, null, params));
}
@Override
! public void logrb(sun.util.logging.PlatformLogger.Level level, String sourceClass,
String sourceMethod, ResourceBundle bundle, String msg,
Throwable thrown) {
log(LogEvent.of(isLoggable(level), name,
sourceClass, sourceMethod,
! level, bundle, msg, thrown, (Object[])null));
}
@Override
! public void logrb(sun.util.logging.PlatformLogger.Level level, ResourceBundle bundle,
String msg, Throwable thrown) {
log(LogEvent.of(isLoggable(level), name, null, null,
! level, bundle, msg, thrown, (Object[])null));
}
@Override
! public boolean isLoggable(sun.util.logging.PlatformLogger.Level level) {
return this.level != OFF && level.intValue()
>= this.level.intValue();
}
@Override
--- 291,408 ----
Throwable thrown) {
throw new UnsupportedOperationException();
}
@Override
! public void log(PlatformLogger.Level level, String msg) {
log(LogEvent.of(isLoggable(level), name, null, null,
! level, null, msg, null, (Object[]) null));
}
@Override
! public void log(PlatformLogger.Level level,
Supplier<String> msgSupplier) {
log(LogEvent.of(isLoggable(level), name, null, null,
! level, null, msgSupplier, null, (Object[]) null));
}
@Override
! public void log(PlatformLogger.Level level, String msg,
Object... params) {
log(LogEvent.of(isLoggable(level), name, null, null,
level, null, msg, null, params));
}
@Override
! public void log(PlatformLogger.Level level, String msg,
Throwable thrown) {
log(LogEvent.of(isLoggable(level), name, null, null,
! level, null, msg, thrown, (Object[]) null));
}
@Override
! public void log(PlatformLogger.Level level, Throwable thrown,
Supplier<String> msgSupplier) {
log(LogEvent.of(isLoggable(level), name, null, null,
! level, null, msgSupplier, thrown, (Object[]) null));
}
@Override
! public void logp(PlatformLogger.Level level, String sourceClass,
String sourceMethod, String msg) {
log(LogEvent.of(isLoggable(level), name,
sourceClass, sourceMethod,
! level, null, msg, null, (Object[]) null));
}
@Override
! public void logp(PlatformLogger.Level level, String sourceClass,
String sourceMethod, Supplier<String> msgSupplier) {
log(LogEvent.of(isLoggable(level), name,
sourceClass, sourceMethod,
! level, null, msgSupplier, null, (Object[]) null));
}
@Override
! public void logp(PlatformLogger.Level level, String sourceClass,
String sourceMethod, String msg, Object... params) {
log(LogEvent.of(isLoggable(level), name,
sourceClass, sourceMethod,
level, null, msg, null, params));
}
@Override
! public void logp(PlatformLogger.Level level, String sourceClass,
String sourceMethod, String msg, Throwable thrown) {
log(LogEvent.of(isLoggable(level), name,
sourceClass, sourceMethod,
! level, null, msg, thrown, (Object[]) null));
}
@Override
! public void logp(PlatformLogger.Level level, String sourceClass,
String sourceMethod, Throwable thrown,
Supplier<String> msgSupplier) {
log(LogEvent.of(isLoggable(level), name,
sourceClass, sourceMethod,
! level, null, msgSupplier, thrown, (Object[]) null));
}
@Override
! public void logrb(PlatformLogger.Level level, String sourceClass,
String sourceMethod, ResourceBundle bundle, String msg,
Object... params) {
log(LogEvent.of(isLoggable(level), name,
sourceClass, sourceMethod,
level, bundle, msg, null, params));
}
@Override
! public void logrb(PlatformLogger.Level level, ResourceBundle bundle,
String msg, Object... params) {
log(LogEvent.of(isLoggable(level), name, null, null,
level, bundle, msg, null, params));
}
@Override
! public void logrb(PlatformLogger.Level level, String sourceClass,
String sourceMethod, ResourceBundle bundle, String msg,
Throwable thrown) {
log(LogEvent.of(isLoggable(level), name,
sourceClass, sourceMethod,
! level, bundle, msg, thrown, (Object[]) null));
}
@Override
! public void logrb(PlatformLogger.Level level, ResourceBundle bundle,
String msg, Throwable thrown) {
log(LogEvent.of(isLoggable(level), name, null, null,
! level, bundle, msg, thrown, (Object[]) null));
}
@Override
! public boolean isLoggable(PlatformLogger.Level level) {
return this.level != OFF && level.intValue()
>= this.level.intValue();
}
@Override
*** 413,438 ****
return this.level != OFF;
}
}
- @Override
- public Logger getLogger(String name, Module caller) {
- SecurityManager sm = System.getSecurityManager();
- if (sm != null) {
- sm.checkPermission(LOGGERFINDER_PERMISSION);
- }
- PrivilegedAction<ClassLoader> pa = () -> caller.getClassLoader();
- ClassLoader callerLoader = AccessController.doPrivileged(pa);
- if (callerLoader == null) {
- return system.computeIfAbsent(name, (n) -> new LoggerImpl(n));
- } else {
- return user.computeIfAbsent(name, (n) -> new LoggerImpl(n));
- }
- }
- }
-
static ClassLoader getClassLoader(Module m) {
final boolean before = allowAll.get().getAndSet(true);
try {
return m.getClassLoader();
} finally {
--- 410,419 ----
*** 673,690 ****
if (hasRequiredPermissions && sysLogger2 == sysLogger1) {
throw new RuntimeException("identical loggers");
}
! final LogProducerFinder.LoggerImpl appSink;
! final LogProducerFinder.LoggerImpl sysSink;
boolean old = allowControl.get().get();
allowControl.get().set(true);
try {
! appSink = LogProducerFinder.LoggerImpl.class.cast(
provider.getLogger("foo", LoggerBridgeTest.class.getModule()));
! sysSink = LogProducerFinder.LoggerImpl.class.cast(
provider.getLogger("foo", Thread.class.getModule()));
} finally {
allowControl.get().set(old);
}
--- 654,671 ----
if (hasRequiredPermissions && sysLogger2 == sysLogger1) {
throw new RuntimeException("identical loggers");
}
! final LoggerImpl appSink;
! final LoggerImpl sysSink;
boolean old = allowControl.get().get();
allowControl.get().set(true);
try {
! appSink = LoggerImpl.class.cast(
provider.getLogger("foo", LoggerBridgeTest.class.getModule()));
! sysSink = LoggerImpl.class.cast(
provider.getLogger("foo", Thread.class.getModule()));
} finally {
allowControl.get().set(old);
}
*** 737,760 ****
verbose("Got expected results for "
+ desc + "\n\t" + expected);
}
}
! static void setLevel( LogProducerFinder.LoggerImpl sink,
sun.util.logging.PlatformLogger.Level loggerLevel) {
! sink.level = loggerLevel;
}
! // Calls the methods defined on LogProducer and verify the
! // parameters received by the underlying LogProducerFinder.LoggerImpl
// logger.
private static void testLogger(LoggerFinder provider,
Map<Object, String> loggerDescMap,
String name,
ResourceBundle loggerBundle,
PlatformLogger.Bridge logger,
! LogProducerFinder.LoggerImpl sink) {
System.out.println("Testing " + loggerDescMap.get(logger) + "[" + logger + "]");
final sun.util.logging.PlatformLogger.Level OFF = sun.util.logging.PlatformLogger.Level.OFF;
Foo foo = new Foo();
--- 718,741 ----
verbose("Got expected results for "
+ desc + "\n\t" + expected);
}
}
! static void setLevel(LoggerImpl sink,
sun.util.logging.PlatformLogger.Level loggerLevel) {
! sink.configureLevel(loggerLevel);
}
! // Calls the methods defined on PlatformLogger.Bridge and verify the
! // parameters received by the underlying LoggerImpl
// logger.
private static void testLogger(LoggerFinder provider,
Map<Object, String> loggerDescMap,
String name,
ResourceBundle loggerBundle,
PlatformLogger.Bridge logger,
! LoggerImpl sink) {
System.out.println("Testing " + loggerDescMap.get(logger) + "[" + logger + "]");
final sun.util.logging.PlatformLogger.Level OFF = sun.util.logging.PlatformLogger.Level.OFF;
Foo foo = new Foo();
< prev index next >