Path: blob/master/test/jdk/java/lang/System/LoggerFinder/internal/LoggerBridgeTest/LoggerBridgeTest.java
41161 views
/*1* Copyright (c) 2015, 2019, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation.7*8* This code is distributed in the hope that it will be useful, but WITHOUT9* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or10* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License11* version 2 for more details (a copy is included in the LICENSE file that12* accompanied this code).13*14* You should have received a copy of the GNU General Public License version15* 2 along with this work; if not, write to the Free Software Foundation,16* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.17*18* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA19* or visit www.oracle.com if you need additional information or have any20* questions.21*/22import java.lang.reflect.InvocationTargetException;23import java.lang.reflect.Method;24import java.security.AccessControlException;25import java.security.CodeSource;26import java.security.Permission;27import java.security.PermissionCollection;28import java.security.Permissions;29import java.security.Policy;30import java.security.ProtectionDomain;31import java.util.Arrays;32import java.util.Collections;33import java.util.Enumeration;34import java.util.HashMap;35import java.util.Map;36import java.util.Objects;37import java.util.Queue;38import java.util.ResourceBundle;39import java.util.concurrent.ArrayBlockingQueue;40import java.util.concurrent.ConcurrentHashMap;41import java.util.concurrent.atomic.AtomicBoolean;42import java.util.concurrent.atomic.AtomicLong;43import java.util.function.Supplier;44import java.util.logging.Handler;45import java.util.logging.LogRecord;46import java.lang.System.LoggerFinder;47import java.lang.System.Logger;48import java.util.stream.Stream;49import sun.util.logging.PlatformLogger;5051/**52* @test53* @bug 814036454* @summary JDK implementation specific unit test for JDK internal artifacts.55* Tests all bridge methods with the a custom backend whose56* loggers implement PlatformLogger.Bridge.57* @modules java.base/sun.util.logging58* java.base/jdk.internal.logger59* java.logging60* @build CustomSystemClassLoader LogProducerFinder LoggerBridgeTest61* @run main/othervm -Djava.system.class.loader=CustomSystemClassLoader LoggerBridgeTest NOSECURITY62* @run main/othervm -Djava.security.manager=allow -Djava.system.class.loader=CustomSystemClassLoader LoggerBridgeTest NOPERMISSIONS63* @run main/othervm -Djava.security.manager=allow -Djava.system.class.loader=CustomSystemClassLoader LoggerBridgeTest WITHPERMISSIONS64* @author danielfuchs65*/66public class LoggerBridgeTest {6768public static final RuntimePermission LOGGERFINDER_PERMISSION =69new RuntimePermission("loggerFinder");7071final static AtomicLong sequencer = new AtomicLong();72final static boolean VERBOSE = false;73static final ThreadLocal<AtomicBoolean> allowControl = new ThreadLocal<AtomicBoolean>() {74@Override75protected AtomicBoolean initialValue() {76return new AtomicBoolean(false);77}78};79static final ThreadLocal<AtomicBoolean> allowAccess = new ThreadLocal<AtomicBoolean>() {80@Override81protected AtomicBoolean initialValue() {82return new AtomicBoolean(false);83}84};85static final ThreadLocal<AtomicBoolean> allowAll = new ThreadLocal<AtomicBoolean>() {86@Override87protected AtomicBoolean initialValue() {88return new AtomicBoolean(false);89}90};9192public static final Queue<LogEvent> eventQueue = new ArrayBlockingQueue<>(128);9394public static final class LogEvent implements Cloneable {9596public LogEvent() {97this(sequencer.getAndIncrement());98}99100LogEvent(long sequenceNumber) {101this.sequenceNumber = sequenceNumber;102}103104long sequenceNumber;105boolean isLoggable;106String loggerName;107sun.util.logging.PlatformLogger.Level level;108ResourceBundle bundle;109Throwable thrown;110Object[] args;111String msg;112Supplier<String> supplier;113String className;114String methodName;115116Object[] toArray() {117return new Object[] {118sequenceNumber,119loggerName,120level,121isLoggable,122bundle,123msg,124supplier,125thrown,126args,127className,128methodName,129};130}131132@Override133public String toString() {134return Arrays.deepToString(toArray());135}136137@Override138public boolean equals(Object obj) {139return obj instanceof LogEvent140&& Objects.deepEquals(this.toArray(), ((LogEvent)obj).toArray());141}142143@Override144public int hashCode() {145return Objects.hash(toArray());146}147148public LogEvent cloneWith(long sequenceNumber)149throws CloneNotSupportedException {150LogEvent cloned = (LogEvent)super.clone();151cloned.sequenceNumber = sequenceNumber;152return cloned;153}154155public static LogEvent of(long sequenceNumber,156boolean isLoggable, String name,157sun.util.logging.PlatformLogger.Level level, ResourceBundle bundle,158String key, Throwable thrown, Object... params) {159return LogEvent.of(sequenceNumber, isLoggable, name,160null, null, level, bundle, key,161thrown, params);162}163164public static LogEvent of(long sequenceNumber,165boolean isLoggable, String name,166sun.util.logging.PlatformLogger.Level level, ResourceBundle bundle,167Supplier<String> supplier, Throwable thrown, Object... params) {168return LogEvent.of(sequenceNumber, isLoggable, name,169null, null, level, bundle, supplier,170thrown, params);171}172173public static LogEvent of(long sequenceNumber,174boolean isLoggable, String name,175String className, String methodName,176sun.util.logging.PlatformLogger.Level level, ResourceBundle bundle,177String key, Throwable thrown, Object... params) {178LogEvent evt = new LogEvent(sequenceNumber);179evt.loggerName = name;180evt.level = level;181evt.args = params;182evt.bundle = bundle;183evt.thrown = thrown;184evt.msg = key;185evt.isLoggable = isLoggable;186evt.className = className;187evt.methodName = methodName;188return evt;189}190191public static LogEvent of(boolean isLoggable, String name,192String className, String methodName,193sun.util.logging.PlatformLogger.Level level, ResourceBundle bundle,194String key, Throwable thrown, Object... params) {195return LogEvent.of(sequencer.getAndIncrement(), isLoggable, name,196className, methodName, level, bundle, key, thrown, params);197}198199public static LogEvent of(long sequenceNumber,200boolean isLoggable, String name,201String className, String methodName,202sun.util.logging.PlatformLogger.Level level, ResourceBundle bundle,203Supplier<String> supplier, Throwable thrown, Object... params) {204LogEvent evt = new LogEvent(sequenceNumber);205evt.loggerName = name;206evt.level = level;207evt.args = params;208evt.bundle = bundle;209evt.thrown = thrown;210evt.supplier = supplier;211evt.isLoggable = isLoggable;212evt.className = className;213evt.methodName = methodName;214return evt;215}216217public static LogEvent of(boolean isLoggable, String name,218String className, String methodName,219sun.util.logging.PlatformLogger.Level level, ResourceBundle bundle,220Supplier<String> supplier, Throwable thrown, Object... params) {221return LogEvent.of(sequencer.getAndIncrement(), isLoggable, name,222className, methodName, level, bundle, supplier, thrown, params);223}224225}226static final Class<?> providerClass;227static {228try {229// Preload classes before the security manager is on.230providerClass = ClassLoader.getSystemClassLoader().loadClass("LogProducerFinder");231((LoggerFinder)providerClass.newInstance()).getLogger("foo", providerClass.getModule());232} catch (Exception ex) {233throw new ExceptionInInitializerError(ex);234}235}236237public static class LoggerImpl implements System.Logger, PlatformLogger.Bridge {238private final String name;239private PlatformLogger.Level level = PlatformLogger.Level.INFO;240private PlatformLogger.Level OFF = PlatformLogger.Level.OFF;241private PlatformLogger.Level FINE = PlatformLogger.Level.FINE;242private PlatformLogger.Level FINER = PlatformLogger.Level.FINER;243private PlatformLogger.Level FINEST = PlatformLogger.Level.FINEST;244private PlatformLogger.Level CONFIG = PlatformLogger.Level.CONFIG;245private PlatformLogger.Level INFO = PlatformLogger.Level.INFO;246private PlatformLogger.Level WARNING = PlatformLogger.Level.WARNING;247private PlatformLogger.Level SEVERE = PlatformLogger.Level.SEVERE;248249public LoggerImpl(String name) {250this.name = name;251}252253public void configureLevel(PlatformLogger.Level level) {254this.level = level;255}256257@Override258public String getName() {259return name;260}261262@Override263public boolean isLoggable(Level level) {264return this.level != OFF && this.level.intValue() <= level.getSeverity();265}266267@Override268public void log(Level level, ResourceBundle bundle,269String key, Throwable thrown) {270throw new UnsupportedOperationException();271}272273@Override274public void log(Level level, ResourceBundle bundle,275String format, Object... params) {276throw new UnsupportedOperationException();277}278279void log(LogEvent event) {280eventQueue.add(event);281}282283@Override284public void log(Level level, Supplier<String> msgSupplier) {285throw new UnsupportedOperationException();286}287288@Override289public void log(Level level, Supplier<String> msgSupplier,290Throwable thrown) {291throw new UnsupportedOperationException();292}293294@Override295public void log(PlatformLogger.Level level, String msg) {296log(LogEvent.of(isLoggable(level), name, null, null,297level, null, msg, null, (Object[]) null));298}299300@Override301public void log(PlatformLogger.Level level,302Supplier<String> msgSupplier) {303log(LogEvent.of(isLoggable(level), name, null, null,304level, null, msgSupplier, null, (Object[]) null));305}306307@Override308public void log(PlatformLogger.Level level, String msg,309Object... params) {310log(LogEvent.of(isLoggable(level), name, null, null,311level, null, msg, null, params));312}313314@Override315public void log(PlatformLogger.Level level, String msg,316Throwable thrown) {317log(LogEvent.of(isLoggable(level), name, null, null,318level, null, msg, thrown, (Object[]) null));319}320321@Override322public void log(PlatformLogger.Level level, Throwable thrown,323Supplier<String> msgSupplier) {324log(LogEvent.of(isLoggable(level), name, null, null,325level, null, msgSupplier, thrown, (Object[]) null));326}327328@Override329public void logp(PlatformLogger.Level level, String sourceClass,330String sourceMethod, String msg) {331log(LogEvent.of(isLoggable(level), name,332sourceClass, sourceMethod,333level, null, msg, null, (Object[]) null));334}335336@Override337public void logp(PlatformLogger.Level level, String sourceClass,338String sourceMethod, Supplier<String> msgSupplier) {339log(LogEvent.of(isLoggable(level), name,340sourceClass, sourceMethod,341level, null, msgSupplier, null, (Object[]) null));342}343344@Override345public void logp(PlatformLogger.Level level, String sourceClass,346String sourceMethod, String msg, Object... params) {347log(LogEvent.of(isLoggable(level), name,348sourceClass, sourceMethod,349level, null, msg, null, params));350}351352@Override353public void logp(PlatformLogger.Level level, String sourceClass,354String sourceMethod, String msg, Throwable thrown) {355log(LogEvent.of(isLoggable(level), name,356sourceClass, sourceMethod,357level, null, msg, thrown, (Object[]) null));358}359360@Override361public void logp(PlatformLogger.Level level, String sourceClass,362String sourceMethod, Throwable thrown,363Supplier<String> msgSupplier) {364log(LogEvent.of(isLoggable(level), name,365sourceClass, sourceMethod,366level, null, msgSupplier, thrown, (Object[]) null));367}368369@Override370public void logrb(PlatformLogger.Level level, String sourceClass,371String sourceMethod, ResourceBundle bundle, String msg,372Object... params) {373log(LogEvent.of(isLoggable(level), name,374sourceClass, sourceMethod,375level, bundle, msg, null, params));376}377378@Override379public void logrb(PlatformLogger.Level level, ResourceBundle bundle,380String msg, Object... params) {381log(LogEvent.of(isLoggable(level), name, null, null,382level, bundle, msg, null, params));383}384385@Override386public void logrb(PlatformLogger.Level level, String sourceClass,387String sourceMethod, ResourceBundle bundle, String msg,388Throwable thrown) {389log(LogEvent.of(isLoggable(level), name,390sourceClass, sourceMethod,391level, bundle, msg, thrown, (Object[]) null));392}393394@Override395public void logrb(PlatformLogger.Level level, ResourceBundle bundle,396String msg, Throwable thrown) {397log(LogEvent.of(isLoggable(level), name, null, null,398level, bundle, msg, thrown, (Object[]) null));399}400401@Override402public boolean isLoggable(PlatformLogger.Level level) {403return this.level != OFF && level.intValue()404>= this.level.intValue();405}406407@Override408public boolean isEnabled() {409return this.level != OFF;410}411412}413414static ClassLoader getClassLoader(Module m) {415final boolean before = allowAll.get().getAndSet(true);416try {417return m.getClassLoader();418} finally {419allowAll.get().set(before);420}421}422423static final sun.util.logging.PlatformLogger.Level[] julLevels = {424sun.util.logging.PlatformLogger.Level.ALL,425sun.util.logging.PlatformLogger.Level.FINEST,426sun.util.logging.PlatformLogger.Level.FINER,427sun.util.logging.PlatformLogger.Level.FINE,428sun.util.logging.PlatformLogger.Level.CONFIG,429sun.util.logging.PlatformLogger.Level.INFO,430sun.util.logging.PlatformLogger.Level.WARNING,431sun.util.logging.PlatformLogger.Level.SEVERE,432sun.util.logging.PlatformLogger.Level.OFF,433};434435public static class MyBundle extends ResourceBundle {436437final ConcurrentHashMap<String,String> map = new ConcurrentHashMap<>();438439@Override440protected Object handleGetObject(String key) {441if (key.contains(" (translated)")) {442throw new RuntimeException("Unexpected key: " + key);443}444return map.computeIfAbsent(key, k -> k + " (translated)");445}446447@Override448public Enumeration<String> getKeys() {449return Collections.enumeration(map.keySet());450}451452}453454public static class MyHandler extends Handler {455456@Override457public java.util.logging.Level getLevel() {458return java.util.logging.Level.ALL;459}460461@Override462public void publish(LogRecord record) {463eventQueue.add(LogEvent.of(sequencer.getAndIncrement(),464true, record.getLoggerName(),465record.getSourceClassName(),466record.getSourceMethodName(),467PlatformLogger.Level.valueOf(record.getLevel().getName()),468record.getResourceBundle(), record.getMessage(),469record.getThrown(), record.getParameters()));470}471@Override472public void flush() {473}474@Override475public void close() throws SecurityException {476}477478}479480public static class MyLoggerBundle extends MyBundle {481482}483484final static Method lazyGetLogger;485static {486// jdk.internal.logging.LoggerBridge.getLogger(name, caller)487try {488Class<?> bridgeClass = Class.forName("jdk.internal.logger.LazyLoggers");489lazyGetLogger = bridgeClass.getDeclaredMethod("getLogger",490String.class, Module.class);491lazyGetLogger.setAccessible(true);492} catch (Throwable ex) {493throw new ExceptionInInitializerError(ex);494}495}496497static Logger getLogger(LoggerFinder provider, String name, Module caller) {498Logger logger;499try {500logger = Logger.class.cast(lazyGetLogger.invoke(null, name, caller));501} catch (Throwable x) {502Throwable t = (x instanceof InvocationTargetException) ?503((InvocationTargetException)x).getTargetException() : x;504if (t instanceof RuntimeException) {505throw (RuntimeException)t;506} else if (t instanceof Exception) {507throw new RuntimeException(t);508} else {509throw (Error)t;510}511}512// The method above does not throw exception...513// call the provider here to verify that an exception would have514// been thrown by the provider.515if (logger != null && caller == Thread.class.getModule()) {516Logger log = provider.getLogger(name, caller);517}518return logger;519}520521static Logger getLogger(LoggerFinder provider, String name, ResourceBundle bundle, Module caller) {522if (getClassLoader(caller) != null) {523return System.getLogger(name,bundle);524} else {525return provider.getLocalizedLogger(name, bundle, caller);526}527}528529static PlatformLogger.Bridge convert(Logger logger) {530return PlatformLogger.Bridge.convert(logger);531}532533static enum TestCases {NOSECURITY, NOPERMISSIONS, WITHPERMISSIONS};534535static void setSecurityManager() {536if (System.getSecurityManager() == null) {537Policy.setPolicy(new SimplePolicy(allowControl, allowAccess, allowAll));538System.setSecurityManager(new SecurityManager());539}540}541542public static void main(String[] args) {543if (args.length == 0)544args = new String[] {545//"NOSECURITY",546"NOPERMISSIONS",547"WITHPERMISSIONS"548};549550551Stream.of(args).map(TestCases::valueOf).forEach((testCase) -> {552LoggerFinder provider;553switch (testCase) {554case NOSECURITY:555System.out.println("\n*** Without Security Manager\n");556provider = LoggerFinder.getLoggerFinder();557test(provider, true);558System.out.println("Tetscase count: " + sequencer.get());559break;560case NOPERMISSIONS:561System.out.println("\n*** With Security Manager, without permissions\n");562setSecurityManager();563try {564provider = LoggerFinder.getLoggerFinder();565throw new RuntimeException("Expected exception not raised");566} catch (AccessControlException x) {567if (!LOGGERFINDER_PERMISSION.equals(x.getPermission())) {568throw new RuntimeException("Unexpected permission check", x);569}570final boolean control = allowControl.get().get();571try {572allowControl.get().set(true);573provider = LoggerFinder.getLoggerFinder();574} finally {575allowControl.get().set(control);576}577}578test(provider, false);579System.out.println("Tetscase count: " + sequencer.get());580break;581case WITHPERMISSIONS:582System.out.println("\n*** With Security Manager, with control permission\n");583setSecurityManager();584final boolean control = allowControl.get().get();585try {586allowControl.get().set(true);587provider = LoggerFinder.getLoggerFinder();588test(provider, true);589} finally {590allowControl.get().set(control);591}592break;593default:594throw new RuntimeException("Unknown test case: " + testCase);595}596});597System.out.println("\nPASSED: Tested " + sequencer.get() + " cases.");598}599600public static void test(LoggerFinder provider, boolean hasRequiredPermissions) {601602ResourceBundle loggerBundle = ResourceBundle.getBundle(MyLoggerBundle.class.getName());603final Map<Object, String> loggerDescMap = new HashMap<>();604605606Logger appLogger1 = System.getLogger("foo");607loggerDescMap.put(appLogger1, "System.getLogger(\"foo\")");608609Logger sysLogger1 = null;610try {611sysLogger1 = getLogger(provider, "foo", Thread.class.getModule());612loggerDescMap.put(sysLogger1, "provider.getLogger(\"foo\", Thread.class.getModule())");613if (!hasRequiredPermissions) {614throw new RuntimeException("Managed to obtain a system logger without permission");615}616} catch (AccessControlException acx) {617if (hasRequiredPermissions) {618throw new RuntimeException("Unexpected security exception: ", acx);619}620if (!acx.getPermission().equals(LOGGERFINDER_PERMISSION)) {621throw new RuntimeException("Unexpected permission in exception: " + acx, acx);622}623System.out.println("Got expected exception for system logger: " + acx);624}625626627Logger appLogger2 =628System.getLogger("foo", loggerBundle);629loggerDescMap.put(appLogger2, "System.getLogger(\"foo\", loggerBundle)");630631Logger sysLogger2 = null;632try {633sysLogger2 = getLogger(provider, "foo", loggerBundle, Thread.class.getModule());634loggerDescMap.put(sysLogger2, "provider.getLogger(\"foo\", loggerBundle, Thread.class.getModule())");635if (!hasRequiredPermissions) {636throw new RuntimeException("Managed to obtain a system logger without permission");637}638} catch (AccessControlException acx) {639if (hasRequiredPermissions) {640throw new RuntimeException("Unexpected security exception: ", acx);641}642if (!acx.getPermission().equals(LOGGERFINDER_PERMISSION)) {643throw new RuntimeException("Unexpected permission in exception: " + acx, acx);644}645System.out.println("Got expected exception for localized system logger: " + acx);646}647if (hasRequiredPermissions && appLogger2 == sysLogger2) {648throw new RuntimeException("identical loggers");649}650if (appLogger2 == appLogger1) {651throw new RuntimeException("identical loggers");652}653if (hasRequiredPermissions && sysLogger2 == sysLogger1) {654throw new RuntimeException("identical loggers");655}656657658final LoggerImpl appSink;659final LoggerImpl sysSink;660boolean old = allowControl.get().get();661allowControl.get().set(true);662try {663appSink = LoggerImpl.class.cast(664provider.getLogger("foo", LoggerBridgeTest.class.getModule()));665sysSink = LoggerImpl.class.cast(666provider.getLogger("foo", Thread.class.getModule()));667} finally {668allowControl.get().set(old);669}670671testLogger(provider, loggerDescMap, "foo", null, convert(appLogger1), appSink);672if (hasRequiredPermissions) {673testLogger(provider, loggerDescMap, "foo", null, convert(sysLogger1), sysSink);674}675testLogger(provider, loggerDescMap, "foo", loggerBundle, convert(appLogger2), appSink);676if (hasRequiredPermissions) {677testLogger(provider, loggerDescMap, "foo", loggerBundle, convert(sysLogger2), sysSink);678}679}680681public static class Foo {682683}684685static void verbose(String msg) {686if (VERBOSE) {687System.out.println(msg);688}689}690691static void checkLogEvent(LoggerFinder provider, String desc,692LogEvent expected) {693LogEvent actual = eventQueue.poll();694if (!expected.equals(actual)) {695throw new RuntimeException("mismatch for " + desc696+ "\n\texpected=" + expected697+ "\n\t actual=" + actual);698} else {699verbose("Got expected results for "700+ desc + "\n\t" + expected);701}702}703704static void checkLogEvent(LoggerFinder provider, String desc,705LogEvent expected, boolean expectNotNull) {706LogEvent actual = eventQueue.poll();707if (actual == null && !expectNotNull) return;708if (actual != null && !expectNotNull) {709throw new RuntimeException("Unexpected log event found for " + desc710+ "\n\tgot: " + actual);711}712if (!expected.equals(actual)) {713throw new RuntimeException("mismatch for " + desc714+ "\n\texpected=" + expected715+ "\n\t actual=" + actual);716} else {717verbose("Got expected results for "718+ desc + "\n\t" + expected);719}720}721722static void setLevel(LoggerImpl sink,723sun.util.logging.PlatformLogger.Level loggerLevel) {724sink.configureLevel(loggerLevel);725}726727// Calls the methods defined on PlatformLogger.Bridge and verify the728// parameters received by the underlying LoggerImpl729// logger.730private static void testLogger(LoggerFinder provider,731Map<Object, String> loggerDescMap,732String name,733ResourceBundle loggerBundle,734PlatformLogger.Bridge logger,735LoggerImpl sink) {736737System.out.println("Testing " + loggerDescMap.get(logger) + "[" + logger + "]");738final sun.util.logging.PlatformLogger.Level OFF = sun.util.logging.PlatformLogger.Level.OFF;739740Foo foo = new Foo();741String fooMsg = foo.toString();742System.out.println("\tlogger.log(messageLevel, fooMsg)");743System.out.println("\tlogger.<level>(fooMsg)");744for (sun.util.logging.PlatformLogger.Level loggerLevel : julLevels) {745setLevel(sink, loggerLevel);746for (sun.util.logging.PlatformLogger.Level messageLevel :julLevels) {747String desc = "logger.log(messageLevel, fooMsg): loggerLevel="748+ loggerLevel+", messageLevel="+messageLevel;749LogEvent expected =750LogEvent.of(751sequencer.get(),752loggerLevel != OFF && messageLevel.intValue() >= loggerLevel.intValue(),753name, messageLevel, loggerBundle,754fooMsg, (Throwable)null, (Object[])null);755logger.log(messageLevel, fooMsg);756checkLogEvent(provider, desc, expected);757}758}759760Supplier<String> supplier = new Supplier<String>() {761@Override762public String get() {763return this.toString();764}765};766System.out.println("\tlogger.log(messageLevel, supplier)");767System.out.println("\tlogger.<level>(supplier)");768for (sun.util.logging.PlatformLogger.Level loggerLevel : julLevels) {769setLevel(sink, loggerLevel);770for (sun.util.logging.PlatformLogger.Level messageLevel :julLevels) {771String desc = "logger.log(messageLevel, supplier): loggerLevel="772+ loggerLevel+", messageLevel="+messageLevel;773LogEvent expected =774LogEvent.of(775sequencer.get(),776loggerLevel != OFF && messageLevel.intValue() >= loggerLevel.intValue(),777name, messageLevel, null,778supplier, (Throwable)null, (Object[])null);779logger.log(messageLevel, supplier);780checkLogEvent(provider, desc, expected);781}782}783784String format = "two params [{1} {2}]";785Object arg1 = foo;786Object arg2 = fooMsg;787System.out.println("\tlogger.log(messageLevel, format, arg1, arg2)");788for (sun.util.logging.PlatformLogger.Level loggerLevel : julLevels) {789setLevel(sink, loggerLevel);790for (sun.util.logging.PlatformLogger.Level messageLevel :julLevels) {791String desc = "logger.log(messageLevel, format, foo, fooMsg): loggerLevel="792+ loggerLevel+", messageLevel="+messageLevel;793LogEvent expected =794LogEvent.of(795sequencer.get(),796loggerLevel != OFF && messageLevel.intValue() >= loggerLevel.intValue(),797name, messageLevel, loggerBundle,798format, (Throwable)null, arg1, arg2);799logger.log(messageLevel, format, arg1, arg2);800checkLogEvent(provider, desc, expected);801}802}803804Throwable thrown = new Exception("OK: log me!");805System.out.println("\tlogger.log(messageLevel, fooMsg, thrown)");806for (sun.util.logging.PlatformLogger.Level loggerLevel : julLevels) {807setLevel(sink, loggerLevel);808for (sun.util.logging.PlatformLogger.Level messageLevel :julLevels) {809String desc = "logger.log(messageLevel, fooMsg, thrown): loggerLevel="810+ loggerLevel+", messageLevel="+messageLevel;811LogEvent expected =812LogEvent.of(813sequencer.get(),814loggerLevel != OFF && messageLevel.intValue() >= loggerLevel.intValue(),815name, messageLevel, loggerBundle,816fooMsg, thrown, (Object[])null);817logger.log(messageLevel, fooMsg, thrown);818checkLogEvent(provider, desc, expected);819}820}821822System.out.println("\tlogger.log(messageLevel, thrown, supplier)");823for (sun.util.logging.PlatformLogger.Level loggerLevel : julLevels) {824setLevel(sink, loggerLevel);825for (sun.util.logging.PlatformLogger.Level messageLevel :julLevels) {826String desc = "logger.log(messageLevel, thrown, supplier): loggerLevel="827+ loggerLevel+", messageLevel="+messageLevel;828LogEvent expected =829LogEvent.of(830sequencer.get(),831loggerLevel != OFF && messageLevel.intValue() >= loggerLevel.intValue(),832name, messageLevel, null,833supplier, thrown, (Object[])null);834logger.log(messageLevel, thrown, supplier);835checkLogEvent(provider, desc, expected);836}837}838839String sourceClass = "blah.Blah";840String sourceMethod = "blih";841System.out.println("\tlogger.logp(messageLevel, sourceClass, sourceMethod, fooMsg)");842for (sun.util.logging.PlatformLogger.Level loggerLevel : julLevels) {843setLevel(sink, loggerLevel);844for (sun.util.logging.PlatformLogger.Level messageLevel :julLevels) {845String desc = "logger.logp(messageLevel, sourceClass, sourceMethod, fooMsg): loggerLevel="846+ loggerLevel+", messageLevel="+messageLevel;847LogEvent expected =848LogEvent.of(849sequencer.get(),850loggerLevel != OFF && messageLevel.intValue() >= loggerLevel.intValue(),851name, sourceClass, sourceMethod, messageLevel, loggerBundle,852fooMsg, (Throwable)null, (Object[])null);853logger.logp(messageLevel, sourceClass, sourceMethod, fooMsg);854checkLogEvent(provider, desc, expected);855}856}857858System.out.println("\tlogger.logp(messageLevel, sourceClass, sourceMethod, supplier)");859for (sun.util.logging.PlatformLogger.Level loggerLevel : julLevels) {860setLevel(sink, loggerLevel);861for (sun.util.logging.PlatformLogger.Level messageLevel :julLevels) {862String desc = "logger.logp(messageLevel, sourceClass, sourceMethod, supplier): loggerLevel="863+ loggerLevel+", messageLevel="+messageLevel;864LogEvent expected =865LogEvent.of(866sequencer.get(),867loggerLevel != OFF && messageLevel.intValue() >= loggerLevel.intValue(),868name, sourceClass, sourceMethod, messageLevel, null,869supplier, (Throwable)null, (Object[])null);870logger.logp(messageLevel, sourceClass, sourceMethod, supplier);871checkLogEvent(provider, desc, expected);872}873}874875System.out.println("\tlogger.logp(messageLevel, sourceClass, sourceMethod, format, arg1, arg2)");876for (sun.util.logging.PlatformLogger.Level loggerLevel : julLevels) {877setLevel(sink, loggerLevel);878for (sun.util.logging.PlatformLogger.Level messageLevel :julLevels) {879String desc = "logger.logp(messageLevel, sourceClass, sourceMethod, format, arg1, arg2): loggerLevel="880+ loggerLevel+", messageLevel="+messageLevel;881LogEvent expected =882LogEvent.of(883sequencer.get(),884loggerLevel != OFF && messageLevel.intValue() >= loggerLevel.intValue(),885name, sourceClass, sourceMethod, messageLevel, loggerBundle,886format, (Throwable)null, arg1, arg2);887logger.logp(messageLevel, sourceClass, sourceMethod, format, arg1, arg2);888checkLogEvent(provider, desc, expected);889}890}891892System.out.println("\tlogger.logp(messageLevel, sourceClass, sourceMethod, fooMsg, thrown)");893for (sun.util.logging.PlatformLogger.Level loggerLevel : julLevels) {894setLevel(sink, loggerLevel);895for (sun.util.logging.PlatformLogger.Level messageLevel :julLevels) {896String desc = "logger.logp(messageLevel, sourceClass, sourceMethod, fooMsg, thrown): loggerLevel="897+ loggerLevel+", messageLevel="+messageLevel;898LogEvent expected =899LogEvent.of(900sequencer.get(),901loggerLevel != OFF && messageLevel.intValue() >= loggerLevel.intValue(),902name, sourceClass, sourceMethod, messageLevel, loggerBundle,903fooMsg, thrown, (Object[])null);904logger.logp(messageLevel, sourceClass, sourceMethod, fooMsg, thrown);905checkLogEvent(provider, desc, expected);906}907}908909System.out.println("\tlogger.logp(messageLevel, sourceClass, sourceMethod, thrown, supplier)");910for (sun.util.logging.PlatformLogger.Level loggerLevel : julLevels) {911setLevel(sink, loggerLevel);912for (sun.util.logging.PlatformLogger.Level messageLevel :julLevels) {913String desc = "logger.logp(messageLevel, sourceClass, sourceMethod, thrown, supplier): loggerLevel="914+ loggerLevel+", messageLevel="+messageLevel;915LogEvent expected =916LogEvent.of(917sequencer.get(),918loggerLevel != OFF && messageLevel.intValue() >= loggerLevel.intValue(),919name, sourceClass, sourceMethod, messageLevel, null,920supplier, thrown, (Object[])null);921logger.logp(messageLevel, sourceClass, sourceMethod, thrown, supplier);922checkLogEvent(provider, desc, expected);923}924}925926ResourceBundle bundle = ResourceBundle.getBundle(MyBundle.class.getName());927System.out.println("\tlogger.logrb(messageLevel, bundle, format, arg1, arg2)");928for (sun.util.logging.PlatformLogger.Level loggerLevel : julLevels) {929setLevel(sink, loggerLevel);930for (sun.util.logging.PlatformLogger.Level messageLevel :julLevels) {931String desc = "logger.logrb(messageLevel, bundle, format, arg1, arg2): loggerLevel="932+ loggerLevel+", messageLevel="+messageLevel;933LogEvent expected =934LogEvent.of(935sequencer.get(),936loggerLevel != OFF && messageLevel.intValue() >= loggerLevel.intValue(),937name, messageLevel, bundle,938format, (Throwable)null, arg1, arg2);939logger.logrb(messageLevel, bundle, format, arg1, arg2);940checkLogEvent(provider, desc, expected);941}942}943944System.out.println("\tlogger.logrb(messageLevel, bundle, msg, thrown)");945for (sun.util.logging.PlatformLogger.Level loggerLevel : julLevels) {946setLevel(sink, loggerLevel);947for (sun.util.logging.PlatformLogger.Level messageLevel :julLevels) {948String desc = "logger.logrb(messageLevel, bundle, msg, thrown): loggerLevel="949+ loggerLevel+", messageLevel="+messageLevel;950LogEvent expected =951LogEvent.of(952sequencer.get(),953loggerLevel != OFF && messageLevel.intValue() >= loggerLevel.intValue(),954name, messageLevel, bundle,955fooMsg, thrown, (Object[])null);956logger.logrb(messageLevel, bundle, fooMsg, thrown);957checkLogEvent(provider, desc, expected);958}959}960961System.out.println("\tlogger.logrb(messageLevel, sourceClass, sourceMethod, bundle, format, arg1, arg2)");962for (sun.util.logging.PlatformLogger.Level loggerLevel : julLevels) {963setLevel(sink, loggerLevel);964for (sun.util.logging.PlatformLogger.Level messageLevel :julLevels) {965String desc = "logger.logrb(messageLevel, sourceClass, sourceMethod, bundle, format, arg1, arg2): loggerLevel="966+ loggerLevel+", messageLevel="+messageLevel;967LogEvent expected =968LogEvent.of(969sequencer.get(),970loggerLevel != OFF && messageLevel.intValue() >= loggerLevel.intValue(),971name, sourceClass, sourceMethod, messageLevel, bundle,972format, (Throwable)null, arg1, arg2);973logger.logrb(messageLevel, sourceClass, sourceMethod, bundle, format, arg1, arg2);974checkLogEvent(provider, desc, expected);975}976}977978System.out.println("\tlogger.logrb(messageLevel, sourceClass, sourceMethod, bundle, msg, thrown)");979for (sun.util.logging.PlatformLogger.Level loggerLevel : julLevels) {980setLevel(sink, loggerLevel);981for (sun.util.logging.PlatformLogger.Level messageLevel :julLevels) {982String desc = "logger.logrb(messageLevel, sourceClass, sourceMethod, bundle, msg, thrown): loggerLevel="983+ loggerLevel+", messageLevel="+messageLevel;984LogEvent expected =985LogEvent.of(986sequencer.get(),987loggerLevel != OFF && messageLevel.intValue() >= loggerLevel.intValue(),988name, sourceClass, sourceMethod, messageLevel, bundle,989fooMsg, thrown, (Object[])null);990logger.logrb(messageLevel, sourceClass, sourceMethod, bundle, fooMsg, thrown);991checkLogEvent(provider, desc, expected);992}993}994}995996final static class PermissionsBuilder {997final Permissions perms;998public PermissionsBuilder() {999this(new Permissions());1000}1001public PermissionsBuilder(Permissions perms) {1002this.perms = perms;1003}1004public PermissionsBuilder add(Permission p) {1005perms.add(p);1006return this;1007}1008public PermissionsBuilder addAll(PermissionCollection col) {1009if (col != null) {1010for (Enumeration<Permission> e = col.elements(); e.hasMoreElements(); ) {1011perms.add(e.nextElement());1012}1013}1014return this;1015}1016public Permissions toPermissions() {1017final PermissionsBuilder builder = new PermissionsBuilder();1018builder.addAll(perms);1019return builder.perms;1020}1021}10221023public static class SimplePolicy extends Policy {1024final static RuntimePermission CONTROL = LOGGERFINDER_PERMISSION;1025final static RuntimePermission ACCESS_LOGGER = new RuntimePermission("accessClassInPackage.jdk.internal.logger");1026final static RuntimePermission ACCESS_LOGGING = new RuntimePermission("accessClassInPackage.sun.util.logging");10271028static final Policy DEFAULT_POLICY = Policy.getPolicy();10291030final Permissions permissions;1031final Permissions allPermissions;1032final ThreadLocal<AtomicBoolean> allowControl;1033final ThreadLocal<AtomicBoolean> allowAccess;1034final ThreadLocal<AtomicBoolean> allowAll;1035public SimplePolicy(ThreadLocal<AtomicBoolean> allowControl,1036ThreadLocal<AtomicBoolean> allowAccess,1037ThreadLocal<AtomicBoolean> allowAll) {1038this.allowControl = allowControl;1039this.allowAccess = allowAccess;1040this.allowAll = allowAll;1041permissions = new Permissions();1042allPermissions = new PermissionsBuilder()1043.add(new java.security.AllPermission())1044.toPermissions();1045}10461047Permissions getPermissions() {1048if (allowControl.get().get() || allowAccess.get().get() || allowAll.get().get()) {1049PermissionsBuilder builder = new PermissionsBuilder()1050.addAll(permissions);1051if (allowControl.get().get()) {1052builder.add(CONTROL);1053}1054if (allowAccess.get().get()) {1055builder.add(ACCESS_LOGGER);1056builder.add(ACCESS_LOGGING);1057}1058if (allowAll.get().get()) {1059builder.addAll(allPermissions);1060}1061return builder.toPermissions();1062}1063return permissions;1064}10651066@Override1067public boolean implies(ProtectionDomain domain, Permission permission) {1068return getPermissions().implies(permission) || DEFAULT_POLICY.implies(domain, permission);1069}10701071@Override1072public PermissionCollection getPermissions(CodeSource codesource) {1073return new PermissionsBuilder().addAll(getPermissions()).toPermissions();1074}10751076@Override1077public PermissionCollection getPermissions(ProtectionDomain domain) {1078return new PermissionsBuilder().addAll(getPermissions()).toPermissions();1079}1080}1081}108210831084