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