jdk/test/java/lang/System/LoggerFinder/internal/BasePlatformLoggerTest/BasePlatformLoggerTest.java
author alanb
Fri, 07 Apr 2017 08:05:54 +0000
changeset 44545 83b611b88ac8
parent 37672 03684934dc09
permissions -rw-r--r--
8177530: Module system implementation refresh (4/2017) Reviewed-by: mchung, alanb Contributed-by: alan.bateman@oracle.com, mandy.chung@oracle.com
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) 2015, 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
import java.security.AccessController;
c1c71107d45f 8140364: JEP 264 Platform Logger API and Service Implementation
dfuchs
parents:
diff changeset
    24
import java.security.CodeSource;
c1c71107d45f 8140364: JEP 264 Platform Logger API and Service Implementation
dfuchs
parents:
diff changeset
    25
import java.security.Permission;
c1c71107d45f 8140364: JEP 264 Platform Logger API and Service Implementation
dfuchs
parents:
diff changeset
    26
import java.security.PermissionCollection;
c1c71107d45f 8140364: JEP 264 Platform Logger API and Service Implementation
dfuchs
parents:
diff changeset
    27
import java.security.Permissions;
c1c71107d45f 8140364: JEP 264 Platform Logger API and Service Implementation
dfuchs
parents:
diff changeset
    28
import java.security.Policy;
c1c71107d45f 8140364: JEP 264 Platform Logger API and Service Implementation
dfuchs
parents:
diff changeset
    29
import java.security.PrivilegedAction;
c1c71107d45f 8140364: JEP 264 Platform Logger API and Service Implementation
dfuchs
parents:
diff changeset
    30
import java.security.ProtectionDomain;
c1c71107d45f 8140364: JEP 264 Platform Logger API and Service Implementation
dfuchs
parents:
diff changeset
    31
import java.util.Arrays;
c1c71107d45f 8140364: JEP 264 Platform Logger API and Service Implementation
dfuchs
parents:
diff changeset
    32
import java.util.Collections;
c1c71107d45f 8140364: JEP 264 Platform Logger API and Service Implementation
dfuchs
parents:
diff changeset
    33
import java.util.Enumeration;
c1c71107d45f 8140364: JEP 264 Platform Logger API and Service Implementation
dfuchs
parents:
diff changeset
    34
import java.util.HashMap;
c1c71107d45f 8140364: JEP 264 Platform Logger API and Service Implementation
dfuchs
parents:
diff changeset
    35
import java.util.Map;
c1c71107d45f 8140364: JEP 264 Platform Logger API and Service Implementation
dfuchs
parents:
diff changeset
    36
import java.util.Objects;
c1c71107d45f 8140364: JEP 264 Platform Logger API and Service Implementation
dfuchs
parents:
diff changeset
    37
import java.util.Queue;
c1c71107d45f 8140364: JEP 264 Platform Logger API and Service Implementation
dfuchs
parents:
diff changeset
    38
import java.util.ResourceBundle;
c1c71107d45f 8140364: JEP 264 Platform Logger API and Service Implementation
dfuchs
parents:
diff changeset
    39
import java.util.concurrent.ArrayBlockingQueue;
c1c71107d45f 8140364: JEP 264 Platform Logger API and Service Implementation
dfuchs
parents:
diff changeset
    40
import java.util.concurrent.ConcurrentHashMap;
c1c71107d45f 8140364: JEP 264 Platform Logger API and Service Implementation
dfuchs
parents:
diff changeset
    41
import java.util.concurrent.atomic.AtomicBoolean;
c1c71107d45f 8140364: JEP 264 Platform Logger API and Service Implementation
dfuchs
parents:
diff changeset
    42
import java.util.concurrent.atomic.AtomicLong;
c1c71107d45f 8140364: JEP 264 Platform Logger API and Service Implementation
dfuchs
parents:
diff changeset
    43
import java.util.function.Supplier;
c1c71107d45f 8140364: JEP 264 Platform Logger API and Service Implementation
dfuchs
parents:
diff changeset
    44
import java.lang.System.LoggerFinder;
c1c71107d45f 8140364: JEP 264 Platform Logger API and Service Implementation
dfuchs
parents:
diff changeset
    45
import java.lang.System.Logger;
c1c71107d45f 8140364: JEP 264 Platform Logger API and Service Implementation
dfuchs
parents:
diff changeset
    46
import java.lang.System.Logger.Level;
c1c71107d45f 8140364: JEP 264 Platform Logger API and Service Implementation
dfuchs
parents:
diff changeset
    47
import java.security.AccessControlException;
c1c71107d45f 8140364: JEP 264 Platform Logger API and Service Implementation
dfuchs
parents:
diff changeset
    48
import java.util.stream.Stream;
c1c71107d45f 8140364: JEP 264 Platform Logger API and Service Implementation
dfuchs
parents:
diff changeset
    49
import sun.util.logging.PlatformLogger;
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
 * @summary JDK implementation specific unit test for JDK internal API.
c1c71107d45f 8140364: JEP 264 Platform Logger API and Service Implementation
dfuchs
parents:
diff changeset
    55
 *   Tests a naive implementation of System.Logger, and in particular
c1c71107d45f 8140364: JEP 264 Platform Logger API and Service Implementation
dfuchs
parents:
diff changeset
    56
 *   the default mapping provided by PlatformLogger.
c1c71107d45f 8140364: JEP 264 Platform Logger API and Service Implementation
dfuchs
parents:
diff changeset
    57
 * @modules java.base/sun.util.logging
c1c71107d45f 8140364: JEP 264 Platform Logger API and Service Implementation
dfuchs
parents:
diff changeset
    58
 * @build CustomSystemClassLoader BasePlatformLoggerTest
c1c71107d45f 8140364: JEP 264 Platform Logger API and Service Implementation
dfuchs
parents:
diff changeset
    59
 * @run  main/othervm -Djava.system.class.loader=CustomSystemClassLoader BasePlatformLoggerTest NOSECURITY
c1c71107d45f 8140364: JEP 264 Platform Logger API and Service Implementation
dfuchs
parents:
diff changeset
    60
 * @run  main/othervm -Djava.system.class.loader=CustomSystemClassLoader BasePlatformLoggerTest NOPERMISSIONS
c1c71107d45f 8140364: JEP 264 Platform Logger API and Service Implementation
dfuchs
parents:
diff changeset
    61
 * @run  main/othervm -Djava.system.class.loader=CustomSystemClassLoader BasePlatformLoggerTest WITHPERMISSIONS
c1c71107d45f 8140364: JEP 264 Platform Logger API and Service Implementation
dfuchs
parents:
diff changeset
    62
 * @author danielfuchs
c1c71107d45f 8140364: JEP 264 Platform Logger API and Service Implementation
dfuchs
parents:
diff changeset
    63
 */
c1c71107d45f 8140364: JEP 264 Platform Logger API and Service Implementation
dfuchs
parents:
diff changeset
    64
public class BasePlatformLoggerTest {
c1c71107d45f 8140364: JEP 264 Platform Logger API and Service Implementation
dfuchs
parents:
diff changeset
    65
c1c71107d45f 8140364: JEP 264 Platform Logger API and Service Implementation
dfuchs
parents:
diff changeset
    66
    public static final RuntimePermission LOGGERFINDER_PERMISSION =
c1c71107d45f 8140364: JEP 264 Platform Logger API and Service Implementation
dfuchs
parents:
diff changeset
    67
                new RuntimePermission("loggerFinder");
c1c71107d45f 8140364: JEP 264 Platform Logger API and Service Implementation
dfuchs
parents:
diff changeset
    68
c1c71107d45f 8140364: JEP 264 Platform Logger API and Service Implementation
dfuchs
parents:
diff changeset
    69
    final static AtomicLong sequencer = new AtomicLong();
c1c71107d45f 8140364: JEP 264 Platform Logger API and Service Implementation
dfuchs
parents:
diff changeset
    70
    final static boolean VERBOSE = false;
c1c71107d45f 8140364: JEP 264 Platform Logger API and Service Implementation
dfuchs
parents:
diff changeset
    71
    static final ThreadLocal<AtomicBoolean> allowControl = new ThreadLocal<AtomicBoolean>() {
c1c71107d45f 8140364: JEP 264 Platform Logger API and Service Implementation
dfuchs
parents:
diff changeset
    72
        @Override
c1c71107d45f 8140364: JEP 264 Platform Logger API and Service Implementation
dfuchs
parents:
diff changeset
    73
        protected AtomicBoolean initialValue() {
c1c71107d45f 8140364: JEP 264 Platform Logger API and Service Implementation
dfuchs
parents:
diff changeset
    74
            return  new AtomicBoolean(false);
c1c71107d45f 8140364: JEP 264 Platform Logger API and Service Implementation
dfuchs
parents:
diff changeset
    75
        }
c1c71107d45f 8140364: JEP 264 Platform Logger API and Service Implementation
dfuchs
parents:
diff changeset
    76
    };
c1c71107d45f 8140364: JEP 264 Platform Logger API and Service Implementation
dfuchs
parents:
diff changeset
    77
    static final ThreadLocal<AtomicBoolean> allowAccess = new ThreadLocal<AtomicBoolean>() {
c1c71107d45f 8140364: JEP 264 Platform Logger API and Service Implementation
dfuchs
parents:
diff changeset
    78
        @Override
c1c71107d45f 8140364: JEP 264 Platform Logger API and Service Implementation
dfuchs
parents:
diff changeset
    79
        protected AtomicBoolean initialValue() {
c1c71107d45f 8140364: JEP 264 Platform Logger API and Service Implementation
dfuchs
parents:
diff changeset
    80
            return  new AtomicBoolean(false);
c1c71107d45f 8140364: JEP 264 Platform Logger API and Service Implementation
dfuchs
parents:
diff changeset
    81
        }
c1c71107d45f 8140364: JEP 264 Platform Logger API and Service Implementation
dfuchs
parents:
diff changeset
    82
    };
c1c71107d45f 8140364: JEP 264 Platform Logger API and Service Implementation
dfuchs
parents:
diff changeset
    83
    static final ThreadLocal<AtomicBoolean> allowAll = new ThreadLocal<AtomicBoolean>() {
c1c71107d45f 8140364: JEP 264 Platform Logger API and Service Implementation
dfuchs
parents:
diff changeset
    84
        @Override
c1c71107d45f 8140364: JEP 264 Platform Logger API and Service Implementation
dfuchs
parents:
diff changeset
    85
        protected AtomicBoolean initialValue() {
c1c71107d45f 8140364: JEP 264 Platform Logger API and Service Implementation
dfuchs
parents:
diff changeset
    86
            return  new AtomicBoolean(false);
c1c71107d45f 8140364: JEP 264 Platform Logger API and Service Implementation
dfuchs
parents:
diff changeset
    87
        }
c1c71107d45f 8140364: JEP 264 Platform Logger API and Service Implementation
dfuchs
parents:
diff changeset
    88
    };
c1c71107d45f 8140364: JEP 264 Platform Logger API and Service Implementation
dfuchs
parents:
diff changeset
    89
c1c71107d45f 8140364: JEP 264 Platform Logger API and Service Implementation
dfuchs
parents:
diff changeset
    90
    static final Class<?> providerClass;
c1c71107d45f 8140364: JEP 264 Platform Logger API and Service Implementation
dfuchs
parents:
diff changeset
    91
    static {
c1c71107d45f 8140364: JEP 264 Platform Logger API and Service Implementation
dfuchs
parents:
diff changeset
    92
        try {
c1c71107d45f 8140364: JEP 264 Platform Logger API and Service Implementation
dfuchs
parents:
diff changeset
    93
            providerClass = ClassLoader.getSystemClassLoader().loadClass("BasePlatformLoggerTest$BaseLoggerFinder");
c1c71107d45f 8140364: JEP 264 Platform Logger API and Service Implementation
dfuchs
parents:
diff changeset
    94
        } catch (ClassNotFoundException ex) {
c1c71107d45f 8140364: JEP 264 Platform Logger API and Service Implementation
dfuchs
parents:
diff changeset
    95
            throw new ExceptionInInitializerError(ex);
c1c71107d45f 8140364: JEP 264 Platform Logger API and Service Implementation
dfuchs
parents:
diff changeset
    96
        }
c1c71107d45f 8140364: JEP 264 Platform Logger API and Service Implementation
dfuchs
parents:
diff changeset
    97
    }
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
    static final PlatformLogger.Level[] julLevels = {
c1c71107d45f 8140364: JEP 264 Platform Logger API and Service Implementation
dfuchs
parents:
diff changeset
   100
        PlatformLogger.Level.ALL,
c1c71107d45f 8140364: JEP 264 Platform Logger API and Service Implementation
dfuchs
parents:
diff changeset
   101
        PlatformLogger.Level.FINEST,
c1c71107d45f 8140364: JEP 264 Platform Logger API and Service Implementation
dfuchs
parents:
diff changeset
   102
        PlatformLogger.Level.FINER,
c1c71107d45f 8140364: JEP 264 Platform Logger API and Service Implementation
dfuchs
parents:
diff changeset
   103
        PlatformLogger.Level.FINE,
c1c71107d45f 8140364: JEP 264 Platform Logger API and Service Implementation
dfuchs
parents:
diff changeset
   104
        PlatformLogger.Level.CONFIG,
c1c71107d45f 8140364: JEP 264 Platform Logger API and Service Implementation
dfuchs
parents:
diff changeset
   105
        PlatformLogger.Level.INFO,
c1c71107d45f 8140364: JEP 264 Platform Logger API and Service Implementation
dfuchs
parents:
diff changeset
   106
        PlatformLogger.Level.WARNING,
c1c71107d45f 8140364: JEP 264 Platform Logger API and Service Implementation
dfuchs
parents:
diff changeset
   107
        PlatformLogger.Level.SEVERE,
c1c71107d45f 8140364: JEP 264 Platform Logger API and Service Implementation
dfuchs
parents:
diff changeset
   108
        PlatformLogger.Level.OFF,
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
c1c71107d45f 8140364: JEP 264 Platform Logger API and Service Implementation
dfuchs
parents:
diff changeset
   111
    static final Level[] mappedLevels = {
c1c71107d45f 8140364: JEP 264 Platform Logger API and Service Implementation
dfuchs
parents:
diff changeset
   112
        Level.ALL,     // ALL
c1c71107d45f 8140364: JEP 264 Platform Logger API and Service Implementation
dfuchs
parents:
diff changeset
   113
        Level.TRACE,   // FINEST
c1c71107d45f 8140364: JEP 264 Platform Logger API and Service Implementation
dfuchs
parents:
diff changeset
   114
        Level.TRACE,   // FINER
c1c71107d45f 8140364: JEP 264 Platform Logger API and Service Implementation
dfuchs
parents:
diff changeset
   115
        Level.DEBUG,   // FINE
c1c71107d45f 8140364: JEP 264 Platform Logger API and Service Implementation
dfuchs
parents:
diff changeset
   116
        Level.DEBUG,   // CONFIG
c1c71107d45f 8140364: JEP 264 Platform Logger API and Service Implementation
dfuchs
parents:
diff changeset
   117
        Level.INFO,    // INFO
c1c71107d45f 8140364: JEP 264 Platform Logger API and Service Implementation
dfuchs
parents:
diff changeset
   118
        Level.WARNING, // WARNING
c1c71107d45f 8140364: JEP 264 Platform Logger API and Service Implementation
dfuchs
parents:
diff changeset
   119
        Level.ERROR,   // SEVERE
c1c71107d45f 8140364: JEP 264 Platform Logger API and Service Implementation
dfuchs
parents:
diff changeset
   120
        Level.OFF,     // OFF
c1c71107d45f 8140364: JEP 264 Platform Logger API and Service Implementation
dfuchs
parents:
diff changeset
   121
    };
c1c71107d45f 8140364: JEP 264 Platform Logger API and Service Implementation
dfuchs
parents:
diff changeset
   122
c1c71107d45f 8140364: JEP 264 Platform Logger API and Service Implementation
dfuchs
parents:
diff changeset
   123
    final static Map<PlatformLogger.Level, Level> julToSpiMap;
c1c71107d45f 8140364: JEP 264 Platform Logger API and Service Implementation
dfuchs
parents:
diff changeset
   124
    static {
c1c71107d45f 8140364: JEP 264 Platform Logger API and Service Implementation
dfuchs
parents:
diff changeset
   125
        Map<PlatformLogger.Level, Level> map = new HashMap<>();
c1c71107d45f 8140364: JEP 264 Platform Logger API and Service Implementation
dfuchs
parents:
diff changeset
   126
        if (mappedLevels.length != julLevels.length) {
c1c71107d45f 8140364: JEP 264 Platform Logger API and Service Implementation
dfuchs
parents:
diff changeset
   127
            throw new ExceptionInInitializerError("Array lengths differ"
c1c71107d45f 8140364: JEP 264 Platform Logger API and Service Implementation
dfuchs
parents:
diff changeset
   128
                + "\n\tjulLevels=" + Arrays.deepToString(julLevels)
c1c71107d45f 8140364: JEP 264 Platform Logger API and Service Implementation
dfuchs
parents:
diff changeset
   129
                + "\n\tmappedLevels=" + Arrays.deepToString(mappedLevels));
c1c71107d45f 8140364: JEP 264 Platform Logger API and Service Implementation
dfuchs
parents:
diff changeset
   130
        }
c1c71107d45f 8140364: JEP 264 Platform Logger API and Service Implementation
dfuchs
parents:
diff changeset
   131
        for (int i=0; i<julLevels.length; i++) {
c1c71107d45f 8140364: JEP 264 Platform Logger API and Service Implementation
dfuchs
parents:
diff changeset
   132
            map.put(julLevels[i], mappedLevels[i]);
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
        julToSpiMap = Collections.unmodifiableMap(map);
c1c71107d45f 8140364: JEP 264 Platform Logger API and Service Implementation
dfuchs
parents:
diff changeset
   135
    }
c1c71107d45f 8140364: JEP 264 Platform Logger API and Service Implementation
dfuchs
parents:
diff changeset
   136
c1c71107d45f 8140364: JEP 264 Platform Logger API and Service Implementation
dfuchs
parents:
diff changeset
   137
    public static class MyBundle extends ResourceBundle {
c1c71107d45f 8140364: JEP 264 Platform Logger API and Service Implementation
dfuchs
parents:
diff changeset
   138
c1c71107d45f 8140364: JEP 264 Platform Logger API and Service Implementation
dfuchs
parents:
diff changeset
   139
        final ConcurrentHashMap<String,String> map = new ConcurrentHashMap<>();
c1c71107d45f 8140364: JEP 264 Platform Logger API and Service Implementation
dfuchs
parents:
diff changeset
   140
c1c71107d45f 8140364: JEP 264 Platform Logger API and Service Implementation
dfuchs
parents:
diff changeset
   141
        @Override
c1c71107d45f 8140364: JEP 264 Platform Logger API and Service Implementation
dfuchs
parents:
diff changeset
   142
        protected Object handleGetObject(String key) {
c1c71107d45f 8140364: JEP 264 Platform Logger API and Service Implementation
dfuchs
parents:
diff changeset
   143
            if (key.contains(" (translated)")) {
c1c71107d45f 8140364: JEP 264 Platform Logger API and Service Implementation
dfuchs
parents:
diff changeset
   144
                throw new RuntimeException("Unexpected key: " + key);
c1c71107d45f 8140364: JEP 264 Platform Logger API and Service Implementation
dfuchs
parents:
diff changeset
   145
            }
c1c71107d45f 8140364: JEP 264 Platform Logger API and Service Implementation
dfuchs
parents:
diff changeset
   146
            return map.computeIfAbsent(key, k -> k + " (translated)");
c1c71107d45f 8140364: JEP 264 Platform Logger API and Service Implementation
dfuchs
parents:
diff changeset
   147
        }
c1c71107d45f 8140364: JEP 264 Platform Logger API and Service Implementation
dfuchs
parents:
diff changeset
   148
c1c71107d45f 8140364: JEP 264 Platform Logger API and Service Implementation
dfuchs
parents:
diff changeset
   149
        @Override
c1c71107d45f 8140364: JEP 264 Platform Logger API and Service Implementation
dfuchs
parents:
diff changeset
   150
        public Enumeration<String> getKeys() {
c1c71107d45f 8140364: JEP 264 Platform Logger API and Service Implementation
dfuchs
parents:
diff changeset
   151
            return Collections.enumeration(map.keySet());
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
c1c71107d45f 8140364: JEP 264 Platform Logger API and Service Implementation
dfuchs
parents:
diff changeset
   154
    }
c1c71107d45f 8140364: JEP 264 Platform Logger API and Service Implementation
dfuchs
parents:
diff changeset
   155
    public static class MyLoggerBundle extends MyBundle {
c1c71107d45f 8140364: JEP 264 Platform Logger API and Service Implementation
dfuchs
parents:
diff changeset
   156
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
c1c71107d45f 8140364: JEP 264 Platform Logger API and Service Implementation
dfuchs
parents:
diff changeset
   159
c1c71107d45f 8140364: JEP 264 Platform Logger API and Service Implementation
dfuchs
parents:
diff changeset
   160
    public static interface TestLoggerFinder  {
c1c71107d45f 8140364: JEP 264 Platform Logger API and Service Implementation
dfuchs
parents:
diff changeset
   161
        final ConcurrentHashMap<String, LoggerImpl> system = new ConcurrentHashMap<>();
c1c71107d45f 8140364: JEP 264 Platform Logger API and Service Implementation
dfuchs
parents:
diff changeset
   162
        final ConcurrentHashMap<String, LoggerImpl> user = new ConcurrentHashMap<>();
c1c71107d45f 8140364: JEP 264 Platform Logger API and Service Implementation
dfuchs
parents:
diff changeset
   163
        public Queue<LogEvent> eventQueue = new ArrayBlockingQueue<>(128);
c1c71107d45f 8140364: JEP 264 Platform Logger API and Service Implementation
dfuchs
parents:
diff changeset
   164
c1c71107d45f 8140364: JEP 264 Platform Logger API and Service Implementation
dfuchs
parents:
diff changeset
   165
        public static final class LogEvent implements Cloneable {
c1c71107d45f 8140364: JEP 264 Platform Logger API and Service Implementation
dfuchs
parents:
diff changeset
   166
c1c71107d45f 8140364: JEP 264 Platform Logger API and Service Implementation
dfuchs
parents:
diff changeset
   167
            public LogEvent() {
c1c71107d45f 8140364: JEP 264 Platform Logger API and Service Implementation
dfuchs
parents:
diff changeset
   168
                this(sequencer.getAndIncrement());
c1c71107d45f 8140364: JEP 264 Platform Logger API and Service Implementation
dfuchs
parents:
diff changeset
   169
            }
c1c71107d45f 8140364: JEP 264 Platform Logger API and Service Implementation
dfuchs
parents:
diff changeset
   170
c1c71107d45f 8140364: JEP 264 Platform Logger API and Service Implementation
dfuchs
parents:
diff changeset
   171
            LogEvent(long sequenceNumber) {
c1c71107d45f 8140364: JEP 264 Platform Logger API and Service Implementation
dfuchs
parents:
diff changeset
   172
                this.sequenceNumber = sequenceNumber;
c1c71107d45f 8140364: JEP 264 Platform Logger API and Service Implementation
dfuchs
parents:
diff changeset
   173
            }
c1c71107d45f 8140364: JEP 264 Platform Logger API and Service Implementation
dfuchs
parents:
diff changeset
   174
c1c71107d45f 8140364: JEP 264 Platform Logger API and Service Implementation
dfuchs
parents:
diff changeset
   175
            long sequenceNumber;
c1c71107d45f 8140364: JEP 264 Platform Logger API and Service Implementation
dfuchs
parents:
diff changeset
   176
            boolean isLoggable;
c1c71107d45f 8140364: JEP 264 Platform Logger API and Service Implementation
dfuchs
parents:
diff changeset
   177
            String loggerName;
c1c71107d45f 8140364: JEP 264 Platform Logger API and Service Implementation
dfuchs
parents:
diff changeset
   178
            Level level;
c1c71107d45f 8140364: JEP 264 Platform Logger API and Service Implementation
dfuchs
parents:
diff changeset
   179
            ResourceBundle bundle;
c1c71107d45f 8140364: JEP 264 Platform Logger API and Service Implementation
dfuchs
parents:
diff changeset
   180
            Throwable thrown;
c1c71107d45f 8140364: JEP 264 Platform Logger API and Service Implementation
dfuchs
parents:
diff changeset
   181
            Object[] args;
c1c71107d45f 8140364: JEP 264 Platform Logger API and Service Implementation
dfuchs
parents:
diff changeset
   182
            Supplier<String> supplier;
c1c71107d45f 8140364: JEP 264 Platform Logger API and Service Implementation
dfuchs
parents:
diff changeset
   183
            String msg;
c1c71107d45f 8140364: JEP 264 Platform Logger API and Service Implementation
dfuchs
parents:
diff changeset
   184
c1c71107d45f 8140364: JEP 264 Platform Logger API and Service Implementation
dfuchs
parents:
diff changeset
   185
            Object[] toArray() {
c1c71107d45f 8140364: JEP 264 Platform Logger API and Service Implementation
dfuchs
parents:
diff changeset
   186
                return new Object[] {
c1c71107d45f 8140364: JEP 264 Platform Logger API and Service Implementation
dfuchs
parents:
diff changeset
   187
                    sequenceNumber,
c1c71107d45f 8140364: JEP 264 Platform Logger API and Service Implementation
dfuchs
parents:
diff changeset
   188
                    isLoggable,
c1c71107d45f 8140364: JEP 264 Platform Logger API and Service Implementation
dfuchs
parents:
diff changeset
   189
                    loggerName,
c1c71107d45f 8140364: JEP 264 Platform Logger API and Service Implementation
dfuchs
parents:
diff changeset
   190
                    level,
c1c71107d45f 8140364: JEP 264 Platform Logger API and Service Implementation
dfuchs
parents:
diff changeset
   191
                    bundle,
c1c71107d45f 8140364: JEP 264 Platform Logger API and Service Implementation
dfuchs
parents:
diff changeset
   192
                    thrown,
c1c71107d45f 8140364: JEP 264 Platform Logger API and Service Implementation
dfuchs
parents:
diff changeset
   193
                    args,
c1c71107d45f 8140364: JEP 264 Platform Logger API and Service Implementation
dfuchs
parents:
diff changeset
   194
                    supplier,
c1c71107d45f 8140364: JEP 264 Platform Logger API and Service Implementation
dfuchs
parents:
diff changeset
   195
                    msg,
c1c71107d45f 8140364: JEP 264 Platform Logger API and Service Implementation
dfuchs
parents:
diff changeset
   196
                };
c1c71107d45f 8140364: JEP 264 Platform Logger API and Service Implementation
dfuchs
parents:
diff changeset
   197
            }
c1c71107d45f 8140364: JEP 264 Platform Logger API and Service Implementation
dfuchs
parents:
diff changeset
   198
c1c71107d45f 8140364: JEP 264 Platform Logger API and Service Implementation
dfuchs
parents:
diff changeset
   199
            @Override
c1c71107d45f 8140364: JEP 264 Platform Logger API and Service Implementation
dfuchs
parents:
diff changeset
   200
            public String toString() {
c1c71107d45f 8140364: JEP 264 Platform Logger API and Service Implementation
dfuchs
parents:
diff changeset
   201
                return Arrays.deepToString(toArray());
c1c71107d45f 8140364: JEP 264 Platform Logger API and Service Implementation
dfuchs
parents:
diff changeset
   202
            }
c1c71107d45f 8140364: JEP 264 Platform Logger API and Service Implementation
dfuchs
parents:
diff changeset
   203
c1c71107d45f 8140364: JEP 264 Platform Logger API and Service Implementation
dfuchs
parents:
diff changeset
   204
c1c71107d45f 8140364: JEP 264 Platform Logger API and Service Implementation
dfuchs
parents:
diff changeset
   205
c1c71107d45f 8140364: JEP 264 Platform Logger API and Service Implementation
dfuchs
parents:
diff changeset
   206
            @Override
c1c71107d45f 8140364: JEP 264 Platform Logger API and Service Implementation
dfuchs
parents:
diff changeset
   207
            public boolean equals(Object obj) {
c1c71107d45f 8140364: JEP 264 Platform Logger API and Service Implementation
dfuchs
parents:
diff changeset
   208
                return obj instanceof LogEvent
c1c71107d45f 8140364: JEP 264 Platform Logger API and Service Implementation
dfuchs
parents:
diff changeset
   209
                        && Objects.deepEquals(this.toArray(), ((LogEvent)obj).toArray());
c1c71107d45f 8140364: JEP 264 Platform Logger API and Service Implementation
dfuchs
parents:
diff changeset
   210
            }
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
            @Override
c1c71107d45f 8140364: JEP 264 Platform Logger API and Service Implementation
dfuchs
parents:
diff changeset
   213
            public int hashCode() {
c1c71107d45f 8140364: JEP 264 Platform Logger API and Service Implementation
dfuchs
parents:
diff changeset
   214
                return Objects.hash(toArray());
c1c71107d45f 8140364: JEP 264 Platform Logger API and Service Implementation
dfuchs
parents:
diff changeset
   215
            }
c1c71107d45f 8140364: JEP 264 Platform Logger API and Service Implementation
dfuchs
parents:
diff changeset
   216
c1c71107d45f 8140364: JEP 264 Platform Logger API and Service Implementation
dfuchs
parents:
diff changeset
   217
            public LogEvent cloneWith(long sequenceNumber)
c1c71107d45f 8140364: JEP 264 Platform Logger API and Service Implementation
dfuchs
parents:
diff changeset
   218
                    throws CloneNotSupportedException {
c1c71107d45f 8140364: JEP 264 Platform Logger API and Service Implementation
dfuchs
parents:
diff changeset
   219
                LogEvent cloned = (LogEvent)super.clone();
c1c71107d45f 8140364: JEP 264 Platform Logger API and Service Implementation
dfuchs
parents:
diff changeset
   220
                cloned.sequenceNumber = sequenceNumber;
c1c71107d45f 8140364: JEP 264 Platform Logger API and Service Implementation
dfuchs
parents:
diff changeset
   221
                return cloned;
c1c71107d45f 8140364: JEP 264 Platform Logger API and Service Implementation
dfuchs
parents:
diff changeset
   222
            }
c1c71107d45f 8140364: JEP 264 Platform Logger API and Service Implementation
dfuchs
parents:
diff changeset
   223
c1c71107d45f 8140364: JEP 264 Platform Logger API and Service Implementation
dfuchs
parents:
diff changeset
   224
            public static LogEvent of(boolean isLoggable, String name,
c1c71107d45f 8140364: JEP 264 Platform Logger API and Service Implementation
dfuchs
parents:
diff changeset
   225
                    Level level, ResourceBundle bundle,
c1c71107d45f 8140364: JEP 264 Platform Logger API and Service Implementation
dfuchs
parents:
diff changeset
   226
                    String key, Throwable thrown) {
c1c71107d45f 8140364: JEP 264 Platform Logger API and Service Implementation
dfuchs
parents:
diff changeset
   227
                LogEvent evt = new LogEvent();
c1c71107d45f 8140364: JEP 264 Platform Logger API and Service Implementation
dfuchs
parents:
diff changeset
   228
                evt.isLoggable = isLoggable;
c1c71107d45f 8140364: JEP 264 Platform Logger API and Service Implementation
dfuchs
parents:
diff changeset
   229
                evt.loggerName = name;
c1c71107d45f 8140364: JEP 264 Platform Logger API and Service Implementation
dfuchs
parents:
diff changeset
   230
                evt.level = level;
c1c71107d45f 8140364: JEP 264 Platform Logger API and Service Implementation
dfuchs
parents:
diff changeset
   231
                evt.args = null;
c1c71107d45f 8140364: JEP 264 Platform Logger API and Service Implementation
dfuchs
parents:
diff changeset
   232
                evt.bundle = bundle;
c1c71107d45f 8140364: JEP 264 Platform Logger API and Service Implementation
dfuchs
parents:
diff changeset
   233
                evt.thrown = thrown;
c1c71107d45f 8140364: JEP 264 Platform Logger API and Service Implementation
dfuchs
parents:
diff changeset
   234
                evt.supplier = null;
c1c71107d45f 8140364: JEP 264 Platform Logger API and Service Implementation
dfuchs
parents:
diff changeset
   235
                evt.msg = key;
c1c71107d45f 8140364: JEP 264 Platform Logger API and Service Implementation
dfuchs
parents:
diff changeset
   236
                return evt;
c1c71107d45f 8140364: JEP 264 Platform Logger API and Service Implementation
dfuchs
parents:
diff changeset
   237
            }
c1c71107d45f 8140364: JEP 264 Platform Logger API and Service Implementation
dfuchs
parents:
diff changeset
   238
c1c71107d45f 8140364: JEP 264 Platform Logger API and Service Implementation
dfuchs
parents:
diff changeset
   239
            public static LogEvent of(boolean isLoggable, String name,
c1c71107d45f 8140364: JEP 264 Platform Logger API and Service Implementation
dfuchs
parents:
diff changeset
   240
                    Level level, Throwable thrown, Supplier<String> supplier) {
c1c71107d45f 8140364: JEP 264 Platform Logger API and Service Implementation
dfuchs
parents:
diff changeset
   241
                LogEvent evt = new LogEvent();
c1c71107d45f 8140364: JEP 264 Platform Logger API and Service Implementation
dfuchs
parents:
diff changeset
   242
                evt.isLoggable = isLoggable;
c1c71107d45f 8140364: JEP 264 Platform Logger API and Service Implementation
dfuchs
parents:
diff changeset
   243
                evt.loggerName = name;
c1c71107d45f 8140364: JEP 264 Platform Logger API and Service Implementation
dfuchs
parents:
diff changeset
   244
                evt.level = level;
c1c71107d45f 8140364: JEP 264 Platform Logger API and Service Implementation
dfuchs
parents:
diff changeset
   245
                evt.args = null;
c1c71107d45f 8140364: JEP 264 Platform Logger API and Service Implementation
dfuchs
parents:
diff changeset
   246
                evt.bundle = null;
c1c71107d45f 8140364: JEP 264 Platform Logger API and Service Implementation
dfuchs
parents:
diff changeset
   247
                evt.thrown = thrown;
c1c71107d45f 8140364: JEP 264 Platform Logger API and Service Implementation
dfuchs
parents:
diff changeset
   248
                evt.supplier = supplier;
c1c71107d45f 8140364: JEP 264 Platform Logger API and Service Implementation
dfuchs
parents:
diff changeset
   249
                evt.msg = null;
c1c71107d45f 8140364: JEP 264 Platform Logger API and Service Implementation
dfuchs
parents:
diff changeset
   250
                return evt;
c1c71107d45f 8140364: JEP 264 Platform Logger API and Service Implementation
dfuchs
parents:
diff changeset
   251
            }
c1c71107d45f 8140364: JEP 264 Platform Logger API and Service Implementation
dfuchs
parents:
diff changeset
   252
c1c71107d45f 8140364: JEP 264 Platform Logger API and Service Implementation
dfuchs
parents:
diff changeset
   253
            public static LogEvent of(boolean isLoggable, String name,
c1c71107d45f 8140364: JEP 264 Platform Logger API and Service Implementation
dfuchs
parents:
diff changeset
   254
                    Level level, ResourceBundle bundle,
c1c71107d45f 8140364: JEP 264 Platform Logger API and Service Implementation
dfuchs
parents:
diff changeset
   255
                    String key, Object... params) {
c1c71107d45f 8140364: JEP 264 Platform Logger API and Service Implementation
dfuchs
parents:
diff changeset
   256
                LogEvent evt = new LogEvent();
c1c71107d45f 8140364: JEP 264 Platform Logger API and Service Implementation
dfuchs
parents:
diff changeset
   257
                evt.isLoggable = isLoggable;
c1c71107d45f 8140364: JEP 264 Platform Logger API and Service Implementation
dfuchs
parents:
diff changeset
   258
                evt.loggerName = name;
c1c71107d45f 8140364: JEP 264 Platform Logger API and Service Implementation
dfuchs
parents:
diff changeset
   259
                evt.level = level;
c1c71107d45f 8140364: JEP 264 Platform Logger API and Service Implementation
dfuchs
parents:
diff changeset
   260
                evt.args = params;
c1c71107d45f 8140364: JEP 264 Platform Logger API and Service Implementation
dfuchs
parents:
diff changeset
   261
                evt.bundle = bundle;
c1c71107d45f 8140364: JEP 264 Platform Logger API and Service Implementation
dfuchs
parents:
diff changeset
   262
                evt.thrown = null;
c1c71107d45f 8140364: JEP 264 Platform Logger API and Service Implementation
dfuchs
parents:
diff changeset
   263
                evt.supplier = null;
c1c71107d45f 8140364: JEP 264 Platform Logger API and Service Implementation
dfuchs
parents:
diff changeset
   264
                evt.msg = key;
c1c71107d45f 8140364: JEP 264 Platform Logger API and Service Implementation
dfuchs
parents:
diff changeset
   265
                return evt;
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
c1c71107d45f 8140364: JEP 264 Platform Logger API and Service Implementation
dfuchs
parents:
diff changeset
   268
            public static LogEvent of(long sequenceNumber,
c1c71107d45f 8140364: JEP 264 Platform Logger API and Service Implementation
dfuchs
parents:
diff changeset
   269
                    boolean isLoggable, String name,
c1c71107d45f 8140364: JEP 264 Platform Logger API and Service Implementation
dfuchs
parents:
diff changeset
   270
                    Level level, ResourceBundle bundle,
c1c71107d45f 8140364: JEP 264 Platform Logger API and Service Implementation
dfuchs
parents:
diff changeset
   271
                    String key, Supplier<String> supplier,
c1c71107d45f 8140364: JEP 264 Platform Logger API and Service Implementation
dfuchs
parents:
diff changeset
   272
                    Throwable thrown, Object... params) {
c1c71107d45f 8140364: JEP 264 Platform Logger API and Service Implementation
dfuchs
parents:
diff changeset
   273
                LogEvent evt = new LogEvent(sequenceNumber);
c1c71107d45f 8140364: JEP 264 Platform Logger API and Service Implementation
dfuchs
parents:
diff changeset
   274
                evt.loggerName = name;
c1c71107d45f 8140364: JEP 264 Platform Logger API and Service Implementation
dfuchs
parents:
diff changeset
   275
                evt.level = level;
c1c71107d45f 8140364: JEP 264 Platform Logger API and Service Implementation
dfuchs
parents:
diff changeset
   276
                evt.args = params;
c1c71107d45f 8140364: JEP 264 Platform Logger API and Service Implementation
dfuchs
parents:
diff changeset
   277
                evt.bundle = bundle;
c1c71107d45f 8140364: JEP 264 Platform Logger API and Service Implementation
dfuchs
parents:
diff changeset
   278
                evt.thrown = thrown;
c1c71107d45f 8140364: JEP 264 Platform Logger API and Service Implementation
dfuchs
parents:
diff changeset
   279
                evt.supplier = supplier;
c1c71107d45f 8140364: JEP 264 Platform Logger API and Service Implementation
dfuchs
parents:
diff changeset
   280
                evt.msg = key;
c1c71107d45f 8140364: JEP 264 Platform Logger API and Service Implementation
dfuchs
parents:
diff changeset
   281
                evt.isLoggable = isLoggable;
c1c71107d45f 8140364: JEP 264 Platform Logger API and Service Implementation
dfuchs
parents:
diff changeset
   282
                return evt;
c1c71107d45f 8140364: JEP 264 Platform Logger API and Service Implementation
dfuchs
parents:
diff changeset
   283
            }
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
c1c71107d45f 8140364: JEP 264 Platform Logger API and Service Implementation
dfuchs
parents:
diff changeset
   287
        public class LoggerImpl implements Logger {
c1c71107d45f 8140364: JEP 264 Platform Logger API and Service Implementation
dfuchs
parents:
diff changeset
   288
            private final String name;
c1c71107d45f 8140364: JEP 264 Platform Logger API and Service Implementation
dfuchs
parents:
diff changeset
   289
            private Level level = Level.INFO;
c1c71107d45f 8140364: JEP 264 Platform Logger API and Service Implementation
dfuchs
parents:
diff changeset
   290
c1c71107d45f 8140364: JEP 264 Platform Logger API and Service Implementation
dfuchs
parents:
diff changeset
   291
            public LoggerImpl(String name) {
c1c71107d45f 8140364: JEP 264 Platform Logger API and Service Implementation
dfuchs
parents:
diff changeset
   292
                this.name = name;
c1c71107d45f 8140364: JEP 264 Platform Logger API and Service Implementation
dfuchs
parents:
diff changeset
   293
            }
c1c71107d45f 8140364: JEP 264 Platform Logger API and Service Implementation
dfuchs
parents:
diff changeset
   294
c1c71107d45f 8140364: JEP 264 Platform Logger API and Service Implementation
dfuchs
parents:
diff changeset
   295
            @Override
c1c71107d45f 8140364: JEP 264 Platform Logger API and Service Implementation
dfuchs
parents:
diff changeset
   296
            public String getName() {
c1c71107d45f 8140364: JEP 264 Platform Logger API and Service Implementation
dfuchs
parents:
diff changeset
   297
                return name;
c1c71107d45f 8140364: JEP 264 Platform Logger API and Service Implementation
dfuchs
parents:
diff changeset
   298
            }
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
            @Override
c1c71107d45f 8140364: JEP 264 Platform Logger API and Service Implementation
dfuchs
parents:
diff changeset
   301
            public boolean isLoggable(Level level) {
c1c71107d45f 8140364: JEP 264 Platform Logger API and Service Implementation
dfuchs
parents:
diff changeset
   302
                return this.level != Level.OFF && this.level.getSeverity() <= level.getSeverity();
c1c71107d45f 8140364: JEP 264 Platform Logger API and Service Implementation
dfuchs
parents:
diff changeset
   303
            }
c1c71107d45f 8140364: JEP 264 Platform Logger API and Service Implementation
dfuchs
parents:
diff changeset
   304
c1c71107d45f 8140364: JEP 264 Platform Logger API and Service Implementation
dfuchs
parents:
diff changeset
   305
            @Override
c1c71107d45f 8140364: JEP 264 Platform Logger API and Service Implementation
dfuchs
parents:
diff changeset
   306
            public void log(Level level, ResourceBundle bundle, String key, Throwable thrown) {
c1c71107d45f 8140364: JEP 264 Platform Logger API and Service Implementation
dfuchs
parents:
diff changeset
   307
                log(LogEvent.of(isLoggable(level), this.name, level, bundle, key, thrown));
c1c71107d45f 8140364: JEP 264 Platform Logger API and Service Implementation
dfuchs
parents:
diff changeset
   308
            }
c1c71107d45f 8140364: JEP 264 Platform Logger API and Service Implementation
dfuchs
parents:
diff changeset
   309
c1c71107d45f 8140364: JEP 264 Platform Logger API and Service Implementation
dfuchs
parents:
diff changeset
   310
            @Override
c1c71107d45f 8140364: JEP 264 Platform Logger API and Service Implementation
dfuchs
parents:
diff changeset
   311
            public void log(Level level, ResourceBundle bundle, String format, Object... params) {
c1c71107d45f 8140364: JEP 264 Platform Logger API and Service Implementation
dfuchs
parents:
diff changeset
   312
                log(LogEvent.of(isLoggable(level), name, level, bundle, format, params));
c1c71107d45f 8140364: JEP 264 Platform Logger API and Service Implementation
dfuchs
parents:
diff changeset
   313
            }
c1c71107d45f 8140364: JEP 264 Platform Logger API and Service Implementation
dfuchs
parents:
diff changeset
   314
c1c71107d45f 8140364: JEP 264 Platform Logger API and Service Implementation
dfuchs
parents:
diff changeset
   315
            void log(LogEvent event) {
c1c71107d45f 8140364: JEP 264 Platform Logger API and Service Implementation
dfuchs
parents:
diff changeset
   316
                eventQueue.add(event);
c1c71107d45f 8140364: JEP 264 Platform Logger API and Service Implementation
dfuchs
parents:
diff changeset
   317
            }
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
            @Override
c1c71107d45f 8140364: JEP 264 Platform Logger API and Service Implementation
dfuchs
parents:
diff changeset
   320
            public void log(Level level, Supplier<String> msgSupplier) {
c1c71107d45f 8140364: JEP 264 Platform Logger API and Service Implementation
dfuchs
parents:
diff changeset
   321
                log(LogEvent.of(isLoggable(level), name, level, null, msgSupplier));
c1c71107d45f 8140364: JEP 264 Platform Logger API and Service Implementation
dfuchs
parents:
diff changeset
   322
            }
c1c71107d45f 8140364: JEP 264 Platform Logger API and Service Implementation
dfuchs
parents:
diff changeset
   323
c1c71107d45f 8140364: JEP 264 Platform Logger API and Service Implementation
dfuchs
parents:
diff changeset
   324
            @Override
c1c71107d45f 8140364: JEP 264 Platform Logger API and Service Implementation
dfuchs
parents:
diff changeset
   325
            public void log(Level level,  Supplier<String> msgSupplier, Throwable thrown) {
c1c71107d45f 8140364: JEP 264 Platform Logger API and Service Implementation
dfuchs
parents:
diff changeset
   326
                log(LogEvent.of(isLoggable(level), name, level, thrown, msgSupplier));
c1c71107d45f 8140364: JEP 264 Platform Logger API and Service Implementation
dfuchs
parents:
diff changeset
   327
            }
c1c71107d45f 8140364: JEP 264 Platform Logger API and Service Implementation
dfuchs
parents:
diff changeset
   328
        }
c1c71107d45f 8140364: JEP 264 Platform Logger API and Service Implementation
dfuchs
parents:
diff changeset
   329
37672
03684934dc09 8148568: LoggerFinder.getLogger and LoggerFinder.getLocalizedLogger should take a Module argument instead of a Class.
dfuchs
parents: 33875
diff changeset
   330
        public Logger getLogger(String name, Module caller);
33875
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
    public static class BaseLoggerFinder extends LoggerFinder implements TestLoggerFinder {
c1c71107d45f 8140364: JEP 264 Platform Logger API and Service Implementation
dfuchs
parents:
diff changeset
   334
        @Override
37672
03684934dc09 8148568: LoggerFinder.getLogger and LoggerFinder.getLocalizedLogger should take a Module argument instead of a Class.
dfuchs
parents: 33875
diff changeset
   335
        public Logger getLogger(String name, Module caller) {
33875
c1c71107d45f 8140364: JEP 264 Platform Logger API and Service Implementation
dfuchs
parents:
diff changeset
   336
            SecurityManager sm = System.getSecurityManager();
c1c71107d45f 8140364: JEP 264 Platform Logger API and Service Implementation
dfuchs
parents:
diff changeset
   337
            if (sm != null) {
c1c71107d45f 8140364: JEP 264 Platform Logger API and Service Implementation
dfuchs
parents:
diff changeset
   338
                sm.checkPermission(LOGGERFINDER_PERMISSION);
c1c71107d45f 8140364: JEP 264 Platform Logger API and Service Implementation
dfuchs
parents:
diff changeset
   339
            }
c1c71107d45f 8140364: JEP 264 Platform Logger API and Service Implementation
dfuchs
parents:
diff changeset
   340
            PrivilegedAction<ClassLoader> pa = () -> caller.getClassLoader();
c1c71107d45f 8140364: JEP 264 Platform Logger API and Service Implementation
dfuchs
parents:
diff changeset
   341
            ClassLoader callerLoader = AccessController.doPrivileged(pa);
c1c71107d45f 8140364: JEP 264 Platform Logger API and Service Implementation
dfuchs
parents:
diff changeset
   342
            if (callerLoader == null) {
c1c71107d45f 8140364: JEP 264 Platform Logger API and Service Implementation
dfuchs
parents:
diff changeset
   343
                return system.computeIfAbsent(name, (n) -> new LoggerImpl(n));
c1c71107d45f 8140364: JEP 264 Platform Logger API and Service Implementation
dfuchs
parents:
diff changeset
   344
            } else {
c1c71107d45f 8140364: JEP 264 Platform Logger API and Service Implementation
dfuchs
parents:
diff changeset
   345
                return user.computeIfAbsent(name, (n) -> new LoggerImpl(n));
c1c71107d45f 8140364: JEP 264 Platform Logger API and Service Implementation
dfuchs
parents:
diff changeset
   346
            }
c1c71107d45f 8140364: JEP 264 Platform Logger API and Service Implementation
dfuchs
parents:
diff changeset
   347
        }
c1c71107d45f 8140364: JEP 264 Platform Logger API and Service Implementation
dfuchs
parents:
diff changeset
   348
    }
c1c71107d45f 8140364: JEP 264 Platform Logger API and Service Implementation
dfuchs
parents:
diff changeset
   349
c1c71107d45f 8140364: JEP 264 Platform Logger API and Service Implementation
dfuchs
parents:
diff changeset
   350
    static PlatformLogger getPlatformLogger(String name) {
c1c71107d45f 8140364: JEP 264 Platform Logger API and Service Implementation
dfuchs
parents:
diff changeset
   351
        boolean old = allowAccess.get().get();
c1c71107d45f 8140364: JEP 264 Platform Logger API and Service Implementation
dfuchs
parents:
diff changeset
   352
        allowAccess.get().set(true);
c1c71107d45f 8140364: JEP 264 Platform Logger API and Service Implementation
dfuchs
parents:
diff changeset
   353
        try {
c1c71107d45f 8140364: JEP 264 Platform Logger API and Service Implementation
dfuchs
parents:
diff changeset
   354
            return PlatformLogger.getLogger(name);
c1c71107d45f 8140364: JEP 264 Platform Logger API and Service Implementation
dfuchs
parents:
diff changeset
   355
        } finally {
c1c71107d45f 8140364: JEP 264 Platform Logger API and Service Implementation
dfuchs
parents:
diff changeset
   356
            allowAccess.get().set(old);
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
    }
c1c71107d45f 8140364: JEP 264 Platform Logger API and Service Implementation
dfuchs
parents:
diff changeset
   359
c1c71107d45f 8140364: JEP 264 Platform Logger API and Service Implementation
dfuchs
parents:
diff changeset
   360
    static enum TestCases {NOSECURITY, NOPERMISSIONS, WITHPERMISSIONS};
c1c71107d45f 8140364: JEP 264 Platform Logger API and Service Implementation
dfuchs
parents:
diff changeset
   361
c1c71107d45f 8140364: JEP 264 Platform Logger API and Service Implementation
dfuchs
parents:
diff changeset
   362
    static void setSecurityManager() {
c1c71107d45f 8140364: JEP 264 Platform Logger API and Service Implementation
dfuchs
parents:
diff changeset
   363
        if (System.getSecurityManager() == null) {
c1c71107d45f 8140364: JEP 264 Platform Logger API and Service Implementation
dfuchs
parents:
diff changeset
   364
            Policy.setPolicy(new SimplePolicy(allowControl, allowAccess, allowAll));
c1c71107d45f 8140364: JEP 264 Platform Logger API and Service Implementation
dfuchs
parents:
diff changeset
   365
            System.setSecurityManager(new SecurityManager());
c1c71107d45f 8140364: JEP 264 Platform Logger API and Service Implementation
dfuchs
parents:
diff changeset
   366
        }
c1c71107d45f 8140364: JEP 264 Platform Logger API and Service Implementation
dfuchs
parents:
diff changeset
   367
    }
c1c71107d45f 8140364: JEP 264 Platform Logger API and Service Implementation
dfuchs
parents:
diff changeset
   368
c1c71107d45f 8140364: JEP 264 Platform Logger API and Service Implementation
dfuchs
parents:
diff changeset
   369
    public static void main(String[] args) {
c1c71107d45f 8140364: JEP 264 Platform Logger API and Service Implementation
dfuchs
parents:
diff changeset
   370
        if (args.length == 0)
c1c71107d45f 8140364: JEP 264 Platform Logger API and Service Implementation
dfuchs
parents:
diff changeset
   371
            args = new String[] {
c1c71107d45f 8140364: JEP 264 Platform Logger API and Service Implementation
dfuchs
parents:
diff changeset
   372
                "NOSECURITY",
c1c71107d45f 8140364: JEP 264 Platform Logger API and Service Implementation
dfuchs
parents:
diff changeset
   373
                "NOPERMISSIONS",
c1c71107d45f 8140364: JEP 264 Platform Logger API and Service Implementation
dfuchs
parents:
diff changeset
   374
                "WITHPERMISSIONS"
c1c71107d45f 8140364: JEP 264 Platform Logger API and Service Implementation
dfuchs
parents:
diff changeset
   375
            };
c1c71107d45f 8140364: JEP 264 Platform Logger API and Service Implementation
dfuchs
parents:
diff changeset
   376
c1c71107d45f 8140364: JEP 264 Platform Logger API and Service Implementation
dfuchs
parents:
diff changeset
   377
c1c71107d45f 8140364: JEP 264 Platform Logger API and Service Implementation
dfuchs
parents:
diff changeset
   378
        Stream.of(args).map(TestCases::valueOf).forEach((testCase) -> {
c1c71107d45f 8140364: JEP 264 Platform Logger API and Service Implementation
dfuchs
parents:
diff changeset
   379
            TestLoggerFinder provider;
c1c71107d45f 8140364: JEP 264 Platform Logger API and Service Implementation
dfuchs
parents:
diff changeset
   380
            switch (testCase) {
c1c71107d45f 8140364: JEP 264 Platform Logger API and Service Implementation
dfuchs
parents:
diff changeset
   381
                case NOSECURITY:
c1c71107d45f 8140364: JEP 264 Platform Logger API and Service Implementation
dfuchs
parents:
diff changeset
   382
                    System.out.println("\n*** Without Security Manager\n");
c1c71107d45f 8140364: JEP 264 Platform Logger API and Service Implementation
dfuchs
parents:
diff changeset
   383
                    provider = TestLoggerFinder.class.cast(LoggerFinder.getLoggerFinder());
c1c71107d45f 8140364: JEP 264 Platform Logger API and Service Implementation
dfuchs
parents:
diff changeset
   384
                    test(provider, true);
c1c71107d45f 8140364: JEP 264 Platform Logger API and Service Implementation
dfuchs
parents:
diff changeset
   385
                    System.out.println("Tetscase count: " + sequencer.get());
c1c71107d45f 8140364: JEP 264 Platform Logger API and Service Implementation
dfuchs
parents:
diff changeset
   386
                    break;
c1c71107d45f 8140364: JEP 264 Platform Logger API and Service Implementation
dfuchs
parents:
diff changeset
   387
                case NOPERMISSIONS:
c1c71107d45f 8140364: JEP 264 Platform Logger API and Service Implementation
dfuchs
parents:
diff changeset
   388
                    System.out.println("\n*** With Security Manager, without permissions\n");
c1c71107d45f 8140364: JEP 264 Platform Logger API and Service Implementation
dfuchs
parents:
diff changeset
   389
                    setSecurityManager();
c1c71107d45f 8140364: JEP 264 Platform Logger API and Service Implementation
dfuchs
parents:
diff changeset
   390
                    try {
c1c71107d45f 8140364: JEP 264 Platform Logger API and Service Implementation
dfuchs
parents:
diff changeset
   391
                        provider = TestLoggerFinder.class.cast(LoggerFinder.getLoggerFinder());
c1c71107d45f 8140364: JEP 264 Platform Logger API and Service Implementation
dfuchs
parents:
diff changeset
   392
                        throw new RuntimeException("Expected exception not raised");
c1c71107d45f 8140364: JEP 264 Platform Logger API and Service Implementation
dfuchs
parents:
diff changeset
   393
                    } catch (AccessControlException x) {
c1c71107d45f 8140364: JEP 264 Platform Logger API and Service Implementation
dfuchs
parents:
diff changeset
   394
                        if (!LOGGERFINDER_PERMISSION.equals(x.getPermission())) {
c1c71107d45f 8140364: JEP 264 Platform Logger API and Service Implementation
dfuchs
parents:
diff changeset
   395
                            throw new RuntimeException("Unexpected permission check", x);
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
                        final boolean control = allowControl.get().get();
c1c71107d45f 8140364: JEP 264 Platform Logger API and Service Implementation
dfuchs
parents:
diff changeset
   398
                        try {
c1c71107d45f 8140364: JEP 264 Platform Logger API and Service Implementation
dfuchs
parents:
diff changeset
   399
                            allowControl.get().set(true);
c1c71107d45f 8140364: JEP 264 Platform Logger API and Service Implementation
dfuchs
parents:
diff changeset
   400
                            provider = TestLoggerFinder.class.cast(LoggerFinder.getLoggerFinder());
c1c71107d45f 8140364: JEP 264 Platform Logger API and Service Implementation
dfuchs
parents:
diff changeset
   401
                        } finally {
c1c71107d45f 8140364: JEP 264 Platform Logger API and Service Implementation
dfuchs
parents:
diff changeset
   402
                            allowControl.get().set(control);
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
                    test(provider, false);
c1c71107d45f 8140364: JEP 264 Platform Logger API and Service Implementation
dfuchs
parents:
diff changeset
   406
                    System.out.println("Tetscase count: " + sequencer.get());
c1c71107d45f 8140364: JEP 264 Platform Logger API and Service Implementation
dfuchs
parents:
diff changeset
   407
                    break;
c1c71107d45f 8140364: JEP 264 Platform Logger API and Service Implementation
dfuchs
parents:
diff changeset
   408
                case WITHPERMISSIONS:
c1c71107d45f 8140364: JEP 264 Platform Logger API and Service Implementation
dfuchs
parents:
diff changeset
   409
                    System.out.println("\n*** With Security Manager, with control permission\n");
c1c71107d45f 8140364: JEP 264 Platform Logger API and Service Implementation
dfuchs
parents:
diff changeset
   410
                    setSecurityManager();
c1c71107d45f 8140364: JEP 264 Platform Logger API and Service Implementation
dfuchs
parents:
diff changeset
   411
                    final boolean control = allowControl.get().get();
c1c71107d45f 8140364: JEP 264 Platform Logger API and Service Implementation
dfuchs
parents:
diff changeset
   412
                    try {
c1c71107d45f 8140364: JEP 264 Platform Logger API and Service Implementation
dfuchs
parents:
diff changeset
   413
                        allowControl.get().set(true);
c1c71107d45f 8140364: JEP 264 Platform Logger API and Service Implementation
dfuchs
parents:
diff changeset
   414
                        provider = TestLoggerFinder.class.cast(LoggerFinder.getLoggerFinder());
c1c71107d45f 8140364: JEP 264 Platform Logger API and Service Implementation
dfuchs
parents:
diff changeset
   415
                        test(provider, true);
c1c71107d45f 8140364: JEP 264 Platform Logger API and Service Implementation
dfuchs
parents:
diff changeset
   416
                    } finally {
c1c71107d45f 8140364: JEP 264 Platform Logger API and Service Implementation
dfuchs
parents:
diff changeset
   417
                        allowControl.get().set(control);
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
                    break;
c1c71107d45f 8140364: JEP 264 Platform Logger API and Service Implementation
dfuchs
parents:
diff changeset
   420
                default:
c1c71107d45f 8140364: JEP 264 Platform Logger API and Service Implementation
dfuchs
parents:
diff changeset
   421
                    throw new RuntimeException("Unknown test case: " + testCase);
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
        System.out.println("\nPASSED: Tested " + sequencer.get() + " cases.");
c1c71107d45f 8140364: JEP 264 Platform Logger API and Service Implementation
dfuchs
parents:
diff changeset
   425
    }
c1c71107d45f 8140364: JEP 264 Platform Logger API and Service Implementation
dfuchs
parents:
diff changeset
   426
c1c71107d45f 8140364: JEP 264 Platform Logger API and Service Implementation
dfuchs
parents:
diff changeset
   427
    public static void test(TestLoggerFinder provider, boolean hasRequiredPermissions) {
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
        final Map<PlatformLogger, String> loggerDescMap = new HashMap<>();
c1c71107d45f 8140364: JEP 264 Platform Logger API and Service Implementation
dfuchs
parents:
diff changeset
   430
c1c71107d45f 8140364: JEP 264 Platform Logger API and Service Implementation
dfuchs
parents:
diff changeset
   431
        TestLoggerFinder.LoggerImpl appSink;
c1c71107d45f 8140364: JEP 264 Platform Logger API and Service Implementation
dfuchs
parents:
diff changeset
   432
        boolean before = allowControl.get().get();
c1c71107d45f 8140364: JEP 264 Platform Logger API and Service Implementation
dfuchs
parents:
diff changeset
   433
        try {
c1c71107d45f 8140364: JEP 264 Platform Logger API and Service Implementation
dfuchs
parents:
diff changeset
   434
            allowControl.get().set(true);
c1c71107d45f 8140364: JEP 264 Platform Logger API and Service Implementation
dfuchs
parents:
diff changeset
   435
            appSink = TestLoggerFinder.LoggerImpl.class.cast(
37672
03684934dc09 8148568: LoggerFinder.getLogger and LoggerFinder.getLocalizedLogger should take a Module argument instead of a Class.
dfuchs
parents: 33875
diff changeset
   436
                        provider.getLogger("foo", BasePlatformLoggerTest.class.getModule()));
33875
c1c71107d45f 8140364: JEP 264 Platform Logger API and Service Implementation
dfuchs
parents:
diff changeset
   437
        } finally {
c1c71107d45f 8140364: JEP 264 Platform Logger API and Service Implementation
dfuchs
parents:
diff changeset
   438
            allowControl.get().set(before);
c1c71107d45f 8140364: JEP 264 Platform Logger API and Service Implementation
dfuchs
parents:
diff changeset
   439
        }
c1c71107d45f 8140364: JEP 264 Platform Logger API and Service Implementation
dfuchs
parents:
diff changeset
   440
c1c71107d45f 8140364: JEP 264 Platform Logger API and Service Implementation
dfuchs
parents:
diff changeset
   441
        TestLoggerFinder.LoggerImpl sysSink = null;
c1c71107d45f 8140364: JEP 264 Platform Logger API and Service Implementation
dfuchs
parents:
diff changeset
   442
        before = allowControl.get().get();
c1c71107d45f 8140364: JEP 264 Platform Logger API and Service Implementation
dfuchs
parents:
diff changeset
   443
        try {
c1c71107d45f 8140364: JEP 264 Platform Logger API and Service Implementation
dfuchs
parents:
diff changeset
   444
            allowControl.get().set(true);
37672
03684934dc09 8148568: LoggerFinder.getLogger and LoggerFinder.getLocalizedLogger should take a Module argument instead of a Class.
dfuchs
parents: 33875
diff changeset
   445
            sysSink = TestLoggerFinder.LoggerImpl.class.cast(
03684934dc09 8148568: LoggerFinder.getLogger and LoggerFinder.getLocalizedLogger should take a Module argument instead of a Class.
dfuchs
parents: 33875
diff changeset
   446
                        provider.getLogger("foo", Thread.class.getModule()));
33875
c1c71107d45f 8140364: JEP 264 Platform Logger API and Service Implementation
dfuchs
parents:
diff changeset
   447
        } finally {
c1c71107d45f 8140364: JEP 264 Platform Logger API and Service Implementation
dfuchs
parents:
diff changeset
   448
            allowControl.get().set(before);
c1c71107d45f 8140364: JEP 264 Platform Logger API and Service Implementation
dfuchs
parents:
diff changeset
   449
        }
c1c71107d45f 8140364: JEP 264 Platform Logger API and Service Implementation
dfuchs
parents:
diff changeset
   450
c1c71107d45f 8140364: JEP 264 Platform Logger API and Service Implementation
dfuchs
parents:
diff changeset
   451
        if (hasRequiredPermissions && appSink == sysSink) {
c1c71107d45f 8140364: JEP 264 Platform Logger API and Service Implementation
dfuchs
parents:
diff changeset
   452
            throw new RuntimeException("identical loggers");
c1c71107d45f 8140364: JEP 264 Platform Logger API and Service Implementation
dfuchs
parents:
diff changeset
   453
        }
c1c71107d45f 8140364: JEP 264 Platform Logger API and Service Implementation
dfuchs
parents:
diff changeset
   454
c1c71107d45f 8140364: JEP 264 Platform Logger API and Service Implementation
dfuchs
parents:
diff changeset
   455
        if (provider.system.contains(appSink)) {
c1c71107d45f 8140364: JEP 264 Platform Logger API and Service Implementation
dfuchs
parents:
diff changeset
   456
            throw new RuntimeException("app logger in system map");
c1c71107d45f 8140364: JEP 264 Platform Logger API and Service Implementation
dfuchs
parents:
diff changeset
   457
        }
c1c71107d45f 8140364: JEP 264 Platform Logger API and Service Implementation
dfuchs
parents:
diff changeset
   458
        if (!provider.user.contains(appSink)) {
c1c71107d45f 8140364: JEP 264 Platform Logger API and Service Implementation
dfuchs
parents:
diff changeset
   459
            throw new RuntimeException("app logger not in appplication map");
c1c71107d45f 8140364: JEP 264 Platform Logger API and Service Implementation
dfuchs
parents:
diff changeset
   460
        }
c1c71107d45f 8140364: JEP 264 Platform Logger API and Service Implementation
dfuchs
parents:
diff changeset
   461
        if (hasRequiredPermissions && provider.user.contains(sysSink)) {
c1c71107d45f 8140364: JEP 264 Platform Logger API and Service Implementation
dfuchs
parents:
diff changeset
   462
            throw new RuntimeException("sys logger in appplication map");
c1c71107d45f 8140364: JEP 264 Platform Logger API and Service Implementation
dfuchs
parents:
diff changeset
   463
        }
c1c71107d45f 8140364: JEP 264 Platform Logger API and Service Implementation
dfuchs
parents:
diff changeset
   464
        if (hasRequiredPermissions && !provider.system.contains(sysSink)) {
c1c71107d45f 8140364: JEP 264 Platform Logger API and Service Implementation
dfuchs
parents:
diff changeset
   465
            throw new RuntimeException("sys logger not in system map");
c1c71107d45f 8140364: JEP 264 Platform Logger API and Service Implementation
dfuchs
parents:
diff changeset
   466
        }
c1c71107d45f 8140364: JEP 264 Platform Logger API and Service Implementation
dfuchs
parents:
diff changeset
   467
c1c71107d45f 8140364: JEP 264 Platform Logger API and Service Implementation
dfuchs
parents:
diff changeset
   468
        PlatformLogger platform = getPlatformLogger("foo");
c1c71107d45f 8140364: JEP 264 Platform Logger API and Service Implementation
dfuchs
parents:
diff changeset
   469
        loggerDescMap.put(platform, "PlatformLogger.getLogger(\"foo\")");
c1c71107d45f 8140364: JEP 264 Platform Logger API and Service Implementation
dfuchs
parents:
diff changeset
   470
c1c71107d45f 8140364: JEP 264 Platform Logger API and Service Implementation
dfuchs
parents:
diff changeset
   471
        testLogger(provider, loggerDescMap, "foo", null, platform, sysSink);
c1c71107d45f 8140364: JEP 264 Platform Logger API and Service Implementation
dfuchs
parents:
diff changeset
   472
    }
c1c71107d45f 8140364: JEP 264 Platform Logger API and Service Implementation
dfuchs
parents:
diff changeset
   473
c1c71107d45f 8140364: JEP 264 Platform Logger API and Service Implementation
dfuchs
parents:
diff changeset
   474
    public static class Foo {
c1c71107d45f 8140364: JEP 264 Platform Logger API and Service Implementation
dfuchs
parents:
diff changeset
   475
c1c71107d45f 8140364: JEP 264 Platform Logger API and Service Implementation
dfuchs
parents:
diff changeset
   476
    }
c1c71107d45f 8140364: JEP 264 Platform Logger API and Service Implementation
dfuchs
parents:
diff changeset
   477
c1c71107d45f 8140364: JEP 264 Platform Logger API and Service Implementation
dfuchs
parents:
diff changeset
   478
    static void verbose(String msg) {
c1c71107d45f 8140364: JEP 264 Platform Logger API and Service Implementation
dfuchs
parents:
diff changeset
   479
       if (VERBOSE) {
c1c71107d45f 8140364: JEP 264 Platform Logger API and Service Implementation
dfuchs
parents:
diff changeset
   480
           System.out.println(msg);
c1c71107d45f 8140364: JEP 264 Platform Logger API and Service Implementation
dfuchs
parents:
diff changeset
   481
       }
c1c71107d45f 8140364: JEP 264 Platform Logger API and Service Implementation
dfuchs
parents:
diff changeset
   482
    }
c1c71107d45f 8140364: JEP 264 Platform Logger API and Service Implementation
dfuchs
parents:
diff changeset
   483
c1c71107d45f 8140364: JEP 264 Platform Logger API and Service Implementation
dfuchs
parents:
diff changeset
   484
    static void checkLogEvent(TestLoggerFinder provider, String desc,
c1c71107d45f 8140364: JEP 264 Platform Logger API and Service Implementation
dfuchs
parents:
diff changeset
   485
            TestLoggerFinder.LogEvent expected) {
c1c71107d45f 8140364: JEP 264 Platform Logger API and Service Implementation
dfuchs
parents:
diff changeset
   486
        TestLoggerFinder.LogEvent actual =  provider.eventQueue.poll();
c1c71107d45f 8140364: JEP 264 Platform Logger API and Service Implementation
dfuchs
parents:
diff changeset
   487
        if (!expected.equals(actual)) {
c1c71107d45f 8140364: JEP 264 Platform Logger API and Service Implementation
dfuchs
parents:
diff changeset
   488
            throw new RuntimeException("mismatch for " + desc
c1c71107d45f 8140364: JEP 264 Platform Logger API and Service Implementation
dfuchs
parents:
diff changeset
   489
                    + "\n\texpected=" + expected
c1c71107d45f 8140364: JEP 264 Platform Logger API and Service Implementation
dfuchs
parents:
diff changeset
   490
                    + "\n\t  actual=" + actual);
c1c71107d45f 8140364: JEP 264 Platform Logger API and Service Implementation
dfuchs
parents:
diff changeset
   491
        } else {
c1c71107d45f 8140364: JEP 264 Platform Logger API and Service Implementation
dfuchs
parents:
diff changeset
   492
            verbose("Got expected results for "
c1c71107d45f 8140364: JEP 264 Platform Logger API and Service Implementation
dfuchs
parents:
diff changeset
   493
                    + desc + "\n\t" + expected);
c1c71107d45f 8140364: JEP 264 Platform Logger API and Service Implementation
dfuchs
parents:
diff changeset
   494
        }
c1c71107d45f 8140364: JEP 264 Platform Logger API and Service Implementation
dfuchs
parents:
diff changeset
   495
    }
c1c71107d45f 8140364: JEP 264 Platform Logger API and Service Implementation
dfuchs
parents:
diff changeset
   496
c1c71107d45f 8140364: JEP 264 Platform Logger API and Service Implementation
dfuchs
parents:
diff changeset
   497
    static void checkLogEvent(TestLoggerFinder provider, String desc,
c1c71107d45f 8140364: JEP 264 Platform Logger API and Service Implementation
dfuchs
parents:
diff changeset
   498
            TestLoggerFinder.LogEvent expected, boolean expectNotNull) {
c1c71107d45f 8140364: JEP 264 Platform Logger API and Service Implementation
dfuchs
parents:
diff changeset
   499
        TestLoggerFinder.LogEvent actual =  provider.eventQueue.poll();
c1c71107d45f 8140364: JEP 264 Platform Logger API and Service Implementation
dfuchs
parents:
diff changeset
   500
        if (actual == null && !expectNotNull) return;
c1c71107d45f 8140364: JEP 264 Platform Logger API and Service Implementation
dfuchs
parents:
diff changeset
   501
        if (actual != null && !expectNotNull) {
c1c71107d45f 8140364: JEP 264 Platform Logger API and Service Implementation
dfuchs
parents:
diff changeset
   502
            throw new RuntimeException("Unexpected log event found for " + desc
c1c71107d45f 8140364: JEP 264 Platform Logger API and Service Implementation
dfuchs
parents:
diff changeset
   503
                + "\n\tgot: " + actual);
c1c71107d45f 8140364: JEP 264 Platform Logger API and Service Implementation
dfuchs
parents:
diff changeset
   504
        }
c1c71107d45f 8140364: JEP 264 Platform Logger API and Service Implementation
dfuchs
parents:
diff changeset
   505
        if (!expected.equals(actual)) {
c1c71107d45f 8140364: JEP 264 Platform Logger API and Service Implementation
dfuchs
parents:
diff changeset
   506
            throw new RuntimeException("mismatch for " + desc
c1c71107d45f 8140364: JEP 264 Platform Logger API and Service Implementation
dfuchs
parents:
diff changeset
   507
                    + "\n\texpected=" + expected
c1c71107d45f 8140364: JEP 264 Platform Logger API and Service Implementation
dfuchs
parents:
diff changeset
   508
                    + "\n\t  actual=" + actual);
c1c71107d45f 8140364: JEP 264 Platform Logger API and Service Implementation
dfuchs
parents:
diff changeset
   509
        } else {
c1c71107d45f 8140364: JEP 264 Platform Logger API and Service Implementation
dfuchs
parents:
diff changeset
   510
            verbose("Got expected results for "
c1c71107d45f 8140364: JEP 264 Platform Logger API and Service Implementation
dfuchs
parents:
diff changeset
   511
                    + desc + "\n\t" + expected);
c1c71107d45f 8140364: JEP 264 Platform Logger API and Service Implementation
dfuchs
parents:
diff changeset
   512
        }
c1c71107d45f 8140364: JEP 264 Platform Logger API and Service Implementation
dfuchs
parents:
diff changeset
   513
    }
c1c71107d45f 8140364: JEP 264 Platform Logger API and Service Implementation
dfuchs
parents:
diff changeset
   514
c1c71107d45f 8140364: JEP 264 Platform Logger API and Service Implementation
dfuchs
parents:
diff changeset
   515
    // Calls the methods defined on LogProducer and verify the
c1c71107d45f 8140364: JEP 264 Platform Logger API and Service Implementation
dfuchs
parents:
diff changeset
   516
    // parameters received by the underlying TestLoggerFinder.LoggerImpl
c1c71107d45f 8140364: JEP 264 Platform Logger API and Service Implementation
dfuchs
parents:
diff changeset
   517
    // logger.
c1c71107d45f 8140364: JEP 264 Platform Logger API and Service Implementation
dfuchs
parents:
diff changeset
   518
    private static void testLogger(TestLoggerFinder provider,
c1c71107d45f 8140364: JEP 264 Platform Logger API and Service Implementation
dfuchs
parents:
diff changeset
   519
            Map<PlatformLogger, String> loggerDescMap,
c1c71107d45f 8140364: JEP 264 Platform Logger API and Service Implementation
dfuchs
parents:
diff changeset
   520
            String name,
c1c71107d45f 8140364: JEP 264 Platform Logger API and Service Implementation
dfuchs
parents:
diff changeset
   521
            ResourceBundle loggerBundle,
c1c71107d45f 8140364: JEP 264 Platform Logger API and Service Implementation
dfuchs
parents:
diff changeset
   522
            PlatformLogger logger,
c1c71107d45f 8140364: JEP 264 Platform Logger API and Service Implementation
dfuchs
parents:
diff changeset
   523
            TestLoggerFinder.LoggerImpl sink) {
c1c71107d45f 8140364: JEP 264 Platform Logger API and Service Implementation
dfuchs
parents:
diff changeset
   524
c1c71107d45f 8140364: JEP 264 Platform Logger API and Service Implementation
dfuchs
parents:
diff changeset
   525
        System.out.println("Testing " + loggerDescMap.get(logger));
c1c71107d45f 8140364: JEP 264 Platform Logger API and Service Implementation
dfuchs
parents:
diff changeset
   526
c1c71107d45f 8140364: JEP 264 Platform Logger API and Service Implementation
dfuchs
parents:
diff changeset
   527
        Foo foo = new Foo();
c1c71107d45f 8140364: JEP 264 Platform Logger API and Service Implementation
dfuchs
parents:
diff changeset
   528
        String fooMsg = foo.toString();
c1c71107d45f 8140364: JEP 264 Platform Logger API and Service Implementation
dfuchs
parents:
diff changeset
   529
        System.out.println("\tlogger.<level>(fooMsg)");
c1c71107d45f 8140364: JEP 264 Platform Logger API and Service Implementation
dfuchs
parents:
diff changeset
   530
        for (Level loggerLevel : Level.values()) {
c1c71107d45f 8140364: JEP 264 Platform Logger API and Service Implementation
dfuchs
parents:
diff changeset
   531
            sink.level = loggerLevel;
c1c71107d45f 8140364: JEP 264 Platform Logger API and Service Implementation
dfuchs
parents:
diff changeset
   532
            for (PlatformLogger.Level messageLevel :julLevels) {
c1c71107d45f 8140364: JEP 264 Platform Logger API and Service Implementation
dfuchs
parents:
diff changeset
   533
                Level expectedMessageLevel = julToSpiMap.get(messageLevel);
c1c71107d45f 8140364: JEP 264 Platform Logger API and Service Implementation
dfuchs
parents:
diff changeset
   534
                TestLoggerFinder.LogEvent expected =
c1c71107d45f 8140364: JEP 264 Platform Logger API and Service Implementation
dfuchs
parents:
diff changeset
   535
                        TestLoggerFinder.LogEvent.of(
c1c71107d45f 8140364: JEP 264 Platform Logger API and Service Implementation
dfuchs
parents:
diff changeset
   536
                            sequencer.get(),
c1c71107d45f 8140364: JEP 264 Platform Logger API and Service Implementation
dfuchs
parents:
diff changeset
   537
                            loggerLevel != Level.OFF && expectedMessageLevel.compareTo(loggerLevel) >= 0,
c1c71107d45f 8140364: JEP 264 Platform Logger API and Service Implementation
dfuchs
parents:
diff changeset
   538
                            name, expectedMessageLevel, loggerBundle,
c1c71107d45f 8140364: JEP 264 Platform Logger API and Service Implementation
dfuchs
parents:
diff changeset
   539
                            fooMsg, null, (Throwable)null, (Object[])null);
c1c71107d45f 8140364: JEP 264 Platform Logger API and Service Implementation
dfuchs
parents:
diff changeset
   540
                String desc2 = "logger." + messageLevel.toString().toLowerCase()
c1c71107d45f 8140364: JEP 264 Platform Logger API and Service Implementation
dfuchs
parents:
diff changeset
   541
                        + "(fooMsg): loggerLevel="
c1c71107d45f 8140364: JEP 264 Platform Logger API and Service Implementation
dfuchs
parents:
diff changeset
   542
                        + loggerLevel+", messageLevel="+messageLevel;
c1c71107d45f 8140364: JEP 264 Platform Logger API and Service Implementation
dfuchs
parents:
diff changeset
   543
                if (messageLevel == PlatformLogger.Level.FINEST) {
c1c71107d45f 8140364: JEP 264 Platform Logger API and Service Implementation
dfuchs
parents:
diff changeset
   544
                    logger.finest(fooMsg);
c1c71107d45f 8140364: JEP 264 Platform Logger API and Service Implementation
dfuchs
parents:
diff changeset
   545
                    checkLogEvent(provider, desc2, expected);
c1c71107d45f 8140364: JEP 264 Platform Logger API and Service Implementation
dfuchs
parents:
diff changeset
   546
                } else if (messageLevel == PlatformLogger.Level.FINER) {
c1c71107d45f 8140364: JEP 264 Platform Logger API and Service Implementation
dfuchs
parents:
diff changeset
   547
                    logger.finer(fooMsg);
c1c71107d45f 8140364: JEP 264 Platform Logger API and Service Implementation
dfuchs
parents:
diff changeset
   548
                    checkLogEvent(provider, desc2, expected);
c1c71107d45f 8140364: JEP 264 Platform Logger API and Service Implementation
dfuchs
parents:
diff changeset
   549
                } else if (messageLevel == PlatformLogger.Level.FINE) {
c1c71107d45f 8140364: JEP 264 Platform Logger API and Service Implementation
dfuchs
parents:
diff changeset
   550
                    logger.fine(fooMsg);
c1c71107d45f 8140364: JEP 264 Platform Logger API and Service Implementation
dfuchs
parents:
diff changeset
   551
                    checkLogEvent(provider, desc2, expected);
c1c71107d45f 8140364: JEP 264 Platform Logger API and Service Implementation
dfuchs
parents:
diff changeset
   552
                } else if (messageLevel == PlatformLogger.Level.CONFIG) {
c1c71107d45f 8140364: JEP 264 Platform Logger API and Service Implementation
dfuchs
parents:
diff changeset
   553
                    logger.config(fooMsg);
c1c71107d45f 8140364: JEP 264 Platform Logger API and Service Implementation
dfuchs
parents:
diff changeset
   554
                    checkLogEvent(provider, desc2, expected);
c1c71107d45f 8140364: JEP 264 Platform Logger API and Service Implementation
dfuchs
parents:
diff changeset
   555
                } else if (messageLevel == PlatformLogger.Level.INFO) {
c1c71107d45f 8140364: JEP 264 Platform Logger API and Service Implementation
dfuchs
parents:
diff changeset
   556
                    logger.info(fooMsg);
c1c71107d45f 8140364: JEP 264 Platform Logger API and Service Implementation
dfuchs
parents:
diff changeset
   557
                    checkLogEvent(provider, desc2, expected);
c1c71107d45f 8140364: JEP 264 Platform Logger API and Service Implementation
dfuchs
parents:
diff changeset
   558
                } else if (messageLevel == PlatformLogger.Level.WARNING) {
c1c71107d45f 8140364: JEP 264 Platform Logger API and Service Implementation
dfuchs
parents:
diff changeset
   559
                    logger.warning(fooMsg);
c1c71107d45f 8140364: JEP 264 Platform Logger API and Service Implementation
dfuchs
parents:
diff changeset
   560
                    checkLogEvent(provider, desc2, expected);
c1c71107d45f 8140364: JEP 264 Platform Logger API and Service Implementation
dfuchs
parents:
diff changeset
   561
                } else if (messageLevel == PlatformLogger.Level.SEVERE) {
c1c71107d45f 8140364: JEP 264 Platform Logger API and Service Implementation
dfuchs
parents:
diff changeset
   562
                    logger.severe(fooMsg);
c1c71107d45f 8140364: JEP 264 Platform Logger API and Service Implementation
dfuchs
parents:
diff changeset
   563
                    checkLogEvent(provider, desc2, expected);
c1c71107d45f 8140364: JEP 264 Platform Logger API and Service Implementation
dfuchs
parents:
diff changeset
   564
                }
c1c71107d45f 8140364: JEP 264 Platform Logger API and Service Implementation
dfuchs
parents:
diff changeset
   565
            }
c1c71107d45f 8140364: JEP 264 Platform Logger API and Service Implementation
dfuchs
parents:
diff changeset
   566
        }
c1c71107d45f 8140364: JEP 264 Platform Logger API and Service Implementation
dfuchs
parents:
diff changeset
   567
c1c71107d45f 8140364: JEP 264 Platform Logger API and Service Implementation
dfuchs
parents:
diff changeset
   568
        Throwable thrown = new Exception("OK: log me!");
c1c71107d45f 8140364: JEP 264 Platform Logger API and Service Implementation
dfuchs
parents:
diff changeset
   569
        System.out.println("\tlogger.<level>(msg, thrown)");
c1c71107d45f 8140364: JEP 264 Platform Logger API and Service Implementation
dfuchs
parents:
diff changeset
   570
        for (Level loggerLevel : Level.values()) {
c1c71107d45f 8140364: JEP 264 Platform Logger API and Service Implementation
dfuchs
parents:
diff changeset
   571
            sink.level = loggerLevel;
c1c71107d45f 8140364: JEP 264 Platform Logger API and Service Implementation
dfuchs
parents:
diff changeset
   572
            for (PlatformLogger.Level messageLevel :julLevels) {
c1c71107d45f 8140364: JEP 264 Platform Logger API and Service Implementation
dfuchs
parents:
diff changeset
   573
                Level expectedMessageLevel = julToSpiMap.get(messageLevel);
c1c71107d45f 8140364: JEP 264 Platform Logger API and Service Implementation
dfuchs
parents:
diff changeset
   574
                TestLoggerFinder.LogEvent expected =
c1c71107d45f 8140364: JEP 264 Platform Logger API and Service Implementation
dfuchs
parents:
diff changeset
   575
                        TestLoggerFinder.LogEvent.of(
c1c71107d45f 8140364: JEP 264 Platform Logger API and Service Implementation
dfuchs
parents:
diff changeset
   576
                            sequencer.get(),
c1c71107d45f 8140364: JEP 264 Platform Logger API and Service Implementation
dfuchs
parents:
diff changeset
   577
                            loggerLevel != Level.OFF && expectedMessageLevel.compareTo(loggerLevel) >= 0,
c1c71107d45f 8140364: JEP 264 Platform Logger API and Service Implementation
dfuchs
parents:
diff changeset
   578
                            name, expectedMessageLevel, (ResourceBundle) null,
c1c71107d45f 8140364: JEP 264 Platform Logger API and Service Implementation
dfuchs
parents:
diff changeset
   579
                            fooMsg, null, (Throwable)thrown, (Object[])null);
c1c71107d45f 8140364: JEP 264 Platform Logger API and Service Implementation
dfuchs
parents:
diff changeset
   580
                String desc2 = "logger." + messageLevel.toString().toLowerCase()
c1c71107d45f 8140364: JEP 264 Platform Logger API and Service Implementation
dfuchs
parents:
diff changeset
   581
                        + "(msg, thrown): loggerLevel="
c1c71107d45f 8140364: JEP 264 Platform Logger API and Service Implementation
dfuchs
parents:
diff changeset
   582
                        + loggerLevel+", messageLevel="+messageLevel;
c1c71107d45f 8140364: JEP 264 Platform Logger API and Service Implementation
dfuchs
parents:
diff changeset
   583
                if (messageLevel == PlatformLogger.Level.FINEST) {
c1c71107d45f 8140364: JEP 264 Platform Logger API and Service Implementation
dfuchs
parents:
diff changeset
   584
                    logger.finest(fooMsg, thrown);
c1c71107d45f 8140364: JEP 264 Platform Logger API and Service Implementation
dfuchs
parents:
diff changeset
   585
                    checkLogEvent(provider, desc2, expected);
c1c71107d45f 8140364: JEP 264 Platform Logger API and Service Implementation
dfuchs
parents:
diff changeset
   586
                } else if (messageLevel == PlatformLogger.Level.FINER) {
c1c71107d45f 8140364: JEP 264 Platform Logger API and Service Implementation
dfuchs
parents:
diff changeset
   587
                    logger.finer(fooMsg, thrown);
c1c71107d45f 8140364: JEP 264 Platform Logger API and Service Implementation
dfuchs
parents:
diff changeset
   588
                    checkLogEvent(provider, desc2, expected);
c1c71107d45f 8140364: JEP 264 Platform Logger API and Service Implementation
dfuchs
parents:
diff changeset
   589
                } else if (messageLevel == PlatformLogger.Level.FINE) {
c1c71107d45f 8140364: JEP 264 Platform Logger API and Service Implementation
dfuchs
parents:
diff changeset
   590
                    logger.fine(fooMsg, thrown);
c1c71107d45f 8140364: JEP 264 Platform Logger API and Service Implementation
dfuchs
parents:
diff changeset
   591
                    checkLogEvent(provider, desc2, expected);
c1c71107d45f 8140364: JEP 264 Platform Logger API and Service Implementation
dfuchs
parents:
diff changeset
   592
                } else if (messageLevel == PlatformLogger.Level.CONFIG) {
c1c71107d45f 8140364: JEP 264 Platform Logger API and Service Implementation
dfuchs
parents:
diff changeset
   593
                    logger.config(fooMsg, thrown);
c1c71107d45f 8140364: JEP 264 Platform Logger API and Service Implementation
dfuchs
parents:
diff changeset
   594
                    checkLogEvent(provider, desc2, expected);
c1c71107d45f 8140364: JEP 264 Platform Logger API and Service Implementation
dfuchs
parents:
diff changeset
   595
                } else if (messageLevel == PlatformLogger.Level.INFO) {
c1c71107d45f 8140364: JEP 264 Platform Logger API and Service Implementation
dfuchs
parents:
diff changeset
   596
                    logger.info(fooMsg, thrown);
c1c71107d45f 8140364: JEP 264 Platform Logger API and Service Implementation
dfuchs
parents:
diff changeset
   597
                    checkLogEvent(provider, desc2, expected);
c1c71107d45f 8140364: JEP 264 Platform Logger API and Service Implementation
dfuchs
parents:
diff changeset
   598
                } else if (messageLevel == PlatformLogger.Level.WARNING) {
c1c71107d45f 8140364: JEP 264 Platform Logger API and Service Implementation
dfuchs
parents:
diff changeset
   599
                    logger.warning(fooMsg, thrown);
c1c71107d45f 8140364: JEP 264 Platform Logger API and Service Implementation
dfuchs
parents:
diff changeset
   600
                    checkLogEvent(provider, desc2, expected);
c1c71107d45f 8140364: JEP 264 Platform Logger API and Service Implementation
dfuchs
parents:
diff changeset
   601
                } else if (messageLevel == PlatformLogger.Level.SEVERE) {
c1c71107d45f 8140364: JEP 264 Platform Logger API and Service Implementation
dfuchs
parents:
diff changeset
   602
                    logger.severe(fooMsg, thrown);
c1c71107d45f 8140364: JEP 264 Platform Logger API and Service Implementation
dfuchs
parents:
diff changeset
   603
                    checkLogEvent(provider, desc2, expected);
c1c71107d45f 8140364: JEP 264 Platform Logger API and Service Implementation
dfuchs
parents:
diff changeset
   604
                }
c1c71107d45f 8140364: JEP 264 Platform Logger API and Service Implementation
dfuchs
parents:
diff changeset
   605
            }
c1c71107d45f 8140364: JEP 264 Platform Logger API and Service Implementation
dfuchs
parents:
diff changeset
   606
        }
c1c71107d45f 8140364: JEP 264 Platform Logger API and Service Implementation
dfuchs
parents:
diff changeset
   607
c1c71107d45f 8140364: JEP 264 Platform Logger API and Service Implementation
dfuchs
parents:
diff changeset
   608
        String format = "two params [{1} {2}]";
c1c71107d45f 8140364: JEP 264 Platform Logger API and Service Implementation
dfuchs
parents:
diff changeset
   609
        Object arg1 = foo;
c1c71107d45f 8140364: JEP 264 Platform Logger API and Service Implementation
dfuchs
parents:
diff changeset
   610
        Object arg2 = fooMsg;
c1c71107d45f 8140364: JEP 264 Platform Logger API and Service Implementation
dfuchs
parents:
diff changeset
   611
        System.out.println("\tlogger.<level>(format, arg1, arg2)");
c1c71107d45f 8140364: JEP 264 Platform Logger API and Service Implementation
dfuchs
parents:
diff changeset
   612
        for (Level loggerLevel : Level.values()) {
c1c71107d45f 8140364: JEP 264 Platform Logger API and Service Implementation
dfuchs
parents:
diff changeset
   613
            sink.level = loggerLevel;
c1c71107d45f 8140364: JEP 264 Platform Logger API and Service Implementation
dfuchs
parents:
diff changeset
   614
            for (PlatformLogger.Level messageLevel :julLevels) {
c1c71107d45f 8140364: JEP 264 Platform Logger API and Service Implementation
dfuchs
parents:
diff changeset
   615
                Level expectedMessageLevel = julToSpiMap.get(messageLevel);
c1c71107d45f 8140364: JEP 264 Platform Logger API and Service Implementation
dfuchs
parents:
diff changeset
   616
                TestLoggerFinder.LogEvent expected =
c1c71107d45f 8140364: JEP 264 Platform Logger API and Service Implementation
dfuchs
parents:
diff changeset
   617
                        TestLoggerFinder.LogEvent.of(
c1c71107d45f 8140364: JEP 264 Platform Logger API and Service Implementation
dfuchs
parents:
diff changeset
   618
                            sequencer.get(),
c1c71107d45f 8140364: JEP 264 Platform Logger API and Service Implementation
dfuchs
parents:
diff changeset
   619
                            loggerLevel != Level.OFF && expectedMessageLevel.compareTo(loggerLevel) >= 0,
c1c71107d45f 8140364: JEP 264 Platform Logger API and Service Implementation
dfuchs
parents:
diff changeset
   620
                            name, expectedMessageLevel, (ResourceBundle) null,
c1c71107d45f 8140364: JEP 264 Platform Logger API and Service Implementation
dfuchs
parents:
diff changeset
   621
                            format, null, (Throwable)null, foo, fooMsg);
c1c71107d45f 8140364: JEP 264 Platform Logger API and Service Implementation
dfuchs
parents:
diff changeset
   622
                String desc2 = "logger." + messageLevel.toString().toLowerCase()
c1c71107d45f 8140364: JEP 264 Platform Logger API and Service Implementation
dfuchs
parents:
diff changeset
   623
                        + "(format, foo, fooMsg): loggerLevel="
c1c71107d45f 8140364: JEP 264 Platform Logger API and Service Implementation
dfuchs
parents:
diff changeset
   624
                        + loggerLevel+", messageLevel="+messageLevel;
c1c71107d45f 8140364: JEP 264 Platform Logger API and Service Implementation
dfuchs
parents:
diff changeset
   625
                if (messageLevel == PlatformLogger.Level.FINEST) {
c1c71107d45f 8140364: JEP 264 Platform Logger API and Service Implementation
dfuchs
parents:
diff changeset
   626
                    logger.finest(format, foo, fooMsg);
c1c71107d45f 8140364: JEP 264 Platform Logger API and Service Implementation
dfuchs
parents:
diff changeset
   627
                    checkLogEvent(provider, desc2, expected);
c1c71107d45f 8140364: JEP 264 Platform Logger API and Service Implementation
dfuchs
parents:
diff changeset
   628
                } else if (messageLevel == PlatformLogger.Level.FINER) {
c1c71107d45f 8140364: JEP 264 Platform Logger API and Service Implementation
dfuchs
parents:
diff changeset
   629
                    logger.finer(format, foo, fooMsg);
c1c71107d45f 8140364: JEP 264 Platform Logger API and Service Implementation
dfuchs
parents:
diff changeset
   630
                    checkLogEvent(provider, desc2, expected);
c1c71107d45f 8140364: JEP 264 Platform Logger API and Service Implementation
dfuchs
parents:
diff changeset
   631
                } else if (messageLevel == PlatformLogger.Level.FINE) {
c1c71107d45f 8140364: JEP 264 Platform Logger API and Service Implementation
dfuchs
parents:
diff changeset
   632
                    logger.fine(format, foo, fooMsg);
c1c71107d45f 8140364: JEP 264 Platform Logger API and Service Implementation
dfuchs
parents:
diff changeset
   633
                    checkLogEvent(provider, desc2, expected);
c1c71107d45f 8140364: JEP 264 Platform Logger API and Service Implementation
dfuchs
parents:
diff changeset
   634
                } else if (messageLevel == PlatformLogger.Level.CONFIG) {
c1c71107d45f 8140364: JEP 264 Platform Logger API and Service Implementation
dfuchs
parents:
diff changeset
   635
                    logger.config(format, foo, fooMsg);
c1c71107d45f 8140364: JEP 264 Platform Logger API and Service Implementation
dfuchs
parents:
diff changeset
   636
                    checkLogEvent(provider, desc2, expected);
c1c71107d45f 8140364: JEP 264 Platform Logger API and Service Implementation
dfuchs
parents:
diff changeset
   637
                } else if (messageLevel == PlatformLogger.Level.INFO) {
c1c71107d45f 8140364: JEP 264 Platform Logger API and Service Implementation
dfuchs
parents:
diff changeset
   638
                    logger.info(format, foo, fooMsg);
c1c71107d45f 8140364: JEP 264 Platform Logger API and Service Implementation
dfuchs
parents:
diff changeset
   639
                    checkLogEvent(provider, desc2, expected);
c1c71107d45f 8140364: JEP 264 Platform Logger API and Service Implementation
dfuchs
parents:
diff changeset
   640
                } else if (messageLevel == PlatformLogger.Level.WARNING) {
c1c71107d45f 8140364: JEP 264 Platform Logger API and Service Implementation
dfuchs
parents:
diff changeset
   641
                    logger.warning(format, foo, fooMsg);
c1c71107d45f 8140364: JEP 264 Platform Logger API and Service Implementation
dfuchs
parents:
diff changeset
   642
                    checkLogEvent(provider, desc2, expected);
c1c71107d45f 8140364: JEP 264 Platform Logger API and Service Implementation
dfuchs
parents:
diff changeset
   643
                } else if (messageLevel == PlatformLogger.Level.SEVERE) {
c1c71107d45f 8140364: JEP 264 Platform Logger API and Service Implementation
dfuchs
parents:
diff changeset
   644
                    logger.severe(format, foo, fooMsg);
c1c71107d45f 8140364: JEP 264 Platform Logger API and Service Implementation
dfuchs
parents:
diff changeset
   645
                    checkLogEvent(provider, desc2, expected);
c1c71107d45f 8140364: JEP 264 Platform Logger API and Service Implementation
dfuchs
parents:
diff changeset
   646
                }
c1c71107d45f 8140364: JEP 264 Platform Logger API and Service Implementation
dfuchs
parents:
diff changeset
   647
            }
c1c71107d45f 8140364: JEP 264 Platform Logger API and Service Implementation
dfuchs
parents:
diff changeset
   648
        }
c1c71107d45f 8140364: JEP 264 Platform Logger API and Service Implementation
dfuchs
parents:
diff changeset
   649
c1c71107d45f 8140364: JEP 264 Platform Logger API and Service Implementation
dfuchs
parents:
diff changeset
   650
    }
c1c71107d45f 8140364: JEP 264 Platform Logger API and Service Implementation
dfuchs
parents:
diff changeset
   651
c1c71107d45f 8140364: JEP 264 Platform Logger API and Service Implementation
dfuchs
parents:
diff changeset
   652
    final static class PermissionsBuilder {
c1c71107d45f 8140364: JEP 264 Platform Logger API and Service Implementation
dfuchs
parents:
diff changeset
   653
        final Permissions perms;
c1c71107d45f 8140364: JEP 264 Platform Logger API and Service Implementation
dfuchs
parents:
diff changeset
   654
        public PermissionsBuilder() {
c1c71107d45f 8140364: JEP 264 Platform Logger API and Service Implementation
dfuchs
parents:
diff changeset
   655
            this(new Permissions());
c1c71107d45f 8140364: JEP 264 Platform Logger API and Service Implementation
dfuchs
parents:
diff changeset
   656
        }
c1c71107d45f 8140364: JEP 264 Platform Logger API and Service Implementation
dfuchs
parents:
diff changeset
   657
        public PermissionsBuilder(Permissions perms) {
c1c71107d45f 8140364: JEP 264 Platform Logger API and Service Implementation
dfuchs
parents:
diff changeset
   658
            this.perms = perms;
c1c71107d45f 8140364: JEP 264 Platform Logger API and Service Implementation
dfuchs
parents:
diff changeset
   659
        }
c1c71107d45f 8140364: JEP 264 Platform Logger API and Service Implementation
dfuchs
parents:
diff changeset
   660
        public PermissionsBuilder add(Permission p) {
c1c71107d45f 8140364: JEP 264 Platform Logger API and Service Implementation
dfuchs
parents:
diff changeset
   661
            perms.add(p);
c1c71107d45f 8140364: JEP 264 Platform Logger API and Service Implementation
dfuchs
parents:
diff changeset
   662
            return this;
c1c71107d45f 8140364: JEP 264 Platform Logger API and Service Implementation
dfuchs
parents:
diff changeset
   663
        }
c1c71107d45f 8140364: JEP 264 Platform Logger API and Service Implementation
dfuchs
parents:
diff changeset
   664
        public PermissionsBuilder addAll(PermissionCollection col) {
c1c71107d45f 8140364: JEP 264 Platform Logger API and Service Implementation
dfuchs
parents:
diff changeset
   665
            if (col != null) {
c1c71107d45f 8140364: JEP 264 Platform Logger API and Service Implementation
dfuchs
parents:
diff changeset
   666
                for (Enumeration<Permission> e = col.elements(); e.hasMoreElements(); ) {
c1c71107d45f 8140364: JEP 264 Platform Logger API and Service Implementation
dfuchs
parents:
diff changeset
   667
                    perms.add(e.nextElement());
c1c71107d45f 8140364: JEP 264 Platform Logger API and Service Implementation
dfuchs
parents:
diff changeset
   668
                }
c1c71107d45f 8140364: JEP 264 Platform Logger API and Service Implementation
dfuchs
parents:
diff changeset
   669
            }
c1c71107d45f 8140364: JEP 264 Platform Logger API and Service Implementation
dfuchs
parents:
diff changeset
   670
            return this;
c1c71107d45f 8140364: JEP 264 Platform Logger API and Service Implementation
dfuchs
parents:
diff changeset
   671
        }
c1c71107d45f 8140364: JEP 264 Platform Logger API and Service Implementation
dfuchs
parents:
diff changeset
   672
        public Permissions toPermissions() {
c1c71107d45f 8140364: JEP 264 Platform Logger API and Service Implementation
dfuchs
parents:
diff changeset
   673
            final PermissionsBuilder builder = new PermissionsBuilder();
c1c71107d45f 8140364: JEP 264 Platform Logger API and Service Implementation
dfuchs
parents:
diff changeset
   674
            builder.addAll(perms);
c1c71107d45f 8140364: JEP 264 Platform Logger API and Service Implementation
dfuchs
parents:
diff changeset
   675
            return builder.perms;
c1c71107d45f 8140364: JEP 264 Platform Logger API and Service Implementation
dfuchs
parents:
diff changeset
   676
        }
c1c71107d45f 8140364: JEP 264 Platform Logger API and Service Implementation
dfuchs
parents:
diff changeset
   677
    }
c1c71107d45f 8140364: JEP 264 Platform Logger API and Service Implementation
dfuchs
parents:
diff changeset
   678
c1c71107d45f 8140364: JEP 264 Platform Logger API and Service Implementation
dfuchs
parents:
diff changeset
   679
    public static class SimplePolicy extends Policy {
c1c71107d45f 8140364: JEP 264 Platform Logger API and Service Implementation
dfuchs
parents:
diff changeset
   680
        final static RuntimePermission CONTROL = LOGGERFINDER_PERMISSION;
c1c71107d45f 8140364: JEP 264 Platform Logger API and Service Implementation
dfuchs
parents:
diff changeset
   681
        final static RuntimePermission ACCESS_LOGGING = new RuntimePermission("accessClassInPackage.sun.util.logging");
c1c71107d45f 8140364: JEP 264 Platform Logger API and Service Implementation
dfuchs
parents:
diff changeset
   682
c1c71107d45f 8140364: JEP 264 Platform Logger API and Service Implementation
dfuchs
parents:
diff changeset
   683
        final Permissions permissions;
c1c71107d45f 8140364: JEP 264 Platform Logger API and Service Implementation
dfuchs
parents:
diff changeset
   684
        final Permissions allPermissions;
c1c71107d45f 8140364: JEP 264 Platform Logger API and Service Implementation
dfuchs
parents:
diff changeset
   685
        final ThreadLocal<AtomicBoolean> allowControl;
c1c71107d45f 8140364: JEP 264 Platform Logger API and Service Implementation
dfuchs
parents:
diff changeset
   686
        final ThreadLocal<AtomicBoolean> allowAccess;
c1c71107d45f 8140364: JEP 264 Platform Logger API and Service Implementation
dfuchs
parents:
diff changeset
   687
        final ThreadLocal<AtomicBoolean> allowAll;
c1c71107d45f 8140364: JEP 264 Platform Logger API and Service Implementation
dfuchs
parents:
diff changeset
   688
        public SimplePolicy(ThreadLocal<AtomicBoolean> allowControl,
c1c71107d45f 8140364: JEP 264 Platform Logger API and Service Implementation
dfuchs
parents:
diff changeset
   689
                ThreadLocal<AtomicBoolean> allowAccess,
c1c71107d45f 8140364: JEP 264 Platform Logger API and Service Implementation
dfuchs
parents:
diff changeset
   690
                ThreadLocal<AtomicBoolean> allowAll) {
c1c71107d45f 8140364: JEP 264 Platform Logger API and Service Implementation
dfuchs
parents:
diff changeset
   691
            this.allowControl = allowControl;
c1c71107d45f 8140364: JEP 264 Platform Logger API and Service Implementation
dfuchs
parents:
diff changeset
   692
            this.allowAccess = allowAccess;
c1c71107d45f 8140364: JEP 264 Platform Logger API and Service Implementation
dfuchs
parents:
diff changeset
   693
            this.allowAll = allowAll;
c1c71107d45f 8140364: JEP 264 Platform Logger API and Service Implementation
dfuchs
parents:
diff changeset
   694
            permissions = new Permissions();
c1c71107d45f 8140364: JEP 264 Platform Logger API and Service Implementation
dfuchs
parents:
diff changeset
   695
            allPermissions = new PermissionsBuilder()
c1c71107d45f 8140364: JEP 264 Platform Logger API and Service Implementation
dfuchs
parents:
diff changeset
   696
                    .add(new java.security.AllPermission())
c1c71107d45f 8140364: JEP 264 Platform Logger API and Service Implementation
dfuchs
parents:
diff changeset
   697
                    .toPermissions();
c1c71107d45f 8140364: JEP 264 Platform Logger API and Service Implementation
dfuchs
parents:
diff changeset
   698
        }
c1c71107d45f 8140364: JEP 264 Platform Logger API and Service Implementation
dfuchs
parents:
diff changeset
   699
c1c71107d45f 8140364: JEP 264 Platform Logger API and Service Implementation
dfuchs
parents:
diff changeset
   700
        Permissions getPermissions() {
c1c71107d45f 8140364: JEP 264 Platform Logger API and Service Implementation
dfuchs
parents:
diff changeset
   701
            if (allowControl.get().get() || allowAccess.get().get() || allowAll.get().get()) {
c1c71107d45f 8140364: JEP 264 Platform Logger API and Service Implementation
dfuchs
parents:
diff changeset
   702
                PermissionsBuilder builder =  new PermissionsBuilder()
c1c71107d45f 8140364: JEP 264 Platform Logger API and Service Implementation
dfuchs
parents:
diff changeset
   703
                        .addAll(permissions);
c1c71107d45f 8140364: JEP 264 Platform Logger API and Service Implementation
dfuchs
parents:
diff changeset
   704
                if (allowControl.get().get()) {
c1c71107d45f 8140364: JEP 264 Platform Logger API and Service Implementation
dfuchs
parents:
diff changeset
   705
                    builder.add(CONTROL);
c1c71107d45f 8140364: JEP 264 Platform Logger API and Service Implementation
dfuchs
parents:
diff changeset
   706
                }
c1c71107d45f 8140364: JEP 264 Platform Logger API and Service Implementation
dfuchs
parents:
diff changeset
   707
                if (allowAccess.get().get()) {
c1c71107d45f 8140364: JEP 264 Platform Logger API and Service Implementation
dfuchs
parents:
diff changeset
   708
                    builder.add(ACCESS_LOGGING);
c1c71107d45f 8140364: JEP 264 Platform Logger API and Service Implementation
dfuchs
parents:
diff changeset
   709
                }
c1c71107d45f 8140364: JEP 264 Platform Logger API and Service Implementation
dfuchs
parents:
diff changeset
   710
                if (allowAll.get().get()) {
c1c71107d45f 8140364: JEP 264 Platform Logger API and Service Implementation
dfuchs
parents:
diff changeset
   711
                    builder.addAll(allPermissions);
c1c71107d45f 8140364: JEP 264 Platform Logger API and Service Implementation
dfuchs
parents:
diff changeset
   712
                }
c1c71107d45f 8140364: JEP 264 Platform Logger API and Service Implementation
dfuchs
parents:
diff changeset
   713
                return builder.toPermissions();
c1c71107d45f 8140364: JEP 264 Platform Logger API and Service Implementation
dfuchs
parents:
diff changeset
   714
            }
c1c71107d45f 8140364: JEP 264 Platform Logger API and Service Implementation
dfuchs
parents:
diff changeset
   715
            return permissions;
c1c71107d45f 8140364: JEP 264 Platform Logger API and Service Implementation
dfuchs
parents:
diff changeset
   716
        }
c1c71107d45f 8140364: JEP 264 Platform Logger API and Service Implementation
dfuchs
parents:
diff changeset
   717
c1c71107d45f 8140364: JEP 264 Platform Logger API and Service Implementation
dfuchs
parents:
diff changeset
   718
        @Override
c1c71107d45f 8140364: JEP 264 Platform Logger API and Service Implementation
dfuchs
parents:
diff changeset
   719
        public boolean implies(ProtectionDomain domain, Permission permission) {
c1c71107d45f 8140364: JEP 264 Platform Logger API and Service Implementation
dfuchs
parents:
diff changeset
   720
            return getPermissions().implies(permission);
c1c71107d45f 8140364: JEP 264 Platform Logger API and Service Implementation
dfuchs
parents:
diff changeset
   721
        }
c1c71107d45f 8140364: JEP 264 Platform Logger API and Service Implementation
dfuchs
parents:
diff changeset
   722
c1c71107d45f 8140364: JEP 264 Platform Logger API and Service Implementation
dfuchs
parents:
diff changeset
   723
        @Override
c1c71107d45f 8140364: JEP 264 Platform Logger API and Service Implementation
dfuchs
parents:
diff changeset
   724
        public PermissionCollection getPermissions(CodeSource codesource) {
c1c71107d45f 8140364: JEP 264 Platform Logger API and Service Implementation
dfuchs
parents:
diff changeset
   725
            return new PermissionsBuilder().addAll(getPermissions()).toPermissions();
c1c71107d45f 8140364: JEP 264 Platform Logger API and Service Implementation
dfuchs
parents:
diff changeset
   726
        }
c1c71107d45f 8140364: JEP 264 Platform Logger API and Service Implementation
dfuchs
parents:
diff changeset
   727
c1c71107d45f 8140364: JEP 264 Platform Logger API and Service Implementation
dfuchs
parents:
diff changeset
   728
        @Override
c1c71107d45f 8140364: JEP 264 Platform Logger API and Service Implementation
dfuchs
parents:
diff changeset
   729
        public PermissionCollection getPermissions(ProtectionDomain domain) {
c1c71107d45f 8140364: JEP 264 Platform Logger API and Service Implementation
dfuchs
parents:
diff changeset
   730
            return new PermissionsBuilder().addAll(getPermissions()).toPermissions();
c1c71107d45f 8140364: JEP 264 Platform Logger API and Service Implementation
dfuchs
parents:
diff changeset
   731
        }
c1c71107d45f 8140364: JEP 264 Platform Logger API and Service Implementation
dfuchs
parents:
diff changeset
   732
    }
c1c71107d45f 8140364: JEP 264 Platform Logger API and Service Implementation
dfuchs
parents:
diff changeset
   733
}