Add C++11 header <cuchar>.
[official-gcc.git] / libjava / classpath / java / security / ProtectionDomain.java
blob7be5875dc72ef7291334fb56dfdd8d4214a03b88
1 /* ProtectionDomain.java -- A security domain
2 Copyright (C) 1998, 2003, 2004 Free Software Foundation, Inc.
4 This file is part of GNU Classpath.
6 GNU Classpath is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 2, or (at your option)
9 any later version.
11 GNU Classpath is distributed in the hope that it will be useful, but
12 WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with GNU Classpath; see the file COPYING. If not, write to the
18 Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
19 02110-1301 USA.
21 Linking this library statically or dynamically with other modules is
22 making a combined work based on this library. Thus, the terms and
23 conditions of the GNU General Public License cover the whole
24 combination.
26 As a special exception, the copyright holders of this library give you
27 permission to link this library with independent modules to produce an
28 executable, regardless of the license terms of these independent
29 modules, and to copy and distribute the resulting executable under
30 terms of your choice, provided that you also meet, for each linked
31 independent module, the terms and conditions of the license of that
32 module. An independent module is a module which is not derived from
33 or based on this library. If you modify this library, you may extend
34 this exception to your version of the library, but you are not
35 obligated to do so. If you do not wish to do so, delete this
36 exception statement from your version. */
38 package java.security;
40 import gnu.classpath.SystemProperties;
42 import gnu.java.lang.CPStringBuilder;
44 import java.util.Enumeration;
46 /**
47 * This class represents a group of classes, along with their granted
48 * permissions. The classes are identified by a {@link CodeSource}. Thus, any
49 * class loaded from the specified {@link CodeSource} is treated as part of
50 * this domain. The set of permissions is represented by an instance of
51 * {@link PermissionCollection}.
53 * <p>Every class in the system will belong to one and only one
54 * <code>ProtectionDomain</code>.</p>
56 * @author Aaron M. Renn (arenn@urbanophile.com)
57 * @version 0.0
59 public class ProtectionDomain
61 /** This is the <code>CodeSource</code> for this protection domain. */
62 private CodeSource code_source;
64 /** This is the set of permissions granted to this domain. */
65 private PermissionCollection perms;
67 /** The {@link ClassLoader} associated with this domain. */
68 private ClassLoader classloader;
70 /** The array of Principals associated with this domain.. */
71 private Principal[] principals;
73 /** Post 1.4 the policy may be refreshed! use false for pre 1.4. */
74 private boolean staticBinding;
76 /** True if this protection domain has all permissions */
77 private boolean hasAllPermissions;
79 /**
80 * Initializes a new instance of <code>ProtectionDomain</code> representing
81 * the specified {@link CodeSource} and set of permissions. No permissions
82 * can be added later to the {@link PermissionCollection} and this contructor
83 * will call the <code>setReadOnly</code> method on the specified set of
84 * permissions.
86 * @param codesource
87 * The {@link CodeSource} for this domain.
88 * @param permissions
89 * The set of permissions for this domain.
90 * @see PermissionCollection#setReadOnly()
92 public ProtectionDomain(CodeSource codesource, PermissionCollection permissions)
94 this(codesource, permissions, null, null, true);
97 /**
98 * This method initializes a new instance of <code>ProtectionDomain</code>
99 * given its {@link CodeSource}, granted permissions, associated
100 * {@link ClassLoader} and {@link Principal}s.
102 * <p>Similar to the previous constructor, if the designated set of
103 * permissions is not <code>null</code>, the <code>setReadOnly</code> method
104 * is called on that set.</p>
106 * @param codesource
107 * The {@link CodeSource} for this domain.
108 * @param permissions
109 * The permission set for this domain.
110 * @param classloader
111 * the ClassLoader associated with this domain.
112 * @param principals
113 * the array of {@link Principal}s associated with this domain.
114 * @since 1.4
115 * @see PermissionCollection#setReadOnly()
117 public ProtectionDomain(CodeSource codesource,
118 PermissionCollection permissions,
119 ClassLoader classloader, Principal[] principals)
121 this(codesource, permissions, classloader, principals, false);
124 private ProtectionDomain(CodeSource codesource,
125 PermissionCollection permissions,
126 ClassLoader classloader, Principal[] principals,
127 boolean staticBinding)
129 super();
131 code_source = codesource;
132 if (permissions != null)
134 perms = permissions;
135 perms.setReadOnly();
136 /* Check if this protection domain has all permissions */
137 Enumeration<Permission> e = permissions.elements();
138 while (e.hasMoreElements())
140 if (e.nextElement() instanceof AllPermission)
141 hasAllPermissions = true;
145 this.classloader = classloader;
146 this.principals =
147 (principals != null ? (Principal[]) principals.clone() : new Principal[0]);
148 this.staticBinding = staticBinding;
152 * Returns the {@link CodeSource} of this domain.
154 * @return the {@link CodeSource} of this domain.
155 * @since 1.2
157 public final CodeSource getCodeSource()
159 return code_source;
163 * Returns the {@link ClassLoader} of this domain.
165 * @return the {@link ClassLoader} of this domain.
166 * @since 1.4
168 public final ClassLoader getClassLoader()
170 return this.classloader;
174 * Returns a clone of the {@link Principal}s of this domain.
176 * @return a clone of the {@link Principal}s of this domain.
177 * @since 1.4
179 public final Principal[] getPrincipals()
181 return (Principal[]) principals.clone();
185 * Returns the {@link PermissionCollection} of this domain.
187 * @return The {@link PermissionCollection} of this domain.
189 public final PermissionCollection getPermissions()
191 return perms;
195 * Tests whether or not the specified {@link Permission} is implied by the
196 * set of permissions granted to this domain.
198 * @param permission
199 * the {@link Permission} to test.
200 * @return <code>true</code> if the specified {@link Permission} is implied
201 * for this domain, <code>false</code> otherwise.
203 public boolean implies(Permission permission)
205 if (hasAllPermissions)
206 return true;
207 if (staticBinding)
208 return (perms == null ? false : perms.implies(permission));
209 // Else dynamically bound. Do we have it?
210 // NOTE: this will force loading of Policy.currentPolicy
211 return Policy.getCurrentPolicy().implies(this, permission);
215 * Returns a string representation of this object. It will include the
216 * {@link CodeSource} and set of permissions associated with this domain.
218 * @return A string representation of this object.
220 public String toString()
222 String linesep = SystemProperties.getProperty("line.separator");
223 CPStringBuilder sb = new CPStringBuilder("ProtectionDomain (").append(linesep);
225 if (code_source == null)
226 sb.append("CodeSource:null");
227 else
228 sb.append(code_source);
230 sb.append(linesep);
231 if (classloader == null)
232 sb.append("ClassLoader:null");
233 else
234 sb.append(classloader);
236 sb.append(linesep);
237 sb.append("Principals:");
238 if (principals != null && principals.length > 0)
240 sb.append("[");
241 Principal pal;
242 for (int i = 0; i < principals.length; i++)
244 pal = principals[i];
245 sb.append("'").append(pal.getName())
246 .append("' of type ").append(pal.getClass().getName());
247 if (i < principals.length-1)
248 sb.append(", ");
250 sb.append("]");
252 else
253 sb.append("none");
255 sb.append(linesep);
256 if (!staticBinding) // include all but dont force loading Policy.currentPolicy
257 if (Policy.isLoaded())
260 sb.append(Policy.getPolicy().getPermissions(this));
262 catch (SecurityException e)
264 // We are not allowed access to the policy.
265 sb.append(perms);
267 else // fallback on this one's permissions
268 sb.append(perms);
269 else
270 sb.append(perms);
272 return sb.append(linesep).append(")").append(linesep).toString();