View Javadoc

1   /*
2    * Licensed to the University Corporation for Advanced Internet Development, 
3    * Inc. (UCAID) under one or more contributor license agreements.  See the 
4    * NOTICE file distributed with this work for additional information regarding
5    * copyright ownership. The UCAID licenses this file to You under the Apache 
6    * License, Version 2.0 (the "License"); you may not use this file except in 
7    * compliance with 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 edu.internet2.middleware.shibboleth.idp.authn;
19  
20  import java.io.Serializable;
21  import java.security.Principal;
22  
23  import org.opensaml.xml.util.DatatypeHelper;
24  
25  /** A basic implementation of {@link Principal}. */
26  public class UsernamePrincipal implements Principal, Serializable {
27  
28      /** Serial version UID. */
29      private static final long serialVersionUID = 8708917521896240626L;
30      
31      /** Name of the principal. */
32      private String name;
33  
34      /**
35       * Constructor.
36       * 
37       * @param principalName name of the principal
38       */
39      public UsernamePrincipal(String principalName) {
40          name = DatatypeHelper.safeTrimOrNullString(principalName);
41          if(name == null){
42              throw new IllegalArgumentException("principal name may not be null or empty");
43          }
44      }
45  
46      /** {@inheritDoc} */
47      public String getName() {
48          return name;
49      }
50  
51      /** {@inheritDoc} */
52      public String toString() {
53          return "{BasicPrincipal}" + getName();
54      }
55  
56      /** {@inheritDoc} */
57      public int hashCode() {
58          return name.hashCode();
59      }
60  
61      /** {@inheritDoc} */
62      public boolean equals(Object obj) {
63          if (obj == this) {
64              return true;
65          }
66  
67          if (obj instanceof UsernamePrincipal) {
68              return DatatypeHelper.safeEquals(getName(), ((UsernamePrincipal) obj).getName());
69          }
70  
71          return false;
72      }
73  }