1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22 package org.apache.james.userrepository;
23
24 import org.apache.avalon.cornerstone.services.store.ObjectRepository;
25 import org.apache.avalon.cornerstone.services.store.Store;
26 import org.apache.avalon.framework.activity.Initializable;
27 import org.apache.avalon.framework.configuration.Configurable;
28 import org.apache.avalon.framework.configuration.Configuration;
29 import org.apache.avalon.framework.configuration.ConfigurationException;
30 import org.apache.avalon.framework.configuration.DefaultConfiguration;
31 import org.apache.avalon.framework.service.ServiceException;
32 import org.apache.avalon.framework.service.ServiceManager;
33 import org.apache.avalon.framework.service.Serviceable;
34 import org.apache.james.api.user.User;
35 import org.apache.james.impl.jamesuser.AbstractUsersRepository;
36 import org.apache.james.impl.user.DefaultJamesUser;
37
38
39 import java.util.Iterator;
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54 public class UsersFileRepository
55 extends AbstractUsersRepository
56 implements Configurable, Serviceable, Initializable {
57
58
59
60
61 protected static boolean DEEP_DEBUG = false;
62
63 private Store store;
64 private ObjectRepository objectRepository;
65 private static String urlSeparator = "/";
66
67
68
69
70 private String destination;
71
72
73
74
75
76
77 public void setStore(Store store) {
78 this.store = store;
79 }
80
81
82
83
84 public void service( final ServiceManager componentManager )
85 throws ServiceException {
86
87 try {
88 setStore((Store)componentManager.lookup( Store.ROLE ));
89 } catch (Exception e) {
90 final String message = "Failed to retrieve Store component:" + e.getMessage();
91 getLogger().error( message, e );
92 throw new ServiceException ("", message, e );
93 }
94 }
95
96
97
98
99 public void configure( final Configuration configuration )
100 throws ConfigurationException {
101 super.configure(configuration);
102 destination = configuration.getChild( "destination" ).getAttribute( "URL" );
103
104 if (!destination.endsWith(urlSeparator)) {
105 destination += urlSeparator;
106 }
107 }
108
109
110
111
112 public void initialize()
113 throws Exception {
114
115 try {
116
117 final DefaultConfiguration objectConfiguration
118 = new DefaultConfiguration( "repository",
119 "generated:UsersFileRepository.compose()" );
120
121 objectConfiguration.setAttribute( "destinationURL", destination );
122 objectConfiguration.setAttribute( "type", "OBJECT" );
123 objectConfiguration.setAttribute( "model", "SYNCHRONOUS" );
124
125 objectRepository = (ObjectRepository)store.select( objectConfiguration );
126 if (getLogger().isDebugEnabled()) {
127 StringBuffer logBuffer =
128 new StringBuffer(192)
129 .append(this.getClass().getName())
130 .append(" created in ")
131 .append(destination);
132 getLogger().debug(logBuffer.toString());
133 }
134 } catch (Exception e) {
135 if (getLogger().isErrorEnabled()) {
136 getLogger().error("Failed to initialize repository:" + e.getMessage(), e );
137 }
138 throw e;
139 }
140 }
141
142
143
144
145 public Iterator list() {
146 return objectRepository.list();
147 }
148
149
150
151
152 protected void doAddUser(User user) {
153 try {
154 objectRepository.put(user.getUserName(), user);
155 } catch (Exception e) {
156 throw new RuntimeException("Exception caught while storing user: " + e );
157 }
158 }
159
160
161
162
163 public boolean addUser(String username, String password) {
164 User newbie = new DefaultJamesUser(username, "SHA");
165 newbie.setPassword(password);
166 return addUser(newbie);
167 }
168
169
170
171
172 public synchronized User getUserByName(String name) {
173 if (ignoreCase) {
174 name = getRealName(name);
175 if (name == null ) {
176 return null;
177 }
178 }
179 if (contains(name)) {
180 try {
181 return (User)objectRepository.get(name);
182 } catch (Exception e) {
183 throw new RuntimeException("Exception while retrieving user: "
184 + e.getMessage());
185 }
186 } else {
187 return null;
188 }
189 }
190
191
192
193
194 public User getUserByNameCaseInsensitive(String name) {
195 String realName = getRealName(name, true);
196 if (realName == null ) {
197 return null;
198 }
199 return getUserByName(realName);
200 }
201
202
203
204
205 public String getRealName(String name, boolean ignoreCase) {
206 if (ignoreCase) {
207 Iterator it = list();
208 while (it.hasNext()) {
209 String temp = (String) it.next();
210 if (name.equalsIgnoreCase(temp)) {
211 return temp;
212 }
213 }
214 return null;
215 } else {
216 return objectRepository.containsKey(name) ? name : null;
217 }
218 }
219
220
221
222
223 public String getRealName(String name) {
224 return getRealName(name, ignoreCase);
225 }
226
227
228
229
230 public void doUpdateUser(User user) {
231 try {
232 objectRepository.put(user.getUserName(), user);
233 } catch (Exception e) {
234 throw new RuntimeException("Exception caught while storing user: "
235 + e);
236 }
237 }
238
239
240
241
242 public synchronized void removeUser(String name) {
243 objectRepository.remove(name);
244 }
245
246
247
248
249 public boolean contains(String name) {
250 if (ignoreCase) {
251 return containsCaseInsensitive(name);
252 } else {
253 return objectRepository.containsKey(name);
254 }
255 }
256
257
258
259
260 public boolean containsCaseInsensitive(String name) {
261 Iterator it = list();
262 while (it.hasNext()) {
263 if (name.equalsIgnoreCase((String)it.next())) {
264 return true;
265 }
266 }
267 return false;
268 }
269
270
271
272
273 public boolean test(String name, String password) {
274 User user;
275 try {
276 user = getUserByName(name);
277 if (user == null) return false;
278 } catch (Exception e) {
279 throw new RuntimeException("Exception retrieving User" + e);
280 }
281 return user.verifyPassword(password);
282 }
283
284
285
286
287 public int countUsers() {
288 int count = 0;
289 for (Iterator it = list(); it.hasNext(); it.next()) {
290 count++;
291 }
292 return count;
293 }
294
295 }