jdk/src/share/classes/sun/security/acl/GroupImpl.java
author ohair
Wed, 06 Apr 2011 22:06:11 -0700
changeset 9035 1255eb81cc2f
parent 7970 af1579474d16
permissions -rw-r--r--
7033660: Update copyright year to 2011 on any files changed in 2011 Reviewed-by: dholmes
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
9035
1255eb81cc2f 7033660: Update copyright year to 2011 on any files changed in 2011
ohair
parents: 7970
diff changeset
     2
 * Copyright (c) 1996, 2011, Oracle and/or its affiliates. All rights reserved.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
90ce3da70b43 Initial load
duke
parents:
diff changeset
     4
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
90ce3da70b43 Initial load
duke
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
     7
 * published by the Free Software Foundation.  Oracle designates this
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     8
 * particular file as subject to the "Classpath" exception as provided
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
     9
 * by Oracle in the LICENSE file that accompanied this code.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    10
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    11
 * This code is distributed in the hope that it will be useful, but WITHOUT
90ce3da70b43 Initial load
duke
parents:
diff changeset
    12
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
90ce3da70b43 Initial load
duke
parents:
diff changeset
    13
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
90ce3da70b43 Initial load
duke
parents:
diff changeset
    14
 * version 2 for more details (a copy is included in the LICENSE file that
90ce3da70b43 Initial load
duke
parents:
diff changeset
    15
 * accompanied this code).
90ce3da70b43 Initial load
duke
parents:
diff changeset
    16
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    17
 * You should have received a copy of the GNU General Public License version
90ce3da70b43 Initial load
duke
parents:
diff changeset
    18
 * 2 along with this work; if not, write to the Free Software Foundation,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    19
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    20
 *
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    21
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    22
 * or visit www.oracle.com if you need additional information or have any
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    23
 * questions.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    24
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    25
90ce3da70b43 Initial load
duke
parents:
diff changeset
    26
package sun.security.acl;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    27
90ce3da70b43 Initial load
duke
parents:
diff changeset
    28
import java.util.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
import java.security.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
import java.security.acl.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
/**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
 * This class implements a group of principals.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
 * @author      Satish Dharmaraj
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
public class GroupImpl implements Group {
7970
af1579474d16 7008728: diamond conversion of basic security, permissions, authentication
smarks
parents: 5506
diff changeset
    37
    private Vector<Principal> groupMembers = new Vector<>(50, 100);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
    private String group;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
     * Constructs a Group object with no members.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
     * @param groupName the name of the group
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
    public GroupImpl(String groupName) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
        this.group = groupName;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
     * adds the specified member to the group.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
     * @param user The principal to add to the group.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
     * @return true if the member was added - false if the
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
     * member could not be added.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
    public boolean addMember(Principal user) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
        if (groupMembers.contains(user))
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
          return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
        // do not allow groups to be added to itself.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
        if (group.equals(user.toString()))
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
            throw new IllegalArgumentException();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
        groupMembers.addElement(user);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
        return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
     * removes the specified member from the group.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
     * @param user The principal to remove from the group.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
     * @param true if the principal was removed false if
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
     * the principal was not a member
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
    public boolean removeMember(Principal user) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
        return groupMembers.removeElement(user);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
     * returns the enumeration of the members in the group.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
    public Enumeration<? extends Principal> members() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
        return groupMembers.elements();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
     * This function returns true if the group passed matches
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
     * the group represented in this interface.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
     * @param another The group to compare this group to.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
    public boolean equals(Object obj) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
        if (this == obj) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
            return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
        if (obj instanceof Group == false) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
            return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
        Group another = (Group)obj;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
        return group.equals(another.toString());
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
    // equals(Group) for compatibility
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
    public boolean equals(Group another) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
        return equals((Object)another);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
     * Prints a stringified version of the group.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
    public String toString() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
        return group;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
     * return a hashcode for the principal.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
    public int hashCode() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
        return group.hashCode();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
     * returns true if the passed principal is a member of the group.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
     * @param member The principal whose membership must be checked for.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
     * @return true if the principal is a member of this group,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
     * false otherwise
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
    public boolean isMember(Principal member) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
        //
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
        // if the member is part of the group (common case), return true.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
        // if not, recursively search depth first in the group looking for the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
        // principal.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
        //
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
        if (groupMembers.contains(member)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
            return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
        } else {
7970
af1579474d16 7008728: diamond conversion of basic security, permissions, authentication
smarks
parents: 5506
diff changeset
   134
            Vector<Group> alreadySeen = new Vector<>(10);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
            return isMemberRecurse(member, alreadySeen);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
     * return the name of the principal.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
    public String getName() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
        return group;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
    //
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
    // This function is the recursive search of groups for this
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
    // implementation of the Group. The search proceeds building up
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
    // a vector of already seen groups. Only new groups are considered,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
    // thereby avoiding loops.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
    //
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
    boolean isMemberRecurse(Principal member, Vector<Group> alreadySeen) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
        Enumeration<? extends Principal> e = members();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
        while (e.hasMoreElements()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
            boolean mem = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
            Principal p = (Principal) e.nextElement();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
            // if the member is in this collection, return true
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
            if (p.equals(member)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
                return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
            } else if (p instanceof GroupImpl) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
                //
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
                // if not recurse if the group has not been checked already.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
                // Can call method in this package only if the object is an
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
                // instance of this class. Otherwise call the method defined
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
                // in the interface. (This can lead to a loop if a mixture of
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
                // implementations form a loop, but we live with this improbable
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
                // case rather than clutter the interface by forcing the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
                // implementation of this method.)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
                //
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
                GroupImpl g = (GroupImpl) p;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
                alreadySeen.addElement(this);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
                if (!alreadySeen.contains(g))
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
                  mem =  g.isMemberRecurse(member, alreadySeen);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
            } else if (p instanceof Group) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
                Group g = (Group) p;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
                if (!alreadySeen.contains(g))
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
                  mem = g.isMember(member);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
            if (mem)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
              return mem;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   184
        return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   185
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   186
}