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: Group.java 4804 2004-05-25 15:13:29Z benoitf $
024: * --------------------------------------------------------------------------
025: */package org.objectweb.jonas.security.realm.principals;
026:
027: import java.io.Serializable;
028: import java.util.Enumeration;
029: import java.util.StringTokenizer;
030: import java.util.Vector;
031:
032: import org.objectweb.jonas.security.realm.lib.XML;
033:
034: /**
035: * This class define the Group class which represent a group with its associated
036: * roles
037: * @author Florent Benoit
038: */
039:
040: public class Group implements Serializable, GroupMBean {
041:
042: /**
043: * Separator of the roles
044: */
045: protected static final String SEPARATOR = ",";
046:
047: /**
048: * Name of the user
049: */
050: private String name = null;
051:
052: /**
053: * Roles
054: */
055: private Vector roles = new Vector();
056:
057: /**
058: * Description of the role
059: */
060: private String description = null;
061:
062: /**
063: * Default Constructor
064: */
065: public Group() {
066:
067: }
068:
069: /**
070: * Constructor with a given name
071: * @param name the name of this group
072: */
073: public Group(String name) {
074: setName(name);
075: }
076:
077: /**
078: * Set the name of this user
079: * @param name Name of the user
080: */
081: public void setName(String name) {
082: this .name = name;
083: }
084:
085: /**
086: * Get the name of this user
087: * @return the name of this user
088: */
089: public String getName() {
090: return name;
091: }
092:
093: /**
094: * Set the description of this group
095: * @param description description of the group
096: */
097: public void setDescription(String description) {
098: this .description = description;
099: }
100:
101: /**
102: * Get the description of this group
103: * @return the description of this group
104: */
105: public String getDescription() {
106: return description;
107: }
108:
109: /**
110: * Set the roles of the group
111: * @param roles the list of the roles of the group
112: */
113: public void setRoles(String roles) {
114: StringTokenizer st = new StringTokenizer(roles, SEPARATOR);
115: String role = null;
116: while (st.hasMoreTokens()) {
117: role = st.nextToken().trim();
118: addRole(role);
119: }
120: }
121:
122: /**
123: * Add a role to this group
124: * @param role the given role
125: */
126: public void addRole(String role) {
127: if (!roles.contains(role)) {
128: this .roles.addElement(role);
129: }
130: }
131:
132: /**
133: * Remove a role from this group
134: * @param role the given role
135: */
136: public void removeRole(String role) {
137: if (roles.contains(role)) {
138: this .roles.removeElement(role);
139: }
140: }
141:
142: /**
143: * Get the roles
144: * @return the array of the roles
145: */
146: public String getRoles() {
147: String rolesList = "";
148: Enumeration r = roles.elements();
149: int nb = 0;
150: String role = null;
151:
152: while (r.hasMoreElements()) {
153: if (nb > 0) {
154: rolesList += ", ";
155: }
156: role = (String) r.nextElement();
157: rolesList += role;
158: }
159: return rolesList;
160: }
161:
162: /**
163: * Get the roles
164: * @return the array of the roles
165: */
166: public String[] getArrayRoles() {
167: return ((String[]) roles.toArray(new String[roles.size()]));
168: }
169:
170: /**
171: * String representation of the group
172: * @return the xml representation of the group
173: */
174: public String toXML() {
175: StringBuffer xml = new StringBuffer("<group name=\"");
176: xml.append(name);
177: xml.append("\" description=\"");
178: if (description != null) {
179: xml.append(description);
180: }
181: xml.append("\"");
182: XML.appendVectorToBuffer("roles=", xml, roles);
183: xml.append(" />");
184: return xml.toString();
185: }
186:
187: /**
188: * Use the XML representation of this object
189: * @return the XML representation of this object
190: */
191: public String toString() {
192: return this.toXML();
193: }
194:
195: }
|