Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/src/share/classes/com/sun/security/auth/LdapPrincipal.java
38924 views
/*1* Copyright (c) 2006, 2013, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation. Oracle designates this7* particular file as subject to the "Classpath" exception as provided8* by Oracle in the LICENSE file that accompanied this code.9*10* This code is distributed in the hope that it will be useful, but WITHOUT11* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or12* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License13* version 2 for more details (a copy is included in the LICENSE file that14* accompanied this code).15*16* You should have received a copy of the GNU General Public License version17* 2 along with this work; if not, write to the Free Software Foundation,18* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.19*20* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA21* or visit www.oracle.com if you need additional information or have any22* questions.23*/2425package com.sun.security.auth;2627import java.security.Principal;28import javax.naming.InvalidNameException;29import javax.naming.ldap.LdapName;3031/**32* A principal identified by a distinguished name as specified by33* <a href="http://www.ietf.org/rfc/rfc2253.txt">RFC 2253</a>.34*35* <p>36* After successful authentication, a user {@link java.security.Principal}37* can be associated with a particular {@link javax.security.auth.Subject}38* to augment that <code>Subject</code> with an additional identity.39* Authorization decisions can then be based upon the40* <code>Principal</code>s that are associated with a <code>Subject</code>.41*42* <p>43* This class is immutable.44*45* @since 1.646*/47@jdk.Exported48public final class LdapPrincipal implements Principal, java.io.Serializable {4950private static final long serialVersionUID = 6820120005580754861L;5152/**53* The principal's string name54*55* @serial56*/57private final String nameString;5859/**60* The principal's name61*62* @serial63*/64private final LdapName name;6566/**67* Creates an LDAP principal.68*69* @param name The principal's string distinguished name.70* @throws InvalidNameException If a syntax violation is detected.71* @exception NullPointerException If the <code>name</code> is72* <code>null</code>.73*/74public LdapPrincipal(String name) throws InvalidNameException {75if (name == null) {76throw new NullPointerException("null name is illegal");77}78this.name = getLdapName(name);79nameString = name;80}8182/**83* Compares this principal to the specified object.84*85* @param object The object to compare this principal against.86* @return true if they are equal; false otherwise.87*/88public boolean equals(Object object) {89if (this == object) {90return true;91}92if (object instanceof LdapPrincipal) {93try {9495return96name.equals(getLdapName(((LdapPrincipal)object).getName()));9798} catch (InvalidNameException e) {99return false;100}101}102return false;103}104105/**106* Computes the hash code for this principal.107*108* @return The principal's hash code.109*/110public int hashCode() {111return name.hashCode();112}113114/**115* Returns the name originally used to create this principal.116*117* @return The principal's string name.118*/119public String getName() {120return nameString;121}122123/**124* Creates a string representation of this principal's name in the format125* defined by <a href="http://www.ietf.org/rfc/rfc2253.txt">RFC 2253</a>.126* If the name has zero components an empty string is returned.127*128* @return The principal's string name.129*/130public String toString() {131return name.toString();132}133134// Create an LdapName object from a string distinguished name.135private LdapName getLdapName(String name) throws InvalidNameException {136return new LdapName(name);137}138}139140141