jdk/test/java/lang/System/LoggerFinder/BaseLoggerFinderTest/BaseLoggerFinder.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
c1c71107d45f 8140364: JEP 264 Platform Logger API and Service Implementation
dfuchs
parents:
diff changeset
    24
import java.security.AccessController;
c1c71107d45f 8140364: JEP 264 Platform Logger API and Service Implementation
dfuchs
parents:
diff changeset
    25
import java.security.PrivilegedAction;
c1c71107d45f 8140364: JEP 264 Platform Logger API and Service Implementation
dfuchs
parents:
diff changeset
    26
import java.lang.System.LoggerFinder;
c1c71107d45f 8140364: JEP 264 Platform Logger API and Service Implementation
dfuchs
parents:
diff changeset
    27
import java.lang.System.Logger;
c1c71107d45f 8140364: JEP 264 Platform Logger API and Service Implementation
dfuchs
parents:
diff changeset
    28
c1c71107d45f 8140364: JEP 264 Platform Logger API and Service Implementation
dfuchs
parents:
diff changeset
    29
public  class BaseLoggerFinder extends LoggerFinder implements TestLoggerFinder {
c1c71107d45f 8140364: JEP 264 Platform Logger API and Service Implementation
dfuchs
parents:
diff changeset
    30
c1c71107d45f 8140364: JEP 264 Platform Logger API and Service Implementation
dfuchs
parents:
diff changeset
    31
    static final RuntimePermission LOGGERFINDER_PERMISSION =
c1c71107d45f 8140364: JEP 264 Platform Logger API and Service Implementation
dfuchs
parents:
diff changeset
    32
                new RuntimePermission("loggerFinder");
c1c71107d45f 8140364: JEP 264 Platform Logger API and Service Implementation
dfuchs
parents:
diff changeset
    33
    @Override
37672
03684934dc09 8148568: LoggerFinder.getLogger and LoggerFinder.getLocalizedLogger should take a Module argument instead of a Class.
dfuchs
parents: 33875
diff changeset
    34
    public Logger getLogger(String name, Module caller) {
33875
c1c71107d45f 8140364: JEP 264 Platform Logger API and Service Implementation
dfuchs
parents:
diff changeset
    35
        SecurityManager sm = System.getSecurityManager();
c1c71107d45f 8140364: JEP 264 Platform Logger API and Service Implementation
dfuchs
parents:
diff changeset
    36
        if (sm != null) {
c1c71107d45f 8140364: JEP 264 Platform Logger API and Service Implementation
dfuchs
parents:
diff changeset
    37
            sm.checkPermission(LOGGERFINDER_PERMISSION);
c1c71107d45f 8140364: JEP 264 Platform Logger API and Service Implementation
dfuchs
parents:
diff changeset
    38
        }
c1c71107d45f 8140364: JEP 264 Platform Logger API and Service Implementation
dfuchs
parents:
diff changeset
    39
        PrivilegedAction<ClassLoader> pa = () -> caller.getClassLoader();
c1c71107d45f 8140364: JEP 264 Platform Logger API and Service Implementation
dfuchs
parents:
diff changeset
    40
        ClassLoader callerLoader = AccessController.doPrivileged(pa);
c1c71107d45f 8140364: JEP 264 Platform Logger API and Service Implementation
dfuchs
parents:
diff changeset
    41
        if (callerLoader == null) {
c1c71107d45f 8140364: JEP 264 Platform Logger API and Service Implementation
dfuchs
parents:
diff changeset
    42
            return system.computeIfAbsent(name, (n) -> new LoggerImpl(n));
c1c71107d45f 8140364: JEP 264 Platform Logger API and Service Implementation
dfuchs
parents:
diff changeset
    43
        } else {
c1c71107d45f 8140364: JEP 264 Platform Logger API and Service Implementation
dfuchs
parents:
diff changeset
    44
            return user.computeIfAbsent(name, (n) -> new LoggerImpl(n));
c1c71107d45f 8140364: JEP 264 Platform Logger API and Service Implementation
dfuchs
parents:
diff changeset
    45
        }
c1c71107d45f 8140364: JEP 264 Platform Logger API and Service Implementation
dfuchs
parents:
diff changeset
    46
    }
c1c71107d45f 8140364: JEP 264 Platform Logger API and Service Implementation
dfuchs
parents:
diff changeset
    47
}