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