KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > geronimo > security > RealmPrincipal


1 /**
2  * Licensed to the Apache Software Foundation (ASF) under one or more
3  * contributor license agreements. See the NOTICE file distributed with
4  * this work for additional information regarding copyright ownership.
5  * The ASF licenses this file to You under the Apache License, Version 2.0
6  * (the "License"); you may not use this file except in compliance with
7  * the License. You may obtain a copy of the License at
8  *
9  * http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  */

17
18 package org.apache.geronimo.security;
19
20 import java.security.Principal JavaDoc;
21 import java.io.Serializable JavaDoc;
22
23
24 /**
25  * Represents a principal in an realm.
26  *
27  * @version $Rev: 476049 $ $Date: 2006-11-16 23:35:17 -0500 (Thu, 16 Nov 2006) $
28  */

29 public class RealmPrincipal implements Principal JavaDoc, Serializable JavaDoc {
30     private final String JavaDoc realm;
31     private final String JavaDoc domain;
32     private final Principal JavaDoc principal;
33     private transient String JavaDoc name = null;
34
35     public RealmPrincipal(String JavaDoc realm, String JavaDoc domain, Principal JavaDoc principal) {
36
37         if (realm == null) throw new IllegalArgumentException JavaDoc("realm is null");
38         if (domain == null) throw new IllegalArgumentException JavaDoc("domain is null");
39         if (principal == null) throw new IllegalArgumentException JavaDoc("principal is null");
40
41         this.realm = realm;
42         this.domain = domain;
43         this.principal = principal;
44     }
45
46     public boolean equals(Object JavaDoc o) {
47         if (this == o) return true;
48         if (o == null || getClass() != o.getClass()) return false;
49
50         final RealmPrincipal that = (RealmPrincipal) o;
51
52         if (!domain.equals(that.domain)) return false;
53         if (!principal.equals(that.principal)) return false;
54         if (!realm.equals(that.realm)) return false;
55
56         return true;
57     }
58
59     public int hashCode() {
60         int result;
61         result = realm.hashCode();
62         result = 29 * result + domain.hashCode();
63         result = 29 * result + principal.hashCode();
64         return result;
65     }
66
67     /**
68      * Returns a string representation of this principal.
69      *
70      * @return a string representation of this principal.
71      */

72     public String JavaDoc toString() {
73         return getName();
74     }
75
76     /**
77      * Returns the name of this principal.
78      *
79      * @return the name of this principal.
80      */

81     public String JavaDoc getName() {
82         if (name == null) {
83             StringBuffer JavaDoc buffer = new StringBuffer JavaDoc("");
84             buffer.append(realm);
85             buffer.append("::");
86             buffer.append(domain);
87             buffer.append("::");
88             buffer.append(principal.getClass().getName());
89             buffer.append(':');
90             buffer.append(principal.getName());
91
92             name = buffer.toString();
93         }
94         return name;
95     }
96
97     /**
98      * Returns the realm that is associated with the principal.
99      *
100      * @return the realm that is associated with the principal.
101      */

102     public String JavaDoc 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 JavaDoc 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 JavaDoc getLoginDomain() {
121         return domain;
122     }
123 }
124
Popular Tags