1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099
|
/*
* 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.
*
* This code is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
* version 2 for more details (a copy is included in the LICENSE file that
* accompanied this code).
*
* You should have received a copy of the GNU General Public License version
* 2 along with this work; if not, write to the Free Software Foundation,
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
*
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
* or visit www.oracle.com if you need additional information or have any
* 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;
import java.util.Map;
import java.util.Objects;
import java.util.Queue;
import java.util.ResourceBundle;
import java.util.concurrent.ArrayBlockingQueue;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.atomic.AtomicBoolean;
import java.util.concurrent.atomic.AtomicLong;
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
* @bug 8140364
* @summary JDK implementation specific unit test for JDK internal artifacts.
* 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
*/
public class LoggerBridgeTest {
public static final RuntimePermission LOGGERFINDER_PERMISSION =
new RuntimePermission("loggerFinder");
final static AtomicLong sequencer = new AtomicLong();
final static boolean VERBOSE = false;
static final ThreadLocal<AtomicBoolean> allowControl = new ThreadLocal<AtomicBoolean>() {
@Override
protected AtomicBoolean initialValue() {
return new AtomicBoolean(false);
}
};
static final ThreadLocal<AtomicBoolean> allowAccess = new ThreadLocal<AtomicBoolean>() {
@Override
protected AtomicBoolean initialValue() {
return new AtomicBoolean(false);
}
};
static final ThreadLocal<AtomicBoolean> allowAll = new ThreadLocal<AtomicBoolean>() {
@Override
protected AtomicBoolean initialValue() {
return new AtomicBoolean(false);
}
};
public static final Queue<LogEvent> eventQueue = new ArrayBlockingQueue<>(128);
public static final class LogEvent implements Cloneable {
public LogEvent() {
this(sequencer.getAndIncrement());
}
LogEvent(long sequenceNumber) {
this.sequenceNumber = sequenceNumber;
}
long sequenceNumber;
boolean isLoggable;
String loggerName;
sun.util.logging.PlatformLogger.Level level;
ResourceBundle bundle;
Throwable thrown;
Object[] args;
String msg;
Supplier<String> supplier;
String className;
String methodName;
Object[] toArray() {
return new Object[] {
sequenceNumber,
loggerName,
level,
isLoggable,
bundle,
msg,
supplier,
thrown,
args,
className,
methodName,
};
}
@Override
public String toString() {
return Arrays.deepToString(toArray());
}
@Override
public boolean equals(Object obj) {
return obj instanceof LogEvent
&& Objects.deepEquals(this.toArray(), ((LogEvent)obj).toArray());
}
@Override
public int hashCode() {
return Objects.hash(toArray());
}
public LogEvent cloneWith(long sequenceNumber)
throws CloneNotSupportedException {
LogEvent cloned = (LogEvent)super.clone();
cloned.sequenceNumber = sequenceNumber;
return cloned;
}
public static LogEvent of(long sequenceNumber,
boolean isLoggable, String name,
sun.util.logging.PlatformLogger.Level level, ResourceBundle bundle,
String key, Throwable thrown, Object... params) {
return LogEvent.of(sequenceNumber, isLoggable, name,
null, null, level, bundle, key,
thrown, params);
}
public static LogEvent of(long sequenceNumber,
boolean isLoggable, String name,
sun.util.logging.PlatformLogger.Level level, ResourceBundle bundle,
Supplier<String> supplier, Throwable thrown, Object... params) {
return LogEvent.of(sequenceNumber, isLoggable, name,
null, null, level, bundle, supplier,
thrown, params);
}
public static LogEvent of(long sequenceNumber,
boolean isLoggable, String name,
String className, String methodName,
sun.util.logging.PlatformLogger.Level level, ResourceBundle bundle,
String key, Throwable thrown, Object... params) {
LogEvent evt = new LogEvent(sequenceNumber);
evt.loggerName = name;
evt.level = level;
evt.args = params;
evt.bundle = bundle;
evt.thrown = thrown;
evt.msg = key;
evt.isLoggable = isLoggable;
evt.className = className;
evt.methodName = methodName;
return evt;
}
public static LogEvent of(boolean isLoggable, String name,
String className, String methodName,
sun.util.logging.PlatformLogger.Level level, ResourceBundle bundle,
String key, Throwable thrown, Object... params) {
return LogEvent.of(sequencer.getAndIncrement(), isLoggable, name,
className, methodName, level, bundle, key, thrown, params);
}
public static LogEvent of(long sequenceNumber,
boolean isLoggable, String name,
String className, String methodName,
sun.util.logging.PlatformLogger.Level level, ResourceBundle bundle,
Supplier<String> supplier, Throwable thrown, Object... params) {
LogEvent evt = new LogEvent(sequenceNumber);
evt.loggerName = name;
evt.level = level;
evt.args = params;
evt.bundle = bundle;
evt.thrown = thrown;
evt.supplier = supplier;
evt.isLoggable = isLoggable;
evt.className = className;
evt.methodName = methodName;
return evt;
}
public static LogEvent of(boolean isLoggable, String name,
String className, String methodName,
sun.util.logging.PlatformLogger.Level level, ResourceBundle bundle,
Supplier<String> supplier, Throwable thrown, Object... params) {
return LogEvent.of(sequencer.getAndIncrement(), isLoggable, name,
className, methodName, level, bundle, supplier, thrown, params);
}
}
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;
}
@Override
public boolean isLoggable(Level level) {
return this.level != OFF && this.level.intValue() <= level.getSeverity();
}
@Override
public void log(Level level, ResourceBundle bundle,
String key, Throwable thrown) {
throw new UnsupportedOperationException();
}
@Override
public void log(Level level, ResourceBundle bundle,
String format, Object... params) {
throw new UnsupportedOperationException();
}
void log(LogEvent event) {
eventQueue.add(event);
}
@Override
public void log(Level level, Supplier<String> msgSupplier) {
throw new UnsupportedOperationException();
}
@Override
public void log(Level level, Supplier<String> msgSupplier,
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
public boolean isEnabled() {
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 {
allowAll.get().set(before);
}
}
static final sun.util.logging.PlatformLogger.Level[] julLevels = {
sun.util.logging.PlatformLogger.Level.ALL,
sun.util.logging.PlatformLogger.Level.FINEST,
sun.util.logging.PlatformLogger.Level.FINER,
sun.util.logging.PlatformLogger.Level.FINE,
sun.util.logging.PlatformLogger.Level.CONFIG,
sun.util.logging.PlatformLogger.Level.INFO,
sun.util.logging.PlatformLogger.Level.WARNING,
sun.util.logging.PlatformLogger.Level.SEVERE,
sun.util.logging.PlatformLogger.Level.OFF,
};
public static class MyBundle extends ResourceBundle {
final ConcurrentHashMap<String,String> map = new ConcurrentHashMap<>();
@Override
protected Object handleGetObject(String key) {
if (key.contains(" (translated)")) {
throw new RuntimeException("Unexpected key: " + key);
}
return map.computeIfAbsent(key, k -> k + " (translated)");
}
@Override
public Enumeration<String> getKeys() {
return Collections.enumeration(map.keySet());
}
}
public static class MyHandler extends Handler {
@Override
public java.util.logging.Level getLevel() {
return java.util.logging.Level.ALL;
}
@Override
public void publish(LogRecord record) {
eventQueue.add(LogEvent.of(sequencer.getAndIncrement(),
true, record.getLoggerName(),
record.getSourceClassName(),
record.getSourceMethodName(),
PlatformLogger.Level.valueOf(record.getLevel().getName()),
record.getResourceBundle(), record.getMessage(),
record.getThrown(), record.getParameters()));
}
@Override
public void flush() {
}
@Override
public void close() throws SecurityException {
}
}
public static class MyLoggerBundle extends MyBundle {
}
final static Method lazyGetLogger;
static {
// jdk.internal.logging.LoggerBridge.getLogger(name, caller)
try {
Class<?> bridgeClass = Class.forName("jdk.internal.logger.LazyLoggers");
lazyGetLogger = bridgeClass.getDeclaredMethod("getLogger",
String.class, Module.class);
lazyGetLogger.setAccessible(true);
} catch (Throwable ex) {
throw new ExceptionInInitializerError(ex);
}
}
static Logger getLogger(LoggerFinder provider, String name, Module caller) {
Logger logger;
try {
logger = Logger.class.cast(lazyGetLogger.invoke(null, name, caller));
} catch (Throwable x) {
Throwable t = (x instanceof InvocationTargetException) ?
((InvocationTargetException)x).getTargetException() : x;
if (t instanceof RuntimeException) {
throw (RuntimeException)t;
} else if (t instanceof Exception) {
throw new RuntimeException(t);
} else {
throw (Error)t;
}
}
// The method above does not throw exception...
// call the provider here to verify that an exception would have
// been thrown by the provider.
if (logger != null && caller == Thread.class.getModule()) {
Logger log = provider.getLogger(name, caller);
}
return logger;
}
static Logger getLogger(LoggerFinder provider, String name, ResourceBundle bundle, Module caller) {
if (getClassLoader(caller) != null) {
return System.getLogger(name,bundle);
} else {
return provider.getLocalizedLogger(name, bundle, caller);
}
}
static PlatformLogger.Bridge convert(Logger logger) {
return PlatformLogger.Bridge.convert(logger);
}
static enum TestCases {NOSECURITY, NOPERMISSIONS, WITHPERMISSIONS};
static void setSecurityManager() {
if (System.getSecurityManager() == null) {
Policy.setPolicy(new SimplePolicy(allowControl, allowAccess, allowAll));
System.setSecurityManager(new SecurityManager());
}
}
public static void main(String[] args) {
if (args.length == 0)
args = new String[] {
//"NOSECURITY",
"NOPERMISSIONS",
"WITHPERMISSIONS"
};
Stream.of(args).map(TestCases::valueOf).forEach((testCase) -> {
LoggerFinder provider;
switch (testCase) {
case NOSECURITY:
System.out.println("\n*** Without Security Manager\n");
provider = LoggerFinder.getLoggerFinder();
test(provider, true);
System.out.println("Tetscase count: " + sequencer.get());
break;
case NOPERMISSIONS:
System.out.println("\n*** With Security Manager, without permissions\n");
setSecurityManager();
try {
provider = LoggerFinder.getLoggerFinder();
throw new RuntimeException("Expected exception not raised");
} catch (AccessControlException x) {
if (!LOGGERFINDER_PERMISSION.equals(x.getPermission())) {
throw new RuntimeException("Unexpected permission check", x);
}
final boolean control = allowControl.get().get();
try {
allowControl.get().set(true);
provider = LoggerFinder.getLoggerFinder();
} finally {
allowControl.get().set(control);
}
}
test(provider, false);
System.out.println("Tetscase count: " + sequencer.get());
break;
case WITHPERMISSIONS:
System.out.println("\n*** With Security Manager, with control permission\n");
setSecurityManager();
final boolean control = allowControl.get().get();
try {
allowControl.get().set(true);
provider = LoggerFinder.getLoggerFinder();
test(provider, true);
} finally {
allowControl.get().set(control);
}
break;
default:
throw new RuntimeException("Unknown test case: " + testCase);
}
});
System.out.println("\nPASSED: Tested " + sequencer.get() + " cases.");
}
public static void test(LoggerFinder provider, boolean hasRequiredPermissions) {
ResourceBundle loggerBundle = ResourceBundle.getBundle(MyLoggerBundle.class.getName());
final Map<Object, String> loggerDescMap = new HashMap<>();
Logger appLogger1 = System.getLogger("foo");
loggerDescMap.put(appLogger1, "System.getLogger(\"foo\")");
Logger sysLogger1 = null;
try {
sysLogger1 = getLogger(provider, "foo", Thread.class.getModule());
loggerDescMap.put(sysLogger1, "provider.getLogger(\"foo\", Thread.class.getModule())");
if (!hasRequiredPermissions) {
throw new RuntimeException("Managed to obtain a system logger without permission");
}
} catch (AccessControlException acx) {
if (hasRequiredPermissions) {
throw new RuntimeException("Unexpected security exception: ", acx);
}
if (!acx.getPermission().equals(LOGGERFINDER_PERMISSION)) {
throw new RuntimeException("Unexpected permission in exception: " + acx, acx);
}
System.out.println("Got expected exception for system logger: " + acx);
}
Logger appLogger2 =
System.getLogger("foo", loggerBundle);
loggerDescMap.put(appLogger2, "System.getLogger(\"foo\", loggerBundle)");
Logger sysLogger2 = null;
try {
sysLogger2 = getLogger(provider, "foo", loggerBundle, Thread.class.getModule());
loggerDescMap.put(sysLogger2, "provider.getLogger(\"foo\", loggerBundle, Thread.class.getModule())");
if (!hasRequiredPermissions) {
throw new RuntimeException("Managed to obtain a system logger without permission");
}
} catch (AccessControlException acx) {
if (hasRequiredPermissions) {
throw new RuntimeException("Unexpected security exception: ", acx);
}
if (!acx.getPermission().equals(LOGGERFINDER_PERMISSION)) {
throw new RuntimeException("Unexpected permission in exception: " + acx, acx);
}
System.out.println("Got expected exception for localized system logger: " + acx);
}
if (hasRequiredPermissions && appLogger2 == sysLogger2) {
throw new RuntimeException("identical loggers");
}
if (appLogger2 == appLogger1) {
throw new RuntimeException("identical loggers");
}
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);
}
testLogger(provider, loggerDescMap, "foo", null, convert(appLogger1), appSink);
if (hasRequiredPermissions) {
testLogger(provider, loggerDescMap, "foo", null, convert(sysLogger1), sysSink);
}
testLogger(provider, loggerDescMap, "foo", loggerBundle, convert(appLogger2), appSink);
if (hasRequiredPermissions) {
testLogger(provider, loggerDescMap, "foo", loggerBundle, convert(sysLogger2), sysSink);
}
}
public static class Foo {
}
static void verbose(String msg) {
if (VERBOSE) {
System.out.println(msg);
}
}
static void checkLogEvent(LoggerFinder provider, String desc,
LogEvent expected) {
LogEvent actual = eventQueue.poll();
if (!expected.equals(actual)) {
throw new RuntimeException("mismatch for " + desc
+ "\n\texpected=" + expected
+ "\n\t actual=" + actual);
} else {
verbose("Got expected results for "
+ desc + "\n\t" + expected);
}
}
static void checkLogEvent(LoggerFinder provider, String desc,
LogEvent expected, boolean expectNotNull) {
LogEvent actual = eventQueue.poll();
if (actual == null && !expectNotNull) return;
if (actual != null && !expectNotNull) {
throw new RuntimeException("Unexpected log event found for " + desc
+ "\n\tgot: " + actual);
}
if (!expected.equals(actual)) {
throw new RuntimeException("mismatch for " + desc
+ "\n\texpected=" + expected
+ "\n\t actual=" + actual);
} else {
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();
String fooMsg = foo.toString();
System.out.println("\tlogger.log(messageLevel, fooMsg)");
System.out.println("\tlogger.<level>(fooMsg)");
for (sun.util.logging.PlatformLogger.Level loggerLevel : julLevels) {
setLevel(sink, loggerLevel);
for (sun.util.logging.PlatformLogger.Level messageLevel :julLevels) {
String desc = "logger.log(messageLevel, fooMsg): loggerLevel="
+ loggerLevel+", messageLevel="+messageLevel;
LogEvent expected =
LogEvent.of(
sequencer.get(),
loggerLevel != OFF && messageLevel.intValue() >= loggerLevel.intValue(),
name, messageLevel, loggerBundle,
fooMsg, (Throwable)null, (Object[])null);
logger.log(messageLevel, fooMsg);
checkLogEvent(provider, desc, expected);
}
}
Supplier<String> supplier = new Supplier<String>() {
@Override
public String get() {
return this.toString();
}
};
System.out.println("\tlogger.log(messageLevel, supplier)");
System.out.println("\tlogger.<level>(supplier)");
for (sun.util.logging.PlatformLogger.Level loggerLevel : julLevels) {
setLevel(sink, loggerLevel);
for (sun.util.logging.PlatformLogger.Level messageLevel :julLevels) {
String desc = "logger.log(messageLevel, supplier): loggerLevel="
+ loggerLevel+", messageLevel="+messageLevel;
LogEvent expected =
LogEvent.of(
sequencer.get(),
loggerLevel != OFF && messageLevel.intValue() >= loggerLevel.intValue(),
name, messageLevel, null,
supplier, (Throwable)null, (Object[])null);
logger.log(messageLevel, supplier);
checkLogEvent(provider, desc, expected);
}
}
String format = "two params [{1} {2}]";
Object arg1 = foo;
Object arg2 = fooMsg;
System.out.println("\tlogger.log(messageLevel, format, arg1, arg2)");
for (sun.util.logging.PlatformLogger.Level loggerLevel : julLevels) {
setLevel(sink, loggerLevel);
for (sun.util.logging.PlatformLogger.Level messageLevel :julLevels) {
String desc = "logger.log(messageLevel, format, foo, fooMsg): loggerLevel="
+ loggerLevel+", messageLevel="+messageLevel;
LogEvent expected =
LogEvent.of(
sequencer.get(),
loggerLevel != OFF && messageLevel.intValue() >= loggerLevel.intValue(),
name, messageLevel, loggerBundle,
format, (Throwable)null, arg1, arg2);
logger.log(messageLevel, format, arg1, arg2);
checkLogEvent(provider, desc, expected);
}
}
Throwable thrown = new Exception("OK: log me!");
System.out.println("\tlogger.log(messageLevel, fooMsg, thrown)");
for (sun.util.logging.PlatformLogger.Level loggerLevel : julLevels) {
setLevel(sink, loggerLevel);
for (sun.util.logging.PlatformLogger.Level messageLevel :julLevels) {
String desc = "logger.log(messageLevel, fooMsg, thrown): loggerLevel="
+ loggerLevel+", messageLevel="+messageLevel;
LogEvent expected =
LogEvent.of(
sequencer.get(),
loggerLevel != OFF && messageLevel.intValue() >= loggerLevel.intValue(),
name, messageLevel, loggerBundle,
fooMsg, thrown, (Object[])null);
logger.log(messageLevel, fooMsg, thrown);
checkLogEvent(provider, desc, expected);
}
}
System.out.println("\tlogger.log(messageLevel, thrown, supplier)");
for (sun.util.logging.PlatformLogger.Level loggerLevel : julLevels) {
setLevel(sink, loggerLevel);
for (sun.util.logging.PlatformLogger.Level messageLevel :julLevels) {
String desc = "logger.log(messageLevel, thrown, supplier): loggerLevel="
+ loggerLevel+", messageLevel="+messageLevel;
LogEvent expected =
LogEvent.of(
sequencer.get(),
loggerLevel != OFF && messageLevel.intValue() >= loggerLevel.intValue(),
name, messageLevel, null,
supplier, thrown, (Object[])null);
logger.log(messageLevel, thrown, supplier);
checkLogEvent(provider, desc, expected);
}
}
String sourceClass = "blah.Blah";
String sourceMethod = "blih";
System.out.println("\tlogger.logp(messageLevel, sourceClass, sourceMethod, fooMsg)");
for (sun.util.logging.PlatformLogger.Level loggerLevel : julLevels) {
setLevel(sink, loggerLevel);
for (sun.util.logging.PlatformLogger.Level messageLevel :julLevels) {
String desc = "logger.logp(messageLevel, sourceClass, sourceMethod, fooMsg): loggerLevel="
+ loggerLevel+", messageLevel="+messageLevel;
LogEvent expected =
LogEvent.of(
sequencer.get(),
loggerLevel != OFF && messageLevel.intValue() >= loggerLevel.intValue(),
name, sourceClass, sourceMethod, messageLevel, loggerBundle,
fooMsg, (Throwable)null, (Object[])null);
logger.logp(messageLevel, sourceClass, sourceMethod, fooMsg);
checkLogEvent(provider, desc, expected);
}
}
System.out.println("\tlogger.logp(messageLevel, sourceClass, sourceMethod, supplier)");
for (sun.util.logging.PlatformLogger.Level loggerLevel : julLevels) {
setLevel(sink, loggerLevel);
for (sun.util.logging.PlatformLogger.Level messageLevel :julLevels) {
String desc = "logger.logp(messageLevel, sourceClass, sourceMethod, supplier): loggerLevel="
+ loggerLevel+", messageLevel="+messageLevel;
LogEvent expected =
LogEvent.of(
sequencer.get(),
loggerLevel != OFF && messageLevel.intValue() >= loggerLevel.intValue(),
name, sourceClass, sourceMethod, messageLevel, null,
supplier, (Throwable)null, (Object[])null);
logger.logp(messageLevel, sourceClass, sourceMethod, supplier);
checkLogEvent(provider, desc, expected);
}
}
System.out.println("\tlogger.logp(messageLevel, sourceClass, sourceMethod, format, arg1, arg2)");
for (sun.util.logging.PlatformLogger.Level loggerLevel : julLevels) {
setLevel(sink, loggerLevel);
for (sun.util.logging.PlatformLogger.Level messageLevel :julLevels) {
String desc = "logger.logp(messageLevel, sourceClass, sourceMethod, format, arg1, arg2): loggerLevel="
+ loggerLevel+", messageLevel="+messageLevel;
LogEvent expected =
LogEvent.of(
sequencer.get(),
loggerLevel != OFF && messageLevel.intValue() >= loggerLevel.intValue(),
name, sourceClass, sourceMethod, messageLevel, loggerBundle,
format, (Throwable)null, arg1, arg2);
logger.logp(messageLevel, sourceClass, sourceMethod, format, arg1, arg2);
checkLogEvent(provider, desc, expected);
}
}
System.out.println("\tlogger.logp(messageLevel, sourceClass, sourceMethod, fooMsg, thrown)");
for (sun.util.logging.PlatformLogger.Level loggerLevel : julLevels) {
setLevel(sink, loggerLevel);
for (sun.util.logging.PlatformLogger.Level messageLevel :julLevels) {
String desc = "logger.logp(messageLevel, sourceClass, sourceMethod, fooMsg, thrown): loggerLevel="
+ loggerLevel+", messageLevel="+messageLevel;
LogEvent expected =
LogEvent.of(
sequencer.get(),
loggerLevel != OFF && messageLevel.intValue() >= loggerLevel.intValue(),
name, sourceClass, sourceMethod, messageLevel, loggerBundle,
fooMsg, thrown, (Object[])null);
logger.logp(messageLevel, sourceClass, sourceMethod, fooMsg, thrown);
checkLogEvent(provider, desc, expected);
}
}
System.out.println("\tlogger.logp(messageLevel, sourceClass, sourceMethod, thrown, supplier)");
for (sun.util.logging.PlatformLogger.Level loggerLevel : julLevels) {
setLevel(sink, loggerLevel);
for (sun.util.logging.PlatformLogger.Level messageLevel :julLevels) {
String desc = "logger.logp(messageLevel, sourceClass, sourceMethod, thrown, supplier): loggerLevel="
+ loggerLevel+", messageLevel="+messageLevel;
LogEvent expected =
LogEvent.of(
sequencer.get(),
loggerLevel != OFF && messageLevel.intValue() >= loggerLevel.intValue(),
name, sourceClass, sourceMethod, messageLevel, null,
supplier, thrown, (Object[])null);
logger.logp(messageLevel, sourceClass, sourceMethod, thrown, supplier);
checkLogEvent(provider, desc, expected);
}
}
ResourceBundle bundle = ResourceBundle.getBundle(MyBundle.class.getName());
System.out.println("\tlogger.logrb(messageLevel, bundle, format, arg1, arg2)");
for (sun.util.logging.PlatformLogger.Level loggerLevel : julLevels) {
setLevel(sink, loggerLevel);
for (sun.util.logging.PlatformLogger.Level messageLevel :julLevels) {
String desc = "logger.logrb(messageLevel, bundle, format, arg1, arg2): loggerLevel="
+ loggerLevel+", messageLevel="+messageLevel;
LogEvent expected =
LogEvent.of(
sequencer.get(),
loggerLevel != OFF && messageLevel.intValue() >= loggerLevel.intValue(),
name, messageLevel, bundle,
format, (Throwable)null, arg1, arg2);
logger.logrb(messageLevel, bundle, format, arg1, arg2);
checkLogEvent(provider, desc, expected);
}
}
System.out.println("\tlogger.logrb(messageLevel, bundle, msg, thrown)");
for (sun.util.logging.PlatformLogger.Level loggerLevel : julLevels) {
setLevel(sink, loggerLevel);
for (sun.util.logging.PlatformLogger.Level messageLevel :julLevels) {
String desc = "logger.logrb(messageLevel, bundle, msg, thrown): loggerLevel="
+ loggerLevel+", messageLevel="+messageLevel;
LogEvent expected =
LogEvent.of(
sequencer.get(),
loggerLevel != OFF && messageLevel.intValue() >= loggerLevel.intValue(),
name, messageLevel, bundle,
fooMsg, thrown, (Object[])null);
logger.logrb(messageLevel, bundle, fooMsg, thrown);
checkLogEvent(provider, desc, expected);
}
}
System.out.println("\tlogger.logrb(messageLevel, sourceClass, sourceMethod, bundle, format, arg1, arg2)");
for (sun.util.logging.PlatformLogger.Level loggerLevel : julLevels) {
setLevel(sink, loggerLevel);
for (sun.util.logging.PlatformLogger.Level messageLevel :julLevels) {
String desc = "logger.logrb(messageLevel, sourceClass, sourceMethod, bundle, format, arg1, arg2): loggerLevel="
+ loggerLevel+", messageLevel="+messageLevel;
LogEvent expected =
LogEvent.of(
sequencer.get(),
loggerLevel != OFF && messageLevel.intValue() >= loggerLevel.intValue(),
name, sourceClass, sourceMethod, messageLevel, bundle,
format, (Throwable)null, arg1, arg2);
logger.logrb(messageLevel, sourceClass, sourceMethod, bundle, format, arg1, arg2);
checkLogEvent(provider, desc, expected);
}
}
System.out.println("\tlogger.logrb(messageLevel, sourceClass, sourceMethod, bundle, msg, thrown)");
for (sun.util.logging.PlatformLogger.Level loggerLevel : julLevels) {
setLevel(sink, loggerLevel);
for (sun.util.logging.PlatformLogger.Level messageLevel :julLevels) {
String desc = "logger.logrb(messageLevel, sourceClass, sourceMethod, bundle, msg, thrown): loggerLevel="
+ loggerLevel+", messageLevel="+messageLevel;
LogEvent expected =
LogEvent.of(
sequencer.get(),
loggerLevel != OFF && messageLevel.intValue() >= loggerLevel.intValue(),
name, sourceClass, sourceMethod, messageLevel, bundle,
fooMsg, thrown, (Object[])null);
logger.logrb(messageLevel, sourceClass, sourceMethod, bundle, fooMsg, thrown);
checkLogEvent(provider, desc, expected);
}
}
}
final static class PermissionsBuilder {
final Permissions perms;
public PermissionsBuilder() {
this(new Permissions());
}
public PermissionsBuilder(Permissions perms) {
this.perms = perms;
}
public PermissionsBuilder add(Permission p) {
perms.add(p);
return this;
}
public PermissionsBuilder addAll(PermissionCollection col) {
if (col != null) {
for (Enumeration<Permission> e = col.elements(); e.hasMoreElements(); ) {
perms.add(e.nextElement());
}
}
return this;
}
public Permissions toPermissions() {
final PermissionsBuilder builder = new PermissionsBuilder();
builder.addAll(perms);
return builder.perms;
}
}
public static class SimplePolicy extends Policy {
final static RuntimePermission CONTROL = LOGGERFINDER_PERMISSION;
final static RuntimePermission ACCESS_LOGGER = new RuntimePermission("accessClassInPackage.jdk.internal.logger");
final static RuntimePermission ACCESS_LOGGING = new RuntimePermission("accessClassInPackage.sun.util.logging");
final Permissions permissions;
final Permissions allPermissions;
final ThreadLocal<AtomicBoolean> allowControl;
final ThreadLocal<AtomicBoolean> allowAccess;
final ThreadLocal<AtomicBoolean> allowAll;
public SimplePolicy(ThreadLocal<AtomicBoolean> allowControl,
ThreadLocal<AtomicBoolean> allowAccess,
ThreadLocal<AtomicBoolean> allowAll) {
this.allowControl = allowControl;
this.allowAccess = allowAccess;
this.allowAll = allowAll;
permissions = new Permissions();
allPermissions = new PermissionsBuilder()
.add(new java.security.AllPermission())
.toPermissions();
}
Permissions getPermissions() {
if (allowControl.get().get() || allowAccess.get().get() || allowAll.get().get()) {
PermissionsBuilder builder = new PermissionsBuilder()
.addAll(permissions);
if (allowControl.get().get()) {
builder.add(CONTROL);
}
if (allowAccess.get().get()) {
builder.add(ACCESS_LOGGER);
builder.add(ACCESS_LOGGING);
}
if (allowAll.get().get()) {
builder.addAll(allPermissions);
}
return builder.toPermissions();
}
return permissions;
}
@Override
public boolean implies(ProtectionDomain domain, Permission permission) {
return getPermissions().implies(permission);
}
@Override
public PermissionCollection getPermissions(CodeSource codesource) {
return new PermissionsBuilder().addAll(getPermissions()).toPermissions();
}
@Override
public PermissionCollection getPermissions(ProtectionDomain domain) {
return new PermissionsBuilder().addAll(getPermissions()).toPermissions();
}
}
}
|