1 /**
2 *
3 * Copyright 2003-2004 The Apache Software Foundation
4 *
5 * Licensed under the Apache License, Version 2.0 (the "License");
6 * you may not use this file except in compliance with the License.
7 * 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
19
20
21
22
23
24 package javax.security.jacc;
25
26 import java.io.Serializable;
27 import java.security.Permission;
28
29 /**
30 * Class for EJB <code>isCallerInRole(String reference)</code> permissions. An
31 * EJBRoleRefPermission is a named permission and has actions.<p>
32 *
33 * The name of an EJBRoleRefPermission contains the value of the ejb-name
34 * element in the application's deployment descriptor that identifies the EJB
35 * in whose context the permission is being evalutated.<p>
36 *
37 * The actions of an EJBRoleRefPermission identifies the role reference to
38 * which the permission applies. An EJBRoleRefPermission is checked to
39 * determine if the subject is a member of the role identified by the reference.
40 * @version $Rev: 54183 $ $Date: 2004-10-09 15:04:29 -0700 (Sat, 09 Oct 2004) $
41 */
42 public final class EJBRoleRefPermission extends Permission implements Serializable {
43
44 private transient int cachedHashCode = 0;
45 private String actions;
46
47 /**
48 * Creates a new EJBRoleRefPermission with the specified name and actions.
49 * @param name the ejb-name that identifies the EJB in whose context the
50 * role references are to be evaluated.
51 * @param role identifies the role reference to which the permission
52 * pertains. The role reference is scoped to the EJB identified in the
53 * name parameter. The value of the role reference must not be null or
54 * the empty string.
55 */
56 public EJBRoleRefPermission(String name, String role) {
57 super(name);
58
59 if (role == null || role.length() == 0)
60 throw new IllegalArgumentException("Role reference must not be null or the empty string");
61
62 actions = role;
63 }
64
65 /**
66 * Checks two EJBRoleRefPermission objects for equality. EJBRoleRefPermission
67 * objects are equivalent if they have case equivalent name and actions values.<p>
68 *
69 * Two Permission objects, P1 and P2, are equivalent if and only if P1.implies(P2) && P2.implies(P1).
70 * @param o the EJBRoleRefPermission object being tested for equality with this EJBRoleRefPermission.
71 * @return true if the argument EJBRoleRefPermission object is equivalent to this EJBRoleRefPermission.
72 */
73 public boolean equals(Object o) {
74 if (o == null || !(o instanceof EJBRoleRefPermission)) return false;
75
76 EJBRoleRefPermission other = (EJBRoleRefPermission)o;
77 return getName().equals(other.getName()) && actions.equals(other.actions);
78 }
79
80 /**
81 * Returns a canonical String representation of the actions of this EJBRoleRefPermission.
82 * @return a String containing the canonicalized actions of this EJBRoleRefPermission.
83 */
84 public String getActions() {
85 return actions;
86 }
87
88 /**
89 * Returns the hash code value for this EJBRoleRefPermission. The properties
90 * of the returned hash code must be as follows:
91 * <ul>
92 * <li>During the lifetime of a Java application, the hashCode method must
93 * return the same integer value, every time it is called on a EJBRoleRefPermission
94 * object. The value returned by hashCode for a particular EJBRoleRefPermission
95 * need not remain consistent from one execution of an application to another.</li>
96 * <li>If two EJBRoleRefPermission objects are equal according to the equals
97 * method, then calling the hashCode method on each of the two Permission
98 * objects must produce the same integer result (within an application).</li>
99 * </ul>
100 * @return the integer hash code value for this object.
101 */
102 public int hashCode() {
103 if (cachedHashCode == 0) {
104 cachedHashCode = getName().hashCode() ^ actions.hashCode();
105 }
106 return cachedHashCode;
107 }
108
109 /**
110 * Determines if the argument Permission is "implied by" this
111 * EJBRoleRefPermission. For this to be the case,
112 *
113 * <ul>
114 * <li>The argument must be an instanceof EJBRoleRefPermission</li>
115 * <li>with name equivalent to that of this EJBRoleRefPermission, and</li>
116 * <li>with the role reference equivalent to that of this EJBRoleRefPermission applies.</li>
117 * <ul>
118 * The name and actions comparisons described above are case sensitive.
119 * @param permission "this" EJBRoleRefPermission is checked to see if it implies the argument permission.
120 * @return true if the specified permission is implied by this object, false if not.
121 */
122 public boolean implies(Permission permission) {
123 return equals(permission);
124 }
125 }
126