00001 00027 package org.objectweb.jonas.security.realm.principals; 00028 00029 import java.io.Serializable; 00030 import java.util.Enumeration; 00031 import java.util.StringTokenizer; 00032 import java.util.Vector; 00033 00034 import org.objectweb.jonas.security.realm.lib.XML; 00035 00042 public class Group implements Serializable, GroupMBean { 00043 00047 protected static final String SEPARATOR = ","; 00048 00052 private String name = null; 00053 00057 private Vector roles = new Vector(); 00058 00062 private String description = null; 00063 00067 public Group() { 00068 00069 } 00070 00075 public Group(String name) { 00076 setName(name); 00077 } 00078 00083 public void setName(String name) { 00084 this.name = name; 00085 } 00086 00091 public String getName() { 00092 return name; 00093 } 00094 00099 public void setDescription(String description) { 00100 this.description = description; 00101 } 00102 00107 public String getDescription() { 00108 return description; 00109 } 00110 00115 public void setRoles(String roles) { 00116 StringTokenizer st = new StringTokenizer(roles, SEPARATOR); 00117 String role = null; 00118 while (st.hasMoreTokens()) { 00119 role = st.nextToken().trim(); 00120 addRole(role); 00121 } 00122 } 00123 00128 public void addRole(String role) { 00129 if (!roles.contains(role)) { 00130 this.roles.addElement(role); 00131 } 00132 } 00133 00138 public void removeRole(String role) { 00139 if (roles.contains(role)) { 00140 this.roles.removeElement(role); 00141 } 00142 } 00143 00148 public String getRoles() { 00149 String rolesList = ""; 00150 Enumeration r = roles.elements(); 00151 int nb = 0; 00152 String role = null; 00153 00154 while (r.hasMoreElements()) { 00155 if (nb > 0) { 00156 rolesList += ", "; 00157 } 00158 role = (String) r.nextElement(); 00159 rolesList += role; 00160 } 00161 return rolesList; 00162 } 00163 00168 public String[] getArrayRoles() { 00169 return ((String[]) roles.toArray(new String[roles.size()])); 00170 } 00171 00176 public String toXML() { 00177 StringBuffer xml = new StringBuffer("<group name=\""); 00178 xml.append(name); 00179 xml.append("\" description=\""); 00180 if (description != null) { 00181 xml.append(description); 00182 } 00183 xml.append("\""); 00184 XML.appendVectorToBuffer("roles=", xml, roles); 00185 xml.append(" />"); 00186 return xml.toString(); 00187 } 00188 00193 public String toString() { 00194 return this.toXML(); 00195 } 00196 00197 }