001    /**
002     *
003     * Copyright 2003-2004 The Apache Software Foundation
004     *
005     *  Licensed under the Apache License, Version 2.0 (the "License");
006     *  you may not use this file except in compliance with the License.
007     *  You may obtain a copy of the License at
008     *
009     *     http://www.apache.org/licenses/LICENSE-2.0
010     *
011     *  Unless required by applicable law or agreed to in writing, software
012     *  distributed under the License is distributed on an "AS IS" BASIS,
013     *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014     *  See the License for the specific language governing permissions and
015     *  limitations under the License.
016     */
017    
018    //
019    // This source code implements specifications defined by the Java
020    // Community Process. In order to remain compliant with the specification
021    // DO NOT add / change / or delete method signatures!
022    //
023    
024    package javax.security.jacc;
025    
026    import java.io.Serializable;
027    import java.security.Permission;
028    
029    /**
030     * Class for EJB <code>isCallerInRole(String reference)</code> permissions. An
031     * EJBRoleRefPermission is a named permission and has actions.<p>
032     *
033     * The name of an EJBRoleRefPermission contains the value of the ejb-name
034     * element in the application's deployment descriptor that identifies the EJB
035     * in whose context the permission is being evalutated.<p>
036     *
037     * The actions of an EJBRoleRefPermission identifies the role reference to
038     * which the permission applies. An EJBRoleRefPermission is checked to
039     * determine if the subject is a member of the role identified by the reference.
040     * @version $Rev: 54183 $ $Date: 2004-10-09 15:04:29 -0700 (Sat, 09 Oct 2004) $
041     */
042    public final class EJBRoleRefPermission extends Permission implements Serializable {
043    
044        private transient int cachedHashCode = 0;
045        private String actions;
046    
047        /**
048         * Creates a new EJBRoleRefPermission with the specified name and actions.
049         * @param name the ejb-name that identifies the EJB in whose context the
050         * role references are to be evaluated.
051         * @param role identifies the role reference to which the permission
052         * pertains. The role reference is scoped to the EJB identified in the
053         * name parameter. The value of the role reference must not be null or
054         * the empty string.
055         */
056        public EJBRoleRefPermission(String name, String role) {
057            super(name);
058    
059            if (role == null || role.length() == 0)
060                throw new IllegalArgumentException("Role reference must not be null or the empty string");
061    
062            actions = role;
063        }
064    
065        /**
066         * Checks two EJBRoleRefPermission objects for equality. EJBRoleRefPermission
067         * objects are equivalent if they have case equivalent name and actions values.<p>
068         *
069         * Two Permission objects, P1 and P2, are equivalent if and only if P1.implies(P2) && P2.implies(P1).
070         * @param o the EJBRoleRefPermission object being tested for equality with this EJBRoleRefPermission.
071         * @return true if the argument EJBRoleRefPermission object is equivalent to this EJBRoleRefPermission.
072         */
073        public boolean equals(Object o) {
074            if (o == null || !(o instanceof EJBRoleRefPermission)) return false;
075    
076            EJBRoleRefPermission other = (EJBRoleRefPermission)o;
077            return getName().equals(other.getName()) && actions.equals(other.actions);
078        }
079    
080        /**
081         * Returns a canonical String representation of the actions of this EJBRoleRefPermission.
082         * @return a String containing the canonicalized actions of this EJBRoleRefPermission.
083         */
084        public String getActions() {
085            return actions;
086        }
087    
088        /**
089         * Returns the hash code value for this EJBRoleRefPermission. The properties
090         * of the returned hash code must be as follows:
091         * <ul>
092         * <li>During the lifetime of a Java application, the hashCode method must
093         * return the same integer value, every time it is called on a EJBRoleRefPermission
094         * object. The value returned by hashCode for a particular EJBRoleRefPermission
095         * need not remain consistent from one execution of an application to another.</li>
096         * <li>If two EJBRoleRefPermission objects are equal according to the equals
097         * method, then calling the hashCode method on each of the two Permission
098         * objects must produce the same integer result (within an application).</li>
099         * </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