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.security.Principal;
021    import java.io.Serializable;
022    
023    
024    /**
025     * Represents a principal in an realm.
026     *
027     * @version $Rev: 476049 $ $Date: 2006-11-16 23:35:17 -0500 (Thu, 16 Nov 2006) $
028     */
029    public class RealmPrincipal implements Principal, Serializable {
030        private final String realm;
031        private final String domain;
032        private final Principal principal;
033        private transient String name = null;
034    
035        public RealmPrincipal(String realm, String domain, Principal principal) {
036    
037            if (realm == null) throw new IllegalArgumentException("realm is null");
038            if (domain == null) throw new IllegalArgumentException("domain is null");
039            if (principal == null) throw new IllegalArgumentException("principal is null");
040    
041            this.realm = realm;
042            this.domain = domain;
043            this.principal = principal;
044        }
045    
046        public boolean equals(Object o) {
047            if (this == o) return true;
048            if (o == null || getClass() != o.getClass()) return false;
049    
050            final RealmPrincipal that = (RealmPrincipal) o;
051    
052            if (!domain.equals(that.domain)) return false;
053            if (!principal.equals(that.principal)) return false;
054            if (!realm.equals(that.realm)) return false;
055    
056            return true;
057        }
058    
059        public int hashCode() {
060            int result;
061            result = realm.hashCode();
062            result = 29 * result + domain.hashCode();
063            result = 29 * result + principal.hashCode();
064            return result;
065        }
066    
067        /**
068         * Returns a string representation of this principal.
069         *
070         * @return a string representation of this principal.
071         */
072        public String toString() {
073            return getName();
074        }
075    
076        /**
077         * Returns the name of this principal.
078         *
079         * @return the name of this principal.
080         */
081        public String getName() {
082            if (name == null) {
083                StringBuffer buffer = new StringBuffer("");
084                buffer.append(realm);
085                buffer.append("::");
086                buffer.append(domain);
087                buffer.append("::");
088                buffer.append(principal.getClass().getName());
089                buffer.append(':');
090                buffer.append(principal.getName());
091    
092                name = buffer.toString();
093            }
094            return name;
095        }
096    
097        /**
098         * Returns the realm that is associated with the principal.
099         *
100         * @return the realm that is associated with the principal.
101         */
102        public String getRealm() {
103            return realm;
104        }
105    
106        /**
107         * Returns the principal that is associated with the realm.
108         *
109         * @return the principal that is associated with the realm.
110         */
111        public Principal getPrincipal() {
112            return principal;
113        }
114    
115        /**
116         * Returns the realm that is associated with the principal.
117         *
118         * @return the realm that is associated with the principal.
119         */
120        public String getLoginDomain() {
121            return domain;
122        }
123    }