View Javadoc

1   /**
2    *
3    * Copyright 2003-2004 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.security.Principal;
21  import java.io.Serializable;
22  
23  
24  /**
25   * Represents a principal in an realm.
26   *
27   * @version $Rev: 355877 $ $Date: 2005-12-10 18:48:27 -0800 (Sat, 10 Dec 2005) $
28   */
29  public class RealmPrincipal implements Principal, Serializable {
30      private final String realm;
31      private final String domain;
32      private final Principal principal;
33      private transient String name = null;
34  
35      public RealmPrincipal(String realm, String domain, Principal principal) {
36  
37          if (realm == null) throw new IllegalArgumentException("realm is null");
38          if (domain == null) throw new IllegalArgumentException("domain is null");
39          if (principal == null) throw new IllegalArgumentException("principal is null");
40  
41          this.realm = realm;
42          this.domain = domain;
43          this.principal = principal;
44      }
45  
46      public boolean equals(Object 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 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 getName() {
82          if (name == null) {
83              StringBuffer buffer = new StringBuffer("");
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 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 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 getLoginDomain() {
121         return domain;
122     }
123 }