Path: blob/master/src/jdk.dynalink/share/classes/jdk/dynalink/internal/AccessControlContextFactory.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.internal;2627import java.security.AccessControlContext;28import java.security.Permission;29import java.security.Permissions;30import java.security.ProtectionDomain;31import java.util.stream.Stream;3233/**34* Utility class for creating permission-restricting {@link AccessControlContext}s.35*/36public final class AccessControlContextFactory {37private AccessControlContextFactory () {38}3940/**41* Creates an access control context with no permissions.42* @return an access control context with no permissions.43*/44@SuppressWarnings("removal")45public static AccessControlContext createAccessControlContext() {46return createAccessControlContext(new Permission[0]);47}4849/**50* Creates an access control context limited to only the specified permissions.51* @param permissions the permissions for the newly created access control context.52* @return a new access control context limited to only the specified permissions.53*/54@SuppressWarnings("removal")55public static AccessControlContext createAccessControlContext(final Permission... permissions) {56final Permissions perms = new Permissions();57for(final Permission permission: permissions) {58perms.add(permission);59}60return new AccessControlContext(new ProtectionDomain[] { new ProtectionDomain(null, perms) });61}6263/**64* Creates an access control context limited to only the {@link RuntimePermission}s65* of the given names.66* @param runtimePermissionNames the names of runtime permissions for the67* newly created access control context.68* @return a new access control context limited to only the runtime69* permissions with the specified names.70*/71@SuppressWarnings("removal")72public static AccessControlContext createAccessControlContext(final String... runtimePermissionNames) {73return createAccessControlContext(makeRuntimePermissions(runtimePermissionNames));74}7576private static Permission[] makeRuntimePermissions(final String... runtimePermissionNames) {77return Stream.of(runtimePermissionNames).map(RuntimePermission::new).toArray(Permission[]::new);78}79}808182