src/jdk.dynalink/share/classes/jdk/dynalink/linker/GuardingDynamicLinkerExporter.java
author mbaesken
Tue, 02 Jul 2019 13:45:08 +0200
changeset 55556 19d0b382f086
parent 47216 71c04702a3d5
permissions -rw-r--r--
8226238: Improve error output and fix elf issues in os::dll_load Reviewed-by: clanger, mdoerr
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
33340
6c945c826f36 8139895: Introduce GuardingDynamicLinkerExporter
attila
parents:
diff changeset
     1
/*
6c945c826f36 8139895: Introduce GuardingDynamicLinkerExporter
attila
parents:
diff changeset
     2
 * Copyright (c) 2015, Oracle and/or its affiliates. All rights reserved.
6c945c826f36 8139895: Introduce GuardingDynamicLinkerExporter
attila
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
6c945c826f36 8139895: Introduce GuardingDynamicLinkerExporter
attila
parents:
diff changeset
     4
 *
6c945c826f36 8139895: Introduce GuardingDynamicLinkerExporter
attila
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
6c945c826f36 8139895: Introduce GuardingDynamicLinkerExporter
attila
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
6c945c826f36 8139895: Introduce GuardingDynamicLinkerExporter
attila
parents:
diff changeset
     7
 * published by the Free Software Foundation.  Oracle designates this
6c945c826f36 8139895: Introduce GuardingDynamicLinkerExporter
attila
parents:
diff changeset
     8
 * particular file as subject to the "Classpath" exception as provided
6c945c826f36 8139895: Introduce GuardingDynamicLinkerExporter
attila
parents:
diff changeset
     9
 * by Oracle in the LICENSE file that accompanied this code.
6c945c826f36 8139895: Introduce GuardingDynamicLinkerExporter
attila
parents:
diff changeset
    10
 *
6c945c826f36 8139895: Introduce GuardingDynamicLinkerExporter
attila
parents:
diff changeset
    11
 * This code is distributed in the hope that it will be useful, but WITHOUT
6c945c826f36 8139895: Introduce GuardingDynamicLinkerExporter
attila
parents:
diff changeset
    12
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
6c945c826f36 8139895: Introduce GuardingDynamicLinkerExporter
attila
parents:
diff changeset
    13
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
6c945c826f36 8139895: Introduce GuardingDynamicLinkerExporter
attila
parents:
diff changeset
    14
 * version 2 for more details (a copy is included in the LICENSE file that
6c945c826f36 8139895: Introduce GuardingDynamicLinkerExporter
attila
parents:
diff changeset
    15
 * accompanied this code).
6c945c826f36 8139895: Introduce GuardingDynamicLinkerExporter
attila
parents:
diff changeset
    16
 *
6c945c826f36 8139895: Introduce GuardingDynamicLinkerExporter
attila
parents:
diff changeset
    17
 * You should have received a copy of the GNU General Public License version
6c945c826f36 8139895: Introduce GuardingDynamicLinkerExporter
attila
parents:
diff changeset
    18
 * 2 along with this work; if not, write to the Free Software Foundation,
6c945c826f36 8139895: Introduce GuardingDynamicLinkerExporter
attila
parents:
diff changeset
    19
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
6c945c826f36 8139895: Introduce GuardingDynamicLinkerExporter
attila
parents:
diff changeset
    20
 *
6c945c826f36 8139895: Introduce GuardingDynamicLinkerExporter
attila
parents:
diff changeset
    21
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
6c945c826f36 8139895: Introduce GuardingDynamicLinkerExporter
attila
parents:
diff changeset
    22
 * or visit www.oracle.com if you need additional information or have any
6c945c826f36 8139895: Introduce GuardingDynamicLinkerExporter
attila
parents:
diff changeset
    23
 * questions.
6c945c826f36 8139895: Introduce GuardingDynamicLinkerExporter
attila
parents:
diff changeset
    24
 */
6c945c826f36 8139895: Introduce GuardingDynamicLinkerExporter
attila
parents:
diff changeset
    25
34447
ec4c069f9436 8141338: Move jdk.internal.dynalink package to jdk.dynalink
attila
parents: 33340
diff changeset
    26
package jdk.dynalink.linker;
33340
6c945c826f36 8139895: Introduce GuardingDynamicLinkerExporter
attila
parents:
diff changeset
    27
6c945c826f36 8139895: Introduce GuardingDynamicLinkerExporter
attila
parents:
diff changeset
    28
import java.security.Permission;
6c945c826f36 8139895: Introduce GuardingDynamicLinkerExporter
attila
parents:
diff changeset
    29
import java.util.List;
6c945c826f36 8139895: Introduce GuardingDynamicLinkerExporter
attila
parents:
diff changeset
    30
import java.util.ServiceLoader;
6c945c826f36 8139895: Introduce GuardingDynamicLinkerExporter
attila
parents:
diff changeset
    31
import java.util.function.Supplier;
34447
ec4c069f9436 8141338: Move jdk.internal.dynalink package to jdk.dynalink
attila
parents: 33340
diff changeset
    32
import jdk.dynalink.DynamicLinkerFactory;
33340
6c945c826f36 8139895: Introduce GuardingDynamicLinkerExporter
attila
parents:
diff changeset
    33
6c945c826f36 8139895: Introduce GuardingDynamicLinkerExporter
attila
parents:
diff changeset
    34
/**
6c945c826f36 8139895: Introduce GuardingDynamicLinkerExporter
attila
parents:
diff changeset
    35
 * A class acting as a supplier of guarding dynamic linkers that can be
6c945c826f36 8139895: Introduce GuardingDynamicLinkerExporter
attila
parents:
diff changeset
    36
 * automatically loaded by other language runtimes. Language runtimes wishing
6c945c826f36 8139895: Introduce GuardingDynamicLinkerExporter
attila
parents:
diff changeset
    37
 * to export their own linkers should subclass this class and implement the
6c945c826f36 8139895: Introduce GuardingDynamicLinkerExporter
attila
parents:
diff changeset
    38
 * {@link #get()} method to return a list of exported linkers and declare the
6c945c826f36 8139895: Introduce GuardingDynamicLinkerExporter
attila
parents:
diff changeset
    39
 * subclass in
34447
ec4c069f9436 8141338: Move jdk.internal.dynalink package to jdk.dynalink
attila
parents: 33340
diff changeset
    40
 * {@code /META-INF/services/jdk.dynalink.linker.GuardingDynamicLinkerExporter}
33340
6c945c826f36 8139895: Introduce GuardingDynamicLinkerExporter
attila
parents:
diff changeset
    41
 * resource of their distribution (typically, JAR file) so that dynamic linker
6c945c826f36 8139895: Introduce GuardingDynamicLinkerExporter
attila
parents:
diff changeset
    42
 * factories can discover them using the {@link ServiceLoader} mechanism. Note
6c945c826f36 8139895: Introduce GuardingDynamicLinkerExporter
attila
parents:
diff changeset
    43
 * that instantiating this class is tied to a security check for the
6c945c826f36 8139895: Introduce GuardingDynamicLinkerExporter
attila
parents:
diff changeset
    44
 * {@code RuntimePermission("dynalink.exportLinkersAutomatically")} when a
6c945c826f36 8139895: Introduce GuardingDynamicLinkerExporter
attila
parents:
diff changeset
    45
 * security manager is present, to ensure that only trusted runtimes can
6c945c826f36 8139895: Introduce GuardingDynamicLinkerExporter
attila
parents:
diff changeset
    46
 * automatically export their linkers into other runtimes.
6c945c826f36 8139895: Introduce GuardingDynamicLinkerExporter
attila
parents:
diff changeset
    47
 * @see DynamicLinkerFactory#setClassLoader(ClassLoader)
6c945c826f36 8139895: Introduce GuardingDynamicLinkerExporter
attila
parents:
diff changeset
    48
 */
6c945c826f36 8139895: Introduce GuardingDynamicLinkerExporter
attila
parents:
diff changeset
    49
public abstract class GuardingDynamicLinkerExporter implements Supplier<List<GuardingDynamicLinker>> {
6c945c826f36 8139895: Introduce GuardingDynamicLinkerExporter
attila
parents:
diff changeset
    50
    /**
6c945c826f36 8139895: Introduce GuardingDynamicLinkerExporter
attila
parents:
diff changeset
    51
     * The name of the runtime permission for creating instances of this class.
6c945c826f36 8139895: Introduce GuardingDynamicLinkerExporter
attila
parents:
diff changeset
    52
     * Granting this permission to a language runtime allows it to export its
6c945c826f36 8139895: Introduce GuardingDynamicLinkerExporter
attila
parents:
diff changeset
    53
     * linkers for automatic loading into other language runtimes.
6c945c826f36 8139895: Introduce GuardingDynamicLinkerExporter
attila
parents:
diff changeset
    54
     */
6c945c826f36 8139895: Introduce GuardingDynamicLinkerExporter
attila
parents:
diff changeset
    55
    public static final String AUTOLOAD_PERMISSION_NAME = "dynalink.exportLinkersAutomatically";
6c945c826f36 8139895: Introduce GuardingDynamicLinkerExporter
attila
parents:
diff changeset
    56
6c945c826f36 8139895: Introduce GuardingDynamicLinkerExporter
attila
parents:
diff changeset
    57
    private static final Permission AUTOLOAD_PERMISSION = new RuntimePermission(AUTOLOAD_PERMISSION_NAME);
6c945c826f36 8139895: Introduce GuardingDynamicLinkerExporter
attila
parents:
diff changeset
    58
6c945c826f36 8139895: Introduce GuardingDynamicLinkerExporter
attila
parents:
diff changeset
    59
    /**
6c945c826f36 8139895: Introduce GuardingDynamicLinkerExporter
attila
parents:
diff changeset
    60
     * Creates a new linker exporter. If there is a security manager installed
6c945c826f36 8139895: Introduce GuardingDynamicLinkerExporter
attila
parents:
diff changeset
    61
     * checks for the
6c945c826f36 8139895: Introduce GuardingDynamicLinkerExporter
attila
parents:
diff changeset
    62
     * {@code RuntimePermission("dynalink.exportLinkersAutomatically")} runtime
6c945c826f36 8139895: Introduce GuardingDynamicLinkerExporter
attila
parents:
diff changeset
    63
     * permission. This ensures only language runtimes granted this permission
6c945c826f36 8139895: Introduce GuardingDynamicLinkerExporter
attila
parents:
diff changeset
    64
     * will be allowed to export their linkers for automatic loading.
6c945c826f36 8139895: Introduce GuardingDynamicLinkerExporter
attila
parents:
diff changeset
    65
     * @throws SecurityException if the necessary runtime permission is not
6c945c826f36 8139895: Introduce GuardingDynamicLinkerExporter
attila
parents:
diff changeset
    66
     * granted.
6c945c826f36 8139895: Introduce GuardingDynamicLinkerExporter
attila
parents:
diff changeset
    67
     */
6c945c826f36 8139895: Introduce GuardingDynamicLinkerExporter
attila
parents:
diff changeset
    68
    protected GuardingDynamicLinkerExporter() {
6c945c826f36 8139895: Introduce GuardingDynamicLinkerExporter
attila
parents:
diff changeset
    69
        final SecurityManager sm = System.getSecurityManager();
6c945c826f36 8139895: Introduce GuardingDynamicLinkerExporter
attila
parents:
diff changeset
    70
        if (sm != null) {
6c945c826f36 8139895: Introduce GuardingDynamicLinkerExporter
attila
parents:
diff changeset
    71
            sm.checkPermission(AUTOLOAD_PERMISSION);
6c945c826f36 8139895: Introduce GuardingDynamicLinkerExporter
attila
parents:
diff changeset
    72
        }
6c945c826f36 8139895: Introduce GuardingDynamicLinkerExporter
attila
parents:
diff changeset
    73
    }
6c945c826f36 8139895: Introduce GuardingDynamicLinkerExporter
attila
parents:
diff changeset
    74
}