1 /* PrivateCredentialPermission.java -- permissions governing private credentials.
2 Copyright (C) 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., 59 Temple Place, Suite 330, 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. */
39 package javax
.security
.auth
;
41 import java
.io
.Serializable
;
43 import java
.security
.Permission
;
44 import java
.security
.PermissionCollection
;
46 import java
.util
.HashSet
;
47 import java
.util
.Iterator
;
49 import java
.util
.StringTokenizer
;
52 * A permission governing access to a private credential. The action of this
53 * permission is always "read" -- meaning that the private credential
54 * information can be read from an object.
56 * <p>The target of this permission is formatted as follows:</p>
58 * <p><code>CredentialClassName ( PrinicpalClassName PrincipalName )*</code></p>
60 * <p><i>CredentialClassName</i> is either the name of a private credential
61 * class name, or a wildcard character (<code>'*'</code>).
62 * <i>PrinicpalClassName</i> is the class name of a principal object, and
63 * <i>PrincipalName</i> is a string representing the principal, or the
64 * wildcard character.</p>
66 public final class PrivateCredentialPermission
extends Permission
67 implements Serializable
70 * For compatability with Sun's JDK 1.4.2 rev. 5
72 private static final long serialVersionUID
= 5284372143517237068L;
75 // -------------------------------------------------------------------------
78 * @serial The credential class name.
80 private final String credentialClass
;
83 * @serial The principals, a set of CredOwner objects (an undocumented
84 * inner class of this class).
86 private final Set principals
;
91 private final boolean testing
;
94 // -------------------------------------------------------------------------
97 * Create a new private credential permission.
99 * @param name The permission target name.
100 * @param actions The list of actions, which, for this class, must be
101 * <code>"read"</code>.
103 public PrivateCredentialPermission (final String name
, String actions
)
106 actions
= actions
.trim().toLowerCase();
107 if (!"read".equals (actions
))
109 throw new IllegalArgumentException("actions must be \"read\"");
111 StringTokenizer st
= new StringTokenizer (name
, " \"'");
112 principals
= new HashSet();
113 if (st
.countTokens() < 3 || (st
.countTokens() & 1) == 0)
115 throw new IllegalArgumentException ("badly formed credential name");
117 credentialClass
= st
.nextToken();
118 while (st
.hasMoreTokens())
120 principals
.add (new CredOwner (st
.nextToken(), st
.nextToken()));
122 testing
= false; // WTF ever.
126 // -------------------------------------------------------------------------
128 public boolean equals (Object o
)
130 if (! (o
instanceof PrivateCredentialPermission
))
134 PrivateCredentialPermission that
= (PrivateCredentialPermission
) o
;
135 if (!that
.getActions().equals (getActions()))
139 if (!that
.getCredentialClass().equals (getCredentialClass()))
144 final String
[][] principals
= getPrincipals();
145 final String
[][] that_principals
= that
.getPrincipals();
146 if (that_principals
== null)
150 if (that_principals
.length
!= principals
.length
)
154 for (int i
= 0; i
< principals
.length
; i
++)
156 if (!principals
[i
][0].equals (that_principals
[i
][0]) ||
157 !principals
[i
][1].equals (that_principals
[i
][1]))
166 * Returns the actions this permission encompasses. For private credential
167 * permissions, this is always the string <code>"read"</code>.
169 * @return The list of actions.
171 public String
getActions()
177 * Returns the credential class name that was embedded in this permission's
180 * @return The credential class name.
182 public String
getCredentialClass()
184 return credentialClass
;
188 * Returns the principal list that was embedded in this permission's target
191 * <p>Each element of the returned array is a pair; the first element is the
192 * principal class name, and the second is the principal name.
194 * @return The principal list.
196 public String
[][] getPrincipals()
198 String
[][] ret
= new String
[principals
.size()][];
199 Iterator it
= principals
.iterator();
200 for (int i
= 0; i
< principals
.size() && it
.hasNext(); i
++)
202 CredOwner co
= (CredOwner
) it
.next();
203 ret
[i
] = new String
[] { co
.getPrincipalClass(), co
.getPrincipalName() };
208 public int hashCode()
210 return credentialClass
.hashCode() + principals
.hashCode();
214 * Test if this permission implies another. This method returns true if:
217 * <li><i>p</i> is an instance of PrivateCredentialPermission</li>.
218 * <li>The credential class name of this instance matches that of <i>p</i>,
219 * and one of the principals of <i>p</i> is contained in the principals of
222 * <li><code>[ * P "foo" ] implies [ C P "foo" ]</code></li>
223 * <li><code>[ C P1 "foo" ] implies [ C P1 "foo" P2 "bar" ]</code></li>
224 * <li><code>[ C P1 "*" ] implies [ C P1 "foo" ]</code></li>
228 * @param p The permission to check.
229 * @return True if this permission implies <i>p</i>.
231 public boolean implies (Permission p
)
233 if (! (p
instanceof PrivateCredentialPermission
))
237 PrivateCredentialPermission that
= (PrivateCredentialPermission
) p
;
238 if (!credentialClass
.equals ("*")
239 && !credentialClass
.equals (that
.getCredentialClass()))
243 String
[][] principals
= getPrincipals();
244 String
[][] that_principals
= that
.getPrincipals();
245 if (that_principals
== null)
249 for (int i
= 0; i
< principals
.length
; i
++)
251 for (int j
= 0; j
< that_principals
.length
; j
++)
253 if (principals
[i
][0].equals (that_principals
[j
][0]) &&
254 (principals
[i
][1].equals ("*") ||
255 principals
[i
][1].equals (that_principals
[j
][1])))
265 * This method is not necessary for this class, thus it always returns null.
269 public PermissionCollection
newPermissionCollection()
275 // -------------------------------------------------------------------------
278 * An undocumented inner class present for serialization compatibility.
280 private static class CredOwner
implements Serializable
284 // -----------------------------------------------------------------------
286 private final String principalClass
;
287 private final String principalName
;
290 // -----------------------------------------------------------------------
292 CredOwner (final String principalClass
, final String principalName
)
294 this.principalClass
= principalClass
;
295 this.principalName
= principalName
;
299 // -----------------------------------------------------------------------
301 public boolean equals (Object o
)
303 if (!(o
instanceof CredOwner
))
307 return principalClass
.equals (((CredOwner
) o
).getPrincipalClass()) &&
308 principalName
.equals (((CredOwner
) o
).getPrincipalName());
311 public int hashCode()
313 return principalClass
.hashCode() + principalName
.hashCode();
316 public String
getPrincipalClass()
318 return principalClass
;
321 public String
getPrincipalName()
323 return principalName
;