61 |
/** This is the set of permissions granted to this domain. */ |
/** This is the set of permissions granted to this domain. */ |
62 |
private PermissionCollection perms; |
private PermissionCollection perms; |
63 |
|
|
64 |
|
/** The {@link ClassLoader} associated with this domain. */ |
65 |
|
private ClassLoader classloader; |
66 |
|
|
67 |
|
/** The array of Principals associated with this domain.. */ |
68 |
|
private Principal[] principals; |
69 |
|
|
70 |
|
/** Post 1.4 the policy may be refreshed! use false for pre 1.4. */ |
71 |
|
private boolean staticBinding; |
72 |
|
|
73 |
/** |
/** |
74 |
* Creates a new <code>ProtectionDomain</code> with the given {@link |
* Creates a new <code>ProtectionDomain</code> with the given {@link |
75 |
* CodeSource} and {@link Permissions}. If the permissions object is not |
* CodeSource} and {@link Permissions}. If the permissions object is not |
83 |
*/ |
*/ |
84 |
public ProtectionDomain(CodeSource codesource, PermissionCollection permissions) |
public ProtectionDomain(CodeSource codesource, PermissionCollection permissions) |
85 |
{ |
{ |
86 |
this.code_source = codesource; |
this(codesource, permissions, null, null, false); |
87 |
this.perms = permissions; |
} |
88 |
|
|
89 |
|
/** |
90 |
|
* <p>Creates a new ProtectionDomain qualified by the given CodeSource, |
91 |
|
* Permissions, ClassLoader and array of Principals. If the permissions |
92 |
|
* object is not null, then <code>setReadOnly()</code> will be called on the |
93 |
|
* passed in Permissions object. The permissions granted to this domain are |
94 |
|
* dynamic; they include both the static permissions passed to this |
95 |
|
* constructor, and any permissions granted to this domain by the current |
96 |
|
* Policy at the time a permission is checked.</p> |
97 |
|
* |
98 |
|
* <p>This constructor is typically used by {@link ClassLoader}s and {@link |
99 |
|
* DomainCombiner}s which delegate to <code>Policy</code> to actively |
100 |
|
* associate the permissions granted to this domain. This constructor affords |
101 |
|
* the Policy provider the opportunity to augment the supplied |
102 |
|
* PermissionCollection to reflect policy changes.</p> |
103 |
|
* |
104 |
|
* @param codesource the CodeSource associated with this domain. |
105 |
|
* @param permissions the permissions granted to this domain. |
106 |
|
* @param classloader the ClassLoader associated with this domain. |
107 |
|
* @param principals the array of Principals associated with this domain. |
108 |
|
* @since 1.4 |
109 |
|
* @see Policy#refresh() |
110 |
|
* @see Policy#getPermissions(ProtectionDomain) |
111 |
|
*/ |
112 |
|
public ProtectionDomain(CodeSource codesource, |
113 |
|
PermissionCollection permissions, |
114 |
|
ClassLoader classloader, Principal[] principals) |
115 |
|
{ |
116 |
|
this(codesource, permissions, classloader, principals, false); |
117 |
|
} |
118 |
|
|
119 |
|
private ProtectionDomain(CodeSource codesource, |
120 |
|
PermissionCollection permissions, |
121 |
|
ClassLoader classloader, Principal[] principals, |
122 |
|
boolean staticBinding) |
123 |
|
{ |
124 |
|
super(); |
125 |
|
|
126 |
|
code_source = codesource; |
127 |
if (permissions != null) |
if (permissions != null) |
128 |
permissions.setReadOnly(); |
{ |
129 |
|
perms = permissions; |
130 |
|
perms.setReadOnly(); |
131 |
|
} |
132 |
|
|
133 |
|
this.classloader = classloader; |
134 |
|
this.principals = |
135 |
|
(principals != null ? (Principal[]) principals.clone() : new Principal[0]); |
136 |
|
this.staticBinding = staticBinding; |
137 |
} |
} |
138 |
|
|
139 |
/** |
/** |
148 |
} |
} |
149 |
|
|
150 |
/** |
/** |
151 |
|
* Returns the {@link ClassLoader} of this domain. |
152 |
|
* |
153 |
|
* @return the {@link ClassLoader} of this domain which may be |
154 |
|
* <code>null</code>. |
155 |
|
* @since 1.4 |
156 |
|
*/ |
157 |
|
public final ClassLoader getClassLoader() |
158 |
|
{ |
159 |
|
return this.classloader; |
160 |
|
} |
161 |
|
|
162 |
|
/** |
163 |
|
* Returns an array of principals for this domain. |
164 |
|
* |
165 |
|
* @return returns a non-null array of principals for this domain. Changes to |
166 |
|
* this array will have no impact on the <code>ProtectionDomain</code>. |
167 |
|
* @since 1.4 |
168 |
|
*/ |
169 |
|
public final Principal[] getPrincipals() |
170 |
|
{ |
171 |
|
return (Principal[]) principals.clone(); |
172 |
|
} |
173 |
|
|
174 |
|
/** |
175 |
* Returns the static permissions granted to this domain. |
* Returns the static permissions granted to this domain. |
176 |
* |
* |
177 |
* @return the static set of permissions for this domain which may be |
* @return the static set of permissions for this domain which may be |
208 |
*/ |
*/ |
209 |
public boolean implies(Permission permission) |
public boolean implies(Permission permission) |
210 |
{ |
{ |
211 |
PermissionCollection pc = getPermissions(); |
if (staticBinding) |
212 |
if (pc == null) |
return (perms == null ? false : perms.implies(permission)); |
213 |
return (false); |
// else dynamically bound. do we have it? |
214 |
|
// NOTE: this will force loading of Policy.currentPolicy |
215 |
return pc.implies(permission); |
return Policy.getCurrentPolicy().implies(this, permission); |
216 |
} |
} |
217 |
|
|
218 |
/** |
/** |
223 |
public String toString() |
public String toString() |
224 |
{ |
{ |
225 |
String linesep = System.getProperty("line.separator"); |
String linesep = System.getProperty("line.separator"); |
226 |
StringBuffer sb = new StringBuffer(""); |
StringBuffer sb = new StringBuffer("ProtectionDomain (").append(linesep); |
227 |
sb.append("ProtectionDomain (" + linesep); |
|
228 |
if (code_source == null) |
if (code_source == null) |
229 |
sb.append("CodeSource:null" + linesep); |
sb.append("CodeSource:null"); |
230 |
|
else |
231 |
|
sb.append(code_source); |
232 |
|
|
233 |
|
sb.append(linesep); |
234 |
|
if (classloader == null) |
235 |
|
sb.append("ClassLoader:null"); |
236 |
|
else |
237 |
|
sb.append(classloader); |
238 |
|
|
239 |
|
sb.append(linesep); |
240 |
|
sb.append("Principals:"); |
241 |
|
if (principals != null && principals.length > 0) |
242 |
|
{ |
243 |
|
sb.append("["); |
244 |
|
Principal pal; |
245 |
|
for (int i = 0; i < principals.length; i++) |
246 |
|
{ |
247 |
|
pal = principals[i]; |
248 |
|
sb.append("'").append(pal.getName()) |
249 |
|
.append("' of type ").append(pal.getClass().getName()); |
250 |
|
if (i < principals.length-1) |
251 |
|
sb.append(", "); |
252 |
|
} |
253 |
|
sb.append("]"); |
254 |
|
} |
255 |
|
else |
256 |
|
sb.append("none"); |
257 |
|
|
258 |
|
sb.append(linesep); |
259 |
|
if (!staticBinding) // include all but dont force loading Policy.currentPolicy |
260 |
|
if (Policy.isLoaded()) |
261 |
|
sb.append(Policy.getCurrentPolicy().getPermissions(this)); |
262 |
|
else // fallback on this one's permissions |
263 |
|
sb.append(perms); |
264 |
else |
else |
265 |
sb.append(code_source + linesep); |
sb.append(perms); |
|
sb.append(perms); |
|
|
sb.append(linesep + ")" + linesep); |
|
266 |
|
|
267 |
return sb.toString(); |
return sb.append(linesep).append(")").append(linesep).toString(); |
268 |
} |
} |
269 |
} |
} |