001: /**
002: * JOnAS: Java(TM) Open Application Server
003: * Copyright (C) 1999-2004 Bull S.A.
004: * Contact: jonas-team@objectweb.org
005: *
006: * This library is free software; you can redistribute it and/or
007: * modify it under the terms of the GNU Lesser General Public
008: * License as published by the Free Software Foundation; either
009: * version 2.1 of the License, or 1any later version.
010: *
011: * This library is distributed in the hope that it will be useful,
012: * but WITHOUT ANY WARRANTY; without even the implied warranty of
013: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
014: * Lesser General Public License for more details.
015: *
016: * You should have received a copy of the GNU Lesser General Public
017: * License along with this library; if not, write to the Free Software
018: * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
019: * USA
020: *
021: * Initial developer: Florent BENOIT
022: * --------------------------------------------------------------------------
023: * $Id: JGroup.java 5063 2004-07-04 19:35:33Z benoitf $
024: * --------------------------------------------------------------------------
025: */package org.objectweb.jonas.security.auth;
026:
027: import java.io.Serializable;
028: import java.security.Principal;
029: import java.security.acl.Group;
030: import java.util.Enumeration;
031: import java.util.Vector;
032:
033: /**
034: * This class represents all the roles of a principal (group of principals).
035: * These roles are added to the Subject
036: * @author Florent Benoit
037: */
038: public class JGroup implements Group, Serializable {
039:
040: /**
041: * Name of this group
042: */
043: private String name = null;
044:
045: /**
046: * Members of this group
047: */
048: private Vector members = null;
049:
050: /**
051: * Build a new group with the following name
052: * @param name name of the group
053: */
054: public JGroup(String name) {
055: this .name = name;
056: this .members = new Vector();
057:
058: }
059:
060: /**
061: * Compares this principal to the specified object. Returns true if the object passed in matches the principal represented by the implementation of this interface.
062: * @param another principal to compare with.
063: * @return true if the principal passed in is the same as that encapsulated by this principal, and false otherwise.
064: */
065: public boolean equals(Object another) {
066: if (!(another instanceof Group)) {
067: return false;
068: }
069: // else
070: return name.equals(((Group) another).getName());
071: }
072:
073: /**
074: * Returns a string representation of this principal.
075: * @return a string representation of this principal.
076: */
077: public String toString() {
078: return "Principal[" + name + "]";
079: }
080:
081: /**
082: * Returns a hashcode for this principal.
083: * @return a hashcode for this principal.
084: */
085: public int hashCode() {
086: return name.hashCode();
087: }
088:
089: /**
090: * Returns the name of this principal.
091: * @return the name of this principal.
092: */
093: public String getName() {
094: return name;
095: }
096:
097: /**
098: * Adds the specified member to the group.
099: * @param user the principal to add to this group.
100: * @return true if the member was successfully added, false if the principal was already a member.
101: */
102: public boolean addMember(Principal user) {
103: if (isMember(user)) {
104: return false;
105: }
106: // else
107: members.add(user);
108: return true;
109: }
110:
111: /**
112: * Removes the specified member from the group.
113: * @param user the principal to remove from this group.
114: * @return true if the principal was removed, or false if the principal was not a member.
115: */
116: public boolean removeMember(Principal user) {
117: if (!isMember(user)) {
118: return false;
119: }
120: // else
121: members.remove(user);
122: return true;
123: }
124:
125: /**
126: * Returns true if the passed principal is a member of the group. This method does a recursive search, so if a principal belongs to a group which is a member of this group, true is returned.
127: * @param member the principal whose membership is to be checked.
128: * @return true if the principal is a member of this group, false otherwise.
129: */
130: public boolean isMember(Principal member) {
131: return members.contains(member);
132: }
133:
134: /**
135: * Returns an enumeration of the members in the group. The returned objects can be instances of either Principal or Group (which is a subclass of Principal).
136: * @return an enumeration of the group members.
137: */
138: public Enumeration members() {
139: return members.elements();
140: }
141:
142: }
|