View Javadoc

1   /*
2    * Copyright 2008 University Corporation for Advanced Internet Development, Inc.
3    *
4    * Licensed under the Apache License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    *
8    * http://www.apache.org/licenses/LICENSE-2.0
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
15   */
16  
17  package edu.internet2.middleware.shibboleth.idp.authn;
18  
19  import java.io.Serializable;
20  import java.security.Principal;
21  
22  import org.opensaml.xml.util.DatatypeHelper;
23  
24  /** A basic implementation of {@link Principal}. */
25  public class UsernamePrincipal implements Principal, Serializable {
26  
27      /** Serial version UID. */
28      private static final long serialVersionUID = 8708917521896240626L;
29      
30      /** Name of the principal. */
31      private String name;
32  
33      /**
34       * Constructor.
35       * 
36       * @param principalName name of the principal
37       */
38      public UsernamePrincipal(String principalName) {
39          name = DatatypeHelper.safeTrimOrNullString(principalName);
40          if(name == null){
41              throw new IllegalArgumentException("principal name may not be null or empty");
42          }
43      }
44  
45      /** {@inheritDoc} */
46      public String getName() {
47          return name;
48      }
49  
50      /** {@inheritDoc} */
51      public String toString() {
52          return "{BasicPrincipal}" + getName();
53      }
54  
55      /** {@inheritDoc} */
56      public int hashCode() {
57          return name.hashCode();
58      }
59  
60      /** {@inheritDoc} */
61      public boolean equals(Object obj) {
62          if (obj == this) {
63              return true;
64          }
65  
66          if (obj instanceof UsernamePrincipal) {
67              return DatatypeHelper.safeEquals(getName(), ((UsernamePrincipal) obj).getName());
68          }
69  
70          return false;
71      }
72  }