KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jboss > security > auth > spi > Users


1 /*
2 * JBoss, the OpenSource J2EE webOS
3 *
4 * Distributable under LGPL license.
5 * See terms of license at gnu.org.
6 */

7 package org.jboss.security.auth.spi;
8
9 import java.util.Iterator JavaDoc;
10 import java.util.HashMap JavaDoc;
11 import java.util.ArrayList JavaDoc;
12 import java.util.Enumeration JavaDoc;
13 import java.security.acl.Group JavaDoc;
14 import java.security.Principal JavaDoc;
15
16 import org.jboss.security.SimpleGroup;
17 import org.jboss.security.SimplePrincipal;
18
19 /**
20  * @author Scott.Stark@jboss.org
21  * @version $Revision: 1.1.2.2 $
22  */

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