2 * Licensed to the Apache Software Foundation (ASF) under one or more
3 * contributor license agreements. See the NOTICE file distributed with
4 * this work for additional information regarding copyright ownership.
5 * The ASF licenses this file to You under the Apache License, Version 2.0
6 * (the "License"); you may not use this file except in compliance with
7 * the License. You may obtain a copy of the License at
9 * http://www.apache.org/licenses/LICENSE-2.0
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
18 package java.security;
21 * {@code ProtectionDomain} represents all permissions that are granted to a
22 * specific code source. The {@link ClassLoader} associates each class with the
23 * corresponding {@code ProtectionDomain}, depending on the location and the
24 * certificates (encapsulates in {@link CodeSource}) it loads the code from.
26 * A class belongs to exactly one protection domain and the protection domain
27 * can not be changed during the lifetime of the class.
29 public class ProtectionDomain {
31 // CodeSource for this ProtectionDomain
32 private CodeSource codeSource;
34 // Static permissions for this ProtectionDomain
35 private PermissionCollection permissions;
38 private ClassLoader classLoader;
40 // Set of principals associated with this ProtectionDomain
41 private Principal[] principals;
43 // false if this ProtectionDomain was constructed with static
44 // permissions, true otherwise.
45 private boolean dynamicPerms;
48 * Constructs a new instance of {@code ProtectionDomain} with the specified
49 * code source and the specified static permissions.
51 * If {@code permissions} is not {@code null}, the {@code permissions}
52 * collection is made immutable by calling
53 * {@link PermissionCollection#setReadOnly()} and it is considered as
54 * granted statically to this {@code ProtectionDomain}.
56 * The policy will not be consulted by access checks against this {@code
59 * If {@code permissions} is {@code null}, the method {@link
60 * ProtectionDomain#implies(Permission)} always returns {@code false}.
63 * the code source associated with this domain, maybe {@code
66 * the {@code PermissionCollection} containing all permissions to
67 * be statically granted to this {@code ProtectionDomain}, maybe
70 public ProtectionDomain(CodeSource cs, PermissionCollection permissions) {
72 if (permissions != null) {
73 permissions.setReadOnly();
75 this.permissions = permissions;
76 //this.classLoader = null;
77 //this.principals = null;
78 //dynamicPerms = false;
82 * Constructs a new instance of {@code ProtectionDomain} with the specified
83 * code source, the permissions, the class loader and the principals.
85 * If {@code permissions} is {@code null}, and access checks are performed
86 * against this protection domain, the permissions defined by the policy are
87 * consulted. If {@code permissions} is not {@code null}, the {@code
88 * permissions} collection is made immutable by calling
89 * {@link PermissionCollection#setReadOnly()}. If access checks are
90 * performed, the policy and the provided permission collection are checked.
92 * External modifications of the provided {@code principals} array has no
93 * impact on this {@code ProtectionDomain}.
96 * the code source associated with this domain, maybe {@code
99 * the permissions associated with this domain, maybe {@code
102 * the class loader associated with this domain, maybe {@code
105 * the principals associated with this domain, maybe {@code
108 public ProtectionDomain(CodeSource cs, PermissionCollection permissions,
109 ClassLoader cl, Principal[] principals) {
110 this.codeSource = cs;
111 if (permissions != null) {
112 permissions.setReadOnly();
114 this.permissions = permissions;
115 this.classLoader = cl;
116 if (principals != null) {
117 this.principals = new Principal[principals.length];
118 System.arraycopy(principals, 0, this.principals, 0,
119 this.principals.length);
125 * Returns the {@code ClassLoader} associated with this {@code
128 * @return the {@code ClassLoader} associated with this {@code
129 * ProtectionDomain}, maybe {@code null}.
131 public final ClassLoader getClassLoader() {
136 * Returns the {@code CodeSource} of this {@code ProtectionDomain}.
138 * @return the {@code CodeSource} of this {@code ProtectionDomain}, maybe
141 public final CodeSource getCodeSource() {
146 * Returns the static permissions that are granted to this {@code
149 * @return the static permissions that are granted to this {@code
150 * ProtectionDomain}, maybe {@code null}.
152 public final PermissionCollection getPermissions() {
157 * Returns the principals associated with this {@code ProtectionDomain}.
158 * Modifications of the returned {@code Principal} array has no impact on
159 * this {@code ProtectionDomain}.
161 * @return the principals associated with this {@code ProtectionDomain}.
163 public final Principal[] getPrincipals() {
164 if( principals == null ) {
165 return new Principal[0];
167 Principal[] tmp = new Principal[principals.length];
168 System.arraycopy(principals, 0, tmp, 0, tmp.length);
173 * Indicates whether the specified permission is implied by this {@code
176 * If this {@code ProtectionDomain} was constructed with
177 * {@link #ProtectionDomain(CodeSource, PermissionCollection)}, the
178 * specified permission is only checked against the permission collection
179 * provided in the constructor. If {@code null} was provided, {@code false}
182 * If this {@code ProtectionDomain} was constructed with
183 * {@link #ProtectionDomain(CodeSource, PermissionCollection, ClassLoader, Principal[])}
184 * , the specified permission is checked against the policy and the
185 * permission collection provided in the constructor.
188 * the permission to check against the domain.
189 * @return {@code true} if the specified {@code permission} is implied by
190 * this {@code ProtectionDomain}, {@code false} otherwise.
192 public boolean implies(Permission permission) {
193 // First, test with the Policy, as the default Policy.implies()
194 // checks for both dynamic and static collections of the
195 // ProtectionDomain passed...
197 && Policy.getAccessiblePolicy().implies(this, permission)) {
201 // ... and we get here if
202 // either the permissions are static
203 // or Policy.implies() did not check for static permissions
204 // or the permission is not implied
205 return permissions == null ? false : permissions.implies(permission);
209 * Returns a string containing a concise, human-readable description of the
210 * this {@code ProtectionDomain}.
212 * @return a printable representation for this {@code ProtectionDomain}.
215 public String toString() {
216 StringBuilder buf = new StringBuilder(200);
217 buf.append("ProtectionDomain\n");
218 buf.append("CodeSource=").append(
219 codeSource == null ? "<null>" : codeSource.toString()).append(
221 buf.append("ClassLoader=").append(
222 classLoader == null ? "<null>" : classLoader.toString())
224 if (principals == null || principals.length == 0) {
225 buf.append("<no principals>\n");
227 buf.append("Principals: <\n");
228 for (int i = 0; i < principals.length; i++) {
229 buf.append("\t").append(
230 principals[i] == null ? "<null>" : principals[i]
231 .toString()).append("\n");
237 buf.append("Permissions:\n");
238 if (permissions == null) {
239 buf.append("\t\t<no static permissions>\n");
241 buf.append("\t\tstatic: ").append(permissions.toString()).append(
246 if (Policy.isSet()) {
247 PermissionCollection perms;
248 perms = Policy.getAccessiblePolicy().getPermissions(this);
250 buf.append("\t\t<no dynamic permissions>\n");
252 buf.append("\t\tdynamic: ").append(perms.toString())
256 buf.append("\t\t<no dynamic permissions>\n");
259 return buf.toString();