Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/src/share/classes/com/sun/security/auth/UnixNumericGroupPrincipal.java
38924 views
/*1* Copyright (c) 2000, 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;2829/**30* <p> This class implements the <code>Principal</code> interface31* and represents a user's Unix group identification number (GID).32*33* <p> Principals such as this <code>UnixNumericGroupPrincipal</code>34* may be associated with a particular <code>Subject</code>35* to augment that <code>Subject</code> with an additional36* identity. Refer to the <code>Subject</code> class for more information37* on how to achieve this. Authorization decisions can then be based upon38* the Principals associated with a <code>Subject</code>.39*40* @see java.security.Principal41* @see javax.security.auth.Subject42*/43@jdk.Exported44public class UnixNumericGroupPrincipal implements45Principal,46java.io.Serializable {4748private static final long serialVersionUID = 3941535899328403223L;4950/**51* @serial52*/53private String name;5455/**56* @serial57*/58private boolean primaryGroup;5960/**61* Create a <code>UnixNumericGroupPrincipal</code> using a62* <code>String</code> representation of the user's63* group identification number (GID).64*65* <p>66*67* @param name the user's group identification number (GID)68* for this user. <p>69*70* @param primaryGroup true if the specified GID represents the71* primary group to which this user belongs.72*73* @exception NullPointerException if the <code>name</code>74* is <code>null</code>.75*/76public UnixNumericGroupPrincipal(String name, boolean primaryGroup) {77if (name == null) {78java.text.MessageFormat form = new java.text.MessageFormat79(sun.security.util.ResourcesMgr.getString80("invalid.null.input.value",81"sun.security.util.AuthResources"));82Object[] source = {"name"};83throw new NullPointerException(form.format(source));84}8586this.name = name;87this.primaryGroup = primaryGroup;88}8990/**91* Create a <code>UnixNumericGroupPrincipal</code> using a92* long representation of the user's group identification number (GID).93*94* <p>95*96* @param name the user's group identification number (GID) for this user97* represented as a long. <p>98*99* @param primaryGroup true if the specified GID represents the100* primary group to which this user belongs.101*102*/103public UnixNumericGroupPrincipal(long name, boolean primaryGroup) {104this.name = (new Long(name)).toString();105this.primaryGroup = primaryGroup;106}107108/**109* Return the user's group identification number (GID) for this110* <code>UnixNumericGroupPrincipal</code>.111*112* <p>113*114* @return the user's group identification number (GID) for this115* <code>UnixNumericGroupPrincipal</code>116*/117public String getName() {118return name;119}120121/**122* Return the user's group identification number (GID) for this123* <code>UnixNumericGroupPrincipal</code> as a long.124*125* <p>126*127* @return the user's group identification number (GID) for this128* <code>UnixNumericGroupPrincipal</code> as a long.129*/130public long longValue() {131return ((new Long(name)).longValue());132}133134/**135* Return whether this group identification number (GID) represents136* the primary group to which this user belongs.137*138* <p>139*140* @return true if this group identification number (GID) represents141* the primary group to which this user belongs,142* or false otherwise.143*/144public boolean isPrimaryGroup() {145return primaryGroup;146}147148/**149* Return a string representation of this150* <code>UnixNumericGroupPrincipal</code>.151*152* <p>153*154* @return a string representation of this155* <code>UnixNumericGroupPrincipal</code>.156*/157public String toString() {158159if (primaryGroup) {160java.text.MessageFormat form = new java.text.MessageFormat161(sun.security.util.ResourcesMgr.getString162("UnixNumericGroupPrincipal.Primary.Group.name",163"sun.security.util.AuthResources"));164Object[] source = {name};165return form.format(source);166} else {167java.text.MessageFormat form = new java.text.MessageFormat168(sun.security.util.ResourcesMgr.getString169("UnixNumericGroupPrincipal.Supplementary.Group.name",170"sun.security.util.AuthResources"));171Object[] source = {name};172return form.format(source);173}174}175176/**177* Compares the specified Object with this178* <code>UnixNumericGroupPrincipal</code>179* for equality. Returns true if the given object is also a180* <code>UnixNumericGroupPrincipal</code> and the two181* UnixNumericGroupPrincipals182* have the same group identification number (GID).183*184* <p>185*186* @param o Object to be compared for equality with this187* <code>UnixNumericGroupPrincipal</code>.188*189* @return true if the specified Object is equal equal to this190* <code>UnixNumericGroupPrincipal</code>.191*/192public boolean equals(Object o) {193if (o == null)194return false;195196if (this == o)197return true;198199if (!(o instanceof UnixNumericGroupPrincipal))200return false;201UnixNumericGroupPrincipal that = (UnixNumericGroupPrincipal)o;202203if (this.getName().equals(that.getName()) &&204this.isPrimaryGroup() == that.isPrimaryGroup())205return true;206return false;207}208209/**210* Return a hash code for this <code>UnixNumericGroupPrincipal</code>.211*212* <p>213*214* @return a hash code for this <code>UnixNumericGroupPrincipal</code>.215*/216public int hashCode() {217return toString().hashCode();218}219}220221222