KickJava   Java API By Example, From Geeks To Geeks.

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


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.io.Serializable JavaDoc;
21 import java.security.Principal JavaDoc;
22
23 /**
24  * Represents a principal in an realm.
25  *
26  * @version $Rev: 476049 $ $Date: 2006-11-16 23:35:17 -0500 (Thu, 16 Nov 2006) $
27  */

28 public class DomainPrincipal implements Principal JavaDoc, Serializable JavaDoc {
29     private final String JavaDoc domain;
30     private final Principal JavaDoc principal;
31     private transient String JavaDoc name = null;
32
33     public DomainPrincipal(String JavaDoc domain, Principal JavaDoc principal) {
34         if (domain == null) throw new IllegalArgumentException JavaDoc("domain is null");
35         if (principal == null) throw new IllegalArgumentException JavaDoc("principal is null");
36
37         this.domain = domain;
38         this.principal = principal;
39     }
40
41     public boolean equals(Object JavaDoc o) {
42         if (this == o) return true;
43         if (o == null || getClass() != o.getClass()) return false;
44
45         final DomainPrincipal that = (DomainPrincipal) o;
46
47         if (!domain.equals(that.domain)) return false;
48         if (!principal.equals(that.principal)) return false;
49
50         return true;
51     }
52
53     /**
54      * Returns a string representation of this principal.
55      *
56      * @return a string representation of this principal.
57      */

58     public String JavaDoc toString() {
59         return getName();
60     }
61
62     /**
63      * Returns a hashcode for this principal.
64      *
65      * @return a hashcode for this principal.
66      */

67     public int hashCode() {
68         int result;
69         result = domain.hashCode();
70         result = 29 * result + principal.hashCode();
71         return result;
72     }
73
74     /**
75      * Returns the name of this principal.
76      *
77      * @return the name of this principal.
78      */

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

101     public Principal JavaDoc 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 JavaDoc getDomain() {
111         return domain;
112     }
113 }
114
Popular Tags