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 * Represents a principal in an realm.
025 *
026 * @version $Rev: 706640 $ $Date: 2008-10-21 14:44:05 +0000 (Tue, 21 Oct 2008) $
027 */
028 public class DomainPrincipal implements Principal, Serializable {
029 private final String domain;
030 private final Principal principal;
031 private transient String name = null;
032
033 public DomainPrincipal(String domain, Principal principal) {
034 if (domain == null) throw new IllegalArgumentException("domain is null");
035 if (principal == null) throw new IllegalArgumentException("principal is null");
036
037 this.domain = domain;
038 this.principal = principal;
039 }
040
041 public boolean equals(Object o) {
042 if (this == o) return true;
043 if (o == null || getClass() != o.getClass()) return false;
044
045 final DomainPrincipal that = (DomainPrincipal) o;
046
047 if (!domain.equals(that.domain)) return false;
048 if (!principal.equals(that.principal)) return false;
049
050 return true;
051 }
052
053 /**
054 * Returns a string representation of this principal.
055 *
056 * @return a string representation of this principal.
057 */
058 public String toString() {
059 return getName();
060 }
061
062 /**
063 * Returns a hashcode for this principal.
064 *
065 * @return a hashcode for this principal.
066 */
067 public int hashCode() {
068 int result;
069 result = domain.hashCode();
070 result = 29 * result + principal.hashCode();
071 return result;
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(domain);
084 buffer.append("::");
085 buffer.append(principal.getClass().getName());
086 buffer.append(':');
087 buffer.append(principal.getName());
088 buffer.append("]");
089
090 name = buffer.toString();
091 }
092 return name;
093 // return principal.getName();
094 }
095
096 /**
097 * Returns the principal that is associated with the realm.
098 *
099 * @return the principal that is associated with the realm.
100 */
101 public Principal getPrincipal() {
102 return principal;
103 }
104
105 /**
106 * Returns the realm that is associated with the principal.
107 *
108 * @return the realm that is associated with the principal.
109 */
110 public String getDomain() {
111 return domain;
112 }
113 }