KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > james > userrepository > MockUsersRepository


1 /****************************************************************
2  * Licensed to the Apache Software Foundation (ASF) under one *
3  * or more contributor license agreements. See the NOTICE file *
4  * distributed with this work for additional information *
5  * regarding copyright ownership. The ASF licenses this file *
6  * to you under the Apache License, Version 2.0 (the *
7  * "License"); you may not use this file except in compliance *
8  * with the License. You may obtain a copy of the License at *
9  * *
10  * http://www.apache.org/licenses/LICENSE-2.0 *
11  * *
12  * Unless required by applicable law or agreed to in writing, *
13  * software distributed under the License is distributed on an *
14  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY *
15  * KIND, either express or implied. See the License for the *
16  * specific language governing permissions and limitations *
17  * under the License. *
18  ****************************************************************/

19
20 package org.apache.james.userrepository;
21
22 import org.apache.james.security.DigestUtil;
23 import org.apache.james.services.User;
24
25 import java.security.NoSuchAlgorithmException JavaDoc;
26 import java.util.HashMap JavaDoc;
27 import java.util.Iterator JavaDoc;
28 import java.util.LinkedList JavaDoc;
29 import java.util.List JavaDoc;
30 import java.util.Locale JavaDoc;
31
32 public class MockUsersRepository extends AbstractUsersRepository {
33
34     private final HashMap JavaDoc m_users = new HashMap JavaDoc();
35
36     /**
37      * force the repository to hold implementations of JamesUser interface, instead of User
38      * JamesUser is _not_ required as of the UsersRepository interface, so the necessarity forcing it
39      * is due to code using UsersRepository while at the same time expecting it to hold JamesUsers
40      * (like in RemoteManagerHandler)
41      */

42     private boolean m_forceUseJamesUser = false;
43
44     public void setForceUseJamesUser() {
45         m_forceUseJamesUser = true;
46     }
47
48     public boolean addUser(User user) {
49
50         if (m_forceUseJamesUser && user instanceof DefaultUser ) {
51             DefaultUser aUser = (DefaultUser)user;
52             user = new DefaultJamesUser(aUser.getUserName(),
53                                              aUser.getHashedPassword(),
54                                              aUser.getHashAlgorithm());
55         }
56
57         String JavaDoc key = user.getUserName();
58         if (m_users.containsKey(key)) return false;
59         m_users.put(key, user);
60         return true;
61     }
62
63     public void addUser(String JavaDoc name, Object JavaDoc attributes) {
64         if (!(attributes instanceof String JavaDoc)) {
65             throw new IllegalArgumentException JavaDoc();
66         }
67         try {
68             String JavaDoc passwordHash = DigestUtil.digestString(((String JavaDoc) attributes), "SHA");
69
70             User user;
71
72             if (m_forceUseJamesUser) {
73                 user = new DefaultJamesUser(name, passwordHash, "SHA");
74             } else {
75                 user = new DefaultUser(name, passwordHash, "SHA");
76             }
77            
78             addUser(user);
79         } catch (Exception JavaDoc e) {
80             e.printStackTrace(); // encoding failed
81
}
82     }
83
84     public boolean addUser(String JavaDoc username, String JavaDoc password) {
85         if (m_users.containsKey(username)) return false;
86         try {
87             String JavaDoc passwordHash = DigestUtil.digestString((password), "SHA");
88
89             User user;
90
91             if (m_forceUseJamesUser) {
92                 user = new DefaultJamesUser(username, passwordHash, "SHA");
93             } else {
94                 user = new DefaultUser(username, passwordHash, "SHA");
95             }
96            
97             return addUser(user);
98         } catch (NoSuchAlgorithmException JavaDoc e) {
99             e.printStackTrace(); // encoding failed
100
}
101         return false;
102     }
103
104     public Object JavaDoc getAttributes(String JavaDoc name) {
105         return null; // trivial implementation
106
}
107
108     public User getUserByName(String JavaDoc name) {
109         if (ignoreCase) {
110             return getUserByNameCaseInsensitive(name);
111         } else {
112             return (User) m_users.get(name);
113         }
114     }
115
116     public User getUserByNameCaseInsensitive(String JavaDoc name) {
117         return (User) m_users.get(name.toLowerCase(Locale.US));
118     }
119
120     public String JavaDoc getRealName(String JavaDoc name) {
121         if (ignoreCase) {
122             return m_users.get(name.toLowerCase(Locale.US)) != null ? ((User) m_users.get(name.toLowerCase(Locale.US))).getUserName() : null;
123         } else {
124             return m_users.get(name) != null ? name : null;
125         }
126     }
127
128     public boolean updateUser(User user) {
129         if (m_users.containsKey(user.getUserName())) {
130             m_users.put(user.getUserName(), user);
131             return true;
132         } else return false; // trivial implementation
133
}
134
135     public void removeUser(String JavaDoc name) {
136         m_users.remove(name);
137     }
138
139     public boolean contains(String JavaDoc name) {
140         if (ignoreCase) {
141             return containsCaseInsensitive(name);
142         } else {
143             return m_users.containsKey(name);
144         }
145     }
146
147     public boolean containsCaseInsensitive(String JavaDoc name) {
148         throw new UnsupportedOperationException JavaDoc("mock");
149     }
150
151     public boolean test(String JavaDoc name, Object JavaDoc attributes) {
152         throw new UnsupportedOperationException JavaDoc("mock");
153     }
154
155     public boolean test(String JavaDoc name, String JavaDoc password) {
156         User user = getUserByName(name);
157         if (user == null) return false;
158         return user.verifyPassword(password);
159     }
160
161     public int countUsers() {
162         return m_users.size();
163     }
164
165     protected List JavaDoc listUserNames() {
166         Iterator JavaDoc users = m_users.values().iterator();
167         List JavaDoc userNames = new LinkedList JavaDoc();
168         while ( users.hasNext() ) {
169             User user = (User)users.next();
170             userNames.add(user.getUserName());
171         }
172
173         return userNames;
174     }
175     public Iterator JavaDoc list() {
176         return listUserNames().iterator();
177     }
178
179     protected void doAddUser(User user) {
180         // unused
181
}
182
183     protected void doUpdateUser(User user) {
184         // unused
185
}
186 }
187
Popular Tags