View Javadoc

1   /**
2    *
3    * Copyright 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.io.Serializable;
21  import java.security.Principal;
22  
23  
24  /**
25   * @version $Rev: 355877 $ $Date: 2005-12-10 18:48:27 -0800 (Sat, 10 Dec 2005) $
26   */
27  public class IdentificationPrincipal implements Principal, Serializable {
28      private final SubjectId id;
29      private transient String name;
30  
31      public IdentificationPrincipal(SubjectId id) {
32          this.id = id;
33      }
34  
35      public SubjectId getId() {
36          return id;
37      }
38  
39      /**
40       * Compares this principal to the specified object.  Returns true
41       * if the object passed in matches the principal represented by
42       * the implementation of this interface.
43       *
44       * @param another principal to compare with.
45       * @return true if the principal passed in is the same as that
46       *         encapsulated by this principal, and false otherwise.
47       */
48      public boolean equals(Object another) {
49          if (!(another instanceof IdentificationPrincipal)) return false;
50  
51          IdentificationPrincipal idPrincipal = (IdentificationPrincipal) another;
52  
53          return id.equals(idPrincipal.id);
54      }
55  
56      /**
57       * Returns a string representation of this principal.
58       *
59       * @return a string representation of this principal.
60       */
61      public String toString() {
62          return getName();
63      }
64  
65      /**
66       * Returns a hashcode for this principal.
67       *
68       * @return a hashcode for this principal.
69       */
70      public int hashCode() {
71          return getName().hashCode();
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(getClass().getName());
84              buffer.append("[");
85              buffer.append(id);
86              buffer.append("]");
87  
88              name = buffer.toString();
89          }
90          return name;
91      }
92  }