View Javadoc

1   /**
2    *
3    * Copyright 2005 The Apache Software Foundation
4    *
5    *  Licensed under the Apache License, Version 2.0 (the "License");
6    *  you may not use this file except in compliance with the License.
7    *  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;
21  import java.security.Principal;
22  
23  /**
24   * Represents a principal in an realm.
25   *
26   * @version $Rev: 355877 $ $Date: 2005-12-10 18:48:27 -0800 (Sat, 10 Dec 2005) $
27   */
28  public class DomainPrincipal implements Principal, Serializable {
29      private final String domain;
30      private final Principal principal;
31      private transient String name = null;
32  
33      public DomainPrincipal(String domain, Principal principal) {
34          if (domain == null) throw new IllegalArgumentException("domain is null");
35          if (principal == null) throw new IllegalArgumentException("principal is null");
36  
37          this.domain = domain;
38          this.principal = principal;
39      }
40  
41      public boolean equals(Object 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 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 getName() {
80          if (name == null) {
81  
82              StringBuffer buffer = new StringBuffer("");
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 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 }