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)
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
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
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
;
43 * This class represents a group of classes, along with their granted
44 * permissions. The classes are identified by a {@link CodeSource}. Thus, any
45 * class loaded from the specified {@link CodeSource} is treated as part of
46 * this domain. The set of permissions is represented by an instance of
47 * {@link PermissionCollection}.
49 * <p>Every class in the system will belong to one and only one
50 * <code>ProtectionDomain</code>.</p>
52 * @author Aaron M. Renn (arenn@urbanophile.com)
55 public class ProtectionDomain
57 /** This is the <code>CodeSource</code> for this protection domain. */
58 private CodeSource code_source
;
60 /** This is the set of permissions granted to this domain. */
61 private PermissionCollection perms
;
63 /** The {@link ClassLoader} associated with this domain. */
64 private ClassLoader classloader
;
66 /** The array of Principals associated with this domain.. */
67 private Principal
[] principals
;
69 /** Post 1.4 the policy may be refreshed! use false for pre 1.4. */
70 private boolean staticBinding
;
73 * Initializes a new instance of <code>ProtectionDomain</code> representing
74 * the specified {@link CodeSource} and set of permissions. No permissions
75 * can be added later to the {@link PermissionCollection} and this contructor
76 * will call the <code>setReadOnly</code> method on the specified set of
80 * The {@link CodeSource} for this domain.
82 * The set of permissions for this domain.
83 * @see PermissionCollection#setReadOnly()
85 public ProtectionDomain(CodeSource codesource
, PermissionCollection permissions
)
87 this(codesource
, permissions
, null, null, true);
91 * This method initializes a new instance of <code>ProtectionDomain</code>
92 * given its {@link CodeSource}, granted permissions, associated
93 * {@link ClassLoader} and {@link Principal}s.
95 * <p>Similar to the previous constructor, if the designated set of
96 * permissions is not <code>null</code>, the <code>setReadOnly</code> method
97 * is called on that set.</p>
100 * The {@link CodeSource} for this domain.
102 * The permission set for this domain.
104 * the ClassLoader associated with this domain.
106 * the array of {@link Principal}s associated with this domain.
108 * @see PermissionCollection#setReadOnly()
110 public ProtectionDomain(CodeSource codesource
,
111 PermissionCollection permissions
,
112 ClassLoader classloader
, Principal
[] principals
)
114 this(codesource
, permissions
, classloader
, principals
, false);
117 private ProtectionDomain(CodeSource codesource
,
118 PermissionCollection permissions
,
119 ClassLoader classloader
, Principal
[] principals
,
120 boolean staticBinding
)
124 code_source
= codesource
;
125 if (permissions
!= null)
131 this.classloader
= classloader
;
133 (principals
!= null ?
(Principal
[]) principals
.clone() : new Principal
[0]);
134 this.staticBinding
= staticBinding
;
138 * Returns the {@link CodeSource} of this domain.
140 * @return the {@link CodeSource} of this domain.
143 public final CodeSource
getCodeSource()
149 * Returns the {@link ClassLoader} of this domain.
151 * @return the {@link ClassLoader} of this domain.
154 public final ClassLoader
getClassLoader()
156 return this.classloader
;
160 * Returns a clone of the {@link Principal}s of this domain.
162 * @return a clone of the {@link Principal}s of this domain.
165 public final Principal
[] getPrincipals()
167 return (Principal
[]) principals
.clone();
171 * Returns the {@link PermissionCollection} of this domain.
173 * @return The {@link PermissionCollection} of this domain.
175 public final PermissionCollection
getPermissions()
181 * Tests whether or not the specified {@link Permission} is implied by the
182 * set of permissions granted to this domain.
185 * the {@link Permission} to test.
186 * @return <code>true</code> if the specified {@link Permission} is implied
187 * for this domain, <code>false</code> otherwise.
189 public boolean implies(Permission permission
)
192 return (perms
== null ?
false : perms
.implies(permission
));
193 // Else dynamically bound. Do we have it?
194 // NOTE: this will force loading of Policy.currentPolicy
195 return Policy
.getCurrentPolicy().implies(this, permission
);
199 * Returns a string representation of this object. It will include the
200 * {@link CodeSource} and set of permissions associated with this domain.
202 * @return A string representation of this object.
204 public String
toString()
206 String linesep
= SystemProperties
.getProperty("line.separator");
207 StringBuffer sb
= new StringBuffer("ProtectionDomain (").append(linesep
);
209 if (code_source
== null)
210 sb
.append("CodeSource:null");
212 sb
.append(code_source
);
215 if (classloader
== null)
216 sb
.append("ClassLoader:null");
218 sb
.append(classloader
);
221 sb
.append("Principals:");
222 if (principals
!= null && principals
.length
> 0)
226 for (int i
= 0; i
< principals
.length
; i
++)
229 sb
.append("'").append(pal
.getName())
230 .append("' of type ").append(pal
.getClass().getName());
231 if (i
< principals
.length
-1)
240 if (!staticBinding
) // include all but dont force loading Policy.currentPolicy
241 if (Policy
.isLoaded())
242 sb
.append(Policy
.getCurrentPolicy().getPermissions(this));
243 else // fallback on this one's permissions
248 return sb
.append(linesep
).append(")").append(linesep
).toString();