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.test.xml.mbeanserver;
023:
024: import java.util.HashMap;
025: import java.util.ArrayList;
026: import java.util.Enumeration;
027: import java.util.Iterator;
028: import java.security.acl.Group;
029: import java.security.Principal;
030:
031: import org.jboss.security.SimpleGroup;
032: import org.jboss.security.SimplePrincipal;
033:
034: /**
035: * The root object of the user-roles.xsd schema
036: *
037: * @author Scott.Stark@jboss.org
038: * @version $Revision: 57211 $
039: */
040: public class Users {
041: private HashMap users = new HashMap();
042:
043: public static class User implements Comparable {
044: private String name;
045: private String password;
046: private String encoding;
047: private HashMap roleGroups = new HashMap();
048:
049: public User() {
050: }
051:
052: public User(String name) {
053: this .name = name;
054: }
055:
056: public String getName() {
057: return name;
058: }
059:
060: public String getPassword() {
061: return password;
062: }
063:
064: public void setPassword(String password) {
065: this .password = password;
066: }
067:
068: public String getEncoding() {
069: return encoding;
070: }
071:
072: public void setEncoding(String encoding) {
073: this .encoding = encoding;
074: }
075:
076: public Group[] getRoleSets() {
077: Group[] roleSets = new Group[roleGroups.size()];
078: roleGroups.values().toArray(roleSets);
079: return roleSets;
080: }
081:
082: public String[] getRoleNames() {
083: return getRoleNames("Roles");
084: }
085:
086: public String[] getRoleNames(String roleGroup) {
087: Group group = (Group) roleGroups.get(roleGroup);
088: String[] names = {};
089: if (group != null) {
090: ArrayList tmp = new ArrayList();
091: Enumeration iter = group.members();
092: while (iter.hasMoreElements()) {
093: Principal p = (Principal) iter.nextElement();
094: tmp.add(p.getName());
095: }
096: names = new String[tmp.size()];
097: tmp.toArray(names);
098: }
099: return names;
100: }
101:
102: public void addRole(String roleName, String roleGroup) {
103: Group group = (Group) roleGroups.get(roleGroup);
104: if (group == null) {
105: group = new SimpleGroup(roleGroup);
106: roleGroups.put(roleGroup, group);
107: }
108: SimplePrincipal role = new SimplePrincipal(roleName);
109: group.addMember(role);
110: }
111:
112: public int compareTo(Object obj) {
113: Users.User u = (Users.User) obj;
114: return name.compareTo(u.name);
115: }
116:
117: public String toString() {
118: return "User{" + "name='" + name + "'" + ", password=*"
119: + ", encoding='" + encoding + "'" + ", roleGroups="
120: + roleGroups + "}";
121: }
122: }
123:
124: public void addUser(Users.User user) {
125: users.put(user.getName(), user);
126: }
127:
128: public Iterator getUsers() {
129: return users.values().iterator();
130: }
131:
132: public Users.User getUser(String name) {
133: Users.User find = (Users.User) users.get(name);
134: return find;
135: }
136:
137: public String toString() {
138: return "Users(" + System.identityHashCode(this ) + "){"
139: + "users=" + users + "}";
140: }
141: }
|