001: /*
002: * JBoss, Home of Professional Open Source.
003: * Copyright 2006, Red Hat Middleware LLC, and individual contributors
004: * as indicated by the @author tags. See the copyright.txt file in the
005: * distribution for a full listing of individual contributors.
006: *
007: * This is free software; you can redistribute it and/or modify it
008: * under the terms of the GNU Lesser General Public License as
009: * published by the Free Software Foundation; either version 2.1 of
010: * the License, or (at your option) any later version.
011: *
012: * This software is distributed in the hope that it will be useful,
013: * but WITHOUT ANY WARRANTY; without even the implied warranty of
014: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
015: * Lesser General Public License for more details.
016: *
017: * You should have received a copy of the GNU Lesser General Public
018: * License along with this software; if not, write to the Free
019: * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
020: * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
021: */
022: package org.jboss.security;
023:
024: import java.security.Principal;
025: import java.security.acl.Group;
026: import java.util.Collection;
027: import java.util.Collections;
028: import java.util.Enumeration;
029: import java.util.Iterator;
030: import java.util.HashMap;
031:
032: /** An implementation of Group that manages a collection of Principal
033: objects based on their hashCode() and equals() methods. This class
034: is not thread safe.
035:
036: @author Scott.Stark@jboss.org
037: @version $Revision: 57203 $
038: */
039: public class SimpleGroup extends SimplePrincipal implements Group,
040: Cloneable {
041: /** The serialVersionUID */
042: private static final long serialVersionUID = 6051859639378507247L;
043:
044: private HashMap members;
045:
046: public SimpleGroup(String groupName) {
047: super (groupName);
048: members = new HashMap(3);
049: }
050:
051: /** Adds the specified member to the group.
052: @param user the principal to add to this group.
053: @return true if the member was successfully added,
054: false if the principal was already a member.
055: */
056: public boolean addMember(Principal user) {
057: boolean isMember = members.containsKey(user);
058: if (isMember == false)
059: members.put(user, user);
060: return isMember == false;
061: }
062:
063: /** Returns true if the passed principal is a member of the group.
064: This method does a recursive search, so if a principal belongs to a
065: group which is a member of this group, true is returned.
066:
067: A special check is made to see if the member is an instance of
068: org.jboss.security.AnybodyPrincipal or org.jboss.security.NobodyPrincipal
069: since these classes do not hash to meaningful values.
070: @param member the principal whose membership is to be checked.
071: @return true if the principal is a member of this group,
072: false otherwise.
073: */
074: public boolean isMember(Principal member) {
075: // First see if there is a key with the member name
076: boolean isMember = members.containsKey(member);
077: if (isMember == false) { // Check the AnybodyPrincipal & NobodyPrincipal special cases
078: isMember = (member instanceof org.jboss.security.AnybodyPrincipal);
079: if (isMember == false) {
080: if (member instanceof org.jboss.security.NobodyPrincipal)
081: return false;
082: }
083: }
084: if (isMember == false) { // Check any Groups for membership
085: Collection values = members.values();
086: Iterator iter = values.iterator();
087: while (isMember == false && iter.hasNext()) {
088: Object next = iter.next();
089: if (next instanceof Group) {
090: Group group = (Group) next;
091: isMember = group.isMember(member);
092: }
093: }
094: }
095: return isMember;
096: }
097:
098: /** Returns an enumeration of the members in the group.
099: The returned objects can be instances of either Principal
100: or Group (which is a subinterface of Principal).
101: @return an enumeration of the group members.
102: */
103: public Enumeration members() {
104: return Collections.enumeration(members.values());
105: }
106:
107: /** Removes the specified member from the group.
108: @param user the principal to remove from this group.
109: @return true if the principal was removed, or
110: false if the principal was not a member.
111: */
112: public boolean removeMember(Principal user) {
113: Object prev = members.remove(user);
114: return prev != null;
115: }
116:
117: public String toString() {
118: StringBuffer tmp = new StringBuffer(getName());
119: tmp.append("(members:");
120: Iterator iter = members.keySet().iterator();
121: while (iter.hasNext()) {
122: tmp.append(iter.next());
123: tmp.append(',');
124: }
125: tmp.setCharAt(tmp.length() - 1, ')');
126: return tmp.toString();
127: }
128:
129: public synchronized Object clone()
130: throws CloneNotSupportedException {
131: SimpleGroup clone = (SimpleGroup) super .clone();
132: if (clone != null)
133: clone.members = (HashMap) this.members.clone();
134: return clone;
135: }
136: }
|