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    import java.security.Principal;
022    
023    
024    /**
025     * @version $Rev: 476049 $ $Date: 2006-11-16 23:35:17 -0500 (Thu, 16 Nov 2006) $
026     */
027    public class IdentificationPrincipal implements Principal, Serializable {
028        private final SubjectId id;
029        private transient String name;
030    
031        public IdentificationPrincipal(SubjectId id) {
032            this.id = id;
033        }
034    
035        public SubjectId getId() {
036            return id;
037        }
038    
039        /**
040         * Compares this principal to the specified object.  Returns true
041         * if the object passed in matches the principal represented by
042         * the implementation of this interface.
043         *
044         * @param another principal to compare with.
045         * @return true if the principal passed in is the same as that
046         *         encapsulated by this principal, and false otherwise.
047         */
048        public boolean equals(Object another) {
049            if (!(another instanceof IdentificationPrincipal)) return false;
050    
051            IdentificationPrincipal idPrincipal = (IdentificationPrincipal) another;
052    
053            return id.equals(idPrincipal.id);
054        }
055    
056        /**
057         * Returns a string representation of this principal.
058         *
059         * @return a string representation of this principal.
060         */
061        public String toString() {
062            return getName();
063        }
064    
065        /**
066         * Returns a hashcode for this principal.
067         *
068         * @return a hashcode for this principal.
069         */
070        public int hashCode() {
071            return getName().hashCode();
072        }
073    
074        /**
075         * Returns the name of this principal.
076         *
077         * @return the name of this principal.
078         */
079        public String getName() {
080            if (name == null) {
081    
082                StringBuffer buffer = new StringBuffer("");
083                buffer.append(getClass().getName());
084                buffer.append("[");
085                buffer.append(id);
086                buffer.append("]");
087    
088                name = buffer.toString();
089            }
090            return name;
091        }
092    }