KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jboss > test > xml > mbeanserver > Users


1 /*
2   * JBoss, Home of Professional Open Source
3   * Copyright 2005, JBoss Inc., and individual contributors as indicated
4   * by the @authors tag. See the copyright.txt in the distribution for a
5   * full listing of individual contributors.
6   *
7   * This is free software; you can redistribute it and/or modify it
8   * under the terms of the GNU Lesser General Public License as
9   * published by the Free Software Foundation; either version 2.1 of
10   * the License, or (at your option) any later version.
11   *
12   * This software is distributed in the hope that it will be useful,
13   * but WITHOUT ANY WARRANTY; without even the implied warranty of
14   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15   * Lesser General Public License for more details.
16   *
17   * You should have received a copy of the GNU Lesser General Public
18   * License along with this software; if not, write to the Free
19   * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
20   * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
21   */

22 package org.jboss.test.xml.mbeanserver;
23
24 import java.util.HashMap JavaDoc;
25 import java.util.ArrayList JavaDoc;
26 import java.util.Enumeration JavaDoc;
27 import java.util.Iterator JavaDoc;
28 import java.security.acl.Group JavaDoc;
29 import java.security.Principal JavaDoc;
30
31 import org.jboss.security.SimpleGroup;
32 import org.jboss.security.SimplePrincipal;
33
34 /**
35  * The root object of the user-roles.xsd schema
36  *
37  * @author Scott.Stark@jboss.org
38  * @version $Revision: 37406 $
39  */

40 public class Users
41 {
42    private HashMap JavaDoc users = new HashMap JavaDoc();
43
44    public static class User implements Comparable JavaDoc
45    {
46       private String JavaDoc name;
47       private String JavaDoc password;
48       private String JavaDoc encoding;
49       private HashMap JavaDoc roleGroups = new HashMap JavaDoc();
50
51       public User()
52       {
53       }
54       public User(String JavaDoc name)
55       {
56          this.name = name;
57       }
58       public String JavaDoc getName()
59       {
60          return name;
61       }
62       public String JavaDoc getPassword()
63       {
64          return password;
65       }
66       public void setPassword(String JavaDoc password)
67       {
68          this.password = password;
69       }
70
71       public String JavaDoc getEncoding()
72       {
73          return encoding;
74       }
75       public void setEncoding(String JavaDoc encoding)
76       {
77          this.encoding = encoding;
78       }
79
80       public Group JavaDoc[] getRoleSets()
81       {
82          Group JavaDoc[] roleSets = new Group JavaDoc[roleGroups.size()];
83          roleGroups.values().toArray(roleSets);
84          return roleSets;
85       }
86       public String JavaDoc[] getRoleNames()
87       {
88          return getRoleNames("Roles");
89       }
90       public String JavaDoc[] getRoleNames(String JavaDoc roleGroup)
91       {
92          Group JavaDoc group = (Group JavaDoc) roleGroups.get(roleGroup);
93          String JavaDoc[] names = {};
94          if( group != null )
95          {
96             ArrayList JavaDoc tmp = new ArrayList JavaDoc();
97             Enumeration JavaDoc iter = group.members();
98             while( iter.hasMoreElements() )
99             {
100                Principal JavaDoc p = (Principal JavaDoc) iter.nextElement();
101                tmp.add(p.getName());
102             }
103             names = new String JavaDoc[tmp.size()];
104             tmp.toArray(names);
105          }
106          return names;
107       }
108       public void addRole(String JavaDoc roleName, String JavaDoc roleGroup)
109       {
110          Group JavaDoc group = (Group JavaDoc) roleGroups.get(roleGroup);
111          if( group == null )
112          {
113             group = new SimpleGroup(roleGroup);
114             roleGroups.put(roleGroup, group);
115          }
116          SimplePrincipal role = new SimplePrincipal(roleName);
117          group.addMember(role);
118       }
119       public int compareTo(Object JavaDoc obj)
120       {
121          Users.User u = (Users.User) obj;
122          return name.compareTo(u.name);
123       }
124
125       public String JavaDoc toString()
126       {
127          return "User{" +
128             "name='" + name + "'" +
129             ", password=*" +
130             ", encoding='" + encoding + "'" +
131             ", roleGroups=" + roleGroups +
132             "}";
133       }
134    }
135
136    public void addUser(Users.User user)
137    {
138       users.put(user.getName(), user);
139    }
140    public Iterator JavaDoc getUsers()
141    {
142       return users.values().iterator();
143    }
144    public Users.User getUser(String JavaDoc name)
145    {
146       Users.User find = (Users.User) users.get(name);
147       return find;
148    }
149    
150    public String JavaDoc toString()
151    {
152       return "Users("+System.identityHashCode(this)+"){" +
153          "users=" + users +
154          "}";
155    }
156 }
157
Popular Tags