Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
PojavLauncherTeam
GitHub Repository: PojavLauncherTeam/mobile
Path: blob/master/src/jdk.dynalink/share/classes/jdk/dynalink/internal/AccessControlContextFactory.java
41161 views
1
/*
2
* Copyright (c) 2015, 2021, Oracle and/or its affiliates. All rights reserved.
3
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4
*
5
* This code is free software; you can redistribute it and/or modify it
6
* under the terms of the GNU General Public License version 2 only, as
7
* published by the Free Software Foundation. Oracle designates this
8
* particular file as subject to the "Classpath" exception as provided
9
* by Oracle in the LICENSE file that accompanied this code.
10
*
11
* This code is distributed in the hope that it will be useful, but WITHOUT
12
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
14
* version 2 for more details (a copy is included in the LICENSE file that
15
* accompanied this code).
16
*
17
* You should have received a copy of the GNU General Public License version
18
* 2 along with this work; if not, write to the Free Software Foundation,
19
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
20
*
21
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
22
* or visit www.oracle.com if you need additional information or have any
23
* questions.
24
*/
25
26
package jdk.dynalink.internal;
27
28
import java.security.AccessControlContext;
29
import java.security.Permission;
30
import java.security.Permissions;
31
import java.security.ProtectionDomain;
32
import java.util.stream.Stream;
33
34
/**
35
* Utility class for creating permission-restricting {@link AccessControlContext}s.
36
*/
37
public final class AccessControlContextFactory {
38
private AccessControlContextFactory () {
39
}
40
41
/**
42
* Creates an access control context with no permissions.
43
* @return an access control context with no permissions.
44
*/
45
@SuppressWarnings("removal")
46
public static AccessControlContext createAccessControlContext() {
47
return createAccessControlContext(new Permission[0]);
48
}
49
50
/**
51
* Creates an access control context limited to only the specified permissions.
52
* @param permissions the permissions for the newly created access control context.
53
* @return a new access control context limited to only the specified permissions.
54
*/
55
@SuppressWarnings("removal")
56
public static AccessControlContext createAccessControlContext(final Permission... permissions) {
57
final Permissions perms = new Permissions();
58
for(final Permission permission: permissions) {
59
perms.add(permission);
60
}
61
return new AccessControlContext(new ProtectionDomain[] { new ProtectionDomain(null, perms) });
62
}
63
64
/**
65
* Creates an access control context limited to only the {@link RuntimePermission}s
66
* of the given names.
67
* @param runtimePermissionNames the names of runtime permissions for the
68
* newly created access control context.
69
* @return a new access control context limited to only the runtime
70
* permissions with the specified names.
71
*/
72
@SuppressWarnings("removal")
73
public static AccessControlContext createAccessControlContext(final String... runtimePermissionNames) {
74
return createAccessControlContext(makeRuntimePermissions(runtimePermissionNames));
75
}
76
77
private static Permission[] makeRuntimePermissions(final String... runtimePermissionNames) {
78
return Stream.of(runtimePermissionNames).map(RuntimePermission::new).toArray(Permission[]::new);
79
}
80
}
81
82