KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > jetspeed > om > security > BaseJetspeedUser


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

16 package org.apache.jetspeed.om.security;
17
18 import java.io.ByteArrayOutputStream JavaDoc;
19 import java.io.PrintWriter JavaDoc;
20 import java.util.Date JavaDoc;
21 import java.util.Hashtable JavaDoc;
22 import javax.servlet.http.HttpSessionBindingEvent JavaDoc;
23
24 import org.apache.jetspeed.services.JetspeedUserManagement;
25 import org.apache.jetspeed.services.JetspeedAuthentication;
26 import org.apache.jetspeed.services.logging.JetspeedLogFactoryService;
27 import org.apache.jetspeed.services.logging.JetspeedLogger;
28 import org.apache.jetspeed.services.resources.JetspeedResources;
29
30 import org.apache.turbine.om.security.User;
31 import org.apache.turbine.util.ObjectUtils;
32
33 /**
34  * The default Jetspeed implementation of User interface.
35  *
36  * This basic implementation contains the functionality that is
37  * expected to be common among all User implementations.
38  * You are welcome to extend this class if you wish to have
39  * custom functionality in your user objects (like accessor methods
40  * for custom attributes).
41  *
42  * @author <a HREF="mailto:taylor@apache.org">David Sean Taylor</a>
43  * @author <a HREF="mailto:morciuch@apache.org">Mark Orciuch</a>
44  * @version $Id: BaseJetspeedUser.java,v 1.11 2004/02/23 03:14:12 jford Exp $
45  */

46 public class BaseJetspeedUser /*extends SecurityObject*/ implements JetspeedUser
47 {
48     /** The date on which the user account was created. */
49     private Date JavaDoc createDate = null;
50     /** The date on which the user last accessed the application. */
51     private Date JavaDoc lastAccessDate = null;
52
53     /** This is data that will survive a servlet engine restart. */
54     private Hashtable JavaDoc permStorage = null;
55
56     /** This is data that will not survive a servlet engine restart. */
57     private Hashtable JavaDoc tempStorage = null;
58
59     protected String JavaDoc name = "";
60
61     protected boolean isNew = true;
62
63     
64     /**
65      * Static initialization of the logger for this class
66      */

67     private static final JetspeedLogger logger = JetspeedLogFactoryService.getLogger(BaseJetspeedUser.class.getName());
68     
69     /**
70      * Constructor.
71      * Create a new User and set the createDate.
72      */

73     public BaseJetspeedUser()
74     {
75         createDate = new Date JavaDoc();
76         tempStorage = new Hashtable JavaDoc(10);
77         permStorage = new Hashtable JavaDoc(10);
78         setHasLoggedIn(Boolean.FALSE);
79         setDisabled(false);
80         isNew = true;
81     }
82
83
84     /**
85       * Returns the primary principle for this User, the user id.
86       *
87       * @return the user id.
88       */

89     public String JavaDoc getUserId()
90     {
91         String JavaDoc tmp = null;
92         try
93         {
94             tmp = (String JavaDoc) getPerm (JetspeedUser.USER_ID);
95             if (tmp != null && tmp.length() == 0)
96             {
97                 tmp = null;
98             }
99         }
100         catch (Exception JavaDoc e)
101         {
102             logger.error("getUserId(): " + e.getMessage(), e);
103         }
104         return tmp;
105     }
106
107     public void setUserId(String JavaDoc id)
108     {
109         if (getUserId() == null)
110         {
111             setPerm(JetspeedUser.USER_ID, id);
112         }
113     }
114
115     /**
116      * Gets the access counter for a user during a session.
117      *
118      * @return The access counter for the user for the session.
119      */

120     public int getAccessCounterForSession()
121     {
122         int accessCounter = 0;
123         try
124         {
125             Integer JavaDoc temp = (Integer JavaDoc) getTemp(User.SESSION_ACCESS_COUNTER);
126             if(temp != null)
127             {
128                 accessCounter = temp.intValue();
129             }
130         }
131         catch (Exception JavaDoc e)
132         {
133             logger.debug("getAccessCounterForSession(): " + e.getMessage(), e);
134         }
135         
136         return accessCounter;
137     }
138
139     /**
140      * Gets the access counter for a user from perm storage.
141      *
142      * @return The access counter for the user.
143      */

144     public int getAccessCounter()
145     {
146         int accessCounter = 0;
147         try
148         {
149             Integer JavaDoc temp = (Integer JavaDoc) getPerm(User.ACCESS_COUNTER);
150             if(temp != null)
151             {
152                 accessCounter = temp.intValue();
153             }
154         }
155         catch (Exception JavaDoc e)
156         {
157             logger.debug("getAccessCounter(): " + e.getMessage(), e);
158         }
159         return accessCounter;
160     }
161
162     /**
163      * Gets the create date for this User. This is the time at which
164      * the user object was created.
165      *
166      * @return A Java Date with the date of creation for the user.
167      */

168     public java.util.Date JavaDoc getCreateDate()
169     {
170         return createDate;
171     }
172
173     /**
174      * Gets the last access date for this User. This is the last time
175      * that the user object was referenced.
176      *
177      * @return A Java Date with the last access date for the user.
178      */

179     public java.util.Date JavaDoc getLastAccessDate()
180     {
181         if (lastAccessDate == null)
182         {
183             setLastAccessDate();
184         }
185         return lastAccessDate;
186     }
187
188     /**
189      * Get last login date/time for this user.
190      *
191      * @return A Java Date with the last login date for the user.
192      */

193     public java.util.Date JavaDoc getLastLogin()
194     {
195         return (java.util.Date JavaDoc) getPerm(User.LAST_LOGIN);
196     }
197
198     /**
199      * Get password for this user.
200      *
201      * @return A String with the password for the user.
202      */

203     public String JavaDoc getPassword()
204     {
205         return (String JavaDoc) getPerm(User.PASSWORD);
206     }
207
208     /**
209      * Get an object from permanent storage.
210      *
211      * @param name The object's name.
212      * @return An Object with the given name.
213      */

214     public Object JavaDoc getPerm(String JavaDoc name)
215     {
216         return permStorage.get(name);
217     }
218
219     /**
220      * Get an object from permanent storage; return default if value
221      * is null.
222      *
223      * @param name The object's name.
224      * @param def A default value to return.
225      * @return An Object with the given name.
226      */

227     public Object JavaDoc getPerm(String JavaDoc name, Object JavaDoc def)
228     {
229         try
230         {
231             Object JavaDoc val = permStorage.get (name);
232             return (val == null ? def : val);
233         }
234         catch (Exception JavaDoc e)
235         {
236             logger.error("getPerm(" + name + "): " + e.getMessage(), e);
237             return def;
238         }
239     }
240
241     /**
242      * This should only be used in the case where we want to save the
243      * data to the database.
244      *
245      * @return A Hashtable.
246      */

247     public Hashtable JavaDoc getPermStorage()
248     {
249         if (this.permStorage == null)
250         {
251             this.permStorage = new Hashtable JavaDoc();
252         }
253         return this.permStorage;
254     }
255
256     /**
257      * Get an object from temporary storage.
258      *
259      * @param name The object's name.
260      * @return An Object with the given name.
261      */

262     public Object JavaDoc getTemp(String JavaDoc name)
263     {
264         return tempStorage.get(name);
265     }
266
267     /**
268      * Get an object from temporary storage; return default if value
269      * is null.
270      *
271      * @param name The object's name.
272      * @param def A default value to return.
273      * @return An Object with the given name.
274      */

275     public Object JavaDoc getTemp(String JavaDoc name, Object JavaDoc def)
276     {
277         Object JavaDoc val;
278         try
279         {
280             val = tempStorage.get(name);
281             if (val == null)
282             {
283                 val = def;
284             }
285         }
286         catch (Exception JavaDoc e)
287         {
288             logger.error("getTemp(" + name + "): " + e.getMessage(), e);
289             val = def;
290         }
291         return val;
292     }
293
294     /**
295      * Returns the username for this user. If this is defined, then
296      * the user is considered logged in.
297      *
298      * @return A String with the username.
299      */

300     public String JavaDoc getUserName()
301     {
302         String JavaDoc tmp = null;
303         try
304         {
305             tmp = (String JavaDoc) getPerm (User.USERNAME);
306             if ( tmp.length() == 0 )
307             {
308                 tmp = null;
309             }
310         }
311         catch (Exception JavaDoc e)
312         {
313             logger.error("getUserName(): " + e.getMessage(), e);
314         }
315         return tmp;
316     }
317
318     /**
319      * Returns the first name for this user. If this is defined, then
320      * the user is considered logged in.
321      *
322      * @return A String with the user's first name.
323      */

324     public String JavaDoc getFirstName()
325     {
326         String JavaDoc tmp = null;
327         try
328         {
329             tmp = (String JavaDoc) getPerm (User.FIRST_NAME);
330             if (tmp.length() == 0)
331             {
332                 tmp = null;
333             }
334         }
335         catch (Exception JavaDoc e)
336         {
337             logger.error("getFirstName(): " + e.getMessage(), e);
338         }
339         return tmp;
340     }
341
342     /**
343      * Returns the last name for this user. If this is defined, then
344      * the user is considered logged in.
345      *
346      * @return A String with the user's last name.
347      */

348     public String JavaDoc getLastName()
349     {
350         String JavaDoc tmp = null;
351         try
352         {
353             tmp = (String JavaDoc) getPerm (User.LAST_NAME);
354             if (tmp.length() == 0)
355                 tmp = null;
356         }
357         catch (Exception JavaDoc e)
358         {
359             logger.error("getLastName(): " + e.getMessage(), e);
360         }
361         return tmp;
362     }
363
364     /**
365      * The user is considered logged in if they have not timed out.
366      *
367      * @return Whether the user has logged in.
368      */

369     public boolean hasLoggedIn()
370     {
371         Boolean JavaDoc loggedIn = getHasLoggedIn();
372         return (loggedIn != null && loggedIn.booleanValue());
373     }
374
375     /**
376      * Returns the email address for this user.
377      *
378      * @return A String with the user's email address.
379      */

380     public String JavaDoc getEmail()
381     {
382         return (String JavaDoc)getPerm(User.EMAIL);
383     }
384
385     /**
386      * Increments the permanent hit counter for the user.
387      */

388     public void incrementAccessCounter()
389     {
390         setAccessCounter(getAccessCounter() + 1);
391     }
392
393     /**
394      * Increments the session hit counter for the user.
395      */

396     public void incrementAccessCounterForSession()
397     {
398         setAccessCounterForSession(getAccessCounterForSession() + 1);
399     }
400
401     /**
402      * Remove an object from temporary storage and return the object.
403      *
404      * @param name The name of the object to remove.
405      * @return An Object.
406      */

407     public Object JavaDoc removeTemp(String JavaDoc name)
408     {
409         return tempStorage.remove(name);
410     }
411
412     /**
413      * Sets the access counter for a user, saved in perm storage.
414      *
415      * @param cnt The new count.
416      */

417     public void setAccessCounter(int cnt)
418     {
419         setPerm(User.ACCESS_COUNTER, new Integer JavaDoc(cnt));
420     }
421
422     /**
423      * Sets the session access counter for a user, saved in temp
424      * storage.
425      *
426      * @param cnt The new count.
427      */

428     public void setAccessCounterForSession(int cnt)
429     {
430         setTemp(User.SESSION_ACCESS_COUNTER, new Integer JavaDoc(cnt));
431     }
432
433     /**
434      * Sets the last access date for this User. This is the last time
435      * that the user object was referenced.
436      */

437     public void setLastAccessDate()
438     {
439         lastAccessDate = new java.util.Date JavaDoc();
440     }
441
442     /**
443      * Sets the create date for this User. This is the time at which
444      * the user object was created.
445      *
446      * @param date The create date.
447      */

448     public void setCreateDate(java.util.Date JavaDoc date)
449     {
450         createDate = date;
451     }
452
453     /**
454      * Set last login date/time.
455      *
456      * @param date The last login date.
457      */

458     public void setLastLogin(java.util.Date JavaDoc date)
459     {
460         setPerm(User.LAST_LOGIN, date);
461     }
462
463     /**
464      * Set password.
465      *
466      * @param password The new password.
467      */

468     public void setPassword(String JavaDoc password)
469     {
470          setPerm(User.PASSWORD, password);
471     }
472
473     /**
474      * Put an object into permanent storage. If the value is null,
475      * it will convert that to a "" because the underlying storage
476      * mechanism within TurbineUser is currently a Hashtable and
477      * null is not a valid value.
478      *
479      * @param name The object's name.
480      * @param value The object.
481      */

482     public void setPerm(String JavaDoc name, Object JavaDoc value)
483     {
484         ObjectUtils.safeAddToHashtable(getPermStorage(), name, value);
485     }
486
487     /**
488      * This should only be used in the case where we want to save the
489      * data to the database.
490      *
491      * @param stuff A Hashtable.
492      */

493     public void setPermStorage(Hashtable JavaDoc stuff)
494     {
495         this.permStorage = stuff;
496     }
497
498     /**
499      * This should only be used in the case where we want to save the
500      * data to the database.
501      *
502      * @return A Hashtable.
503      */

504     public Hashtable JavaDoc getTempStorage()
505     {
506         if (this.tempStorage == null)
507         {
508             this.tempStorage = new Hashtable JavaDoc();
509         }
510         return this.tempStorage;
511     }
512
513     /**
514      * This should only be used in the case where we want to save the
515      * data to the database.
516      *
517      * @param storage A Hashtable.
518      */

519     public void setTempStorage(Hashtable JavaDoc storage)
520     {
521         this.tempStorage = storage;
522     }
523
524     /**
525      * This gets whether or not someone has logged in. hasLoggedIn()
526      * returns this value as a boolean. This is private because you
527      * should use hasLoggedIn() instead.
528      *
529      * @return True if someone has logged in.
530      */

531     private Boolean JavaDoc getHasLoggedIn()
532     {
533         return (Boolean JavaDoc) getTemp (User.HAS_LOGGED_IN);
534     }
535
536     /**
537      * This sets whether or not someone has logged in. hasLoggedIn()
538      * returns this value.
539      *
540      * @param value Whether someone has logged in or not.
541      */

542     public void setHasLoggedIn (Boolean JavaDoc value)
543     {
544         setTemp (User.HAS_LOGGED_IN, value);
545     }
546
547     /**
548      * Put an object into temporary storage. If the value is null,
549      * it will convert that to a "" because the underlying storage
550      * mechanism within TurbineUser is currently a Hashtable and
551      * null is not a valid value.
552      *
553      * @param name The object's name.
554      * @param value The object.
555      */

556     public void setTemp(String JavaDoc name, Object JavaDoc value)
557     {
558         ObjectUtils.safeAddToHashtable(tempStorage, name, value);
559     }
560
561     /**
562      * Sets the username for this user.
563      *
564      * @param username The user's username.
565      */

566     public void setUserName(String JavaDoc username)
567     {
568         setPerm (User.USERNAME, username);
569     }
570
571     /**
572      * Sets the first name for this user.
573      *
574      * @param firstName User's first name.
575      */

576     public void setFirstName(String JavaDoc firstName)
577     {
578         setPerm(User.FIRST_NAME, firstName);
579     }
580
581     /**
582      * Sets the last name for this user.
583      *
584      * @param lastName User's last name.
585      */

586     public void setLastName(String JavaDoc lastName)
587     {
588         setPerm(User.LAST_NAME, lastName);
589     }
590
591
592     /**
593      * Sets the email address.
594      *
595      * @param address The email address.
596      */

597     public void setEmail(String JavaDoc address)
598     {
599         setPerm(User.EMAIL, address);
600     }
601
602     /**
603      * This method reports whether or not the user has been confirmed
604      * in the system by checking the User.CONFIRM_VALUE
605      * column in the users record to see if it is equal to
606      * User.CONFIRM_DATA.
607      *
608      * @return True if the user has been confirmed.
609      */

610     public boolean isConfirmed()
611     {
612         String JavaDoc value = getConfirmed();
613         return (value != null && value.equals(User.CONFIRM_DATA));
614     }
615
616     /**
617      * Sets the confirmation value. The value should
618      * be either a random string or User.CONFIRM_DATA
619      *
620      * @param value The confirmation key value.
621      */

622     public void setConfirmed(String JavaDoc value)
623     {
624         String JavaDoc val = "";
625         if (value != null)
626         {
627             val = value;
628         }
629         setPerm(User.CONFIRM_VALUE, val);
630     }
631
632     /**
633      * Gets the confirmation value.
634      *
635      * @return status The confirmation value for this User
636      */

637     public String JavaDoc getConfirmed()
638     {
639         return (String JavaDoc)getPerm(User.CONFIRM_VALUE);
640     }
641
642     /**
643      * Updates the last login date in the database.
644      *
645      * @exception Exception, a generic exception.
646      */

647     public void updateLastLogin()
648         throws Exception JavaDoc
649     {
650         setPerm( User.LAST_LOGIN, new java.util.Date JavaDoc() );
651     }
652
653     /**
654      * Implement this method if you wish to be notified when the User
655      * has been Bound to the session.
656      *
657      * @param hsbe The HttpSessionBindingEvent.
658      */

659     public void valueBound(HttpSessionBindingEvent JavaDoc hsbe)
660     {
661         // Currently we have no need for this method.
662
}
663
664     /**
665      * Implement this method if you wish to be notified when the User
666      * has been Unbound from the session.
667      *
668      * @param hsbe The HttpSessionBindingEvent.
669      */

670     public void valueUnbound(HttpSessionBindingEvent JavaDoc hsbe)
671     {
672
673         try
674         {
675             java.util.Date JavaDoc now = new java.util.Date JavaDoc();
676             //System.out.println("*********** value unbound ********************: " + now.toString());
677
if (this.hasLoggedIn())
678             {
679                 if ( JetspeedResources.getBoolean("automatic.logout.save", false) )
680                 {
681                     JetspeedUserManagement.saveUser(this);
682                 }
683                 JetspeedAuthentication.logout();
684             }
685
686         }
687         catch ( Exception JavaDoc e )
688         {
689             logger.error("TurbineUser.valueUnbound(): " + e.getMessage(), e);
690
691             // To prevent messages being lost in case the logging system
692
// goes away before sessions get unbound on servlet container
693
// shutdown, print the stcktrace to the container's console.
694
ByteArrayOutputStream JavaDoc ostr = new ByteArrayOutputStream JavaDoc();
695             e.printStackTrace(new PrintWriter JavaDoc(ostr,true));
696             String JavaDoc stackTrace = ostr.toString();
697             System.out.println(stackTrace);
698         }
699     }
700
701
702     /**
703      * Saves this object to the data store.
704      */

705     public void save()
706         throws Exception JavaDoc
707     {
708         if (this.isNew())
709         {
710             JetspeedUserManagement.addUser(this);
711         }
712         else
713         {
714             JetspeedUserManagement.saveUser(this);
715         }
716     }
717
718     /**
719      * Returns the disabled status for the user
720      *
721      * @return True when the account is disabled
722      */

723     public boolean getDisabled()
724     {
725         boolean disabled = false;
726         try
727         {
728             String JavaDoc tmp = (String JavaDoc) getPerm (JetspeedUser.DISABLED);
729             if ( tmp != null && tmp.length() > 0 )
730             {
731                 if (tmp.equalsIgnoreCase("T"))
732                     disabled = true;
733             }
734         }
735         catch (Exception JavaDoc e)
736         {
737             logger.error("getDisabled(): " + e.getMessage(), e);
738         }
739         return disabled;
740     }
741
742     public void setDisabled(boolean disabled)
743     {
744         setPerm(JetspeedUser.DISABLED, (disabled) ? "T" : "F");
745     }
746
747     public String JavaDoc getName()
748     {
749         return name;
750     }
751
752     public void setName(String JavaDoc name)
753     {
754         this.name = name;
755     }
756
757     public boolean isNew()
758     {
759         return isNew;
760     }
761
762     protected void setNew(boolean isNew)
763     {
764         this.isNew = isNew;
765     }
766
767     /**
768      * @see org.apache.jetspeed.om.security.JetspeedUser#getPasswordChanged
769      */

770     public Date JavaDoc getPasswordChanged()
771     {
772         return (Date JavaDoc) getPerm(JetspeedUser.PASSWORD_CHANGED);
773     }
774  
775     /**
776      * @see org.apache.jetspeed.om.security.JetspeedUser#setPasswordChanged
777      */

778     public void setPasswordChanged(Date JavaDoc value)
779     {
780         setPerm(JetspeedUser.PASSWORD_CHANGED, value);
781     }
782
783 }
Popular Tags