KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > turbine > services > security > torque > TorqueUser


1 package org.apache.turbine.services.security.torque;
2
3 /*
4  * Copyright 2001-2004 The Apache Software Foundation.
5  *
6  * Licensed under the Apache License, Version 2.0 (the "License")
7  * you may not use this file except in compliance with the License.
8  * 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, software
13  * distributed under the License is distributed on an "AS IS" BASIS,
14  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15  * See the License for the specific language governing permissions and
16  * limitations under the License.
17  */

18
19 import java.io.ByteArrayOutputStream JavaDoc;
20 import java.io.PrintWriter JavaDoc;
21
22 import java.sql.Connection JavaDoc;
23
24 import java.util.Date JavaDoc;
25 import java.util.Hashtable JavaDoc;
26
27 import javax.servlet.http.HttpSessionBindingEvent JavaDoc;
28
29 import org.apache.commons.logging.Log;
30 import org.apache.commons.logging.LogFactory;
31 import org.apache.torque.om.Persistent;
32
33 import org.apache.turbine.om.security.User;
34 import org.apache.turbine.services.security.TurbineSecurity;
35 import org.apache.turbine.util.ObjectUtils;
36 import org.apache.turbine.util.security.TurbineSecurityException;
37
38 /**
39  * This is the User class used by the TorqueSecurity Service. It decouples
40  * all the database peer access from the actual Peer object
41  *
42  * @author <a HREF="mailto:josh@stonecottage.com">Josh Lucas</a>
43  * @author <a HREF="mailto:jon@collab.net">Jon S. Stevens</a>
44  * @author <a HREF="mailto:jmcnally@collab.net">John D. McNally</a>
45  * @author <a HREF="mailto:frank.kim@clearink.com">Frank Y. Kim</a>
46  * @author <a HREF="mailto:cberry@gluecode.com">Craig D. Berry</a>
47  * @author <a HREF="mailto:mpoeschl@marmot.at">Martin Poeschl</a>
48  * @author <a HREF="mailto:dlr@collab.net">Daniel Rall</a>
49  * @author <a HREF="mailto:hps@intermeta.de">Henning P. Schmiedehausen</a>
50  * @version $Id: TorqueUser.java,v 1.7.2.3 2004/05/20 03:06:50 seade Exp $
51  */

52
53 public class TorqueUser
54     extends TorqueObject
55     implements User,
56                Persistent
57 {
58     private static Log log = LogFactory.getLog(TorqueUser.class);
59     
60     /** The date on which the user last accessed the application. */
61     private Date JavaDoc lastAccessDate = null;
62
63     /** This is data that will survive a servlet engine restart. */
64     private Hashtable JavaDoc permStorage = null;
65
66     /** This is data that will not survive a servlet engine restart. */
67     private Hashtable JavaDoc tempStorage = null;
68
69     /**
70      * Constructor.
71      * Create a new User and set the createDate.
72      */

73     public TorqueUser()
74     {
75         super();
76         setCreateDate(new Date JavaDoc());
77         tempStorage = new Hashtable JavaDoc(10);
78         setHasLoggedIn(Boolean.FALSE);
79     }
80
81     /**
82      * This Constructor is used when the UserPeerManager
83      * has retrieved a list of Database Objects from the peer and
84      * must 'wrap' them into TorqueRole Objects. You should not use it directly!
85      *
86      * @param obj An Object from the peer
87      */

88     public TorqueUser(Persistent obj)
89     {
90         super(obj);
91
92         // Do not set creation date. This is only called on retrieval from
93
// storage!
94

95         tempStorage = new Hashtable JavaDoc(10);
96         setHasLoggedIn(Boolean.FALSE);
97     }
98
99     /**
100      * Returns the underlying Object for the Peer
101      *
102      * Used in the UserPeerManager when building a new Criteria.
103      *
104      * @return The underlying persistent object
105      *
106      */

107
108     public Persistent getPersistentObj()
109     {
110         if (obj == null)
111         {
112             obj = UserPeerManager.newPersistentInstance();
113         }
114         return obj;
115     }
116
117     /**
118      * Stores the object in the database. If the object is new,
119      * it inserts it; otherwise an update is performed.
120      *
121      * @param torqueName The name under which the object should be stored.
122      *
123      * @exception Exception This method might throw an exceptions
124      */

125     public void save(String JavaDoc torqueName)
126             throws Exception JavaDoc
127     {
128         setObjectdata(ObjectUtils.serializeHashtable(getPermStorage()));
129         super.save(torqueName);
130     }
131
132     /**
133      * Stores the object in the database. If the object is new,
134      * it inserts it; otherwise an update is performed. This method
135      * is meant to be used as part of a transaction, otherwise use
136      * the save() method and the connection details will be handled
137      * internally
138      *
139      * @param con A Connection object to save the object
140      *
141      * @exception Exception This method might throw an exceptions
142      */

143     public void save(Connection JavaDoc con)
144         throws Exception JavaDoc
145     {
146         setObjectdata(ObjectUtils.serializeHashtable(getPermStorage()));
147         super.save(con);
148     }
149
150     /**
151      * Makes changes made to the User attributes permanent.
152      *
153      * @throws TurbineSecurityException if there is a problem while
154      * saving data.
155      */

156     public void save()
157         throws TurbineSecurityException
158     {
159         //
160
// This is inconsistent with all the other security classes
161
// because it calls save() on the underlying object directly.
162
// But the UserManager calls ((Persistent)user).save()
163
// so if we do TurbineSecurity.saveUser(this) here, we get
164
// a nice endless loop. :-(
165
//
166
// Users seem to be a special kind of security objects...
167
//
168

169         try
170         {
171             setObjectdata(ObjectUtils.serializeHashtable(getPermStorage()));
172             getPersistentObj().save();
173         }
174         catch (Exception JavaDoc e)
175         {
176             throw new TurbineSecurityException("User object said "
177                     + e.getMessage(), e);
178         }
179     }
180
181     /**
182      * Returns the name of this object.
183      *
184      * @return The name of the object.
185      */

186     public String JavaDoc getName()
187     {
188         return UserPeerManager.getName(getPersistentObj());
189     }
190
191     /**
192      * Sets the name of this object
193      *
194      * @param name The name of the object
195      */

196     public void setName(String JavaDoc name)
197     {
198         setUserName(name);
199     }
200
201     /**
202      * Gets the Id of this object
203      *
204      * @return The Id of the object
205      */

206     public int getId()
207     {
208         return UserPeerManager.getIdAsObj(getPersistentObj()).intValue();
209     }
210
211     /**
212      * Gets the Id of this object
213      *
214      * @return The Id of the object
215      */

216     public Integer JavaDoc getIdAsObj()
217     {
218         return UserPeerManager.getIdAsObj(getPersistentObj());
219     }
220
221     /**
222      * Sets the Id of this object
223      *
224      * @param id The new Id
225      */

226     public void setId(int id)
227     {
228         UserPeerManager.setId(getPersistentObj(), id);
229     }
230
231     /**
232      * Returns the name of this user.
233      *
234      * @return The name of the user.
235      * @deprecated Use getName() instead.
236      */

237     public String JavaDoc getUserName()
238     {
239         return getName();
240     }
241
242     /**
243      * Sets the name of this user.
244      *
245      * @param name The name of the user.
246      */

247     public void setUserName(String JavaDoc name)
248     {
249         UserPeerManager.setUserName(getPersistentObj(), name);
250     }
251
252     /**
253      * Returns the password of the User
254      *
255      * @return The password of the User
256      */

257     public String JavaDoc getPassword()
258     {
259         return UserPeerManager.getUserPassword(getPersistentObj());
260     }
261
262     /**
263      * Sets the password of the User
264      *
265      * @param password The new password of the User
266      */

267     public void setPassword(String JavaDoc password)
268     {
269         UserPeerManager.setUserPassword(getPersistentObj(), password);
270     }
271
272     /**
273      * Returns the first name of the User
274      *
275      * @return The first name of the User
276      */

277     public String JavaDoc getFirstName()
278     {
279         return UserPeerManager.getUserFirstName(getPersistentObj());
280     }
281
282     /**
283      * Sets the first name of the User
284      *
285      * @param firstName The new first name of the User
286      */

287     public void setFirstName(String JavaDoc firstName)
288     {
289         UserPeerManager.setUserFirstName(getPersistentObj(), firstName);
290     }
291
292     /**
293      * Returns the last name of the User
294      *
295      * @return The last name of the User
296      */

297     public String JavaDoc getLastName()
298     {
299         return UserPeerManager.getUserLastName(getPersistentObj());
300     }
301
302     /**
303      * Sets the last name of User
304      *
305      * @param lastName The new last name of the User
306      */

307     public void setLastName(String JavaDoc lastName)
308     {
309         UserPeerManager.setUserLastName(getPersistentObj(), lastName);
310     }
311
312     /**
313      * Returns the email address of the user
314      *
315      * @return The email address of the user
316      */

317     public String JavaDoc getEmail()
318     {
319         return UserPeerManager.getUserEmail(getPersistentObj());
320     }
321
322     /**
323      * Sets the new email address of the user
324      *
325      * @param email The new email address of the user
326      */

327     public void setEmail(String JavaDoc email)
328     {
329         UserPeerManager.setUserEmail(getPersistentObj(), email);
330     }
331
332     /**
333      * Returns the confirm value of the user
334      *
335      * @return The confirm value of the user
336      */

337     public String JavaDoc getConfirmed()
338     {
339         return UserPeerManager.getUserConfirmed(getPersistentObj());
340     }
341
342     /**
343      * Sets the new confirm value of the user
344      *
345      * @param confirm The new confirm value of the user
346      */

347     public void setConfirmed(String JavaDoc confirm)
348     {
349         UserPeerManager.setUserConfirmed(getPersistentObj(), confirm);
350     }
351
352     /**
353      * Returns the creation date of the user
354      *
355      * @return The creation date of the user
356      */

357     public java.util.Date JavaDoc getCreateDate()
358     {
359         return UserPeerManager.getUserCreateDate(getPersistentObj());
360     }
361
362     /**
363      * Sets the new creation date of the user
364      *
365      * @param createDate The new creation date of the user
366      */

367     public void setCreateDate(java.util.Date JavaDoc createDate)
368     {
369         UserPeerManager.setUserCreateDate(getPersistentObj(), createDate);
370     }
371
372     /**
373      * Returns the date of the last login of the user
374      *
375      * @return The date of the last login of the user
376      */

377     public java.util.Date JavaDoc getLastLogin()
378     {
379         return UserPeerManager.getUserLastLogin(getPersistentObj());
380     }
381
382     /**
383      * Sets the new date of the last login of the user
384      *
385      * @param lastLogin The new the date of the last login of the user
386      */

387     public void setLastLogin(java.util.Date JavaDoc lastLogin)
388     {
389         UserPeerManager.setUserLastLogin(getPersistentObj(), lastLogin);
390     }
391
392     /**
393      * Returns the value of the objectdata for this user.
394      * Objectdata is a VARBINARY column in the table used
395      * to store the permanent storage table from the User
396      * object.
397      *
398      * @return The bytes in the objectdata for this user
399      */

400     public byte [] getObjectdata()
401     {
402         return UserPeerManager.getUserObjectdata(getPersistentObj());
403     }
404
405     /**
406      * Sets the value of the objectdata for the user
407      *
408      * @param objectdata The new the date of the last login of the user
409      */

410     public void setObjectdata(byte [] objectdata)
411     {
412         UserPeerManager.setUserObjectdata(getPersistentObj(), objectdata);
413     }
414
415
416     /**
417      * Gets the access counter for a user from perm storage.
418      *
419      * @return The access counter for the user.
420      */

421     public int getAccessCounter()
422     {
423         try
424         {
425             return ((Integer JavaDoc) getPerm(User.ACCESS_COUNTER)).intValue();
426         }
427         catch (Exception JavaDoc e)
428         {
429             return 0;
430         }
431     }
432
433     /**
434      * Gets the access counter for a user during a session.
435      *
436      * @return The access counter for the user for the session.
437      */

438     public int getAccessCounterForSession()
439     {
440         try
441         {
442             return ((Integer JavaDoc) getTemp(User.SESSION_ACCESS_COUNTER)).intValue();
443         }
444         catch (Exception JavaDoc e)
445         {
446             return 0;
447         }
448     }
449
450     /**
451      * Increments the permanent hit counter for the user.
452      */

453     public void incrementAccessCounter()
454     {
455         // Ugh. Race city, here I come...
456
setAccessCounter(getAccessCounter() + 1);
457     }
458
459     /**
460      * Increments the session hit counter for the user.
461      */

462     public void incrementAccessCounterForSession()
463     {
464         setAccessCounterForSession(getAccessCounterForSession() + 1);
465     }
466
467     /**
468      * Sets the access counter for a user, saved in perm storage.
469      *
470      * @param cnt The new count.
471      */

472     public void setAccessCounter(int cnt)
473     {
474         setPerm(User.ACCESS_COUNTER, new Integer JavaDoc(cnt));
475     }
476
477     /**
478      * Sets the session access counter for a user, saved in temp
479      * storage.
480      *
481      * @param cnt The new count.
482      */

483     public void setAccessCounterForSession(int cnt)
484     {
485         setTemp(User.SESSION_ACCESS_COUNTER, new Integer JavaDoc(cnt));
486     }
487
488     /**
489      * This method reports whether or not the user has been confirmed
490      * in the system by checking the User.CONFIRM_VALUE
491      * column in the users record to see if it is equal to
492      * User.CONFIRM_DATA.
493      *
494      * @return True if the user has been confirmed.
495      */

496     public boolean isConfirmed()
497     {
498         String JavaDoc value = getConfirmed();
499         return (value != null && value.equals(User.CONFIRM_DATA));
500     }
501
502     /**
503      * The user is considered logged in if they have not timed out.
504      *
505      * @return Whether the user has logged in.
506      */

507     public boolean hasLoggedIn()
508     {
509         Boolean JavaDoc loggedIn = getHasLoggedIn();
510         return (loggedIn != null && loggedIn.booleanValue());
511     }
512
513     /**
514      * This sets whether or not someone has logged in. hasLoggedIn()
515      * returns this value.
516      *
517      * @param value Whether someone has logged in or not.
518      */

519     public void setHasLoggedIn(Boolean JavaDoc value)
520     {
521         setTemp(User.HAS_LOGGED_IN, value);
522     }
523
524     /**
525      * Gets the last access date for this User. This is the last time
526      * that the user object was referenced.
527      *
528      * @return A Java Date with the last access date for the user.
529      */

530     public java.util.Date JavaDoc getLastAccessDate()
531     {
532         if (lastAccessDate == null)
533         {
534             setLastAccessDate();
535         }
536         return lastAccessDate;
537     }
538
539     /**
540      * Sets the last access date for this User. This is the last time
541      * that the user object was referenced.
542      */

543     public void setLastAccessDate()
544     {
545         lastAccessDate = new java.util.Date JavaDoc();
546     }
547
548     /**
549      * Returns the permanent storage. This is implemented
550      * as a Hashtable and backed by an VARBINARY column in
551      * the database.
552      *
553      * @return A Hashtable.
554      */

555     public Hashtable JavaDoc getPermStorage()
556     {
557         if (permStorage == null)
558         {
559             byte [] objectdata = getObjectdata();
560
561             if (objectdata != null)
562             {
563                 permStorage = (Hashtable JavaDoc) ObjectUtils.deserialize(objectdata);
564             }
565
566             if (permStorage == null)
567             {
568                 permStorage = new Hashtable JavaDoc();
569             }
570         }
571
572         return permStorage;
573     }
574
575     /**
576      * This should only be used in the case where we want to save the
577      * data to the database.
578      *
579      * @param storage A Hashtable.
580      */

581     public void setPermStorage(Hashtable JavaDoc permStorage)
582     {
583         if (permStorage != null)
584         {
585             this.permStorage = permStorage;
586         }
587     }
588
589     /**
590      * Returns the temporary storage. This is implemented
591      * as a Hashtable
592      *
593      * @return A Hashtable.
594      */

595     public Hashtable JavaDoc getTempStorage()
596     {
597         if (tempStorage == null)
598         {
599             tempStorage = new Hashtable JavaDoc();
600         }
601         return tempStorage;
602     }
603
604     /**
605      * This should only be used in the case where we want to save the
606      * data to the database.
607      *
608      * @param storage A Hashtable.
609      */

610     public void setTempStorage(Hashtable JavaDoc tempStorage)
611     {
612         if (tempStorage != null)
613         {
614             this.tempStorage = tempStorage;
615         }
616     }
617
618     /**
619      * Get an object from permanent storage.
620      *
621      * @param name The object's name.
622      * @return An Object with the given name.
623      */

624     public Object JavaDoc getPerm(String JavaDoc name)
625     {
626         return getPermStorage().get(name);
627     }
628
629     /**
630      * Get an object from permanent storage; return default if value
631      * is null.
632      *
633      * @param name The object's name.
634      * @param def A default value to return.
635      * @return An Object with the given name.
636      */

637     public Object JavaDoc getPerm(String JavaDoc name, Object JavaDoc def)
638     {
639         try
640         {
641             Object JavaDoc val = getPermStorage().get(name);
642             return (val == null ? def : val);
643         }
644         catch (Exception JavaDoc e)
645         {
646             return def;
647         }
648     }
649
650     /**
651      * Put an object into permanent storage. If the value is null,
652      * it will convert that to a "" because the underlying storage
653      * mechanism within TorqueUser is currently a Hashtable and
654      * null is not a valid value.
655      *
656      * @param name The object's name.
657      * @param value The object.
658      */

659     public void setPerm(String JavaDoc name, Object JavaDoc value)
660     {
661         getPermStorage().put(name, (value == null) ? "" : value);
662     }
663
664     /**
665      * Get an object from temporary storage.
666      *
667      * @param name The object's name.
668      * @return An Object with the given name.
669      */

670     public Object JavaDoc getTemp(String JavaDoc name)
671     {
672         return getTempStorage().get(name);
673     }
674
675     /**
676      * Get an object from temporary storage; return default if value
677      * is null.
678      *
679      * @param name The object's name.
680      * @param def A default value to return.
681      * @return An Object with the given name.
682      */

683     public Object JavaDoc getTemp(String JavaDoc name, Object JavaDoc def)
684     {
685         Object JavaDoc val;
686         try
687         {
688             val = getTempStorage().get(name);
689             if (val == null)
690             {
691                 val = def;
692             }
693         }
694         catch (Exception JavaDoc e)
695         {
696             val = def;
697         }
698         return val;
699     }
700
701     /**
702      * Put an object into temporary storage. If the value is null,
703      * it will convert that to a "" because the underlying storage
704      * mechanism within TorqueUser is currently a Hashtable and
705      * null is not a valid value.
706      *
707      * @param name The object's name.
708      * @param value The object.
709      */

710     public void setTemp(String JavaDoc name, Object JavaDoc value)
711     {
712         getTempStorage().put(name, (value == null) ? "" : value);
713     }
714
715     /**
716      * Remove an object from temporary storage and return the object.
717      *
718      * @param name The name of the object to remove.
719      * @return An Object.
720      */

721     public Object JavaDoc removeTemp(String JavaDoc name)
722     {
723         return getTempStorage().remove(name);
724     }
725
726     /**
727      * Updates the last login date in the database.
728      *
729      * @exception Exception A generic exception.
730      */

731     public void updateLastLogin()
732         throws Exception JavaDoc
733     {
734         setLastLogin(new java.util.Date JavaDoc());
735     }
736
737     /**
738      * Implement this method if you wish to be notified when the User
739      * has been Bound to the session.
740      *
741      * @param event Indication of value/session binding.
742      */

743     public void valueBound(HttpSessionBindingEvent JavaDoc hsbe)
744     {
745         // Currently we have no need for this method.
746
}
747
748     /**
749      * Implement this method if you wish to be notified when the User
750      * has been Unbound from the session.
751      *
752      * @param event Indication of value/session unbinding.
753      */

754     public void valueUnbound(HttpSessionBindingEvent JavaDoc hsbe)
755     {
756         try
757         {
758             if (hasLoggedIn())
759             {
760                 TurbineSecurity.saveOnSessionUnbind(this);
761             }
762         }
763         catch (Exception JavaDoc e)
764         {
765             //Log.error("TorqueUser.valueUnbound(): " + e.getMessage(), e);
766

767             // To prevent messages being lost in case the logging system
768
// goes away before sessions get unbound on servlet container
769
// shutdown, print the stcktrace to the container's console.
770
ByteArrayOutputStream JavaDoc ostr = new ByteArrayOutputStream JavaDoc();
771             e.printStackTrace(new PrintWriter JavaDoc(ostr, true));
772             String JavaDoc stackTrace = ostr.toString();
773             System.out.println(stackTrace);
774         }
775     }
776
777     /**
778      * This gets whether or not someone has logged in. hasLoggedIn()
779      * returns this value as a boolean. This is private because you
780      * should use hasLoggedIn() instead.
781      *
782      * @return True if someone has logged in.
783      */

784     private Boolean JavaDoc getHasLoggedIn()
785     {
786         return (Boolean JavaDoc) getTemp(User.HAS_LOGGED_IN);
787     }
788
789 }
790
Popular Tags