KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > jcorporate > expresso > core > security > MapBasedUserInfo


1 package com.jcorporate.expresso.core.security;
2
3 import com.jcorporate.expresso.core.db.DBException;
4 import com.jcorporate.expresso.core.misc.ConfigManager;
5 import com.jcorporate.expresso.core.misc.Base64;
6
7 import java.util.Enumeration JavaDoc;
8 import java.util.HashMap JavaDoc;
9 import java.util.Map JavaDoc;
10 import java.util.Vector JavaDoc;
11
12 /**
13  * A User Info implementation that is Map based instead of database object
14  * based. Although this isn't usually used in Expresso, a few cases, where
15  * we need a SuperUser, for example when database access doesn't exist. It is
16  * not considered something to use on a broad basis, more as a workaround
17  * for some special startup conditions.
18  *
19  * @author Michael Rimov
20  */

21 public class MapBasedUserInfo implements UserInfo {
22     /**
23      * Map of a map of user info objects.
24      */

25     private static Map JavaDoc dataContexts;
26
27
28     /**
29      * Current data context.
30      */

31     private String JavaDoc dataContext;
32
33     private String JavaDoc email;
34
35     private String JavaDoc emailAuthCode;
36
37     private String JavaDoc emailValCode;
38
39     private String JavaDoc accountStatus;
40
41     private String JavaDoc createDate;
42
43     private String JavaDoc loginName;
44
45     private String JavaDoc password;
46
47     private String JavaDoc primaryGroup;
48
49     private boolean regComplete;
50
51     private String JavaDoc registrationDomain;
52
53     private int uid;
54
55     private String JavaDoc updateDate;
56
57     static {
58         synchronized (MapBasedUserInfo.class) {
59             dataContexts = new HashMap JavaDoc();
60             for (Enumeration JavaDoc e = ConfigManager.getAllConfigKeys();
61                  e.hasMoreElements();) {
62                 String JavaDoc contextName = (String JavaDoc) e.nextElement();
63                 dataContexts.put(contextName, new HashMap JavaDoc());
64             }
65         }
66     }
67
68     /**
69      * Default constructor.
70      */

71     public MapBasedUserInfo() {
72     }
73
74     /**
75      * @throws DBException If the add fails
76      * @todo Implement this com.jcorporate.expresso.core.security.UserInfo
77      * method
78      */

79     public synchronized void add()
80             throws DBException {
81         synchronized (MapBasedUserInfo.class) {
82             Map JavaDoc contextMap = (Map JavaDoc) dataContexts.get(this.getDataContext());
83             contextMap.put(new Integer JavaDoc(this.getUid()), this);
84         }
85     }
86
87     /**
88      * @throws DBException If the delete fails
89      * @todo Implement this com.jcorporate.expresso.core.security.UserInfo
90      * method
91      */

92     public synchronized void delete()
93             throws DBException {
94         synchronized (MapBasedUserInfo.class) {
95             Map JavaDoc contextMap = (Map JavaDoc) dataContexts.get(this.getDataContext());
96             contextMap.remove(new Integer JavaDoc(this.getUid()));
97         }
98     }
99
100     /**
101      * @return true if the user is found
102      * @throws DBException If the find fails
103      * @todo Implement this com.jcorporate.expresso.core.security.UserInfo
104      * method
105      */

106     public synchronized boolean find()
107             throws DBException {
108         return false;
109     }
110
111     /**
112      * Retrieve the current account status.
113      *
114      * @return java.lang.String
115      * @throws DBException If the find fails
116      * @todo Implement this com.jcorporate.expresso.core.security.UserInfo
117      * method
118      */

119     public synchronized String JavaDoc getAccountStatus()
120             throws DBException {
121         return accountStatus;
122     }
123
124     /**
125      * Unsupported operation
126      *
127      * @return java.util.Vector
128      * @throws DBException If there is an error during the retrieval
129      */

130     public Vector JavaDoc getAllUsers()
131             throws DBException {
132         throw new java.lang.UnsupportedOperationException JavaDoc("Not Supported");
133     }
134
135     /**
136      * @return java.lang.String
137      * @throws DBException If there is an error during the retrieval
138      * @todo Implement this com.jcorporate.expresso.core.security.UserInfo
139      * method
140      */

141     public synchronized String JavaDoc getCreateDate()
142             throws DBException {
143         return createDate;
144     }
145
146     /**
147      * @return String
148      * @todo Implement this com.jcorporate.expresso.core.security.UserInfo
149      * method
150      */

151     public synchronized String JavaDoc getDBName() {
152         if (dataContext == null) {
153             return "default";
154         }
155         return dataContext;
156     }
157
158     /**
159      * @return String
160      * @todo Implement this com.jcorporate.expresso.core.security.UserInfo
161      * method
162      */

163     public synchronized String JavaDoc getDataContext() {
164         if (dataContext == null) {
165             return "default";
166         }
167         return dataContext;
168     }
169
170     /**
171      * @return java.lang.String
172      * @throws DBException If there is an error during the retrieval
173      * @todo Implement this com.jcorporate.expresso.core.security.UserInfo
174      * method
175      */

176     public synchronized String JavaDoc getEmail()
177             throws DBException {
178         return email;
179     }
180
181     /**
182      * @return java.lang.String
183      * @throws DBException If there is an error during the retrieval
184      * @todo Implement this com.jcorporate.expresso.core.security.UserInfo
185      * method
186      */

187     public synchronized String JavaDoc getEmailAuthCode()
188             throws DBException {
189         return emailAuthCode;
190     }
191
192     /**
193      * @return java.lang.String
194      * @throws DBException If the find fails
195      * @todo Implement this com.jcorporate.expresso.core.security.UserInfo
196      * method
197      */

198     public synchronized String JavaDoc getEmailValCode()
199             throws DBException {
200         return emailValCode;
201     }
202
203     /**
204      * Not Implemented
205      *
206      * @param fieldName The field to retrieve
207      * @return Vector Group names that this user belongs to
208      * @throws DBException If an error occurs when the group info is read
209      * @throws java.lang.UnsupportedOperationException
210      * This is not Implemented
211      * @deprecated Use the direct getLoginName, getEmail, getPassword, etc.
212      */

213     public String JavaDoc getField(String JavaDoc fieldName)
214             throws DBException {
215         throw new java.lang.UnsupportedOperationException JavaDoc("Not Supported");
216     }
217
218     /**
219      * Currently unsupported. Returns a blank Vector
220      *
221      * @return Vector Group names that this user belongs to
222      * @throws DBException If an error occurs when the group info is read
223      */

224     public Vector JavaDoc getGroups()
225             throws DBException {
226         return new Vector JavaDoc();
227     }
228
229     /**
230      * @return java.lang.String
231      * @throws DBException If there is an error during the retrieval
232      * @todo Implement this com.jcorporate.expresso.core.security.UserInfo
233      * method
234      */

235     public synchronized String JavaDoc getLoginName()
236             throws DBException {
237         return loginName;
238     }
239
240     /**
241      * @return java.lang.String
242      * @throws DBException If there is an error during the retrieval
243      * @todo Implement this com.jcorporate.expresso.core.security.UserInfo
244      * method
245      */

246     public synchronized String JavaDoc getPassword()
247             throws DBException {
248         return password;
249     }
250
251     /**
252      * @return name of the primary group of this user; null if no group is
253      * found
254      * @todo Implement this com.jcorporate.expresso.core.security.UserInfo
255      * method
256      */

257     public synchronized String JavaDoc getPrimaryGroup()
258             throws DBException {
259         return primaryGroup;
260     }
261
262     /**
263      * this returns an appropriately hashed password.
264      * @param password to be hashed
265      * @return appropriately hashed password.
266      */

267     public String JavaDoc hashEncodePassword(String JavaDoc password) throws DBException {
268         if (password == null) {
269             throw new DBException("Password Must not be NULL");
270         }
271         if (password.length() == 0) {
272             return password;
273         }
274         try {
275             return Base64.encode(CryptoManager.getInstance().getStringHash().produceHash(password.getBytes()));
276         } catch (Exception JavaDoc ex) {
277             throw new DBException("Error hashing Password:" +
278                     " You may not have installed the" +
279                     " Cryptography Extensions Properly:", ex);
280         }
281     }
282
283     /**
284      * @return java.lang.String
285      * @throws DBException If there is an error during the retrieval
286      * @todo Implement this com.jcorporate.expresso.core.security.UserInfo
287      * method
288      */

289     public synchronized boolean getRegComplete()
290             throws DBException {
291         return regComplete;
292     }
293
294     /**
295      * @return java.lang.String
296      * @throws DBException If the underlying User implementation throws the
297      * same
298      * @todo Implement this com.jcorporate.expresso.core.security.UserInfo
299      * method
300      */

301     public synchronized String JavaDoc getRegistrationDomain()
302             throws DBException {
303         return registrationDomain;
304     }
305
306     /**
307      * @return java.lang.String
308      * @throws DBException If there is an error during the retrieval
309      * @todo Implement this com.jcorporate.expresso.core.security.UserInfo
310      * method
311      */

312     public synchronized int getUid()
313             throws DBException {
314         return uid;
315     }
316
317     /**
318      * @return java.lang.String
319      * @throws DBException If there is an error during the retrieval
320      * @todo Implement this com.jcorporate.expresso.core.security.UserInfo
321      * method
322      */

323     public synchronized String JavaDoc getUpdateDate()
324             throws DBException {
325         return updateDate;
326     }
327
328     /**
329      * @return java.lang.String
330      * @throws DBException If there is an error during the retrieval
331      * @todo Implement this com.jcorporate.expresso.core.security.UserInfo
332      * method
333      */

334     public synchronized String JavaDoc getUserName()
335             throws DBException {
336         return loginName;
337     }
338
339     /**
340      * Unsupported Operation
341      *
342      * @return A ValidValue vector describing what can be looked up.
343      */

344     public Vector JavaDoc getValues()
345             throws DBException {
346         return null;
347     }
348
349     /**
350      * Checks if the given password equals what we have on file.
351      *
352      * @param testPassword The string to test if it's a correct password
353      * @return true if the testPassword equals the password on file.
354      * @throws DBException If an error occurs when the group info is read
355      * @todo Implement this com.jcorporate.expresso.core.security.UserInfo
356      * method
357      */

358     public synchronized boolean passwordEquals(String JavaDoc testPassword)
359             throws DBException {
360         if (password == null) {
361             if (testPassword == null) {
362                 return true;
363             } else {
364                 return true;
365             }
366         }
367         return password.equals(testPassword);
368     }
369
370     /**
371      * @throws DBException If the retrieve fails
372      */

373     public synchronized void retrieve()
374             throws DBException {
375         MapBasedUserInfo returnValue = null;
376         synchronized (MapBasedUserInfo.class) {
377             Map JavaDoc contextMap = (Map JavaDoc) dataContexts.get(this.getDataContext());
378             returnValue = (MapBasedUserInfo) contextMap.get(new Integer JavaDoc(this.getUid()));
379         }
380
381         if (returnValue == null) {
382             throw new DBException("Unable to locate uid: " + this.getUid());
383         }
384
385         this.setEmail(returnValue.getEmail());
386         this.setAccountStatus(returnValue.getAccountStatus());
387         this.setEmailValCode(this.getEmailValCode());
388         this.setLoginName(this.getLoginName());
389         this.setPassword(this.getPassword());
390         this.setRegComplete(this.getRegComplete());
391         this.setRegistrationDomain(this.getRegistrationDomain());
392     }
393
394     /**
395      * Not Implemented
396      */

397     public void sendAuthEmail()
398             throws DBException {
399         throw new java.lang.UnsupportedOperationException JavaDoc("Not supported");
400     }
401
402     /**
403      * Not Implemented
404      */

405     public void sendFollowUpEmail()
406             throws DBException {
407         throw new java.lang.UnsupportedOperationException JavaDoc("Not supported");
408     }
409
410     /**
411      * @param accountStatus java.lang.String
412      * @throws DBException If there is an error
413      * @todo Implement this com.jcorporate.expresso.core.security.UserInfo
414      * method
415      */

416     public synchronized void setAccountStatus(String JavaDoc accountStatus)
417             throws DBException {
418         this.accountStatus = accountStatus;
419     }
420
421     /**
422      * @param newDBName java.lang.String
423      * @throws DBException If there is an error
424      * @todo Implement this com.jcorporate.expresso.core.security.UserInfo
425      * method
426      */

427     public synchronized void setDBName(String JavaDoc newDBName)
428             throws DBException {
429         dataContext = newDBName;
430     }
431
432     /**
433      * @param email java.lang.String
434      * @throws DBException If there is an error
435      * @todo Implement this com.jcorporate.expresso.core.security.UserInfo
436      * method
437      */

438     public synchronized void setEmail(String JavaDoc email)
439             throws DBException {
440         this.email = email;
441     }
442
443     /**
444      * Sets the code required for auth.
445      *
446      * @param code java.lang.String
447      * @throws DBException If there is an error
448      * @todo Implement this com.jcorporate.expresso.core.security.UserInfo
449      * method
450      */

451     public synchronized void setEmailValCode(String JavaDoc code)
452             throws DBException {
453         this.emailValCode = code;
454     }
455
456     /**
457      * @param loginName java.lang.String
458      * @throws DBException If there is an error
459      * @todo Implement this com.jcorporate.expresso.core.security.UserInfo
460      * method
461      */

462     public synchronized void setLoginName(String JavaDoc loginName)
463             throws DBException {
464         this.loginName = loginName;
465     }
466
467     /**
468      * @param password java.lang.String
469      * @throws DBException If there is an error
470      * @todo Implement this com.jcorporate.expresso.core.security.UserInfo
471      * method
472      */

473     public synchronized void setPassword(String JavaDoc password)
474             throws DBException {
475         this.password = password;
476     }
477
478     /**
479      * @param status java.lang.String
480      * @throws DBException If there is an error
481      * @todo Implement this com.jcorporate.expresso.core.security.UserInfo
482      * method
483      */

484     public synchronized void setRegComplete(boolean status)
485             throws DBException {
486         this.regComplete = status;
487     }
488
489     /**
490      * @param id java.lang.String
491      * @throws DBException If there is an error
492      * @todo Implement this com.jcorporate.expresso.core.security.UserInfo
493      * method
494      */

495     public synchronized void setRegistrationDomain(String JavaDoc id)
496             throws DBException {
497         this.registrationDomain = id;
498     }
499
500     /**
501      * @param uid The uid of the user
502      * @throws DBException If there is an error
503      * @todo Implement this com.jcorporate.expresso.core.security.UserInfo
504      * method
505      */

506     public synchronized void setUid(int uid)
507             throws DBException {
508         this.uid = uid;
509     }
510
511     /**
512      * @param name java.lang.String
513      * @throws DBException If there is an error
514      * @todo Implement this com.jcorporate.expresso.core.security.UserInfo
515      * method
516      */

517     public synchronized void setUserName(String JavaDoc name)
518             throws DBException {
519         this.loginName = name;
520     }
521
522     /**
523      * There should be no copying and detaching from the datasource. This
524      * does nothing.
525      *
526      * @throws DBException If the add fails
527      */

528     public void update()
529             throws DBException {
530         return;
531     }
532 }
533
Popular Tags