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: 706640 $ $Date: 2008-10-21 14:44:05 +0000 (Tue, 21 Oct 2008) $
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            if (subjectId == null) throw new IllegalArgumentException("subjectId cannot be null");
034            if (hash == null) throw new IllegalArgumentException("hash cannot be null");
035    
036            this.subjectId = subjectId;
037            this.hash = hash;
038        }
039    
040        public Long getSubjectId() {
041            return subjectId;
042        }
043    
044        public byte[] getHash() {
045            return hash;
046        }
047    
048        public boolean equals(Object obj) {
049            if (!(obj instanceof SubjectId)) return false;
050    
051            SubjectId another = (SubjectId) obj;
052            if (!another.subjectId.equals(subjectId)) return false;
053            for (int i = 0; i < hash.length; i++) {
054                if (another.hash[i] != hash[i]) return false;
055            }
056            return true;
057        }
058    
059        public String toString() {
060            if (name == null) {
061                StringBuffer buffer = new StringBuffer();
062                buffer.append('[');
063                buffer.append(subjectId);
064                buffer.append(":0x");
065                for (int i = 0; i < hash.length; i++) {
066                    buffer.append(HEXCHAR[(hash[i]>>>4)&0x0F]);
067                    buffer.append(HEXCHAR[(hash[i]    )&0x0F]);
068                }
069                buffer.append(']');
070                name = buffer.toString();
071            }
072            return name;
073        }
074    
075        /**
076         * Returns a hashcode for this LoginModuleId.
077         *
078         * @return a hashcode for this LoginModuleId.
079         */
080        public int hashCode() {
081            if (hashCode == 0) {
082                for (int i = 0; i < hash.length; i++) {
083                    hashCode ^= hash[i];
084                }
085                hashCode ^= subjectId.hashCode();
086            }
087            return hashCode;
088        }
089    
090        private static final char[] HEXCHAR = {
091            '0', '1', '2', '3', '4', '5', '6', '7',
092            '8', '9', 'a', 'b', 'c', 'd', 'e', 'f'
093        };
094    }