Path: blob/master/src/jdk.dynalink/share/classes/jdk/dynalink/linker/GuardingDynamicLinkerExporter.java
41161 views
/*1* Copyright (c) 2015, 2021, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation. Oracle designates this7* particular file as subject to the "Classpath" exception as provided8* by Oracle in the LICENSE file that accompanied this code.9*10* This code is distributed in the hope that it will be useful, but WITHOUT11* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or12* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License13* version 2 for more details (a copy is included in the LICENSE file that14* accompanied this code).15*16* You should have received a copy of the GNU General Public License version17* 2 along with this work; if not, write to the Free Software Foundation,18* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.19*20* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA21* or visit www.oracle.com if you need additional information or have any22* questions.23*/2425package jdk.dynalink.linker;2627import java.security.Permission;28import java.util.List;29import java.util.ServiceLoader;30import java.util.function.Supplier;31import jdk.dynalink.DynamicLinkerFactory;3233/**34* A class acting as a supplier of guarding dynamic linkers that can be35* automatically loaded by other language runtimes. Language runtimes wishing36* to export their own linkers should subclass this class and implement the37* {@link #get()} method to return a list of exported linkers and declare the38* subclass in39* {@code /META-INF/services/jdk.dynalink.linker.GuardingDynamicLinkerExporter}40* resource of their distribution (typically, JAR file) so that dynamic linker41* factories can discover them using the {@link ServiceLoader} mechanism. Note42* that instantiating this class is tied to a security check for the43* {@code RuntimePermission("dynalink.exportLinkersAutomatically")} when a44* security manager is present, to ensure that only trusted runtimes can45* automatically export their linkers into other runtimes.46* @see DynamicLinkerFactory#setClassLoader(ClassLoader)47*/48public abstract class GuardingDynamicLinkerExporter implements Supplier<List<GuardingDynamicLinker>> {49/**50* The name of the runtime permission for creating instances of this class.51* Granting this permission to a language runtime allows it to export its52* linkers for automatic loading into other language runtimes.53*/54public static final String AUTOLOAD_PERMISSION_NAME = "dynalink.exportLinkersAutomatically";5556private static final Permission AUTOLOAD_PERMISSION = new RuntimePermission(AUTOLOAD_PERMISSION_NAME);5758/**59* Creates a new linker exporter. If there is a security manager installed60* checks for the61* {@code RuntimePermission("dynalink.exportLinkersAutomatically")} runtime62* permission. This ensures only language runtimes granted this permission63* will be allowed to export their linkers for automatic loading.64* @throws SecurityException if the necessary runtime permission is not65* granted.66*/67protected GuardingDynamicLinkerExporter() {68@SuppressWarnings("removal")69final SecurityManager sm = System.getSecurityManager();70if (sm != null) {71sm.checkPermission(AUTOLOAD_PERMISSION);72}73}74}757677