001    /**
002     *  Licensed to the Apache Software Foundation (ASF) under one or more
003     *  contributor license agreements.  See the NOTICE file distributed with
004     *  this work for additional information regarding copyright ownership.
005     *  The ASF licenses this file to You under the Apache License, Version 2.0
006     *  (the "License"); you may not use this file except in compliance with
007     *  the License.  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    package org.apache.geronimo.security;
019    
020    import java.io.Serializable;
021    
022    
023    /**
024     * @version $Rev: 476049 $ $Date: 2006-11-16 23:35:17 -0500 (Thu, 16 Nov 2006) $
025     */
026    public class SubjectId implements Serializable {
027        private final Long subjectId;
028        private final byte[] hash;
029        private transient int hashCode;
030        private transient String name;
031    
032        public SubjectId(Long subjectId, byte[] hash) {
033            this.subjectId = subjectId;
034            this.hash = hash;
035        }
036    
037        public Long getSubjectId() {
038            return subjectId;
039        }
040    
041        public byte[] getHash() {
042            return hash;
043        }
044    
045        public boolean equals(Object obj) {
046            if (!(obj instanceof SubjectId)) return false;
047    
048            SubjectId another = (SubjectId) obj;
049            if (!another.subjectId.equals(subjectId)) return false;
050            for (int i = 0; i < hash.length; i++) {
051                if (another.hash[i] != hash[i]) return false;
052            }
053            return true;
054        }
055    
056        public String toString() {
057            if (name == null) {
058                StringBuffer buffer = new StringBuffer();
059                buffer.append('[');
060                buffer.append(subjectId);
061                buffer.append(":0x");
062                for (int i = 0; i < hash.length; i++) {
063                    buffer.append(HEXCHAR[(hash[i]>>>4)&0x0F]);
064                    buffer.append(HEXCHAR[(hash[i]    )&0x0F]);
065                }
066                buffer.append(']');
067                name = buffer.toString();
068            }
069            return name;
070        }
071    
072        /**
073         * Returns a hashcode for this LoginModuleId.
074         *
075         * @return a hashcode for this LoginModuleId.
076         */
077        public int hashCode() {
078            if (hashCode == 0) {
079                for (int i = 0; i < hash.length; i++) {
080                    hashCode ^= hash[i];
081                }
082                hashCode ^= subjectId.hashCode();
083            }
084            return hashCode;
085        }
086    
087        private static final char[] HEXCHAR = {
088            '0', '1', '2', '3', '4', '5', '6', '7',
089            '8', '9', 'a', 'b', 'c', 'd', 'e', 'f'
090        };
091    }