Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/src/share/classes/sun/security/acl/GroupImpl.java
38830 views
/*1* Copyright (c) 1996, 2011, 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 sun.security.acl;2627import java.util.*;28import java.security.*;29import java.security.acl.*;3031/**32* This class implements a group of principals.33* @author Satish Dharmaraj34*/35public class GroupImpl implements Group {36private Vector<Principal> groupMembers = new Vector<>(50, 100);37private String group;3839/**40* Constructs a Group object with no members.41* @param groupName the name of the group42*/43public GroupImpl(String groupName) {44this.group = groupName;45}4647/**48* adds the specified member to the group.49* @param user The principal to add to the group.50* @return true if the member was added - false if the51* member could not be added.52*/53public boolean addMember(Principal user) {54if (groupMembers.contains(user))55return false;5657// do not allow groups to be added to itself.58if (group.equals(user.toString()))59throw new IllegalArgumentException();6061groupMembers.addElement(user);62return true;63}6465/**66* removes the specified member from the group.67* @param user The principal to remove from the group.68* @param true if the principal was removed false if69* the principal was not a member70*/71public boolean removeMember(Principal user) {72return groupMembers.removeElement(user);73}7475/**76* returns the enumeration of the members in the group.77*/78public Enumeration<? extends Principal> members() {79return groupMembers.elements();80}8182/**83* This function returns true if the group passed matches84* the group represented in this interface.85* @param another The group to compare this group to.86*/87public boolean equals(Object obj) {88if (this == obj) {89return true;90}91if (obj instanceof Group == false) {92return false;93}94Group another = (Group)obj;95return group.equals(another.toString());96}9798// equals(Group) for compatibility99public boolean equals(Group another) {100return equals((Object)another);101}102103/**104* Prints a stringified version of the group.105*/106public String toString() {107return group;108}109110/**111* return a hashcode for the principal.112*/113public int hashCode() {114return group.hashCode();115}116117/**118* returns true if the passed principal is a member of the group.119* @param member The principal whose membership must be checked for.120* @return true if the principal is a member of this group,121* false otherwise122*/123public boolean isMember(Principal member) {124125//126// if the member is part of the group (common case), return true.127// if not, recursively search depth first in the group looking for the128// principal.129//130if (groupMembers.contains(member)) {131return true;132} else {133Vector<Group> alreadySeen = new Vector<>(10);134return isMemberRecurse(member, alreadySeen);135}136}137138/**139* return the name of the principal.140*/141public String getName() {142return group;143}144145//146// This function is the recursive search of groups for this147// implementation of the Group. The search proceeds building up148// a vector of already seen groups. Only new groups are considered,149// thereby avoiding loops.150//151boolean isMemberRecurse(Principal member, Vector<Group> alreadySeen) {152Enumeration<? extends Principal> e = members();153while (e.hasMoreElements()) {154boolean mem = false;155Principal p = (Principal) e.nextElement();156157// if the member is in this collection, return true158if (p.equals(member)) {159return true;160} else if (p instanceof GroupImpl) {161//162// if not recurse if the group has not been checked already.163// Can call method in this package only if the object is an164// instance of this class. Otherwise call the method defined165// in the interface. (This can lead to a loop if a mixture of166// implementations form a loop, but we live with this improbable167// case rather than clutter the interface by forcing the168// implementation of this method.)169//170GroupImpl g = (GroupImpl) p;171alreadySeen.addElement(this);172if (!alreadySeen.contains(g))173mem = g.isMemberRecurse(member, alreadySeen);174} else if (p instanceof Group) {175Group g = (Group) p;176if (!alreadySeen.contains(g))177mem = g.isMember(member);178}179180if (mem)181return mem;182}183return false;184}185}186187188