jdk/test/java/lang/System/LoggerFinder/internal/BootstrapLogger/BootstrapLoggerTest.java
author dfuchs
Fri, 20 Nov 2015 19:26:16 +0100
changeset 33875 c1c71107d45f
child 34428 39f1caf048c3
permissions -rw-r--r--
8140364: JEP 264 Platform Logger API and Service Implementation Summary: Initial implementation for JEP 264 Platform Logger API and Service Reviewed-by: mchung, psandoz, rriggs
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
33875
c1c71107d45f 8140364: JEP 264 Platform Logger API and Service Implementation
dfuchs
parents:
diff changeset
     1
/*
c1c71107d45f 8140364: JEP 264 Platform Logger API and Service Implementation
dfuchs
parents:
diff changeset
     2
 * Copyright (c) 2014, Oracle and/or its affiliates. All rights reserved.
c1c71107d45f 8140364: JEP 264 Platform Logger API and Service Implementation
dfuchs
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
c1c71107d45f 8140364: JEP 264 Platform Logger API and Service Implementation
dfuchs
parents:
diff changeset
     4
 *
c1c71107d45f 8140364: JEP 264 Platform Logger API and Service Implementation
dfuchs
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
c1c71107d45f 8140364: JEP 264 Platform Logger API and Service Implementation
dfuchs
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
c1c71107d45f 8140364: JEP 264 Platform Logger API and Service Implementation
dfuchs
parents:
diff changeset
     7
 * published by the Free Software Foundation.
c1c71107d45f 8140364: JEP 264 Platform Logger API and Service Implementation
dfuchs
parents:
diff changeset
     8
 *
c1c71107d45f 8140364: JEP 264 Platform Logger API and Service Implementation
dfuchs
parents:
diff changeset
     9
 * This code is distributed in the hope that it will be useful, but WITHOUT
c1c71107d45f 8140364: JEP 264 Platform Logger API and Service Implementation
dfuchs
parents:
diff changeset
    10
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
c1c71107d45f 8140364: JEP 264 Platform Logger API and Service Implementation
dfuchs
parents:
diff changeset
    11
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
c1c71107d45f 8140364: JEP 264 Platform Logger API and Service Implementation
dfuchs
parents:
diff changeset
    12
 * version 2 for more details (a copy is included in the LICENSE file that
c1c71107d45f 8140364: JEP 264 Platform Logger API and Service Implementation
dfuchs
parents:
diff changeset
    13
 * accompanied this code).
c1c71107d45f 8140364: JEP 264 Platform Logger API and Service Implementation
dfuchs
parents:
diff changeset
    14
 *
c1c71107d45f 8140364: JEP 264 Platform Logger API and Service Implementation
dfuchs
parents:
diff changeset
    15
 * You should have received a copy of the GNU General Public License version
c1c71107d45f 8140364: JEP 264 Platform Logger API and Service Implementation
dfuchs
parents:
diff changeset
    16
 * 2 along with this work; if not, write to the Free Software Foundation,
c1c71107d45f 8140364: JEP 264 Platform Logger API and Service Implementation
dfuchs
parents:
diff changeset
    17
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
c1c71107d45f 8140364: JEP 264 Platform Logger API and Service Implementation
dfuchs
parents:
diff changeset
    18
 *
c1c71107d45f 8140364: JEP 264 Platform Logger API and Service Implementation
dfuchs
parents:
diff changeset
    19
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
c1c71107d45f 8140364: JEP 264 Platform Logger API and Service Implementation
dfuchs
parents:
diff changeset
    20
 * or visit www.oracle.com if you need additional information or have any
c1c71107d45f 8140364: JEP 264 Platform Logger API and Service Implementation
dfuchs
parents:
diff changeset
    21
 * questions.
c1c71107d45f 8140364: JEP 264 Platform Logger API and Service Implementation
dfuchs
parents:
diff changeset
    22
 */
c1c71107d45f 8140364: JEP 264 Platform Logger API and Service Implementation
dfuchs
parents:
diff changeset
    23
c1c71107d45f 8140364: JEP 264 Platform Logger API and Service Implementation
dfuchs
parents:
diff changeset
    24
import java.io.ByteArrayOutputStream;
c1c71107d45f 8140364: JEP 264 Platform Logger API and Service Implementation
dfuchs
parents:
diff changeset
    25
import java.io.OutputStream;
c1c71107d45f 8140364: JEP 264 Platform Logger API and Service Implementation
dfuchs
parents:
diff changeset
    26
import java.io.PrintStream;
c1c71107d45f 8140364: JEP 264 Platform Logger API and Service Implementation
dfuchs
parents:
diff changeset
    27
import java.lang.reflect.Array;
c1c71107d45f 8140364: JEP 264 Platform Logger API and Service Implementation
dfuchs
parents:
diff changeset
    28
import java.lang.reflect.Field;
c1c71107d45f 8140364: JEP 264 Platform Logger API and Service Implementation
dfuchs
parents:
diff changeset
    29
import java.util.concurrent.atomic.AtomicBoolean;
c1c71107d45f 8140364: JEP 264 Platform Logger API and Service Implementation
dfuchs
parents:
diff changeset
    30
import java.util.function.BooleanSupplier;
c1c71107d45f 8140364: JEP 264 Platform Logger API and Service Implementation
dfuchs
parents:
diff changeset
    31
import java.lang.System.Logger;
c1c71107d45f 8140364: JEP 264 Platform Logger API and Service Implementation
dfuchs
parents:
diff changeset
    32
import java.lang.System.Logger.Level;
c1c71107d45f 8140364: JEP 264 Platform Logger API and Service Implementation
dfuchs
parents:
diff changeset
    33
import java.lang.ref.ReferenceQueue;
c1c71107d45f 8140364: JEP 264 Platform Logger API and Service Implementation
dfuchs
parents:
diff changeset
    34
import java.lang.ref.WeakReference;
c1c71107d45f 8140364: JEP 264 Platform Logger API and Service Implementation
dfuchs
parents:
diff changeset
    35
import java.lang.reflect.InvocationTargetException;
c1c71107d45f 8140364: JEP 264 Platform Logger API and Service Implementation
dfuchs
parents:
diff changeset
    36
import java.lang.reflect.Method;
c1c71107d45f 8140364: JEP 264 Platform Logger API and Service Implementation
dfuchs
parents:
diff changeset
    37
import java.security.AllPermission;
c1c71107d45f 8140364: JEP 264 Platform Logger API and Service Implementation
dfuchs
parents:
diff changeset
    38
import java.security.CodeSource;
c1c71107d45f 8140364: JEP 264 Platform Logger API and Service Implementation
dfuchs
parents:
diff changeset
    39
import java.security.Permission;
c1c71107d45f 8140364: JEP 264 Platform Logger API and Service Implementation
dfuchs
parents:
diff changeset
    40
import java.security.PermissionCollection;
c1c71107d45f 8140364: JEP 264 Platform Logger API and Service Implementation
dfuchs
parents:
diff changeset
    41
import java.security.Permissions;
c1c71107d45f 8140364: JEP 264 Platform Logger API and Service Implementation
dfuchs
parents:
diff changeset
    42
import java.security.Policy;
c1c71107d45f 8140364: JEP 264 Platform Logger API and Service Implementation
dfuchs
parents:
diff changeset
    43
import java.security.ProtectionDomain;
c1c71107d45f 8140364: JEP 264 Platform Logger API and Service Implementation
dfuchs
parents:
diff changeset
    44
import java.util.Optional;
c1c71107d45f 8140364: JEP 264 Platform Logger API and Service Implementation
dfuchs
parents:
diff changeset
    45
import java.util.Set;
c1c71107d45f 8140364: JEP 264 Platform Logger API and Service Implementation
dfuchs
parents:
diff changeset
    46
import java.util.stream.Collectors;
c1c71107d45f 8140364: JEP 264 Platform Logger API and Service Implementation
dfuchs
parents:
diff changeset
    47
import java.util.stream.Stream;
c1c71107d45f 8140364: JEP 264 Platform Logger API and Service Implementation
dfuchs
parents:
diff changeset
    48
import jdk.internal.logger.BootstrapLogger;
c1c71107d45f 8140364: JEP 264 Platform Logger API and Service Implementation
dfuchs
parents:
diff changeset
    49
import jdk.internal.logger.LazyLoggers;
c1c71107d45f 8140364: JEP 264 Platform Logger API and Service Implementation
dfuchs
parents:
diff changeset
    50
c1c71107d45f 8140364: JEP 264 Platform Logger API and Service Implementation
dfuchs
parents:
diff changeset
    51
/*
c1c71107d45f 8140364: JEP 264 Platform Logger API and Service Implementation
dfuchs
parents:
diff changeset
    52
 * @test
c1c71107d45f 8140364: JEP 264 Platform Logger API and Service Implementation
dfuchs
parents:
diff changeset
    53
 * @bug     8140364
c1c71107d45f 8140364: JEP 264 Platform Logger API and Service Implementation
dfuchs
parents:
diff changeset
    54
 * @author  danielfuchs
c1c71107d45f 8140364: JEP 264 Platform Logger API and Service Implementation
dfuchs
parents:
diff changeset
    55
 * @summary JDK implementation specific unit test for JDK internal artifacts.
c1c71107d45f 8140364: JEP 264 Platform Logger API and Service Implementation
dfuchs
parents:
diff changeset
    56
            Tests the behavior of bootstrap loggers (and SimpleConsoleLoggers
c1c71107d45f 8140364: JEP 264 Platform Logger API and Service Implementation
dfuchs
parents:
diff changeset
    57
 *          too).
c1c71107d45f 8140364: JEP 264 Platform Logger API and Service Implementation
dfuchs
parents:
diff changeset
    58
 * @modules java.base/jdk.internal.logger
c1c71107d45f 8140364: JEP 264 Platform Logger API and Service Implementation
dfuchs
parents:
diff changeset
    59
 * @run main/othervm BootstrapLoggerTest NO_SECURITY
c1c71107d45f 8140364: JEP 264 Platform Logger API and Service Implementation
dfuchs
parents:
diff changeset
    60
 * @run main/othervm BootstrapLoggerTest SECURE
c1c71107d45f 8140364: JEP 264 Platform Logger API and Service Implementation
dfuchs
parents:
diff changeset
    61
 * @run main/othervm/timeout=120 BootstrapLoggerTest SECURE_AND_WAIT
c1c71107d45f 8140364: JEP 264 Platform Logger API and Service Implementation
dfuchs
parents:
diff changeset
    62
 */
c1c71107d45f 8140364: JEP 264 Platform Logger API and Service Implementation
dfuchs
parents:
diff changeset
    63
public class BootstrapLoggerTest {
c1c71107d45f 8140364: JEP 264 Platform Logger API and Service Implementation
dfuchs
parents:
diff changeset
    64
c1c71107d45f 8140364: JEP 264 Platform Logger API and Service Implementation
dfuchs
parents:
diff changeset
    65
    static final Method awaitPending;
c1c71107d45f 8140364: JEP 264 Platform Logger API and Service Implementation
dfuchs
parents:
diff changeset
    66
    static final Method isAlive;
c1c71107d45f 8140364: JEP 264 Platform Logger API and Service Implementation
dfuchs
parents:
diff changeset
    67
    static final Field isBooted;
c1c71107d45f 8140364: JEP 264 Platform Logger API and Service Implementation
dfuchs
parents:
diff changeset
    68
    static final Field logManagerInitialized;
c1c71107d45f 8140364: JEP 264 Platform Logger API and Service Implementation
dfuchs
parents:
diff changeset
    69
    static {
c1c71107d45f 8140364: JEP 264 Platform Logger API and Service Implementation
dfuchs
parents:
diff changeset
    70
        try {
c1c71107d45f 8140364: JEP 264 Platform Logger API and Service Implementation
dfuchs
parents:
diff changeset
    71
            isBooted = BootstrapLogger.class.getDeclaredField("isBooted");
c1c71107d45f 8140364: JEP 264 Platform Logger API and Service Implementation
dfuchs
parents:
diff changeset
    72
            isBooted.setAccessible(true);
c1c71107d45f 8140364: JEP 264 Platform Logger API and Service Implementation
dfuchs
parents:
diff changeset
    73
            // private reflection hook that allows us to test wait until all
c1c71107d45f 8140364: JEP 264 Platform Logger API and Service Implementation
dfuchs
parents:
diff changeset
    74
            // the tasks pending in the BootstrapExecutor are finished.
c1c71107d45f 8140364: JEP 264 Platform Logger API and Service Implementation
dfuchs
parents:
diff changeset
    75
            awaitPending = BootstrapLogger.class
c1c71107d45f 8140364: JEP 264 Platform Logger API and Service Implementation
dfuchs
parents:
diff changeset
    76
                    .getDeclaredMethod("awaitPendingTasks");
c1c71107d45f 8140364: JEP 264 Platform Logger API and Service Implementation
dfuchs
parents:
diff changeset
    77
            awaitPending.setAccessible(true);
c1c71107d45f 8140364: JEP 264 Platform Logger API and Service Implementation
dfuchs
parents:
diff changeset
    78
            // private reflection hook that allows us to test whether
c1c71107d45f 8140364: JEP 264 Platform Logger API and Service Implementation
dfuchs
parents:
diff changeset
    79
            // the BootstrapExecutor is alive.
c1c71107d45f 8140364: JEP 264 Platform Logger API and Service Implementation
dfuchs
parents:
diff changeset
    80
            isAlive = BootstrapLogger.class
c1c71107d45f 8140364: JEP 264 Platform Logger API and Service Implementation
dfuchs
parents:
diff changeset
    81
                    .getDeclaredMethod("isAlive");
c1c71107d45f 8140364: JEP 264 Platform Logger API and Service Implementation
dfuchs
parents:
diff changeset
    82
            isAlive.setAccessible(true);
c1c71107d45f 8140364: JEP 264 Platform Logger API and Service Implementation
dfuchs
parents:
diff changeset
    83
            // private reflection hook that allows us to test whether the LogManager
c1c71107d45f 8140364: JEP 264 Platform Logger API and Service Implementation
dfuchs
parents:
diff changeset
    84
            // has initialized and registered with the BootstrapLogger class
c1c71107d45f 8140364: JEP 264 Platform Logger API and Service Implementation
dfuchs
parents:
diff changeset
    85
            logManagerInitialized = BootstrapLogger.class
c1c71107d45f 8140364: JEP 264 Platform Logger API and Service Implementation
dfuchs
parents:
diff changeset
    86
                    .getDeclaredField("logManagerConfigured");
c1c71107d45f 8140364: JEP 264 Platform Logger API and Service Implementation
dfuchs
parents:
diff changeset
    87
            logManagerInitialized.setAccessible(true);
c1c71107d45f 8140364: JEP 264 Platform Logger API and Service Implementation
dfuchs
parents:
diff changeset
    88
        } catch (Exception ex) {
c1c71107d45f 8140364: JEP 264 Platform Logger API and Service Implementation
dfuchs
parents:
diff changeset
    89
            throw new ExceptionInInitializerError(ex);
c1c71107d45f 8140364: JEP 264 Platform Logger API and Service Implementation
dfuchs
parents:
diff changeset
    90
        }
c1c71107d45f 8140364: JEP 264 Platform Logger API and Service Implementation
dfuchs
parents:
diff changeset
    91
    }
c1c71107d45f 8140364: JEP 264 Platform Logger API and Service Implementation
dfuchs
parents:
diff changeset
    92
c1c71107d45f 8140364: JEP 264 Platform Logger API and Service Implementation
dfuchs
parents:
diff changeset
    93
    static void awaitPending() {
c1c71107d45f 8140364: JEP 264 Platform Logger API and Service Implementation
dfuchs
parents:
diff changeset
    94
        try {
c1c71107d45f 8140364: JEP 264 Platform Logger API and Service Implementation
dfuchs
parents:
diff changeset
    95
            awaitPending.invoke(null);
c1c71107d45f 8140364: JEP 264 Platform Logger API and Service Implementation
dfuchs
parents:
diff changeset
    96
        } catch (IllegalAccessException | IllegalArgumentException | InvocationTargetException ex) {
c1c71107d45f 8140364: JEP 264 Platform Logger API and Service Implementation
dfuchs
parents:
diff changeset
    97
            ex.printStackTrace(LogStream.err);
c1c71107d45f 8140364: JEP 264 Platform Logger API and Service Implementation
dfuchs
parents:
diff changeset
    98
        }
c1c71107d45f 8140364: JEP 264 Platform Logger API and Service Implementation
dfuchs
parents:
diff changeset
    99
    }
c1c71107d45f 8140364: JEP 264 Platform Logger API and Service Implementation
dfuchs
parents:
diff changeset
   100
c1c71107d45f 8140364: JEP 264 Platform Logger API and Service Implementation
dfuchs
parents:
diff changeset
   101
    /**
c1c71107d45f 8140364: JEP 264 Platform Logger API and Service Implementation
dfuchs
parents:
diff changeset
   102
     * We use an instance of this class to check what the logging system has
c1c71107d45f 8140364: JEP 264 Platform Logger API and Service Implementation
dfuchs
parents:
diff changeset
   103
     * printed on System.err.
c1c71107d45f 8140364: JEP 264 Platform Logger API and Service Implementation
dfuchs
parents:
diff changeset
   104
     */
c1c71107d45f 8140364: JEP 264 Platform Logger API and Service Implementation
dfuchs
parents:
diff changeset
   105
    public static class LogStream extends OutputStream {
c1c71107d45f 8140364: JEP 264 Platform Logger API and Service Implementation
dfuchs
parents:
diff changeset
   106
c1c71107d45f 8140364: JEP 264 Platform Logger API and Service Implementation
dfuchs
parents:
diff changeset
   107
        final static PrintStream err = System.err;
c1c71107d45f 8140364: JEP 264 Platform Logger API and Service Implementation
dfuchs
parents:
diff changeset
   108
        final ByteArrayOutputStream baos = new ByteArrayOutputStream();
c1c71107d45f 8140364: JEP 264 Platform Logger API and Service Implementation
dfuchs
parents:
diff changeset
   109
c1c71107d45f 8140364: JEP 264 Platform Logger API and Service Implementation
dfuchs
parents:
diff changeset
   110
        public LogStream() {
c1c71107d45f 8140364: JEP 264 Platform Logger API and Service Implementation
dfuchs
parents:
diff changeset
   111
            super();
c1c71107d45f 8140364: JEP 264 Platform Logger API and Service Implementation
dfuchs
parents:
diff changeset
   112
        }
c1c71107d45f 8140364: JEP 264 Platform Logger API and Service Implementation
dfuchs
parents:
diff changeset
   113
c1c71107d45f 8140364: JEP 264 Platform Logger API and Service Implementation
dfuchs
parents:
diff changeset
   114
        @Override
c1c71107d45f 8140364: JEP 264 Platform Logger API and Service Implementation
dfuchs
parents:
diff changeset
   115
        public synchronized void write(int b) {
c1c71107d45f 8140364: JEP 264 Platform Logger API and Service Implementation
dfuchs
parents:
diff changeset
   116
            baos.write(b);
c1c71107d45f 8140364: JEP 264 Platform Logger API and Service Implementation
dfuchs
parents:
diff changeset
   117
            err.write(b);
c1c71107d45f 8140364: JEP 264 Platform Logger API and Service Implementation
dfuchs
parents:
diff changeset
   118
        }
c1c71107d45f 8140364: JEP 264 Platform Logger API and Service Implementation
dfuchs
parents:
diff changeset
   119
c1c71107d45f 8140364: JEP 264 Platform Logger API and Service Implementation
dfuchs
parents:
diff changeset
   120
        public String drain() {
c1c71107d45f 8140364: JEP 264 Platform Logger API and Service Implementation
dfuchs
parents:
diff changeset
   121
            awaitPending();
c1c71107d45f 8140364: JEP 264 Platform Logger API and Service Implementation
dfuchs
parents:
diff changeset
   122
            synchronized(this) {
c1c71107d45f 8140364: JEP 264 Platform Logger API and Service Implementation
dfuchs
parents:
diff changeset
   123
                String txt = baos.toString();
c1c71107d45f 8140364: JEP 264 Platform Logger API and Service Implementation
dfuchs
parents:
diff changeset
   124
                baos.reset();
c1c71107d45f 8140364: JEP 264 Platform Logger API and Service Implementation
dfuchs
parents:
diff changeset
   125
                return txt;
c1c71107d45f 8140364: JEP 264 Platform Logger API and Service Implementation
dfuchs
parents:
diff changeset
   126
            }
c1c71107d45f 8140364: JEP 264 Platform Logger API and Service Implementation
dfuchs
parents:
diff changeset
   127
        }
c1c71107d45f 8140364: JEP 264 Platform Logger API and Service Implementation
dfuchs
parents:
diff changeset
   128
    }
c1c71107d45f 8140364: JEP 264 Platform Logger API and Service Implementation
dfuchs
parents:
diff changeset
   129
c1c71107d45f 8140364: JEP 264 Platform Logger API and Service Implementation
dfuchs
parents:
diff changeset
   130
    static enum TestCase {
c1c71107d45f 8140364: JEP 264 Platform Logger API and Service Implementation
dfuchs
parents:
diff changeset
   131
        NO_SECURITY, SECURE, SECURE_AND_WAIT
c1c71107d45f 8140364: JEP 264 Platform Logger API and Service Implementation
dfuchs
parents:
diff changeset
   132
    }
c1c71107d45f 8140364: JEP 264 Platform Logger API and Service Implementation
dfuchs
parents:
diff changeset
   133
c1c71107d45f 8140364: JEP 264 Platform Logger API and Service Implementation
dfuchs
parents:
diff changeset
   134
    public static void main(String[] args) throws Exception {
c1c71107d45f 8140364: JEP 264 Platform Logger API and Service Implementation
dfuchs
parents:
diff changeset
   135
        if (args == null || args.length == 0) {
c1c71107d45f 8140364: JEP 264 Platform Logger API and Service Implementation
dfuchs
parents:
diff changeset
   136
            args = new String[] { TestCase.SECURE_AND_WAIT.name() };
c1c71107d45f 8140364: JEP 264 Platform Logger API and Service Implementation
dfuchs
parents:
diff changeset
   137
        }
c1c71107d45f 8140364: JEP 264 Platform Logger API and Service Implementation
dfuchs
parents:
diff changeset
   138
        if (args.length > 1) throw new RuntimeException("Only one argument allowed");
c1c71107d45f 8140364: JEP 264 Platform Logger API and Service Implementation
dfuchs
parents:
diff changeset
   139
        TestCase test = TestCase.valueOf(args[0]);
c1c71107d45f 8140364: JEP 264 Platform Logger API and Service Implementation
dfuchs
parents:
diff changeset
   140
        System.err.println("Testing: " + test);
c1c71107d45f 8140364: JEP 264 Platform Logger API and Service Implementation
dfuchs
parents:
diff changeset
   141
c1c71107d45f 8140364: JEP 264 Platform Logger API and Service Implementation
dfuchs
parents:
diff changeset
   142
c1c71107d45f 8140364: JEP 264 Platform Logger API and Service Implementation
dfuchs
parents:
diff changeset
   143
        // private reflection hook that allows us to simulate a non booted VM
c1c71107d45f 8140364: JEP 264 Platform Logger API and Service Implementation
dfuchs
parents:
diff changeset
   144
        final AtomicBoolean vmBooted = new AtomicBoolean(false);
c1c71107d45f 8140364: JEP 264 Platform Logger API and Service Implementation
dfuchs
parents:
diff changeset
   145
        isBooted.set(null,(BooleanSupplier) () -> vmBooted.get());
c1c71107d45f 8140364: JEP 264 Platform Logger API and Service Implementation
dfuchs
parents:
diff changeset
   146
c1c71107d45f 8140364: JEP 264 Platform Logger API and Service Implementation
dfuchs
parents:
diff changeset
   147
        // We  replace System.err to check the messages that have been logged
c1c71107d45f 8140364: JEP 264 Platform Logger API and Service Implementation
dfuchs
parents:
diff changeset
   148
        // by the JUL ConsoleHandler and default SimpleConsoleLogger
c1c71107d45f 8140364: JEP 264 Platform Logger API and Service Implementation
dfuchs
parents:
diff changeset
   149
        // implementaion
c1c71107d45f 8140364: JEP 264 Platform Logger API and Service Implementation
dfuchs
parents:
diff changeset
   150
        final LogStream err = new LogStream();
c1c71107d45f 8140364: JEP 264 Platform Logger API and Service Implementation
dfuchs
parents:
diff changeset
   151
        System.setErr(new PrintStream(err));
c1c71107d45f 8140364: JEP 264 Platform Logger API and Service Implementation
dfuchs
parents:
diff changeset
   152
c1c71107d45f 8140364: JEP 264 Platform Logger API and Service Implementation
dfuchs
parents:
diff changeset
   153
        if (BootstrapLogger.isBooted()) {
c1c71107d45f 8140364: JEP 264 Platform Logger API and Service Implementation
dfuchs
parents:
diff changeset
   154
            throw new RuntimeException("VM should not be booted!");
c1c71107d45f 8140364: JEP 264 Platform Logger API and Service Implementation
dfuchs
parents:
diff changeset
   155
        }
c1c71107d45f 8140364: JEP 264 Platform Logger API and Service Implementation
dfuchs
parents:
diff changeset
   156
        Logger logger = LazyLoggers.getLogger("foo.bar", Thread.class);
c1c71107d45f 8140364: JEP 264 Platform Logger API and Service Implementation
dfuchs
parents:
diff changeset
   157
c1c71107d45f 8140364: JEP 264 Platform Logger API and Service Implementation
dfuchs
parents:
diff changeset
   158
        if (test != TestCase.NO_SECURITY) {
c1c71107d45f 8140364: JEP 264 Platform Logger API and Service Implementation
dfuchs
parents:
diff changeset
   159
            LogStream.err.println("Setting security manager");
c1c71107d45f 8140364: JEP 264 Platform Logger API and Service Implementation
dfuchs
parents:
diff changeset
   160
            Policy.setPolicy(new SimplePolicy());
c1c71107d45f 8140364: JEP 264 Platform Logger API and Service Implementation
dfuchs
parents:
diff changeset
   161
            System.setSecurityManager(new SecurityManager());
c1c71107d45f 8140364: JEP 264 Platform Logger API and Service Implementation
dfuchs
parents:
diff changeset
   162
        }
c1c71107d45f 8140364: JEP 264 Platform Logger API and Service Implementation
dfuchs
parents:
diff changeset
   163
c1c71107d45f 8140364: JEP 264 Platform Logger API and Service Implementation
dfuchs
parents:
diff changeset
   164
        Level[] levels = {Level.INFO, Level.WARNING, Level.INFO};
c1c71107d45f 8140364: JEP 264 Platform Logger API and Service Implementation
dfuchs
parents:
diff changeset
   165
        int index = 0;
c1c71107d45f 8140364: JEP 264 Platform Logger API and Service Implementation
dfuchs
parents:
diff changeset
   166
        logger.log(levels[index], "Early message #" + (index+1)); index++;
c1c71107d45f 8140364: JEP 264 Platform Logger API and Service Implementation
dfuchs
parents:
diff changeset
   167
        logger.log(levels[index], "Early message #" + (index+1)); index++;
c1c71107d45f 8140364: JEP 264 Platform Logger API and Service Implementation
dfuchs
parents:
diff changeset
   168
        LogStream.err.println("VM Booted: " + vmBooted.get());
c1c71107d45f 8140364: JEP 264 Platform Logger API and Service Implementation
dfuchs
parents:
diff changeset
   169
        LogStream.err.println("LogManager initialized: " + logManagerInitialized.get(null));
c1c71107d45f 8140364: JEP 264 Platform Logger API and Service Implementation
dfuchs
parents:
diff changeset
   170
        logger.log(levels[index], "Early message #" + (index+1)); index++;
c1c71107d45f 8140364: JEP 264 Platform Logger API and Service Implementation
dfuchs
parents:
diff changeset
   171
        if (err.drain().contains("Early message")) {
c1c71107d45f 8140364: JEP 264 Platform Logger API and Service Implementation
dfuchs
parents:
diff changeset
   172
            // We're expecting that logger will be a LazyLogger wrapping a
c1c71107d45f 8140364: JEP 264 Platform Logger API and Service Implementation
dfuchs
parents:
diff changeset
   173
            // BootstrapLogger. The Bootstrap logger will stack the log messages
c1c71107d45f 8140364: JEP 264 Platform Logger API and Service Implementation
dfuchs
parents:
diff changeset
   174
            // it receives until the VM is booted.
c1c71107d45f 8140364: JEP 264 Platform Logger API and Service Implementation
dfuchs
parents:
diff changeset
   175
            // Since our private hook pretend that the VM is not booted yet,
c1c71107d45f 8140364: JEP 264 Platform Logger API and Service Implementation
dfuchs
parents:
diff changeset
   176
            // the logged messages shouldn't have reached System.err yet.
c1c71107d45f 8140364: JEP 264 Platform Logger API and Service Implementation
dfuchs
parents:
diff changeset
   177
            throw new RuntimeException("Early message logged while VM is not booted!");
c1c71107d45f 8140364: JEP 264 Platform Logger API and Service Implementation
dfuchs
parents:
diff changeset
   178
        }
c1c71107d45f 8140364: JEP 264 Platform Logger API and Service Implementation
dfuchs
parents:
diff changeset
   179
c1c71107d45f 8140364: JEP 264 Platform Logger API and Service Implementation
dfuchs
parents:
diff changeset
   180
        // Now pretend that the VM is booted. Nothing should happen yet, until
c1c71107d45f 8140364: JEP 264 Platform Logger API and Service Implementation
dfuchs
parents:
diff changeset
   181
        // we try to log a new message.
c1c71107d45f 8140364: JEP 264 Platform Logger API and Service Implementation
dfuchs
parents:
diff changeset
   182
        vmBooted.getAndSet(true);
c1c71107d45f 8140364: JEP 264 Platform Logger API and Service Implementation
dfuchs
parents:
diff changeset
   183
        LogStream.err.println("VM Booted: " + vmBooted.get());
c1c71107d45f 8140364: JEP 264 Platform Logger API and Service Implementation
dfuchs
parents:
diff changeset
   184
        LogStream.err.println("LogManager initialized: " + logManagerInitialized.get(null));
c1c71107d45f 8140364: JEP 264 Platform Logger API and Service Implementation
dfuchs
parents:
diff changeset
   185
        if (!BootstrapLogger.isBooted()) {
c1c71107d45f 8140364: JEP 264 Platform Logger API and Service Implementation
dfuchs
parents:
diff changeset
   186
            throw new RuntimeException("VM should now be booted!");
c1c71107d45f 8140364: JEP 264 Platform Logger API and Service Implementation
dfuchs
parents:
diff changeset
   187
        }
c1c71107d45f 8140364: JEP 264 Platform Logger API and Service Implementation
dfuchs
parents:
diff changeset
   188
        if (((Boolean)logManagerInitialized.get(null)).booleanValue()) {
c1c71107d45f 8140364: JEP 264 Platform Logger API and Service Implementation
dfuchs
parents:
diff changeset
   189
            throw new RuntimeException("LogManager shouldn't be initialized yet!");
c1c71107d45f 8140364: JEP 264 Platform Logger API and Service Implementation
dfuchs
parents:
diff changeset
   190
        }
c1c71107d45f 8140364: JEP 264 Platform Logger API and Service Implementation
dfuchs
parents:
diff changeset
   191
c1c71107d45f 8140364: JEP 264 Platform Logger API and Service Implementation
dfuchs
parents:
diff changeset
   192
        // Logging a message should cause the BootstrapLogger to replace itself
c1c71107d45f 8140364: JEP 264 Platform Logger API and Service Implementation
dfuchs
parents:
diff changeset
   193
        // by a 'real' logger in the LazyLogger. But since the LogManager isn't
c1c71107d45f 8140364: JEP 264 Platform Logger API and Service Implementation
dfuchs
parents:
diff changeset
   194
        // initialized yet, this should be a SimpleConsoleLogger...
c1c71107d45f 8140364: JEP 264 Platform Logger API and Service Implementation
dfuchs
parents:
diff changeset
   195
        logger.log(Level.INFO, "LOG#4: VM now booted: {0}", vmBooted.get());
c1c71107d45f 8140364: JEP 264 Platform Logger API and Service Implementation
dfuchs
parents:
diff changeset
   196
        logger.log(Level.DEBUG, "LOG#5: hi!");
c1c71107d45f 8140364: JEP 264 Platform Logger API and Service Implementation
dfuchs
parents:
diff changeset
   197
        SimplePolicy.allowAll.set(Boolean.TRUE);
c1c71107d45f 8140364: JEP 264 Platform Logger API and Service Implementation
dfuchs
parents:
diff changeset
   198
        WeakReference<Thread> threadRef = null;
c1c71107d45f 8140364: JEP 264 Platform Logger API and Service Implementation
dfuchs
parents:
diff changeset
   199
        ReferenceQueue<Thread> queue = new ReferenceQueue<>();
c1c71107d45f 8140364: JEP 264 Platform Logger API and Service Implementation
dfuchs
parents:
diff changeset
   200
        try {
c1c71107d45f 8140364: JEP 264 Platform Logger API and Service Implementation
dfuchs
parents:
diff changeset
   201
            Set<Thread> set = Thread.getAllStackTraces().keySet().stream()
c1c71107d45f 8140364: JEP 264 Platform Logger API and Service Implementation
dfuchs
parents:
diff changeset
   202
                    .filter((t) -> t.getName().startsWith("BootstrapMessageLoggerTask-"))
c1c71107d45f 8140364: JEP 264 Platform Logger API and Service Implementation
dfuchs
parents:
diff changeset
   203
                    .collect(Collectors.toSet());
c1c71107d45f 8140364: JEP 264 Platform Logger API and Service Implementation
dfuchs
parents:
diff changeset
   204
            set.stream().forEach(t -> LogStream.err.println("Found: " + t));
c1c71107d45f 8140364: JEP 264 Platform Logger API and Service Implementation
dfuchs
parents:
diff changeset
   205
            if (set.size() > 1) {
c1c71107d45f 8140364: JEP 264 Platform Logger API and Service Implementation
dfuchs
parents:
diff changeset
   206
                throw new RuntimeException("Too many bootsrap threads found");
c1c71107d45f 8140364: JEP 264 Platform Logger API and Service Implementation
dfuchs
parents:
diff changeset
   207
            }
c1c71107d45f 8140364: JEP 264 Platform Logger API and Service Implementation
dfuchs
parents:
diff changeset
   208
            Optional<Thread> t = set.stream().findFirst();
c1c71107d45f 8140364: JEP 264 Platform Logger API and Service Implementation
dfuchs
parents:
diff changeset
   209
            if (t.isPresent()) {
c1c71107d45f 8140364: JEP 264 Platform Logger API and Service Implementation
dfuchs
parents:
diff changeset
   210
                threadRef = new WeakReference<>(t.get(), queue);
c1c71107d45f 8140364: JEP 264 Platform Logger API and Service Implementation
dfuchs
parents:
diff changeset
   211
            }
c1c71107d45f 8140364: JEP 264 Platform Logger API and Service Implementation
dfuchs
parents:
diff changeset
   212
        } finally{
c1c71107d45f 8140364: JEP 264 Platform Logger API and Service Implementation
dfuchs
parents:
diff changeset
   213
            SimplePolicy.allowAll.set(Boolean.FALSE);
c1c71107d45f 8140364: JEP 264 Platform Logger API and Service Implementation
dfuchs
parents:
diff changeset
   214
        }
c1c71107d45f 8140364: JEP 264 Platform Logger API and Service Implementation
dfuchs
parents:
diff changeset
   215
        if (!BootstrapLogger.isBooted()) {
c1c71107d45f 8140364: JEP 264 Platform Logger API and Service Implementation
dfuchs
parents:
diff changeset
   216
            throw new RuntimeException("VM should still be booted!");
c1c71107d45f 8140364: JEP 264 Platform Logger API and Service Implementation
dfuchs
parents:
diff changeset
   217
        }
c1c71107d45f 8140364: JEP 264 Platform Logger API and Service Implementation
dfuchs
parents:
diff changeset
   218
        if (((Boolean)logManagerInitialized.get(null)).booleanValue()) {
c1c71107d45f 8140364: JEP 264 Platform Logger API and Service Implementation
dfuchs
parents:
diff changeset
   219
            throw new RuntimeException("LogManager shouldn't be initialized yet!");
c1c71107d45f 8140364: JEP 264 Platform Logger API and Service Implementation
dfuchs
parents:
diff changeset
   220
        }
c1c71107d45f 8140364: JEP 264 Platform Logger API and Service Implementation
dfuchs
parents:
diff changeset
   221
c1c71107d45f 8140364: JEP 264 Platform Logger API and Service Implementation
dfuchs
parents:
diff changeset
   222
        // Now check that the early messages we had printed before the VM was
c1c71107d45f 8140364: JEP 264 Platform Logger API and Service Implementation
dfuchs
parents:
diff changeset
   223
        // booted have appeared on System.err...
c1c71107d45f 8140364: JEP 264 Platform Logger API and Service Implementation
dfuchs
parents:
diff changeset
   224
        String afterBoot = err.drain();
c1c71107d45f 8140364: JEP 264 Platform Logger API and Service Implementation
dfuchs
parents:
diff changeset
   225
        for (int i=0; i<levels.length; i++) {
c1c71107d45f 8140364: JEP 264 Platform Logger API and Service Implementation
dfuchs
parents:
diff changeset
   226
            String m = levels[i].getName()+": Early message #"+(i+1);
c1c71107d45f 8140364: JEP 264 Platform Logger API and Service Implementation
dfuchs
parents:
diff changeset
   227
            if (!afterBoot.contains(m)) {
c1c71107d45f 8140364: JEP 264 Platform Logger API and Service Implementation
dfuchs
parents:
diff changeset
   228
                throw new RuntimeException("System.err does not contain: "+m);
c1c71107d45f 8140364: JEP 264 Platform Logger API and Service Implementation
dfuchs
parents:
diff changeset
   229
            }
c1c71107d45f 8140364: JEP 264 Platform Logger API and Service Implementation
dfuchs
parents:
diff changeset
   230
        }
c1c71107d45f 8140364: JEP 264 Platform Logger API and Service Implementation
dfuchs
parents:
diff changeset
   231
        // check that the message logged *after* the VM was booted also printed.
c1c71107d45f 8140364: JEP 264 Platform Logger API and Service Implementation
dfuchs
parents:
diff changeset
   232
        if (!afterBoot.contains("INFO: LOG#4")) {
c1c71107d45f 8140364: JEP 264 Platform Logger API and Service Implementation
dfuchs
parents:
diff changeset
   233
            throw new RuntimeException("System.err does not contain: "
c1c71107d45f 8140364: JEP 264 Platform Logger API and Service Implementation
dfuchs
parents:
diff changeset
   234
                    + "INFO: LOG#4");
c1c71107d45f 8140364: JEP 264 Platform Logger API and Service Implementation
dfuchs
parents:
diff changeset
   235
        }
c1c71107d45f 8140364: JEP 264 Platform Logger API and Service Implementation
dfuchs
parents:
diff changeset
   236
        // check that the debug message was not printed.
c1c71107d45f 8140364: JEP 264 Platform Logger API and Service Implementation
dfuchs
parents:
diff changeset
   237
        if (afterBoot.contains("LOG#5")) {
c1c71107d45f 8140364: JEP 264 Platform Logger API and Service Implementation
dfuchs
parents:
diff changeset
   238
            throw new RuntimeException("System.err contain: " + "LOG#5");
c1c71107d45f 8140364: JEP 264 Platform Logger API and Service Implementation
dfuchs
parents:
diff changeset
   239
        }
c1c71107d45f 8140364: JEP 264 Platform Logger API and Service Implementation
dfuchs
parents:
diff changeset
   240
        LogStream.err.println("VM Booted: " + vmBooted.get());
c1c71107d45f 8140364: JEP 264 Platform Logger API and Service Implementation
dfuchs
parents:
diff changeset
   241
        LogStream.err.println("LogManager initialized: " + logManagerInitialized.get(null));
c1c71107d45f 8140364: JEP 264 Platform Logger API and Service Implementation
dfuchs
parents:
diff changeset
   242
        if (!BootstrapLogger.isBooted()) {
c1c71107d45f 8140364: JEP 264 Platform Logger API and Service Implementation
dfuchs
parents:
diff changeset
   243
            throw new RuntimeException("VM should still be booted!");
c1c71107d45f 8140364: JEP 264 Platform Logger API and Service Implementation
dfuchs
parents:
diff changeset
   244
        }
c1c71107d45f 8140364: JEP 264 Platform Logger API and Service Implementation
dfuchs
parents:
diff changeset
   245
        if (((Boolean)logManagerInitialized.get(null)).booleanValue()) {
c1c71107d45f 8140364: JEP 264 Platform Logger API and Service Implementation
dfuchs
parents:
diff changeset
   246
            throw new RuntimeException("LogManager shouldn't be initialized yet!");
c1c71107d45f 8140364: JEP 264 Platform Logger API and Service Implementation
dfuchs
parents:
diff changeset
   247
        }
c1c71107d45f 8140364: JEP 264 Platform Logger API and Service Implementation
dfuchs
parents:
diff changeset
   248
c1c71107d45f 8140364: JEP 264 Platform Logger API and Service Implementation
dfuchs
parents:
diff changeset
   249
        // Now we're going to use reflection to access JUL, and change
c1c71107d45f 8140364: JEP 264 Platform Logger API and Service Implementation
dfuchs
parents:
diff changeset
   250
        // the level of the "foo" logger.
c1c71107d45f 8140364: JEP 264 Platform Logger API and Service Implementation
dfuchs
parents:
diff changeset
   251
        // We're using reflection so that the test can also run in
c1c71107d45f 8140364: JEP 264 Platform Logger API and Service Implementation
dfuchs
parents:
diff changeset
   252
        // configurations where java.util.logging is not present.
c1c71107d45f 8140364: JEP 264 Platform Logger API and Service Implementation
dfuchs
parents:
diff changeset
   253
        boolean hasJUL = false;
c1c71107d45f 8140364: JEP 264 Platform Logger API and Service Implementation
dfuchs
parents:
diff changeset
   254
        SimplePolicy.allowAll.set(Boolean.TRUE);
c1c71107d45f 8140364: JEP 264 Platform Logger API and Service Implementation
dfuchs
parents:
diff changeset
   255
        try {
c1c71107d45f 8140364: JEP 264 Platform Logger API and Service Implementation
dfuchs
parents:
diff changeset
   256
            Class<?> loggerClass = Class.forName("java.util.logging.Logger");
c1c71107d45f 8140364: JEP 264 Platform Logger API and Service Implementation
dfuchs
parents:
diff changeset
   257
            Class<?> levelClass  = Class.forName("java.util.logging.Level");
c1c71107d45f 8140364: JEP 264 Platform Logger API and Service Implementation
dfuchs
parents:
diff changeset
   258
            Class<?> handlerClass  = Class.forName("java.util.logging.Handler");
c1c71107d45f 8140364: JEP 264 Platform Logger API and Service Implementation
dfuchs
parents:
diff changeset
   259
c1c71107d45f 8140364: JEP 264 Platform Logger API and Service Implementation
dfuchs
parents:
diff changeset
   260
            // java.util.logging.Logger.getLogger("foo")
c1c71107d45f 8140364: JEP 264 Platform Logger API and Service Implementation
dfuchs
parents:
diff changeset
   261
            //        .setLevel(java.util.logging.Level.FINEST);
c1c71107d45f 8140364: JEP 264 Platform Logger API and Service Implementation
dfuchs
parents:
diff changeset
   262
            Object fooLogger = loggerClass.getMethod("getLogger", String.class)
c1c71107d45f 8140364: JEP 264 Platform Logger API and Service Implementation
dfuchs
parents:
diff changeset
   263
                    .invoke(null, "foo");
c1c71107d45f 8140364: JEP 264 Platform Logger API and Service Implementation
dfuchs
parents:
diff changeset
   264
            loggerClass.getMethod("setLevel", levelClass)
c1c71107d45f 8140364: JEP 264 Platform Logger API and Service Implementation
dfuchs
parents:
diff changeset
   265
                    .invoke(fooLogger, levelClass.getField("FINEST").get(null));
c1c71107d45f 8140364: JEP 264 Platform Logger API and Service Implementation
dfuchs
parents:
diff changeset
   266
c1c71107d45f 8140364: JEP 264 Platform Logger API and Service Implementation
dfuchs
parents:
diff changeset
   267
            // java.util.logging.Logger.getLogger("").getHandlers()[0]
c1c71107d45f 8140364: JEP 264 Platform Logger API and Service Implementation
dfuchs
parents:
diff changeset
   268
            //        .setLevel(java.util.logging.Level.ALL);
c1c71107d45f 8140364: JEP 264 Platform Logger API and Service Implementation
dfuchs
parents:
diff changeset
   269
            Object rootLogger = loggerClass.getMethod("getLogger", String.class)
c1c71107d45f 8140364: JEP 264 Platform Logger API and Service Implementation
dfuchs
parents:
diff changeset
   270
                    .invoke(null, "");
c1c71107d45f 8140364: JEP 264 Platform Logger API and Service Implementation
dfuchs
parents:
diff changeset
   271
            Object handlers = loggerClass.getMethod("getHandlers").
c1c71107d45f 8140364: JEP 264 Platform Logger API and Service Implementation
dfuchs
parents:
diff changeset
   272
                    invoke(rootLogger);
c1c71107d45f 8140364: JEP 264 Platform Logger API and Service Implementation
dfuchs
parents:
diff changeset
   273
            handlerClass.getMethod("setLevel", levelClass)
c1c71107d45f 8140364: JEP 264 Platform Logger API and Service Implementation
dfuchs
parents:
diff changeset
   274
                    .invoke(Array.get(handlers, 0), levelClass.getField("ALL")
c1c71107d45f 8140364: JEP 264 Platform Logger API and Service Implementation
dfuchs
parents:
diff changeset
   275
                            .get(null));
c1c71107d45f 8140364: JEP 264 Platform Logger API and Service Implementation
dfuchs
parents:
diff changeset
   276
c1c71107d45f 8140364: JEP 264 Platform Logger API and Service Implementation
dfuchs
parents:
diff changeset
   277
            hasJUL = true;
c1c71107d45f 8140364: JEP 264 Platform Logger API and Service Implementation
dfuchs
parents:
diff changeset
   278
        } catch (ClassNotFoundException x) {
c1c71107d45f 8140364: JEP 264 Platform Logger API and Service Implementation
dfuchs
parents:
diff changeset
   279
            LogStream.err.println("JUL is not present: class " + x.getMessage()
c1c71107d45f 8140364: JEP 264 Platform Logger API and Service Implementation
dfuchs
parents:
diff changeset
   280
                    + " not found");
c1c71107d45f 8140364: JEP 264 Platform Logger API and Service Implementation
dfuchs
parents:
diff changeset
   281
            hasJUL = false;
c1c71107d45f 8140364: JEP 264 Platform Logger API and Service Implementation
dfuchs
parents:
diff changeset
   282
        } finally {
c1c71107d45f 8140364: JEP 264 Platform Logger API and Service Implementation
dfuchs
parents:
diff changeset
   283
            SimplePolicy.allowAll.set(Boolean.FALSE);
c1c71107d45f 8140364: JEP 264 Platform Logger API and Service Implementation
dfuchs
parents:
diff changeset
   284
        }
c1c71107d45f 8140364: JEP 264 Platform Logger API and Service Implementation
dfuchs
parents:
diff changeset
   285
c1c71107d45f 8140364: JEP 264 Platform Logger API and Service Implementation
dfuchs
parents:
diff changeset
   286
        logger.log(Level.DEBUG, "hi now!");
c1c71107d45f 8140364: JEP 264 Platform Logger API and Service Implementation
dfuchs
parents:
diff changeset
   287
        String debug = err.drain();
c1c71107d45f 8140364: JEP 264 Platform Logger API and Service Implementation
dfuchs
parents:
diff changeset
   288
        if (hasJUL) {
c1c71107d45f 8140364: JEP 264 Platform Logger API and Service Implementation
dfuchs
parents:
diff changeset
   289
            if (!((Boolean)logManagerInitialized.get(null)).booleanValue()) {
c1c71107d45f 8140364: JEP 264 Platform Logger API and Service Implementation
dfuchs
parents:
diff changeset
   290
                throw new RuntimeException("LogManager should be initialized now!");
c1c71107d45f 8140364: JEP 264 Platform Logger API and Service Implementation
dfuchs
parents:
diff changeset
   291
            }
c1c71107d45f 8140364: JEP 264 Platform Logger API and Service Implementation
dfuchs
parents:
diff changeset
   292
            if (!debug.contains("FINE: hi now!")) {
c1c71107d45f 8140364: JEP 264 Platform Logger API and Service Implementation
dfuchs
parents:
diff changeset
   293
                throw new RuntimeException("System.err does not contain: "
c1c71107d45f 8140364: JEP 264 Platform Logger API and Service Implementation
dfuchs
parents:
diff changeset
   294
                        + "FINE: hi now!");
c1c71107d45f 8140364: JEP 264 Platform Logger API and Service Implementation
dfuchs
parents:
diff changeset
   295
            }
c1c71107d45f 8140364: JEP 264 Platform Logger API and Service Implementation
dfuchs
parents:
diff changeset
   296
        } else {
c1c71107d45f 8140364: JEP 264 Platform Logger API and Service Implementation
dfuchs
parents:
diff changeset
   297
            if (debug.contains("hi now!")) {
c1c71107d45f 8140364: JEP 264 Platform Logger API and Service Implementation
dfuchs
parents:
diff changeset
   298
                throw new RuntimeException("System.err contains: " + "hi now!");
c1c71107d45f 8140364: JEP 264 Platform Logger API and Service Implementation
dfuchs
parents:
diff changeset
   299
            }
c1c71107d45f 8140364: JEP 264 Platform Logger API and Service Implementation
dfuchs
parents:
diff changeset
   300
            if (((Boolean)logManagerInitialized.get(null)).booleanValue()) {
c1c71107d45f 8140364: JEP 264 Platform Logger API and Service Implementation
dfuchs
parents:
diff changeset
   301
                throw new RuntimeException("LogManager shouldn't be initialized yet!");
c1c71107d45f 8140364: JEP 264 Platform Logger API and Service Implementation
dfuchs
parents:
diff changeset
   302
            }
c1c71107d45f 8140364: JEP 264 Platform Logger API and Service Implementation
dfuchs
parents:
diff changeset
   303
            Logger baz = System.getLogger("foo.bar.baz");
c1c71107d45f 8140364: JEP 264 Platform Logger API and Service Implementation
dfuchs
parents:
diff changeset
   304
            if (((Boolean)logManagerInitialized.get(null)).booleanValue()) {
c1c71107d45f 8140364: JEP 264 Platform Logger API and Service Implementation
dfuchs
parents:
diff changeset
   305
                throw new RuntimeException("LogManager shouldn't be initialized yet!");
c1c71107d45f 8140364: JEP 264 Platform Logger API and Service Implementation
dfuchs
parents:
diff changeset
   306
            }
c1c71107d45f 8140364: JEP 264 Platform Logger API and Service Implementation
dfuchs
parents:
diff changeset
   307
        }
c1c71107d45f 8140364: JEP 264 Platform Logger API and Service Implementation
dfuchs
parents:
diff changeset
   308
        Logger bazbaz = null;
c1c71107d45f 8140364: JEP 264 Platform Logger API and Service Implementation
dfuchs
parents:
diff changeset
   309
        SimplePolicy.allowAll.set(Boolean.TRUE);
c1c71107d45f 8140364: JEP 264 Platform Logger API and Service Implementation
dfuchs
parents:
diff changeset
   310
        try {
c1c71107d45f 8140364: JEP 264 Platform Logger API and Service Implementation
dfuchs
parents:
diff changeset
   311
            bazbaz = java.lang.System.LoggerFinder
c1c71107d45f 8140364: JEP 264 Platform Logger API and Service Implementation
dfuchs
parents:
diff changeset
   312
                    .getLoggerFinder().getLogger("foo.bar.baz.baz", BootstrapLoggerTest.class);
c1c71107d45f 8140364: JEP 264 Platform Logger API and Service Implementation
dfuchs
parents:
diff changeset
   313
        } finally {
c1c71107d45f 8140364: JEP 264 Platform Logger API and Service Implementation
dfuchs
parents:
diff changeset
   314
            SimplePolicy.allowAll.set(Boolean.FALSE);
c1c71107d45f 8140364: JEP 264 Platform Logger API and Service Implementation
dfuchs
parents:
diff changeset
   315
        }
c1c71107d45f 8140364: JEP 264 Platform Logger API and Service Implementation
dfuchs
parents:
diff changeset
   316
        if (!((Boolean)logManagerInitialized.get(null)).booleanValue()) {
c1c71107d45f 8140364: JEP 264 Platform Logger API and Service Implementation
dfuchs
parents:
diff changeset
   317
            throw new RuntimeException("LogManager should be initialized now!");
c1c71107d45f 8140364: JEP 264 Platform Logger API and Service Implementation
dfuchs
parents:
diff changeset
   318
        }
c1c71107d45f 8140364: JEP 264 Platform Logger API and Service Implementation
dfuchs
parents:
diff changeset
   319
        Logger bazbaz2 = System.getLogger("foo.bar.baz.baz");
c1c71107d45f 8140364: JEP 264 Platform Logger API and Service Implementation
dfuchs
parents:
diff changeset
   320
        if (bazbaz2.getClass() != bazbaz.getClass()) {
c1c71107d45f 8140364: JEP 264 Platform Logger API and Service Implementation
dfuchs
parents:
diff changeset
   321
            throw new RuntimeException("bazbaz2.class != bazbaz.class ["
c1c71107d45f 8140364: JEP 264 Platform Logger API and Service Implementation
dfuchs
parents:
diff changeset
   322
                    + bazbaz2.getClass() + " != "
c1c71107d45f 8140364: JEP 264 Platform Logger API and Service Implementation
dfuchs
parents:
diff changeset
   323
                    + bazbaz.getClass() + "]");
c1c71107d45f 8140364: JEP 264 Platform Logger API and Service Implementation
dfuchs
parents:
diff changeset
   324
        }
c1c71107d45f 8140364: JEP 264 Platform Logger API and Service Implementation
dfuchs
parents:
diff changeset
   325
        if (hasJUL != bazbaz2.getClass().getName()
c1c71107d45f 8140364: JEP 264 Platform Logger API and Service Implementation
dfuchs
parents:
diff changeset
   326
                .equals("sun.util.logging.internal.LoggingProviderImpl$JULWrapper")) {
c1c71107d45f 8140364: JEP 264 Platform Logger API and Service Implementation
dfuchs
parents:
diff changeset
   327
            throw new RuntimeException("Unexpected class for bazbaz: "
c1c71107d45f 8140364: JEP 264 Platform Logger API and Service Implementation
dfuchs
parents:
diff changeset
   328
                    + bazbaz.getClass().getName()
c1c71107d45f 8140364: JEP 264 Platform Logger API and Service Implementation
dfuchs
parents:
diff changeset
   329
                    + "\n\t expected: "
c1c71107d45f 8140364: JEP 264 Platform Logger API and Service Implementation
dfuchs
parents:
diff changeset
   330
                    + "sun.util.logging.internal.LoggingProviderImpl$JULWrapper");
c1c71107d45f 8140364: JEP 264 Platform Logger API and Service Implementation
dfuchs
parents:
diff changeset
   331
        }
c1c71107d45f 8140364: JEP 264 Platform Logger API and Service Implementation
dfuchs
parents:
diff changeset
   332
c1c71107d45f 8140364: JEP 264 Platform Logger API and Service Implementation
dfuchs
parents:
diff changeset
   333
        // Now we're going to check that the thread of the BootstrapLogger
c1c71107d45f 8140364: JEP 264 Platform Logger API and Service Implementation
dfuchs
parents:
diff changeset
   334
        // executor terminates, and that the Executor is GC'ed after that.
c1c71107d45f 8140364: JEP 264 Platform Logger API and Service Implementation
dfuchs
parents:
diff changeset
   335
        // This will involve a bit of waiting, hence the timeout=120 in
c1c71107d45f 8140364: JEP 264 Platform Logger API and Service Implementation
dfuchs
parents:
diff changeset
   336
        // the @run line.
c1c71107d45f 8140364: JEP 264 Platform Logger API and Service Implementation
dfuchs
parents:
diff changeset
   337
        // If this test fails in timeout - we could envisage skipping this part,
c1c71107d45f 8140364: JEP 264 Platform Logger API and Service Implementation
dfuchs
parents:
diff changeset
   338
        // or adding some System property to configure the keep alive delay
c1c71107d45f 8140364: JEP 264 Platform Logger API and Service Implementation
dfuchs
parents:
diff changeset
   339
        // of the executor.
c1c71107d45f 8140364: JEP 264 Platform Logger API and Service Implementation
dfuchs
parents:
diff changeset
   340
        SimplePolicy.allowAll.set(Boolean.TRUE);
c1c71107d45f 8140364: JEP 264 Platform Logger API and Service Implementation
dfuchs
parents:
diff changeset
   341
        try {
c1c71107d45f 8140364: JEP 264 Platform Logger API and Service Implementation
dfuchs
parents:
diff changeset
   342
            Stream<Thread> stream = Thread.getAllStackTraces().keySet().stream();
c1c71107d45f 8140364: JEP 264 Platform Logger API and Service Implementation
dfuchs
parents:
diff changeset
   343
            stream.filter((t) -> t.getName().startsWith("BootstrapMessageLoggerTask-"))
c1c71107d45f 8140364: JEP 264 Platform Logger API and Service Implementation
dfuchs
parents:
diff changeset
   344
                    .forEach(t -> LogStream.err.println(t));
c1c71107d45f 8140364: JEP 264 Platform Logger API and Service Implementation
dfuchs
parents:
diff changeset
   345
            stream = null;
c1c71107d45f 8140364: JEP 264 Platform Logger API and Service Implementation
dfuchs
parents:
diff changeset
   346
            if (threadRef != null && test == TestCase.SECURE_AND_WAIT) {
c1c71107d45f 8140364: JEP 264 Platform Logger API and Service Implementation
dfuchs
parents:
diff changeset
   347
                Thread t = threadRef.get();
c1c71107d45f 8140364: JEP 264 Platform Logger API and Service Implementation
dfuchs
parents:
diff changeset
   348
                if (t != null) {
c1c71107d45f 8140364: JEP 264 Platform Logger API and Service Implementation
dfuchs
parents:
diff changeset
   349
                    if (!(Boolean)isAlive.invoke(null)) {
c1c71107d45f 8140364: JEP 264 Platform Logger API and Service Implementation
dfuchs
parents:
diff changeset
   350
                        throw new RuntimeException("Executor already terminated");
c1c71107d45f 8140364: JEP 264 Platform Logger API and Service Implementation
dfuchs
parents:
diff changeset
   351
                    } else {
c1c71107d45f 8140364: JEP 264 Platform Logger API and Service Implementation
dfuchs
parents:
diff changeset
   352
                        LogStream.err.println("Executor still alive as expected.");
c1c71107d45f 8140364: JEP 264 Platform Logger API and Service Implementation
dfuchs
parents:
diff changeset
   353
                    }
c1c71107d45f 8140364: JEP 264 Platform Logger API and Service Implementation
dfuchs
parents:
diff changeset
   354
                    LogStream.err.println("Waiting for " + t.getName() + " to terminate (join)");
c1c71107d45f 8140364: JEP 264 Platform Logger API and Service Implementation
dfuchs
parents:
diff changeset
   355
                    t.join(60_000);
c1c71107d45f 8140364: JEP 264 Platform Logger API and Service Implementation
dfuchs
parents:
diff changeset
   356
                    t = null;
c1c71107d45f 8140364: JEP 264 Platform Logger API and Service Implementation
dfuchs
parents:
diff changeset
   357
                }
c1c71107d45f 8140364: JEP 264 Platform Logger API and Service Implementation
dfuchs
parents:
diff changeset
   358
                LogStream.err.println("Calling System.gc()");
c1c71107d45f 8140364: JEP 264 Platform Logger API and Service Implementation
dfuchs
parents:
diff changeset
   359
                System.gc();
c1c71107d45f 8140364: JEP 264 Platform Logger API and Service Implementation
dfuchs
parents:
diff changeset
   360
                LogStream.err.println("Waiting for BootstrapMessageLoggerTask to be gc'ed");
c1c71107d45f 8140364: JEP 264 Platform Logger API and Service Implementation
dfuchs
parents:
diff changeset
   361
                while (queue.remove(1000) == null) {
c1c71107d45f 8140364: JEP 264 Platform Logger API and Service Implementation
dfuchs
parents:
diff changeset
   362
                    LogStream.err.println("Calling System.gc()");
c1c71107d45f 8140364: JEP 264 Platform Logger API and Service Implementation
dfuchs
parents:
diff changeset
   363
                    System.gc();
c1c71107d45f 8140364: JEP 264 Platform Logger API and Service Implementation
dfuchs
parents:
diff changeset
   364
                }
c1c71107d45f 8140364: JEP 264 Platform Logger API and Service Implementation
dfuchs
parents:
diff changeset
   365
c1c71107d45f 8140364: JEP 264 Platform Logger API and Service Implementation
dfuchs
parents:
diff changeset
   366
                // Call the reference here to make sure threadRef will not be
c1c71107d45f 8140364: JEP 264 Platform Logger API and Service Implementation
dfuchs
parents:
diff changeset
   367
                // eagerly garbage collected before the thread it references.
c1c71107d45f 8140364: JEP 264 Platform Logger API and Service Implementation
dfuchs
parents:
diff changeset
   368
                // otherwise, it might not be enqueued, resulting in the
c1c71107d45f 8140364: JEP 264 Platform Logger API and Service Implementation
dfuchs
parents:
diff changeset
   369
                // queue.remove() call above to always return null....
c1c71107d45f 8140364: JEP 264 Platform Logger API and Service Implementation
dfuchs
parents:
diff changeset
   370
                if (threadRef.get() != null) {
c1c71107d45f 8140364: JEP 264 Platform Logger API and Service Implementation
dfuchs
parents:
diff changeset
   371
                    throw new RuntimeException("Reference should have been cleared");
c1c71107d45f 8140364: JEP 264 Platform Logger API and Service Implementation
dfuchs
parents:
diff changeset
   372
                }
c1c71107d45f 8140364: JEP 264 Platform Logger API and Service Implementation
dfuchs
parents:
diff changeset
   373
c1c71107d45f 8140364: JEP 264 Platform Logger API and Service Implementation
dfuchs
parents:
diff changeset
   374
                LogStream.err.println("BootstrapMessageLoggerTask has been gc'ed");
c1c71107d45f 8140364: JEP 264 Platform Logger API and Service Implementation
dfuchs
parents:
diff changeset
   375
                // Wait for the executor to be gc'ed...
c1c71107d45f 8140364: JEP 264 Platform Logger API and Service Implementation
dfuchs
parents:
diff changeset
   376
                for (int i=0; i<10; i++) {
c1c71107d45f 8140364: JEP 264 Platform Logger API and Service Implementation
dfuchs
parents:
diff changeset
   377
                    LogStream.err.println("Calling System.gc()");
c1c71107d45f 8140364: JEP 264 Platform Logger API and Service Implementation
dfuchs
parents:
diff changeset
   378
                    System.gc();
c1c71107d45f 8140364: JEP 264 Platform Logger API and Service Implementation
dfuchs
parents:
diff changeset
   379
                    if (!(Boolean)isAlive.invoke(null)) break;
c1c71107d45f 8140364: JEP 264 Platform Logger API and Service Implementation
dfuchs
parents:
diff changeset
   380
                    // It would be unexpected that we reach here...
c1c71107d45f 8140364: JEP 264 Platform Logger API and Service Implementation
dfuchs
parents:
diff changeset
   381
                    Thread.sleep(1000);
c1c71107d45f 8140364: JEP 264 Platform Logger API and Service Implementation
dfuchs
parents:
diff changeset
   382
                }
c1c71107d45f 8140364: JEP 264 Platform Logger API and Service Implementation
dfuchs
parents:
diff changeset
   383
c1c71107d45f 8140364: JEP 264 Platform Logger API and Service Implementation
dfuchs
parents:
diff changeset
   384
                if ((Boolean)isAlive.invoke(null)) {
c1c71107d45f 8140364: JEP 264 Platform Logger API and Service Implementation
dfuchs
parents:
diff changeset
   385
                    throw new RuntimeException("Executor still alive");
c1c71107d45f 8140364: JEP 264 Platform Logger API and Service Implementation
dfuchs
parents:
diff changeset
   386
                } else {
c1c71107d45f 8140364: JEP 264 Platform Logger API and Service Implementation
dfuchs
parents:
diff changeset
   387
                    LogStream.err.println("Executor terminated as expected.");
c1c71107d45f 8140364: JEP 264 Platform Logger API and Service Implementation
dfuchs
parents:
diff changeset
   388
                }
c1c71107d45f 8140364: JEP 264 Platform Logger API and Service Implementation
dfuchs
parents:
diff changeset
   389
            } else {
c1c71107d45f 8140364: JEP 264 Platform Logger API and Service Implementation
dfuchs
parents:
diff changeset
   390
                LogStream.err.println("Not checking executor termination for " + test);
c1c71107d45f 8140364: JEP 264 Platform Logger API and Service Implementation
dfuchs
parents:
diff changeset
   391
            }
c1c71107d45f 8140364: JEP 264 Platform Logger API and Service Implementation
dfuchs
parents:
diff changeset
   392
        } finally {
c1c71107d45f 8140364: JEP 264 Platform Logger API and Service Implementation
dfuchs
parents:
diff changeset
   393
            SimplePolicy.allowAll.set(Boolean.FALSE);
c1c71107d45f 8140364: JEP 264 Platform Logger API and Service Implementation
dfuchs
parents:
diff changeset
   394
        }
c1c71107d45f 8140364: JEP 264 Platform Logger API and Service Implementation
dfuchs
parents:
diff changeset
   395
        LogStream.err.println(test.name() + ": PASSED");
c1c71107d45f 8140364: JEP 264 Platform Logger API and Service Implementation
dfuchs
parents:
diff changeset
   396
    }
c1c71107d45f 8140364: JEP 264 Platform Logger API and Service Implementation
dfuchs
parents:
diff changeset
   397
c1c71107d45f 8140364: JEP 264 Platform Logger API and Service Implementation
dfuchs
parents:
diff changeset
   398
    final static class SimplePolicy extends Policy {
c1c71107d45f 8140364: JEP 264 Platform Logger API and Service Implementation
dfuchs
parents:
diff changeset
   399
        static final ThreadLocal<Boolean> allowAll = new ThreadLocal<Boolean>() {
c1c71107d45f 8140364: JEP 264 Platform Logger API and Service Implementation
dfuchs
parents:
diff changeset
   400
            @Override
c1c71107d45f 8140364: JEP 264 Platform Logger API and Service Implementation
dfuchs
parents:
diff changeset
   401
            protected Boolean initialValue() {
c1c71107d45f 8140364: JEP 264 Platform Logger API and Service Implementation
dfuchs
parents:
diff changeset
   402
                return Boolean.FALSE;
c1c71107d45f 8140364: JEP 264 Platform Logger API and Service Implementation
dfuchs
parents:
diff changeset
   403
            }
c1c71107d45f 8140364: JEP 264 Platform Logger API and Service Implementation
dfuchs
parents:
diff changeset
   404
        };
c1c71107d45f 8140364: JEP 264 Platform Logger API and Service Implementation
dfuchs
parents:
diff changeset
   405
c1c71107d45f 8140364: JEP 264 Platform Logger API and Service Implementation
dfuchs
parents:
diff changeset
   406
        Permissions getPermissions() {
c1c71107d45f 8140364: JEP 264 Platform Logger API and Service Implementation
dfuchs
parents:
diff changeset
   407
            Permissions perms = new Permissions();
c1c71107d45f 8140364: JEP 264 Platform Logger API and Service Implementation
dfuchs
parents:
diff changeset
   408
            if (allowAll.get()) {
c1c71107d45f 8140364: JEP 264 Platform Logger API and Service Implementation
dfuchs
parents:
diff changeset
   409
                perms.add(new AllPermission());
c1c71107d45f 8140364: JEP 264 Platform Logger API and Service Implementation
dfuchs
parents:
diff changeset
   410
            }
c1c71107d45f 8140364: JEP 264 Platform Logger API and Service Implementation
dfuchs
parents:
diff changeset
   411
            return perms;
c1c71107d45f 8140364: JEP 264 Platform Logger API and Service Implementation
dfuchs
parents:
diff changeset
   412
        }
c1c71107d45f 8140364: JEP 264 Platform Logger API and Service Implementation
dfuchs
parents:
diff changeset
   413
c1c71107d45f 8140364: JEP 264 Platform Logger API and Service Implementation
dfuchs
parents:
diff changeset
   414
        @Override
c1c71107d45f 8140364: JEP 264 Platform Logger API and Service Implementation
dfuchs
parents:
diff changeset
   415
        public boolean implies(ProtectionDomain domain, Permission permission) {
c1c71107d45f 8140364: JEP 264 Platform Logger API and Service Implementation
dfuchs
parents:
diff changeset
   416
            return getPermissions(domain).implies(permission);
c1c71107d45f 8140364: JEP 264 Platform Logger API and Service Implementation
dfuchs
parents:
diff changeset
   417
        }
c1c71107d45f 8140364: JEP 264 Platform Logger API and Service Implementation
dfuchs
parents:
diff changeset
   418
c1c71107d45f 8140364: JEP 264 Platform Logger API and Service Implementation
dfuchs
parents:
diff changeset
   419
        @Override
c1c71107d45f 8140364: JEP 264 Platform Logger API and Service Implementation
dfuchs
parents:
diff changeset
   420
        public PermissionCollection getPermissions(CodeSource codesource) {
c1c71107d45f 8140364: JEP 264 Platform Logger API and Service Implementation
dfuchs
parents:
diff changeset
   421
            return getPermissions();
c1c71107d45f 8140364: JEP 264 Platform Logger API and Service Implementation
dfuchs
parents:
diff changeset
   422
        }
c1c71107d45f 8140364: JEP 264 Platform Logger API and Service Implementation
dfuchs
parents:
diff changeset
   423
c1c71107d45f 8140364: JEP 264 Platform Logger API and Service Implementation
dfuchs
parents:
diff changeset
   424
        @Override
c1c71107d45f 8140364: JEP 264 Platform Logger API and Service Implementation
dfuchs
parents:
diff changeset
   425
        public PermissionCollection getPermissions(ProtectionDomain domain) {
c1c71107d45f 8140364: JEP 264 Platform Logger API and Service Implementation
dfuchs
parents:
diff changeset
   426
            return getPermissions();
c1c71107d45f 8140364: JEP 264 Platform Logger API and Service Implementation
dfuchs
parents:
diff changeset
   427
        }
c1c71107d45f 8140364: JEP 264 Platform Logger API and Service Implementation
dfuchs
parents:
diff changeset
   428
c1c71107d45f 8140364: JEP 264 Platform Logger API and Service Implementation
dfuchs
parents:
diff changeset
   429
    }
c1c71107d45f 8140364: JEP 264 Platform Logger API and Service Implementation
dfuchs
parents:
diff changeset
   430
}