OSDN Git Service

original
[gb-231r1-is01/Gingerbread_2.3.3_r1_IS01.git] / libcore / luni / src / main / java / java / security / ProtectionDomain.java
1 /*
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
8  *
9  *     http://www.apache.org/licenses/LICENSE-2.0
10  *
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.
16  */
17
18 package java.security;
19
20 /**
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.
25  * <p>
26  * A class belongs to exactly one protection domain and the protection domain
27  * can not be changed during the lifetime of the class.
28  */
29 public class ProtectionDomain {
30
31     // CodeSource for this ProtectionDomain
32     private CodeSource codeSource;
33
34     // Static permissions for this ProtectionDomain
35     private PermissionCollection permissions;
36
37     // ClassLoader
38     private ClassLoader classLoader;
39
40     // Set of principals associated with this ProtectionDomain
41     private Principal[] principals;
42
43     // false if this ProtectionDomain was constructed with static
44     // permissions, true otherwise.
45     private boolean dynamicPerms;
46
47     /**
48      * Constructs a new instance of {@code ProtectionDomain} with the specified
49      * code source and the specified static permissions.
50      * <p>
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}.
55      * <p>
56      * The policy will not be consulted by access checks against this {@code
57      * ProtectionDomain}.
58      * <p>
59      * If {@code permissions} is {@code null}, the method {@link
60      * ProtectionDomain#implies(Permission)} always returns {@code false}.
61      *
62      * @param cs
63      *            the code source associated with this domain, maybe {@code
64      *            null}.
65      * @param permissions
66      *            the {@code PermissionCollection} containing all permissions to
67      *            be statically granted to this {@code ProtectionDomain}, maybe
68      *            {@code null}.
69      */
70     public ProtectionDomain(CodeSource cs, PermissionCollection permissions) {
71         this.codeSource = cs;
72         if (permissions != null) {
73             permissions.setReadOnly();
74         }
75         this.permissions = permissions;
76         //this.classLoader = null;
77         //this.principals = null;
78         //dynamicPerms = false;
79     }
80
81     /**
82      * Constructs a new instance of {@code ProtectionDomain} with the specified
83      * code source, the permissions, the class loader and the principals.
84      * <p>
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.
91      * <p>
92      * External modifications of the provided {@code principals} array has no
93      * impact on this {@code ProtectionDomain}.
94      *
95      * @param cs
96      *            the code source associated with this domain, maybe {@code
97      *            null}.
98      * @param permissions
99      *            the permissions associated with this domain, maybe {@code
100      *            null}.
101      * @param cl
102      *            the class loader associated with this domain, maybe {@code
103      *            null}.
104      * @param principals
105      *            the principals associated with this domain, maybe {@code
106      *            null}.
107      */
108     public ProtectionDomain(CodeSource cs, PermissionCollection permissions,
109             ClassLoader cl, Principal[] principals) {
110         this.codeSource = cs;
111         if (permissions != null) {
112             permissions.setReadOnly();
113         }
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);
120         }
121         dynamicPerms = true;
122     }
123
124     /**
125      * Returns the {@code ClassLoader} associated with this {@code
126      * ProtectionDomain}.
127      *
128      * @return the {@code ClassLoader} associated with this {@code
129      *         ProtectionDomain}, maybe {@code null}.
130      */
131     public final ClassLoader getClassLoader() {
132         return classLoader;
133     }
134
135     /**
136      * Returns the {@code CodeSource} of this {@code ProtectionDomain}.
137      *
138      * @return the {@code CodeSource} of this {@code ProtectionDomain}, maybe
139      *         {@code null}.
140      */
141     public final CodeSource getCodeSource() {
142         return codeSource;
143     }
144
145     /**
146      * Returns the static permissions that are granted to this {@code
147      * ProtectionDomain}.
148      *
149      * @return the static permissions that are granted to this {@code
150      *         ProtectionDomain}, maybe {@code null}.
151      */
152     public final PermissionCollection getPermissions() {
153         return permissions;
154     }
155
156     /**
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}.
160      *
161      * @return the principals associated with this {@code ProtectionDomain}.
162      */
163     public final Principal[] getPrincipals() {
164         if( principals == null ) {
165             return new Principal[0];
166         }
167         Principal[] tmp = new Principal[principals.length];
168         System.arraycopy(principals, 0, tmp, 0, tmp.length);
169         return tmp;
170     }
171
172     /**
173      * Indicates whether the specified permission is implied by this {@code
174      * ProtectionDomain}.
175      * <p>
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}
180      * is returned.
181      * <p>
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.
186      *
187      * @param permission
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.
191      */
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...
196         if (dynamicPerms
197                 && Policy.getAccessiblePolicy().implies(this, permission)) {
198             return true;
199         }
200
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);
206     }
207
208     /**
209      * Returns a string containing a concise, human-readable description of the
210      * this {@code ProtectionDomain}.
211      *
212      * @return a printable representation for this {@code ProtectionDomain}.
213      */
214     @Override
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(
220                 "\n");
221         buf.append("ClassLoader=").append(
222                 classLoader == null ? "<null>" : classLoader.toString())
223                 .append("\n");
224         if (principals == null || principals.length == 0) {
225             buf.append("<no principals>\n");
226         } else {
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");
232             }
233             buf.append(">");
234         }
235
236         //permissions here
237         buf.append("Permissions:\n");
238         if (permissions == null) {
239             buf.append("\t\t<no static permissions>\n");
240         } else {
241             buf.append("\t\tstatic: ").append(permissions.toString()).append(
242                     "\n");
243         }
244
245         if (dynamicPerms) {
246             if (Policy.isSet()) {
247                 PermissionCollection perms;
248                 perms = Policy.getAccessiblePolicy().getPermissions(this);
249                 if (perms == null) {
250                     buf.append("\t\t<no dynamic permissions>\n");
251                 } else {
252                     buf.append("\t\tdynamic: ").append(perms.toString())
253                             .append("\n");
254                 }
255             } else {
256                 buf.append("\t\t<no dynamic permissions>\n");
257             }
258         }
259         return buf.toString();
260     }
261 }